-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathqdrant_light_server.py
195 lines (166 loc) · 5.95 KB
/
qdrant_light_server.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import os
from openai import AsyncOpenAI
import numpy as np
from typing import Optional, List
import asyncio
import nest_asyncio
from qdrant_client import QdrantClient, models
# Apply nest_asyncio to solve event loop issues
nest_asyncio.apply()
app = FastAPI(title="LightRAG API with Qdrant", description="API for RAG operations")
# Configuration
QDRANT_URL = "https://blahblahblah.us-east4-0.gcp.cloud.qdrant.io:6333"
LLM_MODEL = os.environ.get("LLM_MODEL", "gpt-4")
EMBEDDING_MODEL = os.environ.get("EMBEDDING_MODEL", "text-embedding-3-large")
EMBEDDING_MAX_TOKEN_SIZE = int(os.environ.get("EMBEDDING_MAX_TOKEN_SIZE", 8192))
print(f"LLM_MODEL: {LLM_MODEL}")
print(f"EMBEDDING_MODEL: {EMBEDDING_MODEL}")
print(f"EMBEDDING_MAX_TOKEN_SIZE: {EMBEDDING_MAX_TOKEN_SIZE}")
# Initialize clients
openai_client = AsyncOpenAI()
qdrant_client = QdrantClient(url=QDRANT_URL, api_key="your_api_key_here", timeout=60.0)
async def get_embedding(text: str) -> np.ndarray:
"""Get embeddings from OpenAI"""
response = await openai_client.embeddings.create(
model=EMBEDDING_MODEL,
input=text
)
return np.array(response.data[0].embedding)
async def llm_query(context: str, query: str) -> str:
"""Query LLM with context"""
messages = [
{"role": "system", "content": "You are a helpful assistant. Use the provided context to answer questions accurately."},
{"role": "user", "content": f"Context:\n{context}\n\nQuestion: {query}\n\nAnswer:"}
]
response = await openai_client.chat.completions.create(
model=LLM_MODEL,
messages=messages,
temperature=0.7
)
return response.choices[0].message.content
async def search_qdrant(query: str, mode: str = "hybrid") -> List[dict]:
"""Search Qdrant based on mode"""
query_vector = await get_embedding(query)
context = []
if mode == "naive":
# Simple semantic search on chunks
results = qdrant_client.search(
collection_name="chunks",
query_vector=query_vector.tolist(),
limit=5
)
context = [result.payload.get('content', '') for result in results]
elif mode == "local":
# Search entities first, then get related chunks
entities = qdrant_client.search(
collection_name="entities",
query_vector=query_vector.tolist(),
limit=3
)
for entity in entities:
# Add entity description
context.append(entity.payload.get('description', ''))
# Add associated chunk content
if 'chunks_content' in entity.payload:
context.extend(entity.payload['chunks_content'])
else: # hybrid or global
# Search both entities and chunks
chunks = qdrant_client.search(
collection_name="chunks",
query_vector=query_vector.tolist(),
limit=3
)
entities = qdrant_client.search(
collection_name="entities",
query_vector=query_vector.tolist(),
limit=2
)
# Combine results
context.extend([chunk.payload.get('content', '') for chunk in chunks])
context.extend([entity.payload.get('description', '') for entity in entities])
return context
# Data models
class QueryRequest(BaseModel):
query: str
mode: str = "hybrid"
only_need_context: bool = False
class InsertRequest(BaseModel):
text: str
class Response(BaseModel):
status: str
data: Optional[str] = None
message: Optional[str] = None
context: Optional[List[str]] = None
# API routes
@app.post("/query", response_model=Response)
async def query_endpoint(request: QueryRequest):
try:
# Get context from Qdrant
context = await search_qdrant(request.query, request.mode)
if request.only_need_context:
return Response(
status="success",
context=context
)
if not context:
return Response(
status="success",
data="I couldn't find any relevant information to answer your question."
)
# Get LLM response
context_str = "\n\n".join(context)
result = await llm_query(context_str, request.query)
return Response(
status="success",
data=result,
context=context
)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/insert", response_model=Response)
async def insert_endpoint(request: InsertRequest):
try:
# Get embedding for the text
embedding = await get_embedding(request.text)
# Create point for Qdrant
point = models.PointStruct(
id=abs(hash(request.text)) % (2**63),
vector=embedding.tolist(),
payload={
"content": request.text
}
)
# Insert into chunks collection
qdrant_client.upsert(
collection_name="chunks",
points=[point]
)
return Response(
status="success",
message="Text inserted successfully"
)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/health")
async def health_check():
try:
collections = qdrant_client.get_collections()
stats = {
collection.name: qdrant_client.get_collection(collection.name).vectors_count
for collection in collections.collections
}
return {
"status": "healthy",
"qdrant_connected": True,
"collection_stats": stats
}
except Exception as e:
return {
"status": "unhealthy",
"error": str(e)
}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8020)