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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import json
|
3 |
+
|
4 |
+
# Build model mapping
|
5 |
+
original_models = [
|
6 |
+
# OpenAI Models
|
7 |
+
"gpt-3.5-turbo",
|
8 |
+
"gpt-3.5-turbo-202201",
|
9 |
+
"gpt-4o",
|
10 |
+
"gpt-4o-2024-05-13",
|
11 |
+
"o1-preview",
|
12 |
+
|
13 |
+
# Claude Models
|
14 |
+
"claude",
|
15 |
+
"claude-3-5-sonnet",
|
16 |
+
"claude-sonnet-3.5",
|
17 |
+
"claude-3-5-sonnet-20240620",
|
18 |
+
|
19 |
+
# Meta/LLaMA Models
|
20 |
+
"@cf/meta/llama-2-7b-chat-fp16",
|
21 |
+
"@cf/meta/llama-2-7b-chat-int8",
|
22 |
+
"@cf/meta/llama-3-8b-instruct",
|
23 |
+
"@cf/meta/llama-3.1-8b-instruct",
|
24 |
+
"@cf/meta-llama/llama-2-7b-chat-hf-lora",
|
25 |
+
"llama-3.1-405b",
|
26 |
+
"llama-3.1-70b",
|
27 |
+
"llama-3.1-8b",
|
28 |
+
"meta-llama/Llama-2-7b-chat-hf",
|
29 |
+
"meta-llama/Llama-3.1-70B-Instruct",
|
30 |
+
"meta-llama/Llama-3.1-8B-Instruct",
|
31 |
+
"meta-llama/Llama-3.2-11B-Vision-Instruct",
|
32 |
+
"meta-llama/Llama-3.2-1B-Instruct",
|
33 |
+
"meta-llama/Llama-3.2-3B-Instruct",
|
34 |
+
"meta-llama/Llama-3.2-90B-Vision-Instruct",
|
35 |
+
"meta-llama/Llama-Guard-3-8B",
|
36 |
+
"meta-llama/Meta-Llama-3-70B-Instruct",
|
37 |
+
"meta-llama/Meta-Llama-3-8B-Instruct",
|
38 |
+
"meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo",
|
39 |
+
"meta-llama/Meta-Llama-3.1-8B-Instruct",
|
40 |
+
"meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo",
|
41 |
+
|
42 |
+
# Mistral Models
|
43 |
+
"mistral",
|
44 |
+
"mistral-large",
|
45 |
+
"@cf/mistral/mistral-7b-instruct-v0.1",
|
46 |
+
"@cf/mistral/mistral-7b-instruct-v0.2-lora",
|
47 |
+
"@hf/mistralai/mistral-7b-instruct-v0.2",
|
48 |
+
"mistralai/Mistral-7B-Instruct-v0.2",
|
49 |
+
"mistralai/Mistral-7B-Instruct-v0.3",
|
50 |
+
"mistralai/Mixtral-8x22B-Instruct-v0.1",
|
51 |
+
"mistralai/Mixtral-8x7B-Instruct-v0.1",
|
52 |
+
|
53 |
+
# Qwen Models
|
54 |
+
"@cf/qwen/qwen1.5-0.5b-chat",
|
55 |
+
"@cf/qwen/qwen1.5-1.8b-chat",
|
56 |
+
"@cf/qwen/qwen1.5-7b-chat-awq",
|
57 |
+
"@cf/qwen/qwen1.5-14b-chat-awq",
|
58 |
+
"Qwen/Qwen2.5-3B-Instruct",
|
59 |
+
"Qwen/Qwen2.5-72B-Instruct",
|
60 |
+
"Qwen/Qwen2.5-Coder-32B-Instruct",
|
61 |
+
|
62 |
+
# Google/Gemini Models
|
63 |
+
"@cf/google/gemma-2b-it-lora",
|
64 |
+
"@cf/google/gemma-7b-it-lora",
|
65 |
+
"@hf/google/gemma-7b-it",
|
66 |
+
"google/gemma-1.1-2b-it",
|
67 |
+
"google/gemma-1.1-7b-it",
|
68 |
+
"gemini-pro",
|
69 |
+
"gemini-1.5-pro",
|
70 |
+
"gemini-1.5-pro-latest",
|
71 |
+
"gemini-1.5-flash",
|
72 |
+
|
73 |
+
# Cohere Models
|
74 |
+
"c4ai-aya-23-35b",
|
75 |
+
"c4ai-aya-23-8b",
|
76 |
+
"command",
|
77 |
+
"command-light",
|
78 |
+
"command-light-nightly",
|
79 |
+
"command-nightly",
|
80 |
+
"command-r",
|
81 |
+
"command-r-08-2024",
|
82 |
+
"command-r-plus",
|
83 |
+
"command-r-plus-08-2024",
|
84 |
+
"rerank-english-v2.0",
|
85 |
+
"rerank-english-v3.0",
|
86 |
+
"rerank-multilingual-v2.0",
|
87 |
+
"rerank-multilingual-v3.0",
|
88 |
+
|
89 |
+
# Microsoft Models
|
90 |
+
"@cf/microsoft/phi-2",
|
91 |
+
"microsoft/DialoGPT-medium",
|
92 |
+
"microsoft/Phi-3-medium-4k-instruct",
|
93 |
+
"microsoft/Phi-3-mini-4k-instruct",
|
94 |
+
"microsoft/Phi-3.5-mini-instruct",
|
95 |
+
"microsoft/WizardLM-2-8x22B",
|
96 |
+
|
97 |
+
# Yi Models
|
98 |
+
"01-ai/Yi-1.5-34B-Chat",
|
99 |
+
"01-ai/Yi-34B-Chat",
|
100 |
+
]
|
101 |
+
|
102 |
+
# Create mapping from simplified model names to original model names
|
103 |
+
model_mapping = {}
|
104 |
+
simplified_models = []
|
105 |
+
|
106 |
+
for original_model in original_models:
|
107 |
+
simplified_name = original_model.split('/')[-1]
|
108 |
+
if simplified_name in model_mapping:
|
109 |
+
# Conflict detected, handle as per instructions
|
110 |
+
print(f"Conflict detected for model name '{simplified_name}'. Excluding '{original_model}' from available models.")
|
111 |
+
continue
|
112 |
+
model_mapping[simplified_name] = original_model
|
113 |
+
simplified_models.append(simplified_name)
|
114 |
+
|
115 |
+
def generate(
|
116 |
+
model,
|
117 |
+
messages,
|
118 |
+
temperature=0.7,
|
119 |
+
top_p=1.0,
|
120 |
+
n=1,
|
121 |
+
stream=False,
|
122 |
+
stop=None,
|
123 |
+
max_tokens=None,
|
124 |
+
presence_penalty=0.0,
|
125 |
+
frequency_penalty=0.0,
|
126 |
+
logit_bias=None,
|
127 |
+
user=None,
|
128 |
+
timeout=30,
|
129 |
+
):
|
130 |
+
"""
|
131 |
+
Generates a chat completion using the provided model and messages.
|
132 |
+
"""
|
133 |
+
# Use the simplified model names
|
134 |
+
models = simplified_models
|
135 |
+
|
136 |
+
if model not in models:
|
137 |
+
raise ValueError(f"Invalid model: {model}. Choose from: {', '.join(models)}")
|
138 |
+
|
139 |
+
# Map simplified model name to original model name
|
140 |
+
original_model = model_mapping[model]
|
141 |
+
|
142 |
+
api_endpoint = "https://chat.typegpt.net/api/openai/v1/chat/completions"
|
143 |
+
|
144 |
+
headers = {
|
145 |
+
"authority": "chat.typegpt.net",
|
146 |
+
"accept": "application/json, text/event-stream",
|
147 |
+
"accept-language": "en-US,en;q=0.9",
|
148 |
+
"content-type": "application/json",
|
149 |
+
"origin": "https://chat.typegpt.net",
|
150 |
+
"referer": "https://chat.typegpt.net/",
|
151 |
+
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
|
152 |
+
}
|
153 |
+
|
154 |
+
# Payload
|
155 |
+
payload = {
|
156 |
+
"messages": messages,
|
157 |
+
"stream": stream,
|
158 |
+
"model": original_model,
|
159 |
+
"temperature": temperature,
|
160 |
+
"presence_penalty": presence_penalty,
|
161 |
+
"frequency_penalty": frequency_penalty,
|
162 |
+
"top_p": top_p,
|
163 |
+
}
|
164 |
+
|
165 |
+
# Only include max_tokens if it's not None
|
166 |
+
if max_tokens is not None:
|
167 |
+
payload["max_tokens"] = max_tokens
|
168 |
+
|
169 |
+
# Only include 'stop' if it's not None
|
170 |
+
if stop is not None:
|
171 |
+
payload["stop"] = stop
|
172 |
+
|
173 |
+
# Check if logit_bias is provided
|
174 |
+
if logit_bias is not None:
|
175 |
+
payload["logit_bias"] = logit_bias
|
176 |
+
|
177 |
+
# Include 'user' if provided
|
178 |
+
if user is not None:
|
179 |
+
payload["user"] = user
|
180 |
+
|
181 |
+
# Start the request
|
182 |
+
session = requests.Session()
|
183 |
+
response = session.post(
|
184 |
+
api_endpoint, headers=headers, json=payload, stream=stream, timeout=timeout
|
185 |
+
)
|
186 |
+
|
187 |
+
if not response.ok:
|
188 |
+
raise Exception(f"Failed to generate response - ({response.status_code}, {response.reason}) - {response.text}")
|
189 |
+
|
190 |
+
def stream_response():
|
191 |
+
for line in response.iter_lines():
|
192 |
+
if line:
|
193 |
+
line = line.decode("utf-8")
|
194 |
+
if line.startswith("data: "):
|
195 |
+
line = line[6:] # Remove "data: " prefix
|
196 |
+
if line.strip() == "[DONE]":
|
197 |
+
break
|
198 |
+
try:
|
199 |
+
data = json.loads(line)
|
200 |
+
yield data
|
201 |
+
except json.JSONDecodeError:
|
202 |
+
continue
|
203 |
+
|
204 |
+
if stream:
|
205 |
+
return stream_response()
|
206 |
+
else:
|
207 |
+
return response.json()
|
208 |
+
|
209 |
+
if __name__ == "__main__":
|
210 |
+
# Example usage
|
211 |
+
# model = "claude-3-5-sonnet-20240620"
|
212 |
+
# model = "qwen1.5-0.5b-chat"
|
213 |
+
# model = "llama-2-7b-chat-fp16"
|
214 |
+
model = "gpt-3.5-turbo"
|
215 |
+
messages = [
|
216 |
+
{"role": "system", "content": "Be Detailed"},
|
217 |
+
{"role": "user", "content": "What is the knowledge cut off? Be specific and also specify the month, year and date. If not sure, then provide approximate."}
|
218 |
+
]
|
219 |
+
|
220 |
+
# try:
|
221 |
+
# # For non-streamed response
|
222 |
+
# response = generate(
|
223 |
+
# model=model,
|
224 |
+
# messages=messages,
|
225 |
+
# temperature=0.5,
|
226 |
+
# max_tokens=4000,
|
227 |
+
# stream=False # Change to True for streaming
|
228 |
+
# )
|
229 |
+
# if 'choices' in response:
|
230 |
+
# reply = response['choices'][0]['message']['content']
|
231 |
+
# print(reply)
|
232 |
+
# else:
|
233 |
+
# print("No response received.")
|
234 |
+
# except Exception as e:
|
235 |
+
# print(e)
|
236 |
+
|
237 |
+
|
238 |
+
try:
|
239 |
+
# For streamed response
|
240 |
+
response = generate(
|
241 |
+
model=model,
|
242 |
+
messages=messages,
|
243 |
+
temperature=0.5,
|
244 |
+
max_tokens=4000,
|
245 |
+
stream=True, # Change to False for non-streamed response
|
246 |
+
)
|
247 |
+
for data in response:
|
248 |
+
if 'choices' in data:
|
249 |
+
reply = data['choices'][0]['delta']['content']
|
250 |
+
print(reply, end="", flush=True)
|
251 |
+
else:
|
252 |
+
print("No response received.")
|
253 |
+
except Exception as e:
|
254 |
+
print(e)
|