-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcode.py
More file actions
136 lines (109 loc) · 3.68 KB
/
Copy pathcode.py
File metadata and controls
136 lines (109 loc) · 3.68 KB
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
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse, StreamingResponse
from sth import *
import asyncio
import time
import hashlib
from typing import Dict, Optional
token_cache: Dict[str, dict] = {}
cache_lock = asyncio.Lock()
def get_cache_key(email: str) -> str:
return hashlib.md5(email.encode()).hexdigest()
async def get_cached_token(email: str) -> Optional[str]:
key = get_cache_key(email)
async with cache_lock:
if key in token_cache:
cached = token_cache[key]
if time.time() < cached["expires_at"]:
return cached["token"]
else:
del token_cache[key]
return None
async def cache_token(email: str, token: str, expires_at: int):
key = get_cache_key(email)
async with cache_lock:
token_cache[key] = {"token": token, "expires_at": expires_at, "email": email}
app = FastAPI()
# @app.get("/")
# def index():
# return {"Hello": "World"}
@app.get("/v1/models")
def get_models():
modellist={
'data': [
{
'id': 'coder-model',
'name': 'Coder Model',
'object': 'model',
'owned_by': 'qwen'
},
{
'id': 'vision-model',
'name': 'Vision Model',
'object': 'model',
'owned_by': 'qwen',
'info': {
'id': 'vision-model',
'base_model_id': None,
'name': 'Vision Model',
'meta': {
'capabilities': {
'vision': True
}
}
}
}
]
}
return modellist
@app.post("/v1/chat/completions")
async def chat_completions(request: Request):
body = await request.json()
# print('Body: ',body)
model = body.get("model")
messages = body.get("messages")
stream = body.get("stream")
# print(messages)
# print('Headers: ',request.headers)
auth = request.headers.get("Authorization", "")
# Key format: 'email:password'
email, password = parse_api_key(auth)
# 使用缓存的 token
token = await get_cached_token(email)
if not token:
token, expires_at = await login_with_password(email, password)
await cache_token(email, token, expires_at)
# print(f"Cached token for {email} expires at {expires_at}")
# To authorize
code_verifier, code_challenge = generate_pkce_pair()
device_code_info = request_device_code(code_challenge)
# print(f"To {device_code_info['verification_uri']}?user_code={device_code_info['user_code']}&client=qwen-code")
authorize(device_code_info["user_code"], token)
# print('Authorized.')
# Get the token
token_response = poll_for_token(device_code_info["device_code"], code_verifier)
access_token = token_response["access_token"]
# refresh_token = token_response['refresh_token']
result = call_qwen_api(access_token, model, messages, stream)
if stream:
def iter_chunks():
try:
for chunk in result:
yield f"data: {chunk.to_json(indent=None)}\n\n"
yield "data: [DONE]\n\n"
finally:
try:
result.close()
except Exception:
pass
return StreamingResponse(iter_chunks(), media_type="text/event-stream")
else:
return JSONResponse(content=result.to_dict(mode="json"))
if __name__ == "__main__":
import uvicorn
uvicorn.run(
app,
host="0.0.0.0",
port=8000,
log_level="info",
)