Update app.py
Browse files
app.py
CHANGED
@@ -1,33 +1,30 @@
|
|
1 |
-
from
|
2 |
-
from
|
3 |
-
import
|
4 |
import json
|
5 |
import random
|
6 |
import os
|
7 |
|
8 |
from helper import create_jwt, generate_random_tuple
|
9 |
|
10 |
-
app =
|
11 |
|
12 |
-
@app.
|
13 |
-
async def chat():
|
14 |
"""
|
15 |
Handle chat completion requests.
|
16 |
-
|
17 |
This function processes incoming POST requests to the '/chat/completions' endpoint.
|
18 |
It prepares the payload for the LLM API, generates a JWT for authentication,
|
19 |
and streams the response from the LLM API back to the client.
|
20 |
-
|
21 |
Returns:
|
22 |
-
|
23 |
-
|
24 |
Note:
|
25 |
- The function uses environment variables for proxy configuration.
|
26 |
- It generates random GitHub username and Zed user ID for each request.
|
27 |
- The LLM model defaults to "claude-3-5-sonnet-20240620" if not specified.
|
28 |
"""
|
29 |
# Get the payload from the request
|
30 |
-
payload = request.json
|
31 |
|
32 |
# Get the model from the payload, defaulting to "claude-3-5-sonnet-20240620"
|
33 |
model = payload.get('model', 'claude-3-5-sonnet-20240620')
|
@@ -48,7 +45,6 @@ async def chat():
|
|
48 |
}
|
49 |
}
|
50 |
|
51 |
-
# github_username, zed_user_id = random.choice(github_username_zed_userid_list)
|
52 |
github_username, zed_user_id = generate_random_tuple()
|
53 |
jwt = create_jwt(github_username, zed_user_id)
|
54 |
|
@@ -65,16 +61,13 @@ async def chat():
|
|
65 |
proxies = {'http': proxy, 'https': proxy} if proxy else None
|
66 |
|
67 |
async def generate():
|
68 |
-
with
|
69 |
-
|
70 |
-
|
71 |
yield chunk
|
72 |
|
73 |
-
return
|
74 |
-
|
75 |
-
# Convert the Flask app to an ASGI app
|
76 |
-
asgi_app = WsgiToAsgi(app)
|
77 |
|
78 |
if __name__ == '__main__':
|
79 |
import uvicorn
|
80 |
-
uvicorn.run(
|
|
|
1 |
+
from fastapi import FastAPI, Request, Response
|
2 |
+
from fastapi.responses import StreamingResponse
|
3 |
+
import httpx
|
4 |
import json
|
5 |
import random
|
6 |
import os
|
7 |
|
8 |
from helper import create_jwt, generate_random_tuple
|
9 |
|
10 |
+
app = FastAPI()
|
11 |
|
12 |
+
@app.post('/ai/v1/chat/completions')
|
13 |
+
async def chat(request: Request):
|
14 |
"""
|
15 |
Handle chat completion requests.
|
|
|
16 |
This function processes incoming POST requests to the '/chat/completions' endpoint.
|
17 |
It prepares the payload for the LLM API, generates a JWT for authentication,
|
18 |
and streams the response from the LLM API back to the client.
|
|
|
19 |
Returns:
|
20 |
+
StreamingResponse: A streaming response containing the LLM API's output.
|
|
|
21 |
Note:
|
22 |
- The function uses environment variables for proxy configuration.
|
23 |
- It generates random GitHub username and Zed user ID for each request.
|
24 |
- The LLM model defaults to "claude-3-5-sonnet-20240620" if not specified.
|
25 |
"""
|
26 |
# Get the payload from the request
|
27 |
+
payload = await request.json()
|
28 |
|
29 |
# Get the model from the payload, defaulting to "claude-3-5-sonnet-20240620"
|
30 |
model = payload.get('model', 'claude-3-5-sonnet-20240620')
|
|
|
45 |
}
|
46 |
}
|
47 |
|
|
|
48 |
github_username, zed_user_id = generate_random_tuple()
|
49 |
jwt = create_jwt(github_username, zed_user_id)
|
50 |
|
|
|
61 |
proxies = {'http': proxy, 'https': proxy} if proxy else None
|
62 |
|
63 |
async def generate():
|
64 |
+
async with httpx.AsyncClient(proxies=proxies) as client:
|
65 |
+
async with client.stream('POST', url, headers=headers, json=llm_payload) as response:
|
66 |
+
async for chunk in response.aiter_bytes():
|
67 |
yield chunk
|
68 |
|
69 |
+
return StreamingResponse(generate(), media_type='application/octet-stream')
|
|
|
|
|
|
|
70 |
|
71 |
if __name__ == '__main__':
|
72 |
import uvicorn
|
73 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|