Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from asgiref.wsgi import WsgiToAsgi
|
2 |
+
from flask import Flask, request, Response
|
3 |
+
import requests
|
4 |
+
import json
|
5 |
+
import random
|
6 |
+
from helper import create_jwt, generate_random_tuple
|
7 |
+
|
8 |
+
app = Flask(__name__)
|
9 |
+
|
10 |
+
@app.route('/chat/completions', methods=['POST'])
|
11 |
+
async def chat():
|
12 |
+
"""
|
13 |
+
Handle chat completion requests.
|
14 |
+
|
15 |
+
This function processes incoming POST requests to the '/chat/completions' endpoint.
|
16 |
+
It prepares the payload for the LLM API, generates a JWT for authentication,
|
17 |
+
and streams the response from the LLM API back to the client.
|
18 |
+
|
19 |
+
Returns:
|
20 |
+
Response: A streaming response containing the LLM API's output.
|
21 |
+
|
22 |
+
Note:
|
23 |
+
- The function uses environment variables for proxy configuration.
|
24 |
+
- It generates random GitHub username and Zed user ID for each request.
|
25 |
+
- The LLM model defaults to "claude-3-5-sonnet-20240620" if not specified.
|
26 |
+
"""
|
27 |
+
# Get the payload from the request
|
28 |
+
payload = request.json
|
29 |
+
|
30 |
+
# Get the model from the payload, defaulting to "claude-3-5-sonnet-20240620"
|
31 |
+
model = payload.get('model', 'claude-3-5-sonnet-20240620')
|
32 |
+
|
33 |
+
# Prepare the request for the LLM API
|
34 |
+
url = "https://llm.zed.dev/completion?"
|
35 |
+
|
36 |
+
llm_payload = {
|
37 |
+
"provider": "anthropic",
|
38 |
+
"model": model,
|
39 |
+
"provider_request": {
|
40 |
+
"model": model,
|
41 |
+
"max_tokens": payload.get('max_tokens', 8192),
|
42 |
+
"temperature": payload.get('temperature', 0),
|
43 |
+
"top_p": payload.get('top_p', 0.7),
|
44 |
+
"messages": payload['messages'],
|
45 |
+
"system": ""
|
46 |
+
}
|
47 |
+
}
|
48 |
+
|
49 |
+
# github_username, zed_user_id = random.choice(github_username_zed_userid_list)
|
50 |
+
github_username, zed_user_id = generate_random_tuple()
|
51 |
+
jwt = create_jwt(github_username, zed_user_id)
|
52 |
+
|
53 |
+
headers = {
|
54 |
+
'Host': 'llm.zed.dev',
|
55 |
+
'accept': '*/*',
|
56 |
+
'content-type': 'application/json',
|
57 |
+
'authorization': f'Bearer {jwt}',
|
58 |
+
'user-agent': 'Zed/0.149.3 (macos; aarch64)'
|
59 |
+
}
|
60 |
+
|
61 |
+
# Get proxy from environment variable
|
62 |
+
proxy = os.environ.get('HTTP_PROXY', None)
|
63 |
+
proxies = {'http': proxy, 'https': proxy} if proxy else None
|
64 |
+
|
65 |
+
async def generate():
|
66 |
+
with requests.post(url, headers=headers, json=llm_payload, stream=True, proxies=proxies) as response:
|
67 |
+
for chunk in response.iter_content(chunk_size=1024):
|
68 |
+
if chunk:
|
69 |
+
yield chunk
|
70 |
+
|
71 |
+
return Response(generate(), content_type='application/octet-stream')
|
72 |
+
|
73 |
+
# Convert the Flask app to an ASGI app
|
74 |
+
asgi_app = WsgiToAsgi(app)
|
75 |
+
|
76 |
+
if __name__ == '__main__':
|
77 |
+
import uvicorn
|
78 |
+
uvicorn.run(asgi_app, host="0.0.0.0", port=8000)
|