-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllama.py
74 lines (53 loc) · 2.04 KB
/
llama.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
62
63
64
65
66
67
68
69
70
71
72
73
74
from langchain.chat_models import ChatOllama
from langchain_community.vectorstores import Chroma
from langchain_community.embeddings import OllamaEmbeddings
from langchain.schema.runnable import RunnablePassthrough
from langchain.prompts import PromptTemplate
import os
def chat_with(vector):
print(f"Now chatting with the {vector}")
print("Type pdf() at any time to select a different pdf")
ollamaEmbeddings = OllamaEmbeddings(model="llama3")
vector_path = f"./vector/my_data/{vector}"
new_vector_store_connection = Chroma(
persist_directory=vector_path,
embedding_function=ollamaEmbeddings,
)
chat_model = ChatOllama(model="llama3", temperature=0.5)
template = """Use this context to answer the question. Do not imagine anything. Use the context to know the information, which you can use to then form sentences. If query not at all in context, say \"The question does not seem to be about the policy documents. Please rephrase the question and try again.\"
{context}
Question: {question}
Helpful Answer:"""
prompt = PromptTemplate.from_template(template)
chain = (
{
"context": new_vector_store_connection.as_retriever(),
"question": RunnablePassthrough(),
}
| prompt
| chat_model
)
while True:
question = input("Enter question: ")
if question == "pdf()":
select_pdf()
result = chain.invoke(question)
print(result.content)
def select_pdf():
i = 1
pdf_dict = {}
for pdf in os.listdir("flattened_pdfs"):
pdf_dict[str(i)] = pdf[:-4]
i += 1
pdf_dict[str(i)] = "complete_text"[:-4]
print("Choose the pdf you want to chat with:")
for key, value in pdf_dict.items():
print(f"{key}. {value}")
print("Press any other key to exit")
vector = input("Selection: ")
if vector not in pdf_dict:
print("Invalid Input, Exiting...")
exit()
vector = pdf_dict[vector]
chat_with(vector)
select_pdf()