-
Notifications
You must be signed in to change notification settings - Fork 1
/
custom_gpt.py
312 lines (267 loc) · 12 KB
/
custom_gpt.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
import hashlib
import json
import re
import uuid
import aiohttp
import time
import requests
import functools
from fastapi import HTTPException, APIRouter, Header
from pydantic import BaseModel, Field
from typing import List, Optional, Literal, AsyncGenerator, Callable, Any
from loguru import logger
from starlette.responses import StreamingResponse
CUSTOM_PERSONA = "You are a helpful assistant."
def async_timer(func: Callable) -> Callable:
@functools.wraps(func)
async def wrapper(*args, **kwargs) -> Any:
start_time = time.time()
result = await func(*args, **kwargs)
end_time = time.time()
logger.info(f"Async function {func.__name__} took {end_time - start_time:.2f}s to execute")
return result
return wrapper
class Message(BaseModel):
role: Literal["user", "assistant", "system"]
content: str
class ChatCompletionRequest(BaseModel):
model: str = Field(default="gpt-4-o")
messages: List[Message]
temperature: Optional[float] = 1.0
top_p: Optional[float] = 1.0
n: Optional[int] = 1
stream: Optional[bool] = False
stop: Optional[List[str]] = None
max_tokens: Optional[int] = None
presence_penalty: Optional[float] = 0
frequency_penalty: Optional[float] = 0
user: Optional[str] = None
class ChatCompletionChoice(BaseModel):
index: int
message: Message
finish_reason: str
class Usage(BaseModel):
prompt_tokens: int
completion_tokens: int
total_tokens: int
class ChatCompletionResponse(BaseModel):
id: str
object: str = "chat.completion"
created: int
model: str
system_fingerprint: str
choices: List[ChatCompletionChoice]
usage: Usage
async def generate_system_fingerprint() -> str:
unique_id = f"{time.time()}{uuid.uuid4()}"
fingerprint = hashlib.sha256(unique_id.encode()).hexdigest()
return fingerprint[:16]
async def format_response(messages: List[Message], assistant_response: str) -> ChatCompletionResponse:
return ChatCompletionResponse(
id=f"chatcmpl-{int(time.time() * 1000)}",
created=int(time.time()),
model="gpt-4-o",
system_fingerprint=f"fp_{await generate_system_fingerprint()}",
choices=[
ChatCompletionChoice(
index=0,
message=Message(
role="user",
content=assistant_response
),
finish_reason="stop"
)
],
usage=Usage(
prompt_tokens=sum(len(m.content) for m in messages),
completion_tokens=len(assistant_response),
total_tokens=sum(len(m.content) for m in messages) + len(assistant_response)
)
)
async def get_all_conversations(project_id, api_key):
url = f"https://app.customgpt.ai/api/v1/projects/{project_id}/conversations"
headers = {
"accept": "application/json",
"authorization": f"Bearer {api_key}"
}
try:
response = requests.get(url, headers=headers)
conversations = response.json()['data']['data']
return conversations
except Exception as e:
raise HTTPException(status_code=500, detail=f"Failed to get conversations: {str(e)}")
async def create_conversation(project_id, api_key, conversation_name: str = "Default"):
url = f"https://app.customgpt.ai/api/v1/projects/{project_id}/conversations"
payload = {"name": conversation_name}
headers = {
"accept": "application/json",
"content-type": "application/json",
"authorization": f"Bearer {api_key}"
}
try:
response = requests.post(url, json=payload, headers=headers)
session_id = response.json()['data']['session_id']
return session_id
except Exception as e:
raise HTTPException(status_code=500, detail=f"Failed to create conversation: {str(e)}")
class ChatBot:
def __init__(self, custom_persona: str):
self.custom_persona = custom_persona
self.conversation_history = []
self.session_id = None
async def none_stream_chat_completion(self, messages: List[Message], project_id, api_key) -> ChatCompletionResponse:
context = "\n".join([f"{msg.role}: {msg.content}" for msg in messages])
url = f"https://app.customgpt.ai/api/v1/projects/{project_id}/conversations/{self.session_id}/messages"
payload = {
"response_source": "openai_content",
"prompt": context,
"custom_persona": self.custom_persona,
"chatbot_model": "gpt-4-o"
}
headers = {
"accept": "application/json",
"content-type": "application/json",
"authorization": f"Bearer {api_key}"
}
try:
response = requests.post(url, json=payload, headers=headers)
response_data = response.json()
assistant_response = response_data['data']['openai_response']
return await format_response(messages, assistant_response)
except Exception as e:
raise HTTPException(status_code=500, detail=f"Failed to send message: {str(e)}")
async def stream_chat_completion(
self,
messages: List[Message],
project_id,
api_key
) -> AsyncGenerator[str, None]:
prompt = messages[-1].content
url = f"https://app.customgpt.ai/api/v1/projects/{project_id}/conversations/{self.session_id}/messages?stream=true&lang=zh"
payload = {
"response_source": "openai_content",
"prompt": prompt,
"custom_persona": self.custom_persona,
"chatbot_model": "gpt-4-o"
}
headers = {
"accept": "application/json",
"content-type": "application/json",
"authorization": f"Bearer {api_key}"
}
try:
async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=300)) as session:
async with session.post(url, json=payload, headers=headers) as response:
if response.status != 200:
error_text = await response.text()
raise HTTPException(
status_code=response.status,
detail=f"CustomGPT API error: {error_text}"
)
start_response = {
"id": f"chatcmpl-{int(time.time() * 1000)}",
"object": "chat.completion.chunk",
"created": int(time.time()),
"model": "gpt-4-o",
"system_fingerprint": f"fp_{await generate_system_fingerprint()}",
"choices": [{
"index": 0,
"delta": {"role": "assistant"},
"finish_reason": None
}]
}
yield f"data: {json.dumps(start_response)}\n\n"
async for line_bytes in response.content:
line = line_bytes.decode('utf-8')
for current_line in line.split('\n'):
if not current_line.strip():
continue
if current_line.startswith("event: "):
current_event = current_line[7:].strip()
elif current_line.startswith("data: "):
try:
data = json.loads(current_line[6:])
if current_event == "progress" and data["status"] == "progress":
chunk_response = {
"id": f"chatcmpl-{int(time.time() * 1000)}",
"object": "chat.completion.chunk",
"created": int(time.time()),
"model": "gpt-4-o",
"system_fingerprint": f"fp_{await generate_system_fingerprint()}",
"choices": [{
"index": 0,
"delta": {
"content": data["message"]
},
"finish_reason": None
}]
}
yield f"data: {json.dumps(chunk_response)}\n\n"
elif current_event == "finish" and data["status"] == "finish":
finish_response = {
"id": f"chatcmpl-{int(time.time() * 1000)}",
"object": "chat.completion.chunk",
"created": int(time.time()),
"model": "gpt-4-o",
"system_fingerprint": f"fp_{await generate_system_fingerprint()}",
"choices": [{
"index": 0,
"delta": {},
"finish_reason": "stop"
}]
}
yield f"data: {json.dumps(finish_response)}\n\n"
yield "data: [DONE]\n\n"
except json.JSONDecodeError:
continue
except aiohttp.ClientError as e:
raise HTTPException(status_code=500, detail=f"Network error: {str(e)}")
except Exception as e:
raise HTTPException(status_code=500, detail=f"Unexpected error: {str(e)}")
chatbot = ChatBot(CUSTOM_PERSONA)
router = APIRouter()
@router.post("/v1/chat/completions", response_model=ChatCompletionResponse)
@async_timer
async def chat(request: ChatCompletionRequest, authorization: str = Header(None)):
try:
# Verify authorization header
if not authorization:
raise HTTPException(
status_code=401,
detail="Authorization header is required"
)
# Extract the API key - assuming Bearer token format
match = re.match(r'Bearer\s+(\S+)', authorization)
if not match:
raise HTTPException(
status_code=401,
detail="Invalid authorization format. Must be 'Bearer <token>'"
)
api_key = match.group(1).split("#")[1].strip()
# Extract project ID using a more flexible method
project_id = match.group(1).split("#")[0].strip()
if not project_id:
raise HTTPException(
status_code=401,
detail="Project ID is missing in the authorization header"
)
conversations = await get_all_conversations(project_id, api_key)
for conv in conversations:
if conv['name'] == 'Default':
chatbot.session_id = conv['session_id']
break
if not chatbot.session_id:
chatbot.session_id = await create_conversation(project_id, api_key)
system_content = next((message.content for message in request.messages if message.role == "system"), None)
if system_content:
chatbot.custom_persona = system_content
if request.stream:
return StreamingResponse(
chatbot.stream_chat_completion(request.messages, project_id, api_key),
media_type="text/event-stream"
)
else:
response = await chatbot.none_stream_chat_completion(request.messages, project_id, api_key)
return response
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))