53 lines
1.3 KiB
Python
53 lines
1.3 KiB
Python
from fastapi import FastAPI, Request
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
import requests
|
|
import redis
|
|
import json
|
|
|
|
r = redis.Redis(host='localhost', port=6379, db=0)
|
|
|
|
def save_chat(user_id, message):
|
|
r.rpush(f"chat:{user_id}", json.dumps(message))
|
|
|
|
def get_chat(user_id):
|
|
messages = r.lrange(f"chat:{user_id}", 0, -1)
|
|
return [json.loads(m.decode('utf-8')) for m in messages]
|
|
|
|
app = FastAPI()
|
|
|
|
# Allow frontend access
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# Store sessions in memory (simple version)
|
|
sessions = {}
|
|
|
|
@app.post("/chat")
|
|
async def chat(request: Request):
|
|
data = await request.json()
|
|
user_id = data.get("user_id", "default")
|
|
message = data["message"]
|
|
|
|
# Retrieve session history
|
|
save_chat(user_id, {"role": "user", "content": message})
|
|
history = get_chat(user_id)
|
|
|
|
# Send to LM Studio
|
|
response = requests.post("http://10.74.83.100:1234/v1/chat/completions", json={
|
|
"model": "qwen/qwen3-4b-2507", # Replace with actual model name
|
|
"messages": history
|
|
})
|
|
|
|
result = response.json()
|
|
reply = result["choices"][0]["message"]["content"]
|
|
|
|
# save in REDIS chat history
|
|
save_chat(user_id, {"role": "assistant", "content": reply})
|
|
|
|
return {"response": reply}
|
|
|