Spaces:
Runtime error
Runtime error
File size: 2,345 Bytes
7b43396 | 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 | import os
from dotenv import load_dotenv
from vector_processor import VectorDBProcessor
from graph_processor import GraphDBProcessor
from openai import OpenAI
# Load environment variables from .env file
load_dotenv()
class HybridProcessor:
def __init__(self):
self.vector_processor = VectorDBProcessor()
self.graph_processor = GraphDBProcessor()
self.openai_client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
def process_and_store_pdf(self, file_obj):
vector_filename = self.vector_processor.process_and_store_pdf(file_obj)
graph_filename = self.graph_processor.process_and_store_pdf(file_obj)
return vector_filename
def query(self, question):
vector_result = self.vector_processor.query(question)
graph_result = self.graph_processor.query(question)
combined_context = f"Vector DB Context:\n{vector_result['answer']}\n\nGraph DB Context:\n{graph_result['answer']}"
prompt = f"""Based on the combined context from vector and graph databases, provide a concise, accurate answer to the question. Prioritize information that appears in both sources for reliability.
Question: {question}
Combined Context: {combined_context}
Answer: """
response = self.openai_client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "You are a helpful assistant that answers questions based on combined vector and graph data."},
{"role": "user", "content": prompt}
],
temperature=0,
max_tokens=100
)
answer = response.choices[0].message.content.strip()
return {
"answer": answer,
"chunks": {
"vector": vector_result["chunks"],
"graph": graph_result["chunks"]
}
}
def get_processed_files(self):
vector_files = self.vector_processor.get_processed_files()
graph_files = self.graph_processor.get_processed_files()
return vector_files.intersection(graph_files)
def delete_file_points(self, filename):
vector_status = self.vector_processor.delete_file_points(filename)
graph_status = self.graph_processor.delete_file_points(filename)
return f"{vector_status}\n{graph_status}" |