Initial upload
This commit is contained in:
8
recipes/ai/memory/memory-api/Dockerfile
Normal file
8
recipes/ai/memory/memory-api/Dockerfile
Normal file
@@ -0,0 +1,8 @@
|
||||
|
||||
FROM python:3.11-slim
|
||||
WORKDIR /app
|
||||
COPY requirements.txt .
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
COPY app.py .
|
||||
EXPOSE 8085
|
||||
CMD ["python", "app.py"]
|
||||
40
recipes/ai/memory/memory-api/app.py
Normal file
40
recipes/ai/memory/memory-api/app.py
Normal file
@@ -0,0 +1,40 @@
|
||||
|
||||
from fastapi import FastAPI
|
||||
import requests, os
|
||||
from qdrant_client import QdrantClient
|
||||
from qdrant_client.models import PointStruct
|
||||
import hashlib
|
||||
import json
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
QDRANT_URL = os.getenv("QDRANT_URL")
|
||||
OLLAMA_API = os.getenv("OLLAMA_API")
|
||||
COLLECTION_NAME = os.getenv("COLLECTION_NAME", "chat-memory")
|
||||
|
||||
client = QdrantClient(url=QDRANT_URL)
|
||||
|
||||
@app.get("/health")
|
||||
def health():
|
||||
return {"status": "ok", "qdrant": QDRANT_URL, "ollama": OLLAMA_API}
|
||||
|
||||
def embed(text):
|
||||
r = requests.post(f"{OLLAMA_API}/api/embeddings", json={"model":"nomic-embed-text","prompt":text})
|
||||
return r.json()["embedding"]
|
||||
|
||||
@app.post("/store")
|
||||
def store(item: dict):
|
||||
text = item["text"]
|
||||
metadata = item.get("metadata", {})
|
||||
vec = embed(text)
|
||||
pid = hashlib.sha256(text.encode()).hexdigest()
|
||||
client.upsert(collection_name=COLLECTION_NAME, points=[PointStruct(id=pid, vector=vec, payload={"text": text, **metadata})])
|
||||
return {"stored": True}
|
||||
|
||||
@app.post("/search")
|
||||
def search(query: dict):
|
||||
q = query["text"]
|
||||
top_k = query.get("top_k", 5)
|
||||
vec = embed(q)
|
||||
result = client.search(collection_name=COLLECTION_NAME, query_vector=vec, limit=top_k)
|
||||
return [{"score": r.score, "text": r.payload["text"]} for r in result]
|
||||
4
recipes/ai/memory/memory-api/requirements.txt
Normal file
4
recipes/ai/memory/memory-api/requirements.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
fastapi
|
||||
uvicorn
|
||||
requests
|
||||
qdrant-client
|
||||
Reference in New Issue
Block a user