-
Notifications
You must be signed in to change notification settings - Fork 0
/
context_retrieval_chat_v2.py
61 lines (46 loc) · 1.92 KB
/
context_retrieval_chat_v2.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
import os
import json
import numpy as np
import faiss
import ollama
from langchain_community.llms import Ollama
from langchain.callbacks.manager import CallbackManager
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
# Load data
output_dir = 'contextual_retrieval_output'
with open(os.path.join(output_dir, 'enhanced_chunks.json'), 'r') as f:
enhanced_chunks = json.load(f)
index = faiss.read_index(os.path.join(output_dir, 'faiss_index.bin'))
# Initialize Ollama LLM for text generation
llm = Ollama(
model="llama3.2:3b",
callback_manager=CallbackManager([StreamingStdOutCallbackHandler()])
)
def generate_embedding(text):
# Use Ollama to generate embeddings with nomic-embed-text
response = ollama.embeddings(model='nomic-embed-text', prompt=text)
return np.array(response['embedding'])
def retrieve_relevant_chunks(query, top_k=3):
query_vector = generate_embedding(f"search_query: {query}")
scores, indices = index.search(np.array([query_vector]).astype('float32'), top_k)
return [enhanced_chunks[i] for i in indices[0]]
def answer_query(query):
relevant_chunks = retrieve_relevant_chunks(query)
context = "\n\n".join([f"{chunk['context']}\n\n{chunk['original_chunk']}" for chunk in relevant_chunks])
prompt = f"""Based on the following context, please answer the query. If the answer is not in the context, say "I don't have enough information to answer that."
Context:
{context}
Query: {query}
Answer:"""
return llm(prompt)
def main():
print("Welcome to the Two Pups Pizza Contextual Retrieval Q&A system!")
print("Ask any question about Two Pups Pizza, or type 'quit' to exit.")
while True:
user_query = input("Enter your query (or 'quit' to exit): ")
if user_query.lower() == 'quit':
break
answer = answer_query(user_query)
print(f"\nAnswer: {answer}\n")
if __name__ == "__main__":
main()