-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeneration.py
More file actions
73 lines (58 loc) · 2.1 KB
/
generation.py
File metadata and controls
73 lines (58 loc) · 2.1 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
70
71
72
73
from langchain.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from retrieval import retrieve
import sys
from init import init
from vectorstore import create_chroma_vector
if __name__ == "__main__":
# Check if arguments are passed
if len(sys.argv) < 2:
print("Please provide a storage path as arguments")
print("Example: python ", sys.argv[0], " ./chroma_storage")
sys.exit(1)
storage_path = sys.argv[1]
print("Storage path=", storage_path)
init()
# vectore database
vectorstore = create_chroma_vector(storage_path)
# Get the number of documents in the vectorstore
num_documents = vectorstore._collection.count()
print("Loaded Chroma vectorstore with", num_documents, "documents")
print("For any query ***CALL TO OPENAI API FOR EMBEDDINGS***")
# Prompt
template = """Répond à la question en tenant compte uniquement du contexte suivant :
{context}
Question: {question}
"""
prompt = ChatPromptTemplate.from_template(template)
# LLM
llm = ChatOpenAI(model_name="gpt-4o-mini", temperature=0)
print(" ***** ")
print()
while True:
# Query the vectorstore from user input
question = input(
"Enter a question in french (or type 'quit' to exit): ")
if question.lower() == 'quit':
print("Exiting...")
break
# Ask for the number of documents to retrieve
try:
num_docs = int(
input("Enter the number of documents to retrieve: "))
except ValueError:
print("Invalid number. Please enter an integer.")
continue
docs_retrieved = retrieve(question, num_docs, vectorstore)
# Chain
chain = prompt | llm
# Run
answer = chain.invoke(
{"context": docs_retrieved, "question": question})
print("Answer:", answer.content)
# pretty print of doc_retrieved
for doc in docs_retrieved:
print(f" + {doc.metadata.get('source', 'Unknown')}")
print()
print(" ***** ")
print()