init: add knowledge, llm-api, scripts + repo settings
This commit is contained in:
commit
75f13af1f1
64 files changed
+6924
No files matched your search
@@ -0,0 +1,110 @@
|
||||
from fastapi import FastAPI, Query
|
||||
from pydantic import BaseModel
|
||||
from typing import List
|
||||
from sentence_transformers import SentenceTransformer
|
||||
from qdrant_client import QdrantClient
|
||||
from qdrant_client.models import VectorParams, Distance, PointStruct
|
||||
import requests
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
# Initialisierung
|
||||
model = SentenceTransformer("all-MiniLM-L6-v2")
|
||||
qdrant = QdrantClient(host="localhost", port=6333)
|
||||
# COLLECTION = "karate-doku"
|
||||
OLLAMA_URL = "http://localhost:11434/api/generate"
|
||||
OLLAMA_MODEL = "mistral" # kann später auch geändert werden
|
||||
|
||||
# Embedding-Input
|
||||
class EmbedRequest(BaseModel):
|
||||
texts: List[str]
|
||||
collection: str = "default"
|
||||
|
||||
class PromptRequest(BaseModel):
|
||||
query: str
|
||||
context_limit: int = 3
|
||||
collection: str = "default"
|
||||
|
||||
|
||||
@app.post("/embed")
|
||||
|
||||
def embed_texts(data: EmbedRequest):
|
||||
collection_name = data.collection
|
||||
|
||||
if not qdrant.collection_exists(collection_name):
|
||||
qdrant.recreate_collection(
|
||||
collection_name=collection_name,
|
||||
vectors_config=VectorParams(size=384, distance=Distance.COSINE)
|
||||
)
|
||||
|
||||
embeddings = model.encode(data.texts).tolist()
|
||||
points = [
|
||||
PointStruct(id=i, vector=vec, payload={"text": data.texts[i]})
|
||||
for i, vec in enumerate(embeddings)
|
||||
]
|
||||
qdrant.upsert(collection_name=collection_name, points=points)
|
||||
return {"status": "✅ embeddings saved", "count": len(points), "collection": collection_name}
|
||||
|
||||
@app.get("/search")
|
||||
def search_text(query: str = Query(...), limit: int = 3, collection: str = Query(...)):
|
||||
vec = model.encode(query).tolist()
|
||||
results = qdrant.search(collection_name=collection, query_vector=vec, limit=limit)
|
||||
return [{"score": r.score, "text": r.payload["text"]} for r in results]
|
||||
|
||||
@app.post("/prompt")
|
||||
def generate_prompt(data: PromptRequest):
|
||||
query_vec = model.encode(data.query).tolist()
|
||||
|
||||
# Suche relevante Einträge aus der angegebenen Collection
|
||||
results = qdrant.search(
|
||||
collection_name=data.collection,
|
||||
query_vector=query_vec,
|
||||
limit=data.context_limit
|
||||
)
|
||||
|
||||
# Kontext für den Prompt aus den gefundenen Texten zusammenbauen
|
||||
context = "\n".join([r.payload["text"] for r in results])
|
||||
full_prompt = f"""Beantworte die folgende Frage basierend auf dem Kontext:
|
||||
|
||||
Kontext:
|
||||
{context}
|
||||
|
||||
Frage:
|
||||
{data.query}
|
||||
"""
|
||||
|
||||
# Anfrage an Ollama stellen
|
||||
ollama_payload = {
|
||||
"model": OLLAMA_MODEL,
|
||||
"prompt": full_prompt,
|
||||
"stream": False
|
||||
}
|
||||
|
||||
response = requests.post(OLLAMA_URL, json=ollama_payload)
|
||||
response.raise_for_status()
|
||||
answer = response.json()["response"]
|
||||
|
||||
return {
|
||||
"answer": answer,
|
||||
"context": context,
|
||||
"collection": data.collection
|
||||
}
|
||||
|
||||
|
||||
Kontext:
|
||||
{context}
|
||||
|
||||
Frage:
|
||||
{data.query}
|
||||
"""
|
||||
|
||||
ollama_payload = {
|
||||
"model": OLLAMA_MODEL,
|
||||
"prompt": full_prompt,
|
||||
"stream": False
|
||||
}
|
||||
|
||||
response = requests.post(OLLAMA_URL, json=ollama_payload)
|
||||
response.raise_for_status()
|
||||
answer = response.json()["response"]
|
||||
return {"answer": answer, "context": context}
|
||||
@@ -0,0 +1,167 @@
|
||||
from fastapi import FastAPI, Query, HTTPException, Request
|
||||
from fastapi.responses import JSONResponse
|
||||
from fastapi.openapi.utils import get_openapi
|
||||
from pydantic import BaseModel, Field
|
||||
from typing import List, Dict, Any, Optional
|
||||
from sentence_transformers import SentenceTransformer
|
||||
from qdrant_client import QdrantClient
|
||||
from qdrant_client.models import VectorParams, Distance, PointStruct, PointIdsList
|
||||
from qdrant_client.http.models import Filter, FieldCondition, MatchValue
|
||||
from uuid import uuid4
|
||||
import requests
|
||||
import os
|
||||
from datetime import datetime
|
||||
|
||||
# Version hochgezählt
|
||||
__version__ = "1.0.20"
|
||||
print(f"[DEBUG] llm_api.py version {__version__} loaded from {__file__}", flush=True)
|
||||
|
||||
# Ollama-Konfiguration
|
||||
OLLAMA_URL = os.getenv("OLLAMA_URL", "http://localhost:11434/api/generate")
|
||||
OLLAMA_MODEL = os.getenv("OLLAMA_MODEL", "mistral:latest")
|
||||
|
||||
# FastAPI-Instanz
|
||||
app = FastAPI(
|
||||
title="KI Trainerassistent API",
|
||||
description="Lokale API für Karate- & Gewaltschutz-Trainingsplanung",
|
||||
version=__version__,
|
||||
docs_url="/docs",
|
||||
redoc_url="/redoc",
|
||||
openapi_url="/openapi.json"
|
||||
)
|
||||
|
||||
# Globaler Fehlerhandler
|
||||
@app.exception_handler(Exception)
|
||||
async def unicorn_exception_handler(request: Request, exc: Exception):
|
||||
return JSONResponse(status_code=500, content={"detail": "Interner Serverfehler. Bitte später erneut versuchen."})
|
||||
|
||||
# Datenmodelle
|
||||
class ChunkInput(BaseModel):
|
||||
text: str
|
||||
source: str
|
||||
source_type: str = ""
|
||||
title: str = ""
|
||||
version: str = ""
|
||||
related_to: str = ""
|
||||
tags: List[str] = []
|
||||
owner: str = ""
|
||||
context_tag: Optional[str] = None
|
||||
imported_at: Optional[str] = None
|
||||
chunk_index: Optional[int] = None
|
||||
category: Optional[str] = None
|
||||
|
||||
class EmbedRequest(BaseModel):
|
||||
chunks: List[ChunkInput]
|
||||
collection: str = "default"
|
||||
|
||||
class PromptRequest(BaseModel):
|
||||
query: str
|
||||
context_limit: int = 3
|
||||
collection: str = "default"
|
||||
|
||||
class EmbedResponse(BaseModel):
|
||||
status: str
|
||||
count: int
|
||||
collection: str
|
||||
|
||||
class SearchResultItem(BaseModel):
|
||||
score: float = Field(..., ge=0)
|
||||
text: str
|
||||
|
||||
class PromptResponse(BaseModel):
|
||||
answer: str
|
||||
context: str
|
||||
collection: str
|
||||
|
||||
class DeleteResponse(BaseModel):
|
||||
status: str
|
||||
count: int
|
||||
collection: str
|
||||
source: Optional[str] = None
|
||||
type: Optional[str] = None
|
||||
|
||||
# Embedding-Modell und Qdrant-Client
|
||||
model = SentenceTransformer("all-MiniLM-L6-v2")
|
||||
qdrant = QdrantClient(host=os.getenv("QDRANT_HOST", "localhost"), port=int(os.getenv("QDRANT_PORT", 6333)))
|
||||
|
||||
# /embed
|
||||
@app.post("/embed", response_model=EmbedResponse)
|
||||
def embed_texts(data: EmbedRequest):
|
||||
if not data.chunks:
|
||||
raise HTTPException(status_code=400, detail="'chunks' darf nicht leer sein.")
|
||||
coll = data.collection
|
||||
if not qdrant.collection_exists(coll):
|
||||
qdrant.recreate_collection(collection_name=coll,
|
||||
vectors_config=VectorParams(size=model.get_sentence_embedding_dimension(), distance=Distance.COSINE)
|
||||
)
|
||||
embeddings = model.encode([c.text for c in data.chunks]).tolist()
|
||||
points = [PointStruct(id=str(uuid4()), vector=embeddings[i], payload={'text': c.text, 'source': c.source})
|
||||
for i, c in enumerate(data.chunks)]
|
||||
qdrant.upsert(collection_name=coll, points=points)
|
||||
return EmbedResponse(status="✅ Saved", count=len(points), collection=coll)
|
||||
|
||||
# /search
|
||||
@app.get("/search", response_model=List[SearchResultItem])
|
||||
def search_text(query: str = Query(..., min_length=1), limit: int = Query(3, ge=1), collection: str = Query("default")):
|
||||
vec = model.encode(query).tolist()
|
||||
res = qdrant.search(collection_name=collection, query_vector=vec, limit=limit)
|
||||
return [SearchResultItem(score=r.score, text=r.payload['text']) for r in res]
|
||||
|
||||
# /prompt
|
||||
@app.post("/prompt", response_model=PromptResponse)
|
||||
def prompt(data: PromptRequest):
|
||||
if not data.query.strip(): raise HTTPException(status_code=400, detail="'query' darf nicht leer sein.")
|
||||
if not (1 <= data.context_limit <= 10): raise HTTPException(status_code=400, detail="'context_limit' muss zwischen 1 und 10 liegen.")
|
||||
hits = qdrant.search(collection_name=data.collection, query_vector=model.encode(data.query).tolist(), limit=data.context_limit)
|
||||
context = '\n'.join(h.payload['text'] for h in hits)
|
||||
payload = {'model': OLLAMA_MODEL, 'prompt': f"Context:\n{context}\nQuestion: {data.query}", 'stream': False}
|
||||
try:
|
||||
r = requests.post(OLLAMA_URL, json=payload, timeout=30); r.raise_for_status()
|
||||
except Exception:
|
||||
raise HTTPException(status_code=502, detail="LLM-Service-Fehler.")
|
||||
return PromptResponse(answer=r.json().get('response', ''), context=context, collection=data.collection)
|
||||
|
||||
# /delete-source (neue Routine gemäß ursprünglicher funktionierender Logik)
|
||||
@app.delete("/delete-source", response_model=DeleteResponse)
|
||||
def delete_by_source(
|
||||
collection: str = Query(...),
|
||||
source: str = Query(...),
|
||||
type: Optional[str] = Query(None)
|
||||
):
|
||||
if not qdrant.collection_exists(collection):
|
||||
raise HTTPException(status_code=404, detail=f"Collection '{collection}' nicht gefunden.")
|
||||
# Filter-Bedingungen
|
||||
must = [{"key": "source", "match": {"value": source}}]
|
||||
if type:
|
||||
must.append({"key": "type", "match": {"value": type}})
|
||||
# IDs sammeln via scroll_filter
|
||||
try:
|
||||
points, _ = qdrant.scroll(
|
||||
collection_name=collection,
|
||||
scroll_filter={"must": must},
|
||||
limit=10000
|
||||
)
|
||||
except Exception as exc:
|
||||
print(f"[ERROR] Scroll failed: {exc}", flush=True)
|
||||
raise HTTPException(status_code=500, detail="Fehler beim Abrufen der Punkte vor dem Löschen.")
|
||||
point_ids = [str(pt.id) for pt in points]
|
||||
if not point_ids:
|
||||
return DeleteResponse(status="🔍 Keine passenden Einträge gefunden.", count=0, collection=collection, source=source, type=type)
|
||||
# Lösche mittels PointIdsList(points=...)
|
||||
try:
|
||||
qdrant.delete(
|
||||
collection_name=collection,
|
||||
points_selector=PointIdsList(points=point_ids)
|
||||
)
|
||||
except Exception as exc:
|
||||
print(f"[ERROR] Delete failed: {exc}", flush=True)
|
||||
raise HTTPException(status_code=500, detail="Fehler beim Löschen nach Source.")
|
||||
return DeleteResponse(status="🗑️ gelöscht", count=len(point_ids), collection=collection, source=source, type=type)
|
||||
|
||||
# /delete-collection
|
||||
@app.delete("/delete-collection", response_model=DeleteResponse)
|
||||
def delete_collection(collection: str = Query(...)):
|
||||
if not qdrant.collection_exists(collection):
|
||||
raise HTTPException(status_code=404, detail=f"Collection '{collection}' nicht gefunden.")
|
||||
qdrant.delete_collection(collection_name=collection)
|
||||
return DeleteResponse(status="🗑️ gelöscht", count=0, collection=collection)
|
||||
@@ -0,0 +1,196 @@
|
||||
from fastapi import FastAPI, Query, HTTPException
|
||||
from pydantic import BaseModel
|
||||
from typing import List
|
||||
from sentence_transformers import SentenceTransformer
|
||||
from qdrant_client import QdrantClient
|
||||
from qdrant_client.models import VectorParams, Distance, PointStruct
|
||||
from fastapi import HTTPException
|
||||
from uuid import uuid4
|
||||
import requests
|
||||
from datetime import datetime
|
||||
|
||||
from qdrant_client.models import PointIdsList
|
||||
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
# Konfiguration
|
||||
model = SentenceTransformer("all-MiniLM-L6-v2")
|
||||
qdrant = QdrantClient(host="localhost", port=6333)
|
||||
OLLAMA_URL = "http://localhost:11434/api/generate"
|
||||
OLLAMA_MODEL = "mistral"
|
||||
|
||||
# Datenmodelle
|
||||
|
||||
from typing import List, Dict, Any
|
||||
|
||||
class ChunkInput(BaseModel):
|
||||
text: str
|
||||
source: str
|
||||
source_type: str = "file"
|
||||
title: str | None = None
|
||||
version: str | None = None
|
||||
related_to: str | None = None
|
||||
tags: List[str] = []
|
||||
owner: str | None = None
|
||||
context_tag: str | None = None
|
||||
imported_at: str | None = None
|
||||
chunk_index: int | None = None
|
||||
category: str | None = None
|
||||
|
||||
class EmbedRequest(BaseModel):
|
||||
chunks: List[ChunkInput]
|
||||
collection: str = "default"
|
||||
|
||||
class PromptRequest(BaseModel):
|
||||
query: str
|
||||
context_limit: int = 3
|
||||
collection: str = "default"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@app.delete("/delete-source")
|
||||
def delete_by_source(
|
||||
collection: str = Query(...),
|
||||
source: str = Query(...),
|
||||
type: str = Query(None)
|
||||
):
|
||||
if not qdrant.collection_exists(collection):
|
||||
raise HTTPException(status_code=404, detail=f"Collection '{collection}' nicht gefunden.")
|
||||
|
||||
must = [{"key": "source", "match": {"value": source}}]
|
||||
if type:
|
||||
must.append({"key": "type", "match": {"value": type}})
|
||||
|
||||
result = qdrant.scroll(
|
||||
collection_name=collection,
|
||||
scroll_filter={"must": must},
|
||||
limit=10000
|
||||
)
|
||||
|
||||
points = result[0]
|
||||
if not points:
|
||||
return {"status": "🔍 Keine passenden Einträge gefunden."}
|
||||
|
||||
point_ids = []
|
||||
for point in points:
|
||||
pid = point.id
|
||||
point_ids.append(str(pid)) # immer zu String casten
|
||||
|
||||
qdrant.delete(
|
||||
collection_name=collection,
|
||||
points_selector=PointIdsList(points=point_ids)
|
||||
|
||||
)
|
||||
|
||||
return {
|
||||
"status": "🗑️ gelöscht",
|
||||
"count": len(point_ids),
|
||||
"collection": collection,
|
||||
"source": source,
|
||||
"type": type
|
||||
}
|
||||
|
||||
|
||||
|
||||
@app.delete("/delete-collection")
|
||||
def delete_collection(collection: str = Query(...)):
|
||||
"""
|
||||
Löscht eine gesamte Collection aus Qdrant.
|
||||
"""
|
||||
if not qdrant.collection_exists(collection):
|
||||
raise HTTPException(status_code=404, detail=f"Collection '{collection}' nicht gefunden.")
|
||||
|
||||
qdrant.delete_collection(collection_name=collection)
|
||||
return {"status": "🗑️ gelöscht", "collection": collection}
|
||||
|
||||
|
||||
@app.post("/embed")
|
||||
def embed_texts(data: EmbedRequest):
|
||||
collection_name = data.collection
|
||||
|
||||
if not qdrant.collection_exists(collection_name):
|
||||
qdrant.recreate_collection(
|
||||
collection_name=collection_name,
|
||||
vectors_config=VectorParams(size=384, distance=Distance.COSINE)
|
||||
)
|
||||
|
||||
embeddings = model.encode([chunk.text for chunk in data.chunks]).tolist()
|
||||
|
||||
points = []
|
||||
for i, chunk in enumerate(data.chunks):
|
||||
payload = {
|
||||
"text": chunk.text,
|
||||
"source": chunk.source,
|
||||
"source_type": chunk.source_type,
|
||||
"title": chunk.title,
|
||||
"version": chunk.version,
|
||||
"related_to": chunk.related_to,
|
||||
"tags": chunk.tags,
|
||||
"owner": chunk.owner,
|
||||
"context_tag": chunk.context_tag,
|
||||
"imported_at": chunk.imported_at or datetime.utcnow().isoformat(),
|
||||
"chunk_index": chunk.chunk_index,
|
||||
"category": chunk.category or data.collection
|
||||
}
|
||||
|
||||
point = PointStruct(
|
||||
id=str(uuid4()),
|
||||
vector=embeddings[i],
|
||||
payload=payload
|
||||
)
|
||||
points.append(point)
|
||||
|
||||
qdrant.upsert(collection_name=collection_name, points=points)
|
||||
|
||||
return {
|
||||
"status": "✅ embeddings saved",
|
||||
"count": len(points),
|
||||
"collection": collection_name
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@app.get("/search")
|
||||
def search_text(query: str = Query(...), limit: int = 3, collection: str = Query(...)):
|
||||
vec = model.encode(query).tolist()
|
||||
results = qdrant.search(collection_name=collection, query_vector=vec, limit=limit)
|
||||
return [{"score": r.score, "text": r.payload["text"]} for r in results]
|
||||
|
||||
@app.post("/prompt")
|
||||
def generate_prompt(data: PromptRequest):
|
||||
query_vec = model.encode(data.query).tolist()
|
||||
results = qdrant.search(
|
||||
collection_name=data.collection,
|
||||
query_vector=query_vec,
|
||||
limit=data.context_limit
|
||||
)
|
||||
|
||||
context = "\n".join([r.payload["text"] for r in results])
|
||||
full_prompt = f"""Beantworte die folgende Frage basierend auf dem Kontext:
|
||||
|
||||
Kontext:
|
||||
{context}
|
||||
|
||||
Frage:
|
||||
{data.query}
|
||||
"""
|
||||
|
||||
ollama_payload = {
|
||||
"model": OLLAMA_MODEL,
|
||||
"prompt": full_prompt,
|
||||
"stream": False
|
||||
}
|
||||
|
||||
response = requests.post(OLLAMA_URL, json=ollama_payload)
|
||||
response.raise_for_status()
|
||||
answer = response.json()["response"]
|
||||
|
||||
return {
|
||||
"answer": answer,
|
||||
"context": context,
|
||||
"collection": data.collection
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
from fastapi import FastAPI, Query, HTTPException
|
||||
from pydantic import BaseModel
|
||||
from typing import List
|
||||
from sentence_transformers import SentenceTransformer
|
||||
from qdrant_client import QdrantClient
|
||||
from qdrant_client.models import VectorParams, Distance, PointStruct
|
||||
from fastapi import HTTPException
|
||||
from uuid import uuid4
|
||||
import requests
|
||||
from datetime import datetime
|
||||
|
||||
from qdrant_client.models import PointIdsList
|
||||
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
# Konfiguration
|
||||
model = SentenceTransformer("all-MiniLM-L6-v2")
|
||||
qdrant = QdrantClient(host="localhost", port=6333)
|
||||
OLLAMA_URL = "http://localhost:11434/api/generate"
|
||||
OLLAMA_MODEL = "mistral"
|
||||
|
||||
# Datenmodelle
|
||||
|
||||
from typing import List, Dict, Any
|
||||
|
||||
class ChunkInput(BaseModel):
|
||||
text: str
|
||||
source: str
|
||||
source_type: str = "file"
|
||||
title: str | None = None
|
||||
version: str | None = None
|
||||
related_to: str | None = None
|
||||
tags: List[str] = []
|
||||
owner: str | None = None
|
||||
context_tag: str | None = None
|
||||
imported_at: str | None = None
|
||||
chunk_index: int | None = None
|
||||
category: str | None = None
|
||||
|
||||
class EmbedRequest(BaseModel):
|
||||
chunks: List[ChunkInput]
|
||||
collection: str = "default"
|
||||
|
||||
class PromptRequest(BaseModel):
|
||||
query: str
|
||||
context_limit: int = 3
|
||||
collection: str = "default"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@app.delete("/delete-source")
|
||||
def delete_by_source(
|
||||
collection: str = Query(...),
|
||||
source: str = Query(...),
|
||||
type: str = Query(None)
|
||||
):
|
||||
if not qdrant.collection_exists(collection):
|
||||
raise HTTPException(status_code=404, detail=f"Collection '{collection}' nicht gefunden.")
|
||||
|
||||
must = [{"key": "source", "match": {"value": source}}]
|
||||
if type:
|
||||
must.append({"key": "type", "match": {"value": type}})
|
||||
|
||||
result = qdrant.scroll(
|
||||
collection_name=collection,
|
||||
scroll_filter={"must": must},
|
||||
limit=10000
|
||||
)
|
||||
|
||||
points = result[0]
|
||||
if not points:
|
||||
return {"status": "🔍 Keine passenden Einträge gefunden."}
|
||||
|
||||
point_ids = []
|
||||
for point in points:
|
||||
pid = point.id
|
||||
point_ids.append(str(pid)) # immer zu String casten
|
||||
|
||||
qdrant.delete(
|
||||
collection_name=collection,
|
||||
points_selector=PointIdsList(points=point_ids)
|
||||
|
||||
)
|
||||
|
||||
return {
|
||||
"status": "🗑️ gelöscht",
|
||||
"count": len(point_ids),
|
||||
"collection": collection,
|
||||
"source": source,
|
||||
"type": type
|
||||
}
|
||||
|
||||
|
||||
|
||||
@app.delete("/delete-collection")
|
||||
def delete_collection(collection: str = Query(...)):
|
||||
"""
|
||||
Löscht eine gesamte Collection aus Qdrant.
|
||||
"""
|
||||
if not qdrant.collection_exists(collection):
|
||||
raise HTTPException(status_code=404, detail=f"Collection '{collection}' nicht gefunden.")
|
||||
|
||||
qdrant.delete_collection(collection_name=collection)
|
||||
return {"status": "🗑️ gelöscht", "collection": collection}
|
||||
|
||||
|
||||
@app.post("/embed")
|
||||
def embed_texts(data: EmbedRequest):
|
||||
collection_name = data.collection
|
||||
|
||||
if not qdrant.collection_exists(collection_name):
|
||||
qdrant.recreate_collection(
|
||||
collection_name=collection_name,
|
||||
vectors_config=VectorParams(size=384, distance=Distance.COSINE)
|
||||
)
|
||||
|
||||
embeddings = model.encode([chunk.text for chunk in data.chunks]).tolist()
|
||||
|
||||
points = []
|
||||
for i, chunk in enumerate(data.chunks):
|
||||
payload = {
|
||||
"text": chunk.text,
|
||||
"source": chunk.source,
|
||||
"source_type": chunk.source_type,
|
||||
"title": chunk.title,
|
||||
"version": chunk.version,
|
||||
"related_to": chunk.related_to,
|
||||
"tags": chunk.tags,
|
||||
"owner": chunk.owner,
|
||||
"context_tag": chunk.context_tag,
|
||||
"imported_at": chunk.imported_at or datetime.utcnow().isoformat(),
|
||||
"chunk_index": chunk.chunk_index,
|
||||
"category": chunk.category or data.collection
|
||||
}
|
||||
|
||||
point = PointStruct(
|
||||
id=str(uuid4()),
|
||||
vector=embeddings[i],
|
||||
payload=payload
|
||||
)
|
||||
points.append(point)
|
||||
|
||||
qdrant.upsert(collection_name=collection_name, points=points)
|
||||
|
||||
return {
|
||||
"status": "✅ embeddings saved",
|
||||
"count": len(points),
|
||||
"collection": collection_name
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@app.get("/search")
|
||||
def search_text(query: str = Query(...), limit: int = 3, collection: str = Query(...)):
|
||||
vec = model.encode(query).tolist()
|
||||
results = qdrant.search(collection_name=collection, query_vector=vec, limit=limit)
|
||||
return [{"score": r.score, "text": r.payload["text"]} for r in results]
|
||||
|
||||
@app.post("/prompt")
|
||||
def generate_prompt(data: PromptRequest):
|
||||
query_vec = model.encode(data.query).tolist()
|
||||
results = qdrant.search(
|
||||
collection_name=data.collection,
|
||||
query_vector=query_vec,
|
||||
limit=data.context_limit
|
||||
)
|
||||
|
||||
context = "\n".join([r.payload["text"] for r in results])
|
||||
full_prompt = f"""Beantworte die folgende Frage basierend auf dem Kontext:
|
||||
|
||||
Kontext:
|
||||
{context}
|
||||
|
||||
Frage:
|
||||
{data.query}
|
||||
"""
|
||||
|
||||
ollama_payload = {
|
||||
"model": OLLAMA_MODEL,
|
||||
"prompt": full_prompt,
|
||||
"stream": False
|
||||
}
|
||||
|
||||
response = requests.post(OLLAMA_URL, json=ollama_payload)
|
||||
response.raise_for_status()
|
||||
answer = response.json()["response"]
|
||||
|
||||
return {
|
||||
"answer": answer,
|
||||
"context": context,
|
||||
"collection": data.collection
|
||||
}
|
||||
@@ -0,0 +1,220 @@
|
||||
from fastapi import FastAPI, Query, HTTPException
|
||||
from fastapi.openapi.utils import get_openapi
|
||||
from pydantic import BaseModel
|
||||
from typing import List
|
||||
from sentence_transformers import SentenceTransformer
|
||||
from qdrant_client import QdrantClient
|
||||
from qdrant_client.models import VectorParams, Distance, PointStruct
|
||||
from fastapi import HTTPException
|
||||
from uuid import uuid4
|
||||
import requests
|
||||
from datetime import datetime
|
||||
|
||||
from qdrant_client.models import PointIdsList
|
||||
|
||||
|
||||
app = FastAPI(
|
||||
title="Lokaler KI Agent",
|
||||
description="Lokale API zur Ansteuerung des LLM und qdrant",
|
||||
version="1.0.0",
|
||||
docs_url="/docs",
|
||||
redoc_url="/redoc",
|
||||
openapi_url="/openapi.json",
|
||||
)
|
||||
|
||||
|
||||
# Konfiguration
|
||||
model = SentenceTransformer("all-MiniLM-L6-v2")
|
||||
qdrant = QdrantClient(host="localhost", port=6333)
|
||||
OLLAMA_URL = "http://localhost:11434/api/generate"
|
||||
OLLAMA_MODEL = "mistral"
|
||||
|
||||
# Datenmodelle
|
||||
|
||||
from typing import List, Dict, Any
|
||||
|
||||
class ChunkInput(BaseModel):
|
||||
text: str
|
||||
source: str
|
||||
source_type: str = "file"
|
||||
title: str | None = None
|
||||
version: str | None = None
|
||||
related_to: str | None = None
|
||||
tags: List[str] = []
|
||||
owner: str | None = None
|
||||
context_tag: str | None = None
|
||||
imported_at: str | None = None
|
||||
chunk_index: int | None = None
|
||||
category: str | None = None
|
||||
|
||||
class EmbedRequest(BaseModel):
|
||||
chunks: List[ChunkInput]
|
||||
collection: str = "default"
|
||||
|
||||
class PromptRequest(BaseModel):
|
||||
query: str
|
||||
context_limit: int = 3
|
||||
collection: str = "default"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@app.delete("/delete-source")
|
||||
def delete_by_source(
|
||||
collection: str = Query(...),
|
||||
source: str = Query(...),
|
||||
type: str = Query(None)
|
||||
):
|
||||
if not qdrant.collection_exists(collection):
|
||||
raise HTTPException(status_code=404, detail=f"Collection '{collection}' nicht gefunden.")
|
||||
|
||||
must = [{"key": "source", "match": {"value": source}}]
|
||||
if type:
|
||||
must.append({"key": "type", "match": {"value": type}})
|
||||
|
||||
result = qdrant.scroll(
|
||||
collection_name=collection,
|
||||
scroll_filter={"must": must},
|
||||
limit=10000
|
||||
)
|
||||
|
||||
points = result[0]
|
||||
if not points:
|
||||
return {"status": "🔍 Keine passenden Einträge gefunden."}
|
||||
|
||||
point_ids = []
|
||||
for point in points:
|
||||
pid = point.id
|
||||
point_ids.append(str(pid)) # immer zu String casten
|
||||
|
||||
qdrant.delete(
|
||||
collection_name=collection,
|
||||
points_selector=PointIdsList(points=point_ids)
|
||||
|
||||
)
|
||||
|
||||
return {
|
||||
"status": "🗑️ gelöscht",
|
||||
"count": len(point_ids),
|
||||
"collection": collection,
|
||||
"source": source,
|
||||
"type": type
|
||||
}
|
||||
|
||||
|
||||
|
||||
@app.delete("/delete-collection")
|
||||
def delete_collection(collection: str = Query(...)):
|
||||
"""
|
||||
Löscht eine gesamte Collection aus Qdrant.
|
||||
"""
|
||||
if not qdrant.collection_exists(collection):
|
||||
raise HTTPException(status_code=404, detail=f"Collection '{collection}' nicht gefunden.")
|
||||
|
||||
qdrant.delete_collection(collection_name=collection)
|
||||
return {"status": "🗑️ gelöscht", "collection": collection}
|
||||
|
||||
|
||||
@app.post("/embed")
|
||||
def embed_texts(data: EmbedRequest):
|
||||
collection_name = data.collection
|
||||
|
||||
if not qdrant.collection_exists(collection_name):
|
||||
qdrant.recreate_collection(
|
||||
collection_name=collection_name,
|
||||
vectors_config=VectorParams(size=384, distance=Distance.COSINE)
|
||||
)
|
||||
|
||||
embeddings = model.encode([chunk.text for chunk in data.chunks]).tolist()
|
||||
|
||||
points = []
|
||||
for i, chunk in enumerate(data.chunks):
|
||||
payload = {
|
||||
"text": chunk.text,
|
||||
"source": chunk.source,
|
||||
"source_type": chunk.source_type,
|
||||
"title": chunk.title,
|
||||
"version": chunk.version,
|
||||
"related_to": chunk.related_to,
|
||||
"tags": chunk.tags,
|
||||
"owner": chunk.owner,
|
||||
"context_tag": chunk.context_tag,
|
||||
"imported_at": chunk.imported_at or datetime.utcnow().isoformat(),
|
||||
"chunk_index": chunk.chunk_index,
|
||||
"category": chunk.category or data.collection
|
||||
}
|
||||
|
||||
point = PointStruct(
|
||||
id=str(uuid4()),
|
||||
vector=embeddings[i],
|
||||
payload=payload
|
||||
)
|
||||
points.append(point)
|
||||
|
||||
qdrant.upsert(collection_name=collection_name, points=points)
|
||||
|
||||
return {
|
||||
"status": "✅ embeddings saved",
|
||||
"count": len(points),
|
||||
"collection": collection_name
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@app.get("/search")
|
||||
def search_text(query: str = Query(...), limit: int = 3, collection: str = Query(...)):
|
||||
vec = model.encode(query).tolist()
|
||||
results = qdrant.search(collection_name=collection, query_vector=vec, limit=limit)
|
||||
return [{"score": r.score, "text": r.payload["text"]} for r in results]
|
||||
|
||||
@app.post("/prompt")
|
||||
def generate_prompt(data: PromptRequest):
|
||||
query_vec = model.encode(data.query).tolist()
|
||||
results = qdrant.search(
|
||||
collection_name=data.collection,
|
||||
query_vector=query_vec,
|
||||
limit=data.context_limit
|
||||
)
|
||||
|
||||
context = "\n".join([r.payload["text"] for r in results])
|
||||
full_prompt = f"""Beantworte die folgende Frage basierend auf dem Kontext:
|
||||
|
||||
Kontext:
|
||||
{context}
|
||||
|
||||
Frage:
|
||||
{data.query}
|
||||
"""
|
||||
|
||||
ollama_payload = {
|
||||
"model": OLLAMA_MODEL,
|
||||
"prompt": full_prompt,
|
||||
"stream": False
|
||||
}
|
||||
|
||||
response = requests.post(OLLAMA_URL, json=ollama_payload)
|
||||
response.raise_for_status()
|
||||
answer = response.json()["response"]
|
||||
|
||||
return {
|
||||
"answer": answer,
|
||||
"context": context,
|
||||
"collection": data.collection
|
||||
}
|
||||
|
||||
def custom_openapi():
|
||||
if app.openapi_schema:
|
||||
return app.openapi_schema
|
||||
openapi_schema = get_openapi(
|
||||
title=app.title,
|
||||
version=app.version,
|
||||
description=app.description,
|
||||
routes=app.routes,
|
||||
)
|
||||
# hier können z.B. Security-Schemes ergänzt werden
|
||||
app.openapi_schema = openapi_schema
|
||||
return app.openapi_schema
|
||||
|
||||
app.openapi = custom_openapi
|
||||
Reference in new issue
Block a user