-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
57 lines (42 loc) · 1.39 KB
/
Copy pathmain.py
File metadata and controls
57 lines (42 loc) · 1.39 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
from dotenv import load_dotenv
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_community.vectorstores import Chroma
from langchain_mistralai import ChatMistralAI
from langchain_core.prompts import ChatPromptTemplate
load_dotenv()
embedding_model = HuggingFaceEmbeddings()
vectorstore = Chroma(
embedding_function=embedding_model,
persist_directory="chroma_db"
)
retriever = vectorstore.as_retriever(
search_type="mmr",
search_kwargs={
"k":3,
"fetch_k":10,
"lambda_mult":0.5
}
)
llm = ChatMistralAI(model="mistral-small-latest")
prompt = ChatPromptTemplate.from_messages(
[
("system", """You are a helpful assistant that provides concise answers based on retrieved documents. If the retrieved documents do not contain enough information to answer the question, say "I could not find the information in the given document" """),
("human", "Context:{context} Question:{question}")
]
)
print("Rag system created ")
print("press 0 to exit ")
while True:
query = input("You : ")
if query == "0":
break
docs = retriever.invoke(query)
context = "\n\n".join(
[doc.page_content for doc in docs]
)
final_prompt = prompt.invoke({
"context" :context,
"question": query
})
response = llm.invoke(final_prompt)
print(f"\n AI: {response.content}")