-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquery.py
66 lines (56 loc) · 1.89 KB
/
query.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
import os
from pinecone import Pinecone
from openai import OpenAI
import logging
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import List, Optional
LOGGER = logging.getLogger(__name__)
app = FastAPI()
class SearchResult(BaseModel):
filename: str
path: str
score: float
class SearchResponse(BaseModel):
results: List[SearchResult]
class SearchQuery(BaseModel):
query: str
def search_files(query_text: str, client: OpenAI, index) -> Optional[List[SearchResult]]:
"""Execute semantic search against the vector database."""
try:
# Get embedding for query
query_embedding = client.embeddings.create(
input=query_text,
model="text-embedding-3-small"
).data[0].embedding
# Search with user_id filter
results = index.query(
vector=query_embedding,
filter={
"user_id": os.getenv('USER_ID')
},
top_k=5,
include_metadata=True
)
if not results or not results.matches:
return []
return [
SearchResult(
filename=match.metadata['filename'],
path=match.metadata['path'],
score=match.score
)
for match in results.matches
]
except Exception as e:
LOGGER.error(f"Error during search: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
# Initialize clients
pc = Pinecone(api_key=os.getenv('PINECONE_API_KEY'))
index = pc.Index(os.getenv('PINECONE_INDEX_NAME'))
client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
@app.post("/search", response_model=SearchResponse)
async def search_endpoint(query: SearchQuery):
"""API endpoint for semantic search"""
results = search_files(query.query, client, index)
return SearchResponse(results=results)