File size: 5,466 Bytes
b21d047
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import os
import json
import time
import requests
import openai
import copy

from loguru import logger
from dotenv import load_dotenv

load_dotenv()

API_KEY = os.getenv("API_KEY")
API_BASE = os.getenv("API_BASE")

API_KEY_2 = os.getenv("API_KEY_2")
API_BASE_2 = os.getenv("API_BASE_2")

MAX_TOKENS = os.getenv("MAX_TOKENS")
TEMPERATURE = os.getenv("TEMPERATURE")

DEBUG = int(os.environ.get("DEBUG", "0"))


def generate_together(
    model,
    messages,
    max_tokens=MAX_TOKENS,
    temperature=TEMPERATURE,
    api_key=API_KEY,
    streaming=False,
):
    logger.info(
        f"Input data: model={model}, messages={messages}, max_tokens={max_tokens}, temperature={temperature}"
    )

    output = None

    for sleep_time in [1, 2, 4, 8, 16, 32]:
        try:
            endpoint = "http://localhost:11434/v1/chat/completions"
            logger.info(f"Sending request to {endpoint}")

            # Assuming model is a list with one element, e.g., ['qwen2']
            chat_model = model[0] if isinstance(model, list) else model

            # Convert temperature to float
            temperature = float(temperature)

            # Ensure messages are in the correct format
            formatted_messages = []
            for msg in messages:
                if isinstance(msg['content'], list):
                    # If content is a list, join it into a single string
                    msg['content'] = ' '.join([m['content'] for m in msg['content'] if 'content' in m])
                formatted_messages.append(msg)

            res = requests.post(
                endpoint,
                json={
                    "model": chat_model,
                    "max_tokens": int(max_tokens),
                    "temperature": temperature if temperature > 1e-4 else 0,
                    "messages": formatted_messages,
                },
                headers={
                    "Authorization": f"Bearer {api_key}",
                },
            )

            res.raise_for_status()  # This will raise an exception for HTTP errors
            output = res.json()["choices"][0]["message"]["content"]
            break

        except Exception as e:
            logger.error(f"Error in generate_together: {str(e)}")
            output = f"Error: {str(e)}"
            logger.info(f"Retry in {sleep_time}s..")
            time.sleep(sleep_time)

    if output is None:
        return output

    output = output.strip()
    logger.info(f"Output: `{output[:20]}...`.")
    return output

def generate_together_stream(
    model,
    messages,
    max_tokens=MAX_TOKENS,
    temperature=TEMPERATURE,
    api_key=API_KEY
):
    # endpoint = f"{api_base}/chat/completions"
    endpoint = API_BASE
    client = openai.OpenAI(api_key=api_key, base_url=endpoint)
    response = client.chat.completions.create(
        model=model,
        messages=messages,
        temperature=temperature if temperature > 1e-4 else 0,
        max_tokens=max_tokens,
        stream=True,  # this time, we set stream=True
    )

    return response


def generate_openai(
    model,
    messages,
    max_tokens=MAX_TOKENS,
    temperature=TEMPERATURE,
):

    client = openai.OpenAI(
        base_url=API_BASE_2,
        api_key=API_KEY_2,
    )

    for sleep_time in [1, 2, 4, 8, 16, 32]:
        try:

            if DEBUG:
                logger.debug(
                    f"Sending messages ({len(messages)}) (last message: `{messages[-1]['content'][:20]}`) to `{model}`."
                )

            completion = client.chat.completions.create(
                model=model,
                messages=messages,
                temperature=temperature,
                max_tokens=max_tokens,
            )
            output = completion.choices[0].message.content
            break

        except Exception as e:
            logger.error(e)
            logger.info(f"Retry in {sleep_time}s..")
            time.sleep(sleep_time)

    output = output.strip()

    return output


def inject_references_to_messages(
    messages,
    references,
):

    messages = copy.deepcopy(messages)

    system = f"""You have been provided with a set of responses from various open-source models to the latest user query. Your task is to synthesize these responses into a single, high-quality response. It is crucial to critically evaluate the information provided in these responses, recognizing that some of it may be biased or incorrect. Your response should not simply replicate the given answers but should offer a refined, accurate, and comprehensive reply to the instruction. Ensure your response is well-structured, coherent, and adheres to the highest standards of accuracy and reliability.

Responses from models:"""

    for i, reference in enumerate(references):

        system += f"\n{i+1}. {reference}"

    # if messages[0]["role"] == "system":

    #     messages[0]["content"] += "\n\n" + system

    # else:

    messages = [{"role": "system", "content": system}] + messages

    return messages


def generate_with_references(
    model,
    messages,
    references=[],
    max_tokens=MAX_TOKENS,
    temperature=TEMPERATURE,
    generate_fn=generate_together,
    api_key=API_KEY
):
    if len(references) > 0:
        messages = inject_references_to_messages(messages, references)

    return generate_fn(
        model=model,
        messages=messages,
        temperature=temperature,
        max_tokens=max_tokens,
        api_key=api_key
    )