Reem159 commited on
Commit
2dc5b29
·
1 Parent(s): 85fe962
Files changed (1) hide show
  1. app.py +19 -6
app.py CHANGED
@@ -1,17 +1,30 @@
1
- from fastapi import FastAPI, Request, HTTPException
2
  from fastapi.responses import Response
 
3
  import ollama
4
-
5
  app = FastAPI()
6
 
7
  @app.get('/')
8
  def home():
9
  return "Hello world"
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  @app.post("/chat")
12
- async def chat(query: str):
13
- if not query:
14
- return {"error": "Message cannot be empty."}
15
 
16
- response = ollama.chat(model="llama3", messages=[{"role": "user", "content": query}])
17
  return {"response": response['message']['content']}
 
1
+ from fastapi import FastAPI, HTTPException
2
  from fastapi.responses import Response
3
+ from pydantic import BaseModel
4
  import ollama
 
5
  app = FastAPI()
6
 
7
  @app.get('/')
8
  def home():
9
  return "Hello world"
10
 
11
+ # @app.post("/chat")
12
+ # async def chat(query: str):
13
+ # if not query:
14
+ # return {"error": "Message cannot be empty."}
15
+
16
+ # response = ollama.chat(model="llama3", messages=[{"role": "user", "content": query}])
17
+ # return {"response": response['message']['content']}
18
+
19
+
20
+
21
+ class ChatRequest(BaseModel):
22
+ query: str
23
+
24
  @app.post("/chat")
25
+ async def chat(request: ChatRequest):
26
+ if not request.query.strip():
27
+ raise HTTPException(status_code=400, detail="Message cannot be empty.")
28
 
29
+ response = ollama.chat(model="llama3", messages=[{"role": "user", "content": request.query}])
30
  return {"response": response['message']['content']}