-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
87 lines (68 loc) · 2.95 KB
/
Copy pathagent.py
File metadata and controls
87 lines (68 loc) · 2.95 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
from langchain_community.vectorstores import Chroma
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_core.prompts import PromptTemplate
from langchain_core.runnables import RunnablePassthrough
import os
import requests
DATA_PATH = "datasets/pharma_data"
DB_CHROMA_PATH = "vector_stores/db_chroma"
EMBEDDINGS_MODEL = "thenlper/gte-large"
LOCAL_API_URL = "http://127.0.0.1:1234"
def get_embeddings_model(model_name=EMBEDDINGS_MODEL, device="cpu"):
embeddings_model = HuggingFaceEmbeddings(model_name=model_name, model_kwargs={"device": device})
return embeddings_model
def load_vector_db():
if not os.path.exists(DB_CHROMA_PATH):
raise FileNotFoundError(f"Chroma database not found at {DB_CHROMA_PATH}")
embeddings = get_embeddings_model()
return Chroma(persist_directory=DB_CHROMA_PATH, embedding_function=embeddings)
custom_prompt_template = """
<s> [INST] You are an assistant for answering pharma-related queries.
Use the provided context to answer the question.
If the information is not available, state it clearly. [/INST] </s>
[INST] Question: {question}
Context: {context}
Answer: [/INST]
"""
def set_custom_prompt():
return PromptTemplate(template=custom_prompt_template, input_variables=["context", "question"])
def search_tool(query):
vectordb = load_vector_db()
retriever = vectordb.as_retriever(search_kwargs={"k": 5})
docs = retriever.get_relevant_documents(query)
return "\n\n".join([doc.page_content for doc in docs])
def call_local_model_api(prompt):
try:
response = requests.post(
f"{LOCAL_API_URL}/v1/chat/completions",
json={"messages": [{"role": "user", "content": prompt}], "max_tokens": 800, "temperature": 0}
)
response.raise_for_status()
return response.json().get("choices", [{}])[0].get("message", {}).get("content", "No response")
except requests.exceptions.RequestException as e:
raise RuntimeError(f"Error with local API: {e}")
def router_node(query):
if "search" in query.lower() or "find" in query.lower():
context = search_tool(query)
if context:
return {"tool": "search", "result": context}
else:
return {"tool": "search", "result": "No relevant documents found."}
else:
context = search_tool(query)
prompt = set_custom_prompt().format(context=context, question=query)
answer = call_local_model_api(prompt)
return {"tool": "qa", "result": answer}
def pharma_query_bot():
print("Welcome to the Pharma Query Assistant!")
print("Type your query or 'quit' to exit.")
while True:
query = input("Your Query: ")
if query.lower() == "quit":
print("Goodbye!")
break
result = router_node(query)
print(f"Tool Used: {result['tool'].capitalize()}")
print(f"Response: {result['result']}\n")
if __name__ == "__main__":
pharma_query_bot()