Spaces:
Running
Running
File size: 12,130 Bytes
501c69f |
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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
```python
import requests
import json
# Build model mapping
original_models = [
# OpenAI Models
"gpt-3.5-turbo",
"gpt-3.5-turbo-202201",
"gpt-4o",
"gpt-4o-2024-05-13",
"o1-preview",
# Claude Models
"claude",
"claude-3-5-sonnet",
"claude-sonnet-3.5",
"claude-3-5-sonnet-20240620",
# Meta/LLaMA Models
"@cf/meta/llama-2-7b-chat-fp16",
"@cf/meta/llama-2-7b-chat-int8",
"@cf/meta/llama-3-8b-instruct",
"@cf/meta/llama-3.1-8b-instruct",
"@cf/meta-llama/llama-2-7b-chat-hf-lora",
"llama-3.1-405b",
"llama-3.1-70b",
"llama-3.1-8b",
"meta-llama/Llama-2-7b-chat-hf",
"meta-llama/Llama-3.1-70B-Instruct",
"meta-llama/Llama-3.1-8B-Instruct",
"meta-llama/Llama-3.2-11B-Vision-Instruct",
"meta-llama/Llama-3.2-1B-Instruct",
"meta-llama/Llama-3.2-3B-Instruct",
"meta-llama/Llama-3.2-90B-Vision-Instruct",
"meta-llama/Llama-Guard-3-8B",
"meta-llama/Meta-Llama-3-70B-Instruct",
"meta-llama/Meta-Llama-3-8B-Instruct",
"meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo",
"meta-llama/Meta-Llama-3.1-8B-Instruct",
"meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo",
# Mistral Models
"mistral",
"mistral-large",
"@cf/mistral/mistral-7b-instruct-v0.1",
"@cf/mistral/mistral-7b-instruct-v0.2-lora",
"@hf/mistralai/mistral-7b-instruct-v0.2",
"mistralai/Mistral-7B-Instruct-v0.2",
"mistralai/Mistral-7B-Instruct-v0.3",
"mistralai/Mixtral-8x22B-Instruct-v0.1",
"mistralai/Mixtral-8x7B-Instruct-v0.1",
# Qwen Models
"@cf/qwen/qwen1.5-0.5b-chat",
"@cf/qwen/qwen1.5-1.8b-chat",
"@cf/qwen/qwen1.5-7b-chat-awq",
"@cf/qwen/qwen1.5-14b-chat-awq",
"Qwen/Qwen2.5-3B-Instruct",
"Qwen/Qwen2.5-72B-Instruct",
"Qwen/Qwen2.5-Coder-32B-Instruct",
# Google/Gemini Models
"@cf/google/gemma-2b-it-lora",
"@cf/google/gemma-7b-it-lora",
"@hf/google/gemma-7b-it",
"google/gemma-1.1-2b-it",
"google/gemma-1.1-7b-it",
"gemini-pro",
"gemini-1.5-pro",
"gemini-1.5-pro-latest",
"gemini-1.5-flash",
# Cohere Models
"c4ai-aya-23-35b",
"c4ai-aya-23-8b",
"command",
"command-light",
"command-light-nightly",
"command-nightly",
"command-r",
"command-r-08-2024",
"command-r-plus",
"command-r-plus-08-2024",
"rerank-english-v2.0",
"rerank-english-v3.0",
"rerank-multilingual-v2.0",
"rerank-multilingual-v3.0",
# Microsoft Models
"@cf/microsoft/phi-2",
"microsoft/DialoGPT-medium",
"microsoft/Phi-3-medium-4k-instruct",
"microsoft/Phi-3-mini-4k-instruct",
"microsoft/Phi-3.5-mini-instruct",
"microsoft/WizardLM-2-8x22B",
# Yi Models
"01-ai/Yi-1.5-34B-Chat",
"01-ai/Yi-34B-Chat",
]
# Create mapping from simplified model names to original model names
model_mapping = {}
simplified_models = []
for original_model in original_models:
simplified_name = original_model.split('/')[-1]
if simplified_name in model_mapping:
# Conflict detected, handle as per instructions
print(f"Conflict detected for model name '{simplified_name}'. Excluding '{original_model}' from available models.")
continue
model_mapping[simplified_name] = original_model
simplified_models.append(simplified_name)
def generate(
model,
messages,
temperature=0.7,
top_p=1.0,
n=1,
stream=False,
stop=None,
max_tokens=None,
presence_penalty=0.0,
frequency_penalty=0.0,
logit_bias=None,
user=None,
timeout=30,
):
"""
Generates a chat completion using the provided model and messages.
"""
# Use the simplified model names
models = simplified_models
if model not in models:
raise ValueError(f"Invalid model: {model}. Choose from: {', '.join(models)}")
# Map simplified model name to original model name
original_model = model_mapping[model]
api_endpoint = "https://chat.typegpt.net/api/openai/v1/chat/completions"
headers = {
"authority": "chat.typegpt.net",
"accept": "application/json, text/event-stream",
"accept-language": "en-US,en;q=0.9",
"content-type": "application/json",
"origin": "https://chat.typegpt.net",
"referer": "https://chat.typegpt.net/",
"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"
}
# Payload
payload = {
"messages": messages,
"stream": stream,
"model": original_model,
"temperature": temperature,
"presence_penalty": presence_penalty,
"frequency_penalty": frequency_penalty,
"top_p": top_p,
}
# Only include max_tokens if it's not None
if max_tokens is not None:
payload["max_tokens"] = max_tokens
# Only include 'stop' if it's not None
if stop is not None:
payload["stop"] = stop
# Check if logit_bias is provided
if logit_bias is not None:
payload["logit_bias"] = logit_bias
# Include 'user' if provided
if user is not None:
payload["user"] = user
# Start the request
session = requests.Session()
response = session.post(
api_endpoint, headers=headers, json=payload, stream=stream, timeout=timeout
)
if not response.ok:
raise Exception(f"Failed to generate response - ({response.status_code}, {response.reason}) - {response.text}")
def stream_response():
for line in response.iter_lines():
if line:
line = line.decode("utf-8")
if line.startswith("data: "):
line = line[6:] # Remove "data: " prefix
if line.strip() == "[DONE]":
break
try:
data = json.loads(line)
yield data
except json.JSONDecodeError:
continue
if stream:
return stream_response()
else:
return response.json()
if __name__ == "__main__":
# Example usage
# model = "claude-3-5-sonnet-20240620"
# model = "qwen1.5-0.5b-chat"
# model = "llama-2-7b-chat-fp16"
model = "gpt-3.5-turbo"
messages = [
{"role": "system", "content": "Be Detailed"},
{"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."}
]
# try:
# # For non-streamed response
# response = generate(
# model=model,
# messages=messages,
# temperature=0.5,
# max_tokens=4000,
# stream=False # Change to True for streaming
# )
# if 'choices' in response:
# reply = response['choices'][0]['message']['content']
# print(reply)
# else:
# print("No response received.")
# except Exception as e:
# print(e)
try:
# For streamed response
response = generate(
model=model,
messages=messages,
temperature=0.5,
max_tokens=4000,
stream=True, # Change to False for non-streamed response
)
for data in response:
if 'choices' in data:
reply = data['choices'][0]['delta']['content']
print(reply, end="", flush=True)
else:
print("No response received.")
except Exception as e:
print(e)
```
```python
from fastapi import FastAPI, Request, Response
from fastapi.responses import JSONResponse, StreamingResponse
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
import asyncio
import json
import requests
from TYPEGPT.typegpt_api import generate, model_mapping, simplified_models
from api_info import developer_info
app = FastAPI()
# Set up CORS middleware if needed
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/health_check")
async def health_check():
return {"status": "OK"}
@app.get("/models")
async def get_models():
# Retrieve models from TypeGPT API and forward the response
api_endpoint = "https://chat.typegpt.net/api/openai/v1/models"
try:
response = requests.get(api_endpoint)
# return response.text
return JSONResponse(content=response.json(), status_code=response.status_code)
except Exception as e:
return JSONResponse(content={"error": str(e)}, status_code=500)
@app.post("/chat/completions")
async def chat_completions(request: Request):
# Receive the JSON payload
try:
body = await request.json()
except Exception as e:
return JSONResponse(content={"error": "Invalid JSON payload"}, status_code=400)
# Extract parameters
model = body.get("model")
messages = body.get("messages")
temperature = body.get("temperature", 0.7)
top_p = body.get("top_p", 1.0)
n = body.get("n", 1)
stream = body.get("stream", False)
stop = body.get("stop")
max_tokens = body.get("max_tokens")
presence_penalty = body.get("presence_penalty", 0.0)
frequency_penalty = body.get("frequency_penalty", 0.0)
logit_bias = body.get("logit_bias")
user = body.get("user")
timeout = 30 # or set based on your preference
# Validate required parameters
if not model:
return JSONResponse(content={"error": "The 'model' parameter is required."}, status_code=400)
if not messages:
return JSONResponse(content={"error": "The 'messages' parameter is required."}, status_code=400)
# Call the generate function
try:
if stream:
async def generate_stream():
response = generate(
model=model,
messages=messages,
temperature=temperature,
top_p=top_p,
n=n,
stream=True,
stop=stop,
max_tokens=max_tokens,
presence_penalty=presence_penalty,
frequency_penalty=frequency_penalty,
logit_bias=logit_bias,
user=user,
timeout=timeout,
)
for chunk in response:
yield f"data: {json.dumps(chunk)}\n\n"
yield "data: [DONE]\n\n"
return StreamingResponse(
generate_stream(),
media_type="text/event-stream",
headers={
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Transfer-Encoding": "chunked"
}
)
else:
response = generate(
model=model,
messages=messages,
temperature=temperature,
top_p=top_p,
n=n,
stream=False,
stop=stop,
max_tokens=max_tokens,
presence_penalty=presence_penalty,
frequency_penalty=frequency_penalty,
logit_bias=logit_bias,
user=user,
timeout=timeout,
)
return JSONResponse(content=response)
except Exception as e:
return JSONResponse(content={"error": str(e)}, status_code=500)
@app.get("/developer_info")
async def get_developer_info():
return JSONResponse(content=developer_info)
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)
``` |