Update main.py
Browse files
main.py
CHANGED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from fastapi import FastAPI, HTTPException
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
from typing import List, Optional
|
| 5 |
+
import uuid
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
import sqlite3
|
| 8 |
+
from groq import Groq
|
| 9 |
+
|
| 10 |
+
app = FastAPI()
|
| 11 |
+
|
| 12 |
+
# Initialize Groq client
|
| 13 |
+
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 14 |
+
|
| 15 |
+
# Initialize SQLite database
|
| 16 |
+
conn = sqlite3.connect('conversations.db')
|
| 17 |
+
c = conn.cursor()
|
| 18 |
+
c.execute('''CREATE TABLE IF NOT EXISTS conversations
|
| 19 |
+
(id TEXT PRIMARY KEY, messages TEXT, last_updated TIMESTAMP)''')
|
| 20 |
+
conn.commit()
|
| 21 |
+
|
| 22 |
+
class Message(BaseModel):
|
| 23 |
+
role: str
|
| 24 |
+
content: str
|
| 25 |
+
|
| 26 |
+
class ChatRequest(BaseModel):
|
| 27 |
+
conversation_id: Optional[str] = None
|
| 28 |
+
messages: List[Message]
|
| 29 |
+
|
| 30 |
+
class ChatResponse(BaseModel):
|
| 31 |
+
conversation_id: str
|
| 32 |
+
response: str
|
| 33 |
+
|
| 34 |
+
@app.post("/chat", response_model=ChatResponse)
|
| 35 |
+
async def chat(request: ChatRequest):
|
| 36 |
+
conversation_id = request.conversation_id or str(uuid.uuid4())
|
| 37 |
+
|
| 38 |
+
# Retrieve existing conversation or start a new one
|
| 39 |
+
c.execute("SELECT messages FROM conversations WHERE id = ?", (conversation_id,))
|
| 40 |
+
result = c.fetchone()
|
| 41 |
+
|
| 42 |
+
if result:
|
| 43 |
+
existing_messages = eval(result[0])
|
| 44 |
+
messages = existing_messages + [{"role": msg.role, "content": msg.content} for msg in request.messages]
|
| 45 |
+
else:
|
| 46 |
+
messages = [{"role": msg.role, "content": msg.content} for msg in request.messages]
|
| 47 |
+
|
| 48 |
+
# Call Groq API
|
| 49 |
+
chat_completion = client.chat.completions.create(
|
| 50 |
+
messages=messages,
|
| 51 |
+
model="llama3-8b-8192",
|
| 52 |
+
)
|
| 53 |
+
response = chat_completion.choices[0].message.content
|
| 54 |
+
|
| 55 |
+
# Update conversation in database
|
| 56 |
+
messages.append({"role": "assistant", "content": response})
|
| 57 |
+
c.execute("INSERT OR REPLACE INTO conversations (id, messages, last_updated) VALUES (?, ?, ?)",
|
| 58 |
+
(conversation_id, str(messages), datetime.now()))
|
| 59 |
+
conn.commit()
|
| 60 |
+
|
| 61 |
+
return {"conversation_id": conversation_id, "response": response}
|
| 62 |
+
|
| 63 |
+
@app.get("/conversations/{conversation_id}")
|
| 64 |
+
async def get_conversation(conversation_id: str):
|
| 65 |
+
c.execute("SELECT messages FROM conversations WHERE id = ?", (conversation_id,))
|
| 66 |
+
result = c.fetchone()
|
| 67 |
+
if result:
|
| 68 |
+
return {"conversation_id": conversation_id, "messages": eval(result[0])}
|
| 69 |
+
raise HTTPException(status_code=404, detail="Conversation not found")
|
| 70 |
+
|
| 71 |
+
@app.get("/")
|
| 72 |
+
async def read_root():
|
| 73 |
+
return {"message": "Welcome to the Groq chatbot API with conversation persistence!"}
|
| 74 |
+
|
| 75 |
+
if __name__ == "__main__":
|
| 76 |
+
import uvicorn
|
| 77 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|