-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrag.py
More file actions
122 lines (93 loc) · 3.85 KB
/
Copy pathrag.py
File metadata and controls
122 lines (93 loc) · 3.85 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import requests
from langchain_community.vectorstores import Chroma
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import PromptTemplate
from langchain_core.runnables import RunnablePassthrough
from langchain_community.embeddings import HuggingFaceEmbeddings
import os
EMBEDDINGS_MODEL = "thenlper/gte-large"
DB_CHROMA_PATH = "vector_stores/db_chroma"
LOCAL_API_URL = "http://127.0.0.1:1234"
custom_prompt_template = """
<s> [INST] You are an assistant for question-answering tasks.
Use the following pieces of retrieved context to answer the question.
If you don't know, just say that you don't know.
Keep the answer concise. [/INST] </s>
[INST] Question: {question}
Context: {context}
Answer: [/INST]
"""
def set_custom_prompt():
prompt = PromptTemplate(template=custom_prompt_template, input_variables=["context", "question"])
return prompt
'''def call_local_model_api(prompt):
"""
Sends a request to the local API for model inference.
:param prompt: The input prompt for the model.
:return: Model's response as text.
"""
try:
response = requests.post(
f"{LOCAL_API_URL}/v1/chat/completions",
json={"prompt": prompt, "max_tokens": 800, "temperature": 0}
)
response.raise_for_status()
return response.json().get("response", "No response received.")
except requests.exceptions.RequestException as e:
raise RuntimeError(f"Error communicating with the local API: {e}")
'''
def get_retriever():
"""
After texts are ingested into vectordb, get it as a retriever.
"""
embeddings = HuggingFaceEmbeddings(model_name=EMBEDDINGS_MODEL, model_kwargs={"device": "cpu"})
if not os.path.exists(DB_CHROMA_PATH):
raise FileNotFoundError(f"Chroma database path not found: {DB_CHROMA_PATH}")
vectordb = Chroma(persist_directory=DB_CHROMA_PATH, embedding_function=embeddings)
return vectordb
def format_docs(docs):
return "\n\n".join([doc.page_content for doc in docs])
def QAnswer_Bot():
try:
vectordb = get_retriever()
retriever = vectordb.as_retriever(search_kwargs={"k": 10})
print("Retriever initialized successfully!")
chain = (
{"context": retriever | format_docs, "question": RunnablePassthrough()}
| set_custom_prompt()
| call_local_model_api
| StrOutputParser()
)
query = ""
while query.lower() != "quit":
query = input("Your Query: ")
if query.lower() == "quit":
print("Exiting QAnswer_Bot. Goodbye!")
break
retrieved_context = retriever.get_relevant_documents(query)
formatted_context = format_docs(retrieved_context)
prompt = custom_prompt_template.format(context=formatted_context, question=query)
output = call_local_model_api(prompt)
print(f"Answer: {output}")
except Exception as e:
print(f"Error: {e}")
def call_local_model_api(prompt):
"""
Sends a request to the local API for model inference.
"""
messages = [
{"role": "system", "content": "You are an assistant for question-answering tasks."},
{"role": "user", "content": prompt}
]
try:
response = requests.post(
f"{LOCAL_API_URL}/v1/chat/completions",
json={"messages": messages, "max_tokens": 800, "temperature": 0}
)
response.raise_for_status()
print(f"API Response: {response.text}")
return response.json().get("choices", [{}])[0].get("message", {}).get("content", "No response received.")
except requests.exceptions.RequestException as e:
raise RuntimeError(f"Error communicating with the local API: {e}")
if __name__ == "__main__":
QAnswer_Bot()