forked from formzs/poe-to-gpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
381 lines (332 loc) · 15.1 KB
/
app.py
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
from typing import List, Optional, Dict
from pydantic import BaseModel
import asyncio
import uvicorn
import os
import logging
import itertools
import json
from httpx import AsyncClient
from fastapi import FastAPI, HTTPException, Depends, APIRouter
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from fastapi.responses import StreamingResponse
from fastapi.middleware.cors import CORSMiddleware
from fastapi_poe.types import ProtocolMessage
from fastapi_poe.client import get_bot_response, get_final_response, QueryRequest
app = FastAPI()
security = HTTPBearer()
router = APIRouter()
# 从环境变量获取配置
PORT = int(os.getenv("PORT", "3700"))
TIMEOUT = int(os.getenv("TIMEOUT", "120"))
PROXY_URL = os.getenv("PROXY", "")
API_KEYS = os.getenv("POE_API_KEY", "").split(",")
ACCESS_TOKENS = set(os.getenv("ACCESS_TOKEN", "").split(","))
# 设置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
# 初始化代理
proxy = None
if not PROXY_URL:
proxy = AsyncClient(timeout=TIMEOUT)
else:
proxy = AsyncClient(proxy=PROXY_URL, timeout=TIMEOUT)
# 初始化客户端字典和API密钥循环
client_dict = {}
api_key_cycle = None
bot_names = {
"Assistant", "GPT-3.5-Turbo", "GPT-3.5-Turbo-16k", "GPT-3.5-Turbo-lnstruct", "GPT-4o", "GPT-4o-128k",
"GPT-4o-Mini", "GPT-4o-Mini-128k", "ChatGPT-4o-Latest", "ChatGPT-4o-Latest-128k", "GPT-4o-Aug-128k",
"o1", "o1-mini", "o1-preview", "Claude-3.5-Sonnet", "Claude-3.5-Sonnet-200k", "Claude-3.5-Haiku",
"Claude-3.5-Haiku-200k", "Claude-3.5-Sonnet-June", "Claude-3.5-Sonnet-June-200k", "Claude-3-opus",
"Claude-3-opus-200k", "Claude-3-Sonnet", "Claude-3-Sonnet-200k", "Claude-3-Haiku", "Claude-3-Haiku-200k",
"Gemini-2.0-Flash", "Gemini-2.0-Google", "Gemini-1.5-Pro", "Gemini-1.5-Pro-Search", "Gemini-1.5-Pro-128k", "Gemini-1.5-Pro-2M", "Gemini-1.5-Flash",
"Gemini-1.5-Flash-Search", "Gemini-1.5-Flash-128k", "Gemini-1.5-Flash-1M", "Grok-beta","Qwen-QwQ-32b-preview",
"Qwen-2.5-Coder-32B-T", "Qwen-2.5-72B-T", "Llama-3.1-405B", "Llama-3.1-405B-T", "Llama-3.1-405B-FP16",
"Llama-3.1-405B-FW-128k", "Llama-3.1-70B", "Llama-3.1-70B-FP16", "Llama-3.1-70B-T-128k",
"Llama-3.1-70B-FW-128k", "Llama-3.1-8B", "Llama-3.1-8B-FP16", "Llama-3.1-8B-T-128k", "DALL-E-3",
"StableDiffusionXL", "StableDiffusion3.5-T", "StableDiffusion3.5-L", "StableDiffusion3", "SD3-Turbo",
"FLUX-pro", "FLUX-pro-1.1", "FLUX-pro-1.1-T", "FLUX-pro-1.1-ultra", "FLUX-schnell", "FLUX-dev",
"Luma-Photon", "Luma-Photon-Flash", "Playground-v3", "Ideogram-v2", "Imagen3", "Imagen3-Fast", "YUN"
}
bot_names_map = {name.lower(): name for name in bot_names}
class Message(BaseModel):
role: str
content: str
class CompletionRequest(BaseModel):
model: str
messages: List[Message]
stream: Optional[bool] = False
temperature: Optional[float] = 0.7
skip_system_prompt: Optional[bool] = None
frequency_penalty: Optional[float] = 0.0
presence_penalty: Optional[float] = 0.0
logit_bias: Optional[Dict[str, int]] = None
stop_sequences: Optional[List[str]] = None
class Config:
json_schema_extra = {
"example": {
"model": "gpt-3.5-turbo",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
],
"stream": True
}
}
async def add_token(token: str):
global api_key_cycle
if not token:
logger.error("Empty token provided")
return "failed: empty token"
if token not in client_dict:
try:
logger.info(f"Attempting to add apikey: {token[:6]}...") # 只记录前6位
request = CompletionRequest(
model="gpt-3.5-turbo",
messages=[Message(role="user", content="Please return 'OK'")],
temperature=0.7
)
ret = await get_responses(request, token)
if ret == "OK":
client_dict[token] = token
api_key_cycle = itertools.cycle(client_dict.values())
logger.info(f"apikey added successfully: {token[:6]}...")
return "ok"
else:
logger.error(f"Failed to add apikey: {token[:6]}..., response: {ret}")
return "failed"
except Exception as exception:
logger.error(f"Failed to connect to poe due to {str(exception)}")
return f"failed: {str(exception)}"
else:
logger.info(f"apikey already exists: {token[:6]}...")
return "exist"
async def get_responses(request: CompletionRequest, token: str):
if not token:
raise HTTPException(status_code=400, detail="Token is required")
model_lower = request.model.lower()
if model_lower in bot_names_map:
request.model = bot_names_map[model_lower]
message = [
ProtocolMessage(role=msg.role if msg.role in ["user", "system"] else "bot", content=msg.content)
for msg in request.messages
]
additional_params = {
"temperature": request.temperature,
"skip_system_prompt": request.skip_system_prompt if request.skip_system_prompt is not None else False,
"logit_bias": request.logit_bias if request.logit_bias is not None else {},
"stop_sequences": request.stop_sequences if request.stop_sequences is not None else []
}
query = QueryRequest(
query=message,
user_id="",
conversation_id="",
message_id="",
version="1.0",
type="query",
**additional_params
)
try:
return await get_final_response(query, bot_name=request.model, api_key=token, session=proxy)
except Exception as e:
logger.error(f"Error in get_final_response: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
else:
raise HTTPException(status_code=400, detail=f"Model {request.model} is not supported")
async def verify_token(credentials: HTTPAuthorizationCredentials = Depends(security)):
if not credentials:
raise HTTPException(
status_code=401,
detail="Missing authentication credentials",
headers={"WWW-Authenticate": "Bearer"},
)
if not credentials.credentials:
raise HTTPException(
status_code=401,
detail="Invalid authentication credentials",
headers={"WWW-Authenticate": "Bearer"},
)
if credentials.credentials not in ACCESS_TOKENS:
raise HTTPException(
status_code=401,
detail="Invalid API key",
headers={"WWW-Authenticate": "Bearer"},
)
return credentials.credentials
@app.get("/v1/models")
async def list_models(token: str = Depends(verify_token)):
return {
"data": [{"id": name, "object": "model"} for name in bot_names],
"object": "list"
}
@app.post("/v1/chat/completions")
async def chat_completions(request: CompletionRequest, token: str = Depends(verify_token)):
request_id = "chat$poe-to-gpt$-" + token[:6]
try:
# 记录接收到的请求参数
logger.info(f"Received request parameters:")
logger.info(f"Model: {request.model}")
logger.info(f"Messages: {[{'role': msg.role, 'content': msg.content} for msg in request.messages]}")
logger.info(f"Stream: {request.stream}")
logger.info(f"Temperature: {request.temperature}")
logger.info(f"Skip system prompt: {request.skip_system_prompt}")
logger.info(f"Frequency penalty: {request.frequency_penalty}")
logger.info(f"Presence penalty: {request.presence_penalty}")
logger.info(f"Logit bias: {request.logit_bias}")
logger.info(f"Stop sequences: {request.stop_sequences}")
# 记录实际使用的模型名称
model_name = request.model.lower()
if model_name == "yun":
model_name = "gemini-2.0-flash"
actual_model = bot_names_map.get(model_name)
logger.info(f"Mapped model name: {model_name} -> {actual_model}")
# 打印请求参数(隐藏敏感信息)
safe_request = request.model_dump()
if "messages" in safe_request:
safe_request["messages"] = [
{**msg, "content": msg["content"][:100] + "..." if len(msg["content"]) > 100 else msg["content"]}
for msg in safe_request["messages"]
]
logger.info(f"Request [{request_id}]: {json.dumps(safe_request, ensure_ascii=False)}")
if not api_key_cycle:
raise HTTPException(status_code=500, detail="No valid API tokens available")
if model_name not in bot_names_map:
raise HTTPException(status_code=400, detail=f"Model {request.model} not found")
request.model = bot_names_map[model_name]
protocol_messages = [
ProtocolMessage(role=msg.role if msg.role in ["user", "system"] else "bot", content=msg.content)
for msg in request.messages
]
poe_token = next(api_key_cycle)
if request.stream:
async def response_generator():
total_response = ""
first_chunk = True
try:
async for partial in get_bot_response(protocol_messages, bot_name=request.model, api_key=poe_token,
session=proxy):
if partial and partial.text:
total_response += partial.text
chunk = {
"id": request_id,
"object": "chat.completion.chunk",
"created": int(asyncio.get_event_loop().time()),
"model": request.model,
"choices": [{
"delta": {
"role": "assistant" if first_chunk else None,
"content": partial.text
},
"index": 0,
"finish_reason": None
}]
}
# Remove None values from delta
if chunk["choices"][0]["delta"].get("role") is None:
del chunk["choices"][0]["delta"]["role"]
yield f"data: {json.dumps(chunk)}\n\n"
first_chunk = False
# 发送结束标记
end_chunk = {
"id": request_id,
"object": "chat.completion.chunk",
"created": int(asyncio.get_event_loop().time()),
"model": request.model,
"choices": [{
"delta": {},
"index": 0,
"finish_reason": "stop"
}]
}
yield f"data: {json.dumps(end_chunk)}\n\n"
yield "data: [DONE]\n\n"
# 打印完整的流式响应(限制长度)
logger.info(f"Stream Response [{request_id}]: {total_response[:200]}..." if len(
total_response) > 200 else total_response)
except Exception as e:
logger.error(f"Error in stream generation for [{request_id}]: {str(e)}")
error_chunk = {
"id": request_id,
"object": "chat.completion.chunk",
"created": int(asyncio.get_event_loop().time()),
"model": request.model,
"choices": [{
"delta": {"content": f"\nError: {str(e)}"},
"index": 0,
"finish_reason": "error"
}]
}
yield f"data: {json.dumps(error_chunk)}\n\n"
yield "data: [DONE]\n\n"
raise
return StreamingResponse(response_generator(), media_type="text/event-stream")
else:
response = await get_responses(request, poe_token)
response_data = {
"id": request_id,
"object": "chat.completion",
"created": int(asyncio.get_event_loop().time()),
"model": request.model,
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": response
},
"finish_reason": "stop"
}]
}
# 打印完整响应(限制长度)
safe_response = {**response_data}
if len(response) > 200:
logger.info(f"Response [{request_id}]: {json.dumps(safe_response, ensure_ascii=False)[:200]}...")
else:
logger.info(f"Response [{request_id}]: {json.dumps(safe_response, ensure_ascii=False)}")
return response_data
except GeneratorExit:
logger.info(f"GeneratorExit exception caught for request [{request_id}]")
except Exception as e:
error_msg = f"Error during response for request [{request_id}]: {str(e)}"
logger.error(error_msg)
raise HTTPException(status_code=500, detail=str(e))
# Keep the original endpoint for backward compatibility
@app.post("/chat/completions")
async def chat_completions_legacy(request: CompletionRequest, token: str = Depends(verify_token)):
return await chat_completions(request, token)
async def initialize_tokens(tokens: List[str]):
if not tokens or all(not token for token in tokens):
logger.error("No API keys found in the configuration.")
sys.exit(1)
else:
for token in tokens:
await add_token(token)
if not client_dict:
logger.error("No valid tokens were added.")
sys.exit(1)
else:
global api_key_cycle
api_key_cycle = itertools.cycle(client_dict.values())
logger.info(f"Successfully initialized {len(client_dict)} API tokens")
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Allows all origins
allow_credentials=True,
allow_methods=["*"], # Allows all methods
allow_headers=["*"], # Allows all headers
)
async def main(tokens: List[str] = None):
"""
主函数,初始化API令牌并启动服务器
"""
if not tokens and not API_KEYS:
logger.error("No API tokens provided")
return
tokens_to_use = tokens if tokens else API_KEYS
await initialize_tokens(tokens_to_use)
config = uvicorn.Config(app, host="0.0.0.0", port=PORT)
server = uvicorn.Server(config)
await server.serve()
if __name__ == "__main__":
asyncio.run(main())