snackshell commited on
Commit
d5a5d22
·
verified ·
1 Parent(s): ad7664a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -22
app.py CHANGED
@@ -3,19 +3,15 @@ from fastapi.responses import StreamingResponse
3
  from pydantic import BaseModel
4
  from typing import List
5
  from g4f import ChatCompletion
6
- from slowapi import Limiter
7
- from slowapi.util import get_remote_address
8
 
9
  app = FastAPI()
10
 
11
- # Initialize the rate limiter
12
- limiter = Limiter(key_func=get_remote_address)
13
-
14
  # List of available models
15
  models = [
16
  "gpt-4o", "gpt-4o-mini", "gpt-4",
17
  "gpt-4-turbo", "gpt-3.5-turbo",
18
- "claude-3.7-sonnet", "o3-mini", "o1", "claude-3.5", "llama-3.1-405b", "gemini-flash", "blackboxai-pro", "openchat-3.5", "glm-4-9B", "blackboxai"
 
19
  ]
20
 
21
  # Request model
@@ -26,7 +22,7 @@ class Message(BaseModel):
26
  class ChatRequest(BaseModel):
27
  model: str
28
  messages: List[Message]
29
- streaming: bool = True # Add streaming support
30
 
31
  class ChatResponse(BaseModel):
32
  role: str
@@ -34,7 +30,7 @@ class ChatResponse(BaseModel):
34
 
35
  # Dependency to check API key
36
  async def verify_api_key(x_api_key: str = Header(...)):
37
- if x_api_key != "vs-5wEvIw6vfLKIypGm7uiNoWuXrJcg4vAL": # Replace with your actual API key
38
  raise HTTPException(status_code=403, detail="Invalid API key")
39
 
40
  @app.get("/v1/models", tags=["Models"])
@@ -43,12 +39,14 @@ async def get_models():
43
  return {"models": models}
44
 
45
  @app.post("/v1/chat/completions", tags=["Chat Completion"])
46
- @limiter.limit("10/minute") # Rate limit to 10 requests per minute
47
  async def chat_completion(
48
- request: Request,
49
  chat_request: ChatRequest,
50
  api_key: str = Depends(verify_api_key)
51
  ):
 
 
 
 
52
  # Validate model
53
  if chat_request.model not in models:
54
  raise HTTPException(status_code=400, detail="Invalid model selected.")
@@ -67,34 +65,37 @@ async def chat_completion(
67
  response = ChatCompletion.create(
68
  model=chat_request.model,
69
  messages=formatted_messages,
70
- stream=True # Enable streaming
71
  )
72
 
73
  for chunk in response:
74
  if isinstance(chunk, dict) and 'choices' in chunk:
75
  for choice in chunk['choices']:
76
- if 'message' in choice:
77
- yield f"data: {choice['message']['content']}\n\n"
 
 
78
  else:
79
- yield f"data: {chunk}\n\n" # Fallback if chunk is not as expected
80
 
81
  return StreamingResponse(event_stream(), media_type="text/event-stream")
82
  else:
83
  # Non-streaming response
84
  response = ChatCompletion.create(
85
  model=chat_request.model,
86
- messages=formatted_messages
 
87
  )
88
 
89
  if isinstance(response, str):
90
- response_content = response # Directly use if it's a string
 
 
 
 
 
91
  else:
92
- try:
93
- response_content = response['choices'][0]['message']['content']
94
- except (IndexError, KeyError):
95
- raise HTTPException(status_code=500, detail="Unexpected response structure.")
96
-
97
- return ChatResponse(role="assistant", content=response_content)
98
 
99
  except Exception as e:
100
  raise HTTPException(status_code=500, detail=str(e))
 
3
  from pydantic import BaseModel
4
  from typing import List
5
  from g4f import ChatCompletion
 
 
6
 
7
  app = FastAPI()
8
 
 
 
 
9
  # List of available models
10
  models = [
11
  "gpt-4o", "gpt-4o-mini", "gpt-4",
12
  "gpt-4-turbo", "gpt-3.5-turbo",
13
+ "claude-3.7-sonnet", "o3-mini", "o1", "claude-3.5",
14
+ "llama-3.1-405b"
15
  ]
16
 
17
  # Request model
 
22
  class ChatRequest(BaseModel):
23
  model: str
24
  messages: List[Message]
25
+ streaming: bool = True
26
 
27
  class ChatResponse(BaseModel):
28
  role: str
 
30
 
31
  # Dependency to check API key
32
  async def verify_api_key(x_api_key: str = Header(...)):
33
+ if x_api_key != "fb207532285886a5568298b4b4e61124":
34
  raise HTTPException(status_code=403, detail="Invalid API key")
35
 
36
  @app.get("/v1/models", tags=["Models"])
 
39
  return {"models": models}
40
 
41
  @app.post("/v1/chat/completions", tags=["Chat Completion"])
 
42
  async def chat_completion(
 
43
  chat_request: ChatRequest,
44
  api_key: str = Depends(verify_api_key)
45
  ):
46
+ """
47
+ Handle chat completion requests with optional streaming.
48
+ Removed rate limiting for unrestricted access.
49
+ """
50
  # Validate model
51
  if chat_request.model not in models:
52
  raise HTTPException(status_code=400, detail="Invalid model selected.")
 
65
  response = ChatCompletion.create(
66
  model=chat_request.model,
67
  messages=formatted_messages,
68
+ stream=True
69
  )
70
 
71
  for chunk in response:
72
  if isinstance(chunk, dict) and 'choices' in chunk:
73
  for choice in chunk['choices']:
74
+ if 'delta' in choice and 'content' in choice['delta']:
75
+ yield f"data: {json.dumps({'content': choice['delta']['content']})}\n\n"
76
+ elif 'message' in choice:
77
+ yield f"data: {json.dumps({'content': choice['message']['content']})}\n\n"
78
  else:
79
+ yield f"data: {json.dumps({'content': str(chunk)})}\n\n"
80
 
81
  return StreamingResponse(event_stream(), media_type="text/event-stream")
82
  else:
83
  # Non-streaming response
84
  response = ChatCompletion.create(
85
  model=chat_request.model,
86
+ messages=formatted_messages,
87
+ stream=False
88
  )
89
 
90
  if isinstance(response, str):
91
+ return ChatResponse(role="assistant", content=response)
92
+ elif isinstance(response, dict) and 'choices' in response:
93
+ return ChatResponse(
94
+ role="assistant",
95
+ content=response['choices'][0]['message']['content']
96
+ )
97
  else:
98
+ raise HTTPException(status_code=500, detail="Unexpected response structure.")
 
 
 
 
 
99
 
100
  except Exception as e:
101
  raise HTTPException(status_code=500, detail=str(e))