-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
75 lines (64 loc) · 2.25 KB
/
main.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
from fastapi import FastAPI, Request, status, HTTPException, Depends
from fastapi.responses import StreamingResponse
from fastapi.security import OAuth2PasswordBearer
from fastapi.middleware.cors import CORSMiddleware
import asyncio
import json
import uuid
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
def data_generator():
response_id = uuid.uuid4().hex
sentence = "Hello this is a test response from a fixed OpenAI endpoint."
words = sentence.split(" ")
for word in words:
word = word + " "
chunk = {
"id": f"chatcmpl-{response_id}",
"object": "chat.completion.chunk",
"created": 1677652288,
"model": "claude-3-5-sonnet-20241022",
"choices": [{"index": 0, "delta": {"content": word}}],
}
try:
yield f"data: {json.dumps(chunk.dict())}\n\n"
except:
yield f"data: {json.dumps(chunk)}\n\n"
# for completion
@app.post("/v1/messages")
async def completion(request: Request):
data = await request.json()
if data.get("stream") == True:
return StreamingResponse(
content=data_generator(),
media_type="text/event-stream",
)
else:
response = {
"id": "msg_01G7MsdWPT2JZMUuc1UXRavn",
"type": "message",
"role": "assistant",
"content": [
{
"type": "text",
"text": "I'm sorry, but the string of characters \"123450000s0 p kk\" doesn't appear to have any clear meaning or context. It seems to be a random combination of numbers and letters. If you could provide more information or clarify what you're trying to communicate, I'll do my best to assist you."
}
],
"model": "claude-3-5-sonnet-20241022",
"stop_reason": "end_turn",
"stop_sequence": None,
"usage": {
"input_tokens": 17,
"output_tokens": 71
}
}
return response
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8093)