AIMaster7 commited on
Commit
7c9f89e
·
verified ·
1 Parent(s): 5c68b37

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +103 -0
main.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request
2
+ from fastapi.responses import StreamingResponse, JSONResponse
3
+ from pydantic import BaseModel
4
+ import requests
5
+ import time
6
+ import json
7
+ from typing import List, Optional
8
+ from models import AVAILABLE_MODELS
9
+
10
+ app = FastAPI()
11
+
12
+ def unix_id():
13
+ return str(int(time.time() * 1000))
14
+
15
+ class Message(BaseModel):
16
+ role: str
17
+ content: str
18
+
19
+ class ChatRequest(BaseModel):
20
+ messages: List[Message]
21
+ model: str
22
+ stream: Optional[bool] = False
23
+
24
+ @app.get("/v1/models")
25
+ async def list_models():
26
+ return {"object": "list", "data": AVAILABLE_MODELS}
27
+
28
+ @app.post("/v1/chat/completions")
29
+ async def chat_completion(request: ChatRequest):
30
+ headers = {
31
+ 'accept': 'text/event-stream',
32
+ 'content-type': 'application/json',
33
+ 'origin': 'https://www.chatwithmono.xyz',
34
+ 'referer': 'https://www.chatwithmono.xyz/',
35
+ 'user-agent': 'Mozilla/5.0',
36
+ }
37
+
38
+ payload = {
39
+ "messages": [{"role": msg.role, "content": msg.content} for msg in request.messages],
40
+ "model": request.model
41
+ }
42
+
43
+ if request.stream:
44
+ def event_stream():
45
+ with requests.post(
46
+ "https://www.chatwithmono.xyz/api/chat",
47
+ headers=headers,
48
+ json=payload,
49
+ stream=True,
50
+ timeout=120
51
+ ) as response:
52
+ for line in response.iter_lines(decode_unicode=True):
53
+ if line.startswith("0:"):
54
+ piece = line[2:]
55
+ yield f"data: {json.dumps({'choices': [{'delta': {'content': piece}, 'finish_reason': None}]})}\n\n"
56
+ elif line.startswith(("e:", "d:")):
57
+ yield "data: [DONE]\n\n"
58
+ return StreamingResponse(event_stream(), media_type="text/event-stream")
59
+
60
+ else:
61
+ assistant_response = ""
62
+ usage_info = {}
63
+
64
+ with requests.post(
65
+ "https://www.chatwithmono.xyz/api/chat",
66
+ headers=headers,
67
+ json=payload,
68
+ stream=True,
69
+ timeout=120
70
+ ) as response:
71
+ for chunk in response.iter_lines(decode_unicode=True):
72
+ if chunk.startswith("0:"):
73
+ try:
74
+ piece = json.loads(chunk[2:])
75
+ assistant_response += piece
76
+ except:
77
+ continue
78
+ elif chunk.startswith(("e:", "d:")):
79
+ try:
80
+ data = json.loads(chunk[2:])
81
+ usage_info = data.get("usage", {})
82
+ except:
83
+ continue
84
+
85
+ return JSONResponse(content={
86
+ "id": f"chatcmpl-{unix_id()}",
87
+ "object": "chat.completion",
88
+ "created": int(time.time()),
89
+ "model": request.model,
90
+ "choices": [{
91
+ "index": 0,
92
+ "message": {
93
+ "role": "assistant",
94
+ "content": assistant_response
95
+ },
96
+ "finish_reason": "stop"
97
+ }],
98
+ "usage": {
99
+ "prompt_tokens": usage_info.get("promptTokens", 0),
100
+ "completion_tokens": usage_info.get("completionTokens", 0),
101
+ "total_tokens": usage_info.get("promptTokens", 0) + usage_info.get("completionTokens", 0),
102
+ }
103
+ })