pvanand commited on
Commit
adb504f
1 Parent(s): 2fc91ef

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +17 -11
main.py CHANGED
@@ -1,4 +1,5 @@
1
- from fastapi import FastAPI, HTTPException
 
2
  from fastapi.responses import StreamingResponse
3
  from pydantic import BaseModel, Field
4
  from typing import Literal
@@ -8,6 +9,10 @@ from openai import OpenAI
8
 
9
  app = FastAPI()
10
 
 
 
 
 
11
  ModelID = Literal[
12
  "meta-llama/llama-3-70b-instruct",
13
  "anthropic/claude-3.5-sonnet",
@@ -42,8 +47,7 @@ def get_api_keys():
42
  api_keys = get_api_keys()
43
  or_client = OpenAI(api_key=api_keys["OPENROUTER_API_KEY"], base_url="https://openrouter.ai/api/v1")
44
 
45
-
46
- def chat_with_llama_stream(messages, model, max_output_tokens=4000):
47
  try:
48
  response = or_client.chat.completions.create(
49
  model=model,
@@ -51,14 +55,20 @@ def chat_with_llama_stream(messages, model, max_output_tokens=4000):
51
  max_tokens=max_output_tokens,
52
  stream=True
53
  )
 
54
  for chunk in response:
55
  if chunk.choices[0].delta.content is not None:
56
  yield chunk.choices[0].delta.content
57
  except Exception as e:
58
  raise HTTPException(status_code=500, detail=f"Error in model response: {str(e)}")
59
 
 
 
 
 
 
60
  @app.post("/coding-assistant")
61
- async def coding_assistant(query: QueryModel):
62
  """
63
  Coding assistant endpoint that provides programming help based on user queries.
64
 
@@ -70,6 +80,8 @@ async def coding_assistant(query: QueryModel):
70
  - openai/gpt-3.5-turbo-instruct
71
  - qwen/qwen-72b-chat
72
  - google/gemma-2-27b-it
 
 
73
  """
74
  system_prompt = "You are a helpful assistant proficient in coding tasks. Help the user in understanding and writing code."
75
  messages = [
@@ -81,13 +93,7 @@ async def coding_assistant(query: QueryModel):
81
  chat_with_llama_stream(messages, model=query.model_id),
82
  media_type="text/event-stream"
83
  )
84
- app.add_middleware(
85
- CORSMiddleware,
86
- allow_origins=["*"],
87
- allow_credentials=True,
88
- allow_methods=["*"],
89
- allow_headers=["*"],)
90
 
91
  if __name__ == "__main__":
92
  import uvicorn
93
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
+ from fastapi import FastAPI, HTTPException, Depends, Security
2
+ from fastapi.security import APIKeyHeader
3
  from fastapi.responses import StreamingResponse
4
  from pydantic import BaseModel, Field
5
  from typing import Literal
 
9
 
10
  app = FastAPI()
11
 
12
+ API_KEY_NAME = "X-API-Key"
13
+ API_KEY = os.environ.get("API_KEY", "default_secret_key") # Set this in your environment variables
14
+ api_key_header = APIKeyHeader(name=API_KEY_NAME, auto_error=False)
15
+
16
  ModelID = Literal[
17
  "meta-llama/llama-3-70b-instruct",
18
  "anthropic/claude-3.5-sonnet",
 
47
  api_keys = get_api_keys()
48
  or_client = OpenAI(api_key=api_keys["OPENROUTER_API_KEY"], base_url="https://openrouter.ai/api/v1")
49
 
50
+ def chat_with_llama_stream(messages, model, max_output_tokens=2500):
 
51
  try:
52
  response = or_client.chat.completions.create(
53
  model=model,
 
55
  max_tokens=max_output_tokens,
56
  stream=True
57
  )
58
+
59
  for chunk in response:
60
  if chunk.choices[0].delta.content is not None:
61
  yield chunk.choices[0].delta.content
62
  except Exception as e:
63
  raise HTTPException(status_code=500, detail=f"Error in model response: {str(e)}")
64
 
65
+ async def verify_api_key(api_key: str = Security(api_key_header)):
66
+ if api_key != API_KEY:
67
+ raise HTTPException(status_code=403, detail="Could not validate credentials")
68
+ return api_key
69
+
70
  @app.post("/coding-assistant")
71
+ async def coding_assistant(query: QueryModel, api_key: str = Depends(verify_api_key)):
72
  """
73
  Coding assistant endpoint that provides programming help based on user queries.
74
 
 
80
  - openai/gpt-3.5-turbo-instruct
81
  - qwen/qwen-72b-chat
82
  - google/gemma-2-27b-it
83
+
84
+ Requires API Key authentication via X-API-Key header.
85
  """
86
  system_prompt = "You are a helpful assistant proficient in coding tasks. Help the user in understanding and writing code."
87
  messages = [
 
93
  chat_with_llama_stream(messages, model=query.model_id),
94
  media_type="text/event-stream"
95
  )
 
 
 
 
 
 
96
 
97
  if __name__ == "__main__":
98
  import uvicorn
99
+ uvicorn.run(app, host="0.0.0.0", port=7860)