-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
42 lines (29 loc) · 1.11 KB
/
main.py
File metadata and controls
42 lines (29 loc) · 1.11 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
from langchain_chroma import Chroma
from langchain_openai import OpenAIEmbeddings
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from dotenv import load_dotenv
load_dotenv()
DB_PATH="db"
prompt_template = """
Answer the following question from user:
{question}
using the follow knowledge base above:
{knowledge_base}"""
def make_question():
question = input("Question: ")
db = Chroma(persist_directory=DB_PATH, embedding_function=OpenAIEmbeddings())
results = db.similarity_search_with_relevance_scores(question, k=4)
if len(results) == 0 or results[0][1] < 0.7:
print("Couldn't find an answer to that question")
full_answer = []
for result in results:
text = result[0].page_content
full_answer.append(text)
knowledge_base = "\n\n ------ \n\n".join(full_answer)
prompt = ChatPromptTemplate.from_template(prompt_template)
prompt = prompt.invoke({"question": question, "knowledge_base": knowledge_base})
model = ChatOpenAI()
ai_answering = model.invoke(prompt)
print(ai_answering)
make_question()