CooLLaMACEO commited on
Commit
b879e43
·
verified ·
1 Parent(s): e61425d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -10
app.py CHANGED
@@ -1,17 +1,33 @@
1
- from fastapi import FastAPI, Request
2
  from llama_cpp import Llama
3
 
4
  app = FastAPI()
5
- # Load your 3.6GB model
6
- llm = Llama(model_path="./mpt-7b-chat.ggmlv0.q4_0.bin")
7
 
8
- @app.post("/v1/chat")
9
- async def chat(request: Request):
10
- data = await request.json()
11
- # Basic API logic:
12
- response = llm(data["prompt"], max_tokens=100)
13
- return {"choices": [{"text": response["choices"][0]["text"]}]}
 
 
 
 
 
 
14
 
15
  @app.get("/")
16
  def home():
17
- return {"status": "Your AI is Always Up!"}
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request, HTTPException, Depends
2
  from llama_cpp import Llama
3
 
4
  app = FastAPI()
 
 
5
 
6
+ # Set your API Key here
7
+ API_KEY = "my-secret-key-123"
8
+
9
+ # Load the model we downloaded in the Dockerfile
10
+ # MPT models need the 'mpt' type specified for older llama-cpp
11
+ llm = Llama(model_path="./model.bin", model_type="mpt")
12
+
13
+ # Security check for your API Key
14
+ def verify_key(request: Request):
15
+ key = request.headers.get("Authorization")
16
+ if key != f"Bearer {API_KEY}":
17
+ raise HTTPException(status_code=403, detail="Invalid API Key")
18
 
19
  @app.get("/")
20
  def home():
21
+ return {"message": "Your MPT-7B Website is Online!", "api_endpoint": "/v1/chat"}
22
+
23
+ @app.post("/v1/chat", dependencies=[Depends(verify_key)])
24
+ async def chat(request: Request):
25
+ data = await request.json()
26
+ prompt = data.get("prompt", "Hello!")
27
+
28
+ # Generate the response
29
+ output = llm(f"<|im_start|>user\n{prompt}<|im_end|>\n<|im_start|>assistant\n",
30
+ stop=["<|im_end|>"],
31
+ max_tokens=256)
32
+
33
+ return {"response": output["choices"][0]["text"]}