Repository navigation
Expand file tree
/
Copy path3_answer_generation.py
More file actions
69 lines (51 loc) · 2.04 KB
/
Copy path3_answer_generation.py
File metadata and controls
69 lines (51 loc) · 2.04 KB
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
from langchain_chroma import Chroma
from langchain_openai import OpenAIEmbeddings
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage
load_dotenv()
persistent_directory = "db/chroma_db"
# Load embeddings and vector store
embedding_model = OpenAIEmbeddings(model="text-embedding-3-small")
db = Chroma(
persist_directory=persistent_directory,
embedding_function=embedding_model,
collection_metadata={"hnsw:space": "cosine"}
)
# Search for relevant documents
query = "How much did Microsoft pay to acquire GitHub?"
retriever = db.as_retriever(search_kwargs={"k": 5})
# retriever = db.as_retriever(
# search_type="similarity_score_threshold",
# search_kwargs={
# "k": 5,
# "score_threshold": 0.3 # Only return chunks with cosine similarity ≥ 0.3
# }
# )
relevant_docs = retriever.invoke(query)
print(f"User Query: {query}")
# Display results
print("--- Context ---")
for i, doc in enumerate(relevant_docs, 1):
print(f"Document {i}:\n{doc.page_content}\n")
# Combine the query and the relevant document contents
combined_input = f"""Based on the following documents, please answer this question: {query}
Documents:
{chr(10).join([f"- {doc.page_content}" for doc in relevant_docs])}
Please provide a clear, helpful answer using only the information from these documents. If you can't find the answer in the documents, say "I don't have enough information to answer that question based on the provided documents."
"""
# Create a ChatOpenAI model
model = ChatOpenAI(model="gpt-4o")
# Define the messages for the model
messages = [
SystemMessage(content="You are a helpful assistant."),
HumanMessage(content=combined_input),
]
# Invoke the model with the combined input
result = model.invoke(messages)
# Display the full result and content only
print("\n--- Generated Response ---")
# print("Full result:")
# print(result)
print("Content only:")
print(result.content)