Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
import requests
|
| 4 |
+
import json
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
+
# Define the request model
|
| 9 |
+
class ChatRequest(BaseModel):
|
| 10 |
+
system_prompt: str
|
| 11 |
+
user_query: str
|
| 12 |
+
model: str = "gemini-1.5-pro-latest"
|
| 13 |
+
temperature: float = 1.0
|
| 14 |
+
top_p: float = 0.8
|
| 15 |
+
max_tokens: int = 4000
|
| 16 |
+
|
| 17 |
+
# Define the URL and headers
|
| 18 |
+
url = "https://chat.typegpt.net/api/openai/v1/chat/completions"
|
| 19 |
+
headers = {
|
| 20 |
+
"Accept": "application/json, text/event-stream",
|
| 21 |
+
"Content-Type": "application/json",
|
| 22 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0",
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
@app.post("/chat")
|
| 26 |
+
async def chat(request: ChatRequest):
|
| 27 |
+
# Define the payload
|
| 28 |
+
payload = {
|
| 29 |
+
"messages": [
|
| 30 |
+
{
|
| 31 |
+
"role": "system",
|
| 32 |
+
"content": request.system_prompt
|
| 33 |
+
},
|
| 34 |
+
{
|
| 35 |
+
"role": "user",
|
| 36 |
+
"content": request.user_query
|
| 37 |
+
}
|
| 38 |
+
],
|
| 39 |
+
"stream": True,
|
| 40 |
+
"model": request.model,
|
| 41 |
+
"temperature": request.temperature,
|
| 42 |
+
"top_p": request.top_p,
|
| 43 |
+
"max_tokens": request.max_tokens
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
# Make the POST request with streaming
|
| 47 |
+
try:
|
| 48 |
+
with requests.post(url, headers=headers, data=json.dumps(payload), stream=True) as response:
|
| 49 |
+
# Check if the request was successful
|
| 50 |
+
if response.status_code == 200:
|
| 51 |
+
result = ""
|
| 52 |
+
# Stream the response
|
| 53 |
+
for line in response.iter_lines():
|
| 54 |
+
if line:
|
| 55 |
+
# Decode the line
|
| 56 |
+
decoded_line = line.decode('utf-8')
|
| 57 |
+
|
| 58 |
+
# Check if the line starts with "data: "
|
| 59 |
+
if decoded_line.startswith("data: "):
|
| 60 |
+
# Extract the JSON part
|
| 61 |
+
json_data = decoded_line[6:] # Remove "data: " prefix
|
| 62 |
+
|
| 63 |
+
# Parse the JSON
|
| 64 |
+
try:
|
| 65 |
+
parsed_data = json.loads(json_data)
|
| 66 |
+
# Check if 'choices' and 'delta' exist and append the content
|
| 67 |
+
if 'choices' in parsed_data and len(parsed_data['choices']) > 0:
|
| 68 |
+
content = parsed_data['choices'][0]['delta'].get('content', '')
|
| 69 |
+
if content:
|
| 70 |
+
result += content
|
| 71 |
+
except json.JSONDecodeError:
|
| 72 |
+
continue
|
| 73 |
+
return {"response": result}
|
| 74 |
+
else:
|
| 75 |
+
raise HTTPException(status_code=response.status_code, detail=response.text)
|
| 76 |
+
except Exception as e:
|
| 77 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 78 |
+
|
| 79 |
+
if __name__ == "__main__":
|
| 80 |
+
import uvicorn
|
| 81 |
+
uvicorn.run(app, host="0.0.0.0", port=8083)
|