Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -1,9 +1,13 @@
|
|
1 |
import os
|
2 |
from dotenv import load_dotenv
|
3 |
from fastapi import FastAPI, HTTPException, Request
|
4 |
-
from fastapi.responses import StreamingResponse, HTMLResponse
|
5 |
from pydantic import BaseModel
|
6 |
import httpx
|
|
|
|
|
|
|
|
|
7 |
|
8 |
load_dotenv()
|
9 |
|
@@ -14,9 +18,10 @@ api_keys_str = os.getenv('API_KEYS')
|
|
14 |
valid_api_keys = api_keys_str.split(',') if api_keys_str else []
|
15 |
secret_api_endpoint = os.getenv('SECRET_API_ENDPOINT')
|
16 |
secret_api_endpoint_2 = os.getenv('SECRET_API_ENDPOINT_2')
|
|
|
17 |
|
18 |
-
# Validate if the main secret API
|
19 |
-
if not secret_api_endpoint or not secret_api_endpoint_2:
|
20 |
raise HTTPException(status_code=500, detail="API endpoint(s) are not configured in environment variables.")
|
21 |
|
22 |
# Define models that should use the secondary endpoint
|
@@ -27,6 +32,58 @@ class Payload(BaseModel):
|
|
27 |
messages: list
|
28 |
stream: bool
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
@app.get("/", response_class=HTMLResponse)
|
31 |
async def root():
|
32 |
# Open and read the content of index.html (in the same folder as the app)
|
@@ -54,14 +111,7 @@ async def fetch_models():
|
|
54 |
|
55 |
@app.post("/chat/completions")
|
56 |
async def get_completion(payload: Payload, request: Request):
|
57 |
-
|
58 |
-
print(f"API Key Used: {api_key}")
|
59 |
-
print(f"Raw Payload: {payload.dict()}")
|
60 |
-
# Validate API key
|
61 |
-
if api_key not in valid_api_keys:
|
62 |
-
raise HTTPException(status_code=403, detail="Forbidden: Invalid API key. Join dsc.gg/chadgang and DM @mr_leaderyt on Discord for a free API key :)")
|
63 |
-
|
64 |
-
# Determine which endpoint to use based on the model
|
65 |
endpoint = secret_api_endpoint_2 if payload.model in alternate_models else secret_api_endpoint
|
66 |
|
67 |
# Use the payload directly as it includes stream and other user data
|
@@ -89,9 +139,9 @@ async def startup_event():
|
|
89 |
print("API endpoints:")
|
90 |
print("GET /")
|
91 |
print("GET /models")
|
92 |
-
print("
|
|
|
93 |
|
94 |
if __name__ == "__main__":
|
95 |
import uvicorn
|
96 |
uvicorn.run(app, host="0.0.0.0", port=8000)
|
97 |
-
|
|
|
1 |
import os
|
2 |
from dotenv import load_dotenv
|
3 |
from fastapi import FastAPI, HTTPException, Request
|
4 |
+
from fastapi.responses import StreamingResponse, HTMLResponse, JSONResponse
|
5 |
from pydantic import BaseModel
|
6 |
import httpx
|
7 |
+
import requests
|
8 |
+
import re
|
9 |
+
import json
|
10 |
+
from typing import Optional
|
11 |
|
12 |
load_dotenv()
|
13 |
|
|
|
18 |
valid_api_keys = api_keys_str.split(',') if api_keys_str else []
|
19 |
secret_api_endpoint = os.getenv('SECRET_API_ENDPOINT')
|
20 |
secret_api_endpoint_2 = os.getenv('SECRET_API_ENDPOINT_2')
|
21 |
+
secret_api_endpoint_3 = os.getenv('SECRET_API_ENDPOINT_3') # New endpoint for searchgpt
|
22 |
|
23 |
+
# Validate if the main secret API endpoints are set
|
24 |
+
if not secret_api_endpoint or not secret_api_endpoint_2 or not secret_api_endpoint_3:
|
25 |
raise HTTPException(status_code=500, detail="API endpoint(s) are not configured in environment variables.")
|
26 |
|
27 |
# Define models that should use the secondary endpoint
|
|
|
32 |
messages: list
|
33 |
stream: bool
|
34 |
|
35 |
+
def generate_search(query: str, stream: bool = True) -> str:
|
36 |
+
headers = {"User-Agent": ""}
|
37 |
+
prompt = [
|
38 |
+
{"role": "user", "content": query},
|
39 |
+
]
|
40 |
+
|
41 |
+
# Insert the system prompt at the beginning of the conversation history
|
42 |
+
prompt.insert(0, {"content": "Be Helpful and Friendly coz ur searchgpt", "role": "system"})
|
43 |
+
|
44 |
+
payload = {
|
45 |
+
"is_vscode_extension": True,
|
46 |
+
"message_history": prompt,
|
47 |
+
"requested_model": "searchgpt",
|
48 |
+
"user_input": prompt[-1]["content"],
|
49 |
+
}
|
50 |
+
|
51 |
+
# Use the newly added SECRET_API_ENDPOINT_3 for the search API call
|
52 |
+
chat_endpoint = secret_api_endpoint_3
|
53 |
+
response = requests.post(chat_endpoint, headers=headers, json=payload, stream=True)
|
54 |
+
|
55 |
+
# Collect streamed text content
|
56 |
+
streaming_text = ""
|
57 |
+
for value in response.iter_lines(decode_unicode=True, chunk_size=12):
|
58 |
+
modified_value = re.sub("data:", "", value)
|
59 |
+
if modified_value:
|
60 |
+
try:
|
61 |
+
json_modified_value = json.loads(modified_value)
|
62 |
+
content = json_modified_value["choices"][0]["delta"]["content"]
|
63 |
+
if stream:
|
64 |
+
yield f"data: {content}\n\n"
|
65 |
+
streaming_text += content
|
66 |
+
except:
|
67 |
+
continue
|
68 |
+
|
69 |
+
if not stream:
|
70 |
+
yield streaming_text
|
71 |
+
|
72 |
+
@app.get("/searchgpt")
|
73 |
+
async def search_gpt(q: str, stream: Optional[bool] = True):
|
74 |
+
if not q:
|
75 |
+
raise HTTPException(status_code=400, detail="Query parameter 'q' is required")
|
76 |
+
|
77 |
+
if stream:
|
78 |
+
return StreamingResponse(
|
79 |
+
generate_search(q, stream=True),
|
80 |
+
media_type="text/event-stream"
|
81 |
+
)
|
82 |
+
else:
|
83 |
+
# For non-streaming response, collect all content and return as JSON
|
84 |
+
response_text = "".join([chunk for chunk in generate_search(q, stream=False)])
|
85 |
+
return JSONResponse(content={"response": response_text})
|
86 |
+
|
87 |
@app.get("/", response_class=HTMLResponse)
|
88 |
async def root():
|
89 |
# Open and read the content of index.html (in the same folder as the app)
|
|
|
111 |
|
112 |
@app.post("/chat/completions")
|
113 |
async def get_completion(payload: Payload, request: Request):
|
114 |
+
# Use the correct endpoint depending on the model type (no authentication now 😉)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
endpoint = secret_api_endpoint_2 if payload.model in alternate_models else secret_api_endpoint
|
116 |
|
117 |
# Use the payload directly as it includes stream and other user data
|
|
|
139 |
print("API endpoints:")
|
140 |
print("GET /")
|
141 |
print("GET /models")
|
142 |
+
print("GET /searchgpt") # We now have the new search API
|
143 |
+
print("POST /chat/completions")
|
144 |
|
145 |
if __name__ == "__main__":
|
146 |
import uvicorn
|
147 |
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|