-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
59 lines (47 loc) · 1.49 KB
/
app.py
File metadata and controls
59 lines (47 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from fastapi import FastAPI
import chromadb
import ollama
import os
import logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)s %(message)s",
)
MODEL_NAME = os.getenv("MODEL_NAME", "tinyllama")
logging.info(f"Using model: {MODEL_NAME}")
app = FastAPI()
chroma = chromadb.PersistentClient(path="./my_db")
collection = chroma.get_or_create_collection(name="docs")
@app.post("/query")
def query(q:str):
results = collection.query(query_texts=[q], n_results=1)
context = results["documents"][0][0] if results["documents"][0] else ""
answer = ollama.generate(
model=MODEL_NAME,
prompt=f"Context:\n{context}\n\nQuestion: {q}\n\nAnswer clearly and concisely:"
)
return {"answer": answer["response"]}
logging.info(f"/query asked: {q}")
@app.post("/add")
def add_knowledge(text: str):
"""Add new content to the knowledge base dynamically."""
try:
# Generate a unique ID for this document
import uuid
doc_id = str(uuid.uuid4())
# Add the text to Chroma collection
collection.add(documents=[text], ids=[doc_id])
return {
"status": "success",
"message": "Content added to knowledge base",
"id": doc_id
}
except Exception as e:
return {
"status": "error",
"message": str(e)
}
logging.info(f"/add received new text (id will be generated)")
@app.get("/health")
def health():
return {"status": "ok"}