Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,31 +7,38 @@ from langchain_community.tools.tavily_search import TavilySearchResults
|
|
| 7 |
from crewai_tools import tool
|
| 8 |
from crewai import Crew, Task, Agent
|
| 9 |
from sentence_transformers import SentenceTransformer
|
|
|
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 16 |
|
| 17 |
llm = ChatOpenAI(
|
| 18 |
openai_api_base="https://api.groq.com/openai/v1",
|
| 19 |
-
openai_api_key=
|
| 20 |
model_name="llama3-70b-8192",
|
| 21 |
temperature=0.1,
|
| 22 |
max_tokens=1000
|
| 23 |
)
|
| 24 |
|
| 25 |
-
rag_tool = PDFSearchTool(
|
|
|
|
| 26 |
config=dict(
|
| 27 |
llm=dict(
|
| 28 |
-
provider="groq",
|
| 29 |
config=dict(
|
| 30 |
model="llama3-8b-8192",
|
| 31 |
),
|
| 32 |
),
|
| 33 |
embedder=dict(
|
| 34 |
-
provider="huggingface",
|
| 35 |
config=dict(
|
| 36 |
model="BAAI/bge-small-en-v1.5",
|
| 37 |
),
|
|
@@ -39,19 +46,21 @@ rag_tool = PDFSearchTool(pdf='finance.pdf',
|
|
| 39 |
)
|
| 40 |
)
|
| 41 |
|
| 42 |
-
web_search_tool = TavilySearchResults(k=3)
|
| 43 |
|
|
|
|
| 44 |
@tool
|
| 45 |
-
def router_tool(question):
|
| 46 |
-
"""Router Function"""
|
| 47 |
return 'web_search'
|
| 48 |
|
|
|
|
| 49 |
Router_Agent = Agent(
|
| 50 |
role='Router',
|
| 51 |
goal='Route user question to a vectorstore or web search',
|
| 52 |
backstory=(
|
| 53 |
-
"You are an expert at routing a user question to a vectorstore or web search."
|
| 54 |
-
"Use the vectorstore for questions on concept related to Retrieval-Augmented Generation."
|
| 55 |
"You do not need to be stringent with the keywords in the question related to these topics. Otherwise, use web-search."
|
| 56 |
),
|
| 57 |
verbose=True,
|
|
@@ -63,8 +72,8 @@ Retriever_Agent = Agent(
|
|
| 63 |
role="Retriever",
|
| 64 |
goal="Use the information retrieved from the vectorstore to answer the question",
|
| 65 |
backstory=(
|
| 66 |
-
"You are an assistant for question-answering tasks."
|
| 67 |
-
"Use the information present in the retrieved context to answer the question."
|
| 68 |
"You have to provide a clear concise answer."
|
| 69 |
),
|
| 70 |
verbose=True,
|
|
@@ -76,9 +85,9 @@ Grader_agent = Agent(
|
|
| 76 |
role='Answer Grader',
|
| 77 |
goal='Filter out erroneous retrievals',
|
| 78 |
backstory=(
|
| 79 |
-
"You are a grader assessing relevance of a retrieved document to a user question."
|
| 80 |
-
"If the document contains keywords related to the user question, grade it as relevant."
|
| 81 |
-
"It does not need to be a stringent test.You have to make sure that the answer is relevant to the question."
|
| 82 |
),
|
| 83 |
verbose=True,
|
| 84 |
allow_delegation=False,
|
|
@@ -89,8 +98,8 @@ hallucination_grader = Agent(
|
|
| 89 |
role="Hallucination Grader",
|
| 90 |
goal="Filter out hallucination",
|
| 91 |
backstory=(
|
| 92 |
-
"You are a hallucination grader assessing whether an answer is grounded in / supported by a set of facts."
|
| 93 |
-
"Make sure you meticulously review the answer and check if the response provided is in alignment with the question asked"
|
| 94 |
),
|
| 95 |
verbose=True,
|
| 96 |
allow_delegation=False,
|
|
@@ -101,74 +110,93 @@ answer_grader = Agent(
|
|
| 101 |
role="Answer Grader",
|
| 102 |
goal="Filter out hallucination from the answer.",
|
| 103 |
backstory=(
|
| 104 |
-
"You are a grader assessing whether an answer is useful to resolve a question."
|
| 105 |
-
"Make sure you meticulously review the answer and check if it makes sense for the question asked"
|
| 106 |
-
"If the answer is relevant generate a clear and concise response."
|
| 107 |
-
"If the answer generated is not relevant then perform a websearch using 'web_search_tool'"
|
| 108 |
),
|
| 109 |
verbose=True,
|
| 110 |
allow_delegation=False,
|
| 111 |
llm=llm,
|
| 112 |
)
|
| 113 |
|
|
|
|
| 114 |
router_task = Task(
|
| 115 |
-
description=(
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
),
|
| 121 |
-
expected_output=("Give a binary choice 'websearch' or 'vectorstore' based on the question"
|
| 122 |
-
"Do not provide any other preamble or explanation."),
|
| 123 |
agent=Router_Agent,
|
| 124 |
tools=[router_tool],
|
| 125 |
)
|
| 126 |
|
| 127 |
retriever_task = Task(
|
| 128 |
-
description=(
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
),
|
| 132 |
-
expected_output=("You should analyse the output of the 'router_task'"
|
| 133 |
-
"If the response is 'websearch' then use the web_search_tool to retrieve information from the web."
|
| 134 |
-
"If the response is 'vectorstore' then use the rag_tool to retrieve information from the vectorstore."
|
| 135 |
-
"Return a clear and concise text as response."),
|
| 136 |
agent=Retriever_Agent,
|
| 137 |
context=[router_task],
|
| 138 |
)
|
| 139 |
|
| 140 |
grader_task = Task(
|
| 141 |
-
description=(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
),
|
| 143 |
-
expected_output=("Binary score 'yes' or 'no' score to indicate whether the document is relevant to the question"
|
| 144 |
-
"You must answer 'yes' if the response from the 'retriever_task' is in alignment with the question asked."
|
| 145 |
-
"You must answer 'no' if the response from the 'retriever_task' is not in alignment with the question asked."
|
| 146 |
-
"Do not provide any preamble or explanations except for 'yes' or 'no'."),
|
| 147 |
agent=Grader_agent,
|
| 148 |
context=[retriever_task],
|
| 149 |
)
|
| 150 |
|
| 151 |
hallucination_task = Task(
|
| 152 |
-
description=(
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
agent=hallucination_grader,
|
| 158 |
context=[grader_task],
|
| 159 |
)
|
| 160 |
|
| 161 |
answer_task = Task(
|
| 162 |
-
description=(
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
context=[hallucination_task],
|
| 169 |
agent=answer_grader,
|
| 170 |
)
|
| 171 |
|
|
|
|
| 172 |
rag_crew = Crew(
|
| 173 |
agents=[Router_Agent, Retriever_Agent, Grader_agent, hallucination_grader, answer_grader],
|
| 174 |
tasks=[router_task, retriever_task, grader_task, hallucination_task, answer_task],
|
|
@@ -176,13 +204,14 @@ rag_crew = Crew(
|
|
| 176 |
)
|
| 177 |
|
| 178 |
def respond(
|
| 179 |
-
message,
|
| 180 |
-
history:
|
| 181 |
-
system_message,
|
| 182 |
-
max_tokens,
|
| 183 |
-
temperature,
|
| 184 |
-
top_p,
|
| 185 |
):
|
|
|
|
| 186 |
messages = [{"role": "system", "content": system_message}]
|
| 187 |
|
| 188 |
for val in history:
|
|
@@ -208,6 +237,7 @@ def respond(
|
|
| 208 |
response += token
|
| 209 |
yield response
|
| 210 |
|
|
|
|
| 211 |
demo = gr.ChatInterface(
|
| 212 |
respond,
|
| 213 |
additional_inputs=[
|
|
@@ -225,4 +255,4 @@ demo = gr.ChatInterface(
|
|
| 225 |
)
|
| 226 |
|
| 227 |
if __name__ == "__main__":
|
| 228 |
-
demo.launch()
|
|
|
|
| 7 |
from crewai_tools import tool
|
| 8 |
from crewai import Crew, Task, Agent
|
| 9 |
from sentence_transformers import SentenceTransformer
|
| 10 |
+
from typing import List, Tuple
|
| 11 |
|
| 12 |
+
# === Hardcoded API Keys ===
|
| 13 |
+
GROQ_API_KEY = "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"
|
| 14 |
+
TAVILY_API_KEY = "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"
|
| 15 |
|
| 16 |
+
# Set environment variables for API keys
|
| 17 |
+
os.environ['GROQ_API_KEY'] = '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'
|
| 18 |
+
os.environ['TAVILY_API_KEY'] = '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'
|
| 19 |
+
|
| 20 |
+
# === Model and Tool Initialization ===
|
| 21 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 22 |
|
| 23 |
llm = ChatOpenAI(
|
| 24 |
openai_api_base="https://api.groq.com/openai/v1",
|
| 25 |
+
openai_api_key=GROQ_API_KEY,
|
| 26 |
model_name="llama3-70b-8192",
|
| 27 |
temperature=0.1,
|
| 28 |
max_tokens=1000
|
| 29 |
)
|
| 30 |
|
| 31 |
+
rag_tool = PDFSearchTool(
|
| 32 |
+
pdf='finance.pdf',
|
| 33 |
config=dict(
|
| 34 |
llm=dict(
|
| 35 |
+
provider="groq",
|
| 36 |
config=dict(
|
| 37 |
model="llama3-8b-8192",
|
| 38 |
),
|
| 39 |
),
|
| 40 |
embedder=dict(
|
| 41 |
+
provider="huggingface",
|
| 42 |
config=dict(
|
| 43 |
model="BAAI/bge-small-en-v1.5",
|
| 44 |
),
|
|
|
|
| 46 |
)
|
| 47 |
)
|
| 48 |
|
| 49 |
+
web_search_tool = TavilySearchResults(k=3, api_key=TAVILY_API_KEY)
|
| 50 |
|
| 51 |
+
# === Tool Definitions ===
|
| 52 |
@tool
|
| 53 |
+
def router_tool(question: str) -> str:
|
| 54 |
+
"""Router Function: Decides between web search and vectorstore."""
|
| 55 |
return 'web_search'
|
| 56 |
|
| 57 |
+
# === Agent Definitions ===
|
| 58 |
Router_Agent = Agent(
|
| 59 |
role='Router',
|
| 60 |
goal='Route user question to a vectorstore or web search',
|
| 61 |
backstory=(
|
| 62 |
+
"You are an expert at routing a user question to a vectorstore or web search. "
|
| 63 |
+
"Use the vectorstore for questions on concept related to Retrieval-Augmented Generation. "
|
| 64 |
"You do not need to be stringent with the keywords in the question related to these topics. Otherwise, use web-search."
|
| 65 |
),
|
| 66 |
verbose=True,
|
|
|
|
| 72 |
role="Retriever",
|
| 73 |
goal="Use the information retrieved from the vectorstore to answer the question",
|
| 74 |
backstory=(
|
| 75 |
+
"You are an assistant for question-answering tasks. "
|
| 76 |
+
"Use the information present in the retrieved context to answer the question. "
|
| 77 |
"You have to provide a clear concise answer."
|
| 78 |
),
|
| 79 |
verbose=True,
|
|
|
|
| 85 |
role='Answer Grader',
|
| 86 |
goal='Filter out erroneous retrievals',
|
| 87 |
backstory=(
|
| 88 |
+
"You are a grader assessing relevance of a retrieved document to a user question. "
|
| 89 |
+
"If the document contains keywords related to the user question, grade it as relevant. "
|
| 90 |
+
"It does not need to be a stringent test. You have to make sure that the answer is relevant to the question."
|
| 91 |
),
|
| 92 |
verbose=True,
|
| 93 |
allow_delegation=False,
|
|
|
|
| 98 |
role="Hallucination Grader",
|
| 99 |
goal="Filter out hallucination",
|
| 100 |
backstory=(
|
| 101 |
+
"You are a hallucination grader assessing whether an answer is grounded in / supported by a set of facts. "
|
| 102 |
+
"Make sure you meticulously review the answer and check if the response provided is in alignment with the question asked."
|
| 103 |
),
|
| 104 |
verbose=True,
|
| 105 |
allow_delegation=False,
|
|
|
|
| 110 |
role="Answer Grader",
|
| 111 |
goal="Filter out hallucination from the answer.",
|
| 112 |
backstory=(
|
| 113 |
+
"You are a grader assessing whether an answer is useful to resolve a question. "
|
| 114 |
+
"Make sure you meticulously review the answer and check if it makes sense for the question asked. "
|
| 115 |
+
"If the answer is relevant generate a clear and concise response. "
|
| 116 |
+
"If the answer generated is not relevant then perform a websearch using 'web_search_tool'."
|
| 117 |
),
|
| 118 |
verbose=True,
|
| 119 |
allow_delegation=False,
|
| 120 |
llm=llm,
|
| 121 |
)
|
| 122 |
|
| 123 |
+
# === Task Definitions ===
|
| 124 |
router_task = Task(
|
| 125 |
+
description=(
|
| 126 |
+
"Analyse the keywords in the question {question}. "
|
| 127 |
+
"Based on the keywords decide whether it is eligible for a vectorstore search or a web search. "
|
| 128 |
+
"Return a single word 'vectorstore' if it is eligible for vectorstore search. "
|
| 129 |
+
"Return a single word 'websearch' if it is eligible for web search. "
|
| 130 |
+
"Do not provide any other preamble or explanation."
|
| 131 |
+
),
|
| 132 |
+
expected_output=(
|
| 133 |
+
"Give a binary choice 'websearch' or 'vectorstore' based on the question. "
|
| 134 |
+
"Do not provide any other preamble or explanation."
|
| 135 |
),
|
|
|
|
|
|
|
| 136 |
agent=Router_Agent,
|
| 137 |
tools=[router_tool],
|
| 138 |
)
|
| 139 |
|
| 140 |
retriever_task = Task(
|
| 141 |
+
description=(
|
| 142 |
+
"Based on the response from the router task extract information for the question {question} with the help of the respective tool. "
|
| 143 |
+
"Use the web_search_tool to retrieve information from the web in case the router task output is 'websearch'. "
|
| 144 |
+
"Use the rag_tool to retrieve information from the vectorstore in case the router task output is 'vectorstore'."
|
| 145 |
+
),
|
| 146 |
+
expected_output=(
|
| 147 |
+
"You should analyse the output of the 'router_task'. "
|
| 148 |
+
"If the response is 'websearch' then use the web_search_tool to retrieve information from the web. "
|
| 149 |
+
"If the response is 'vectorstore' then use the rag_tool to retrieve information from the vectorstore. "
|
| 150 |
+
"Return a clear and concise text as response."
|
| 151 |
),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
agent=Retriever_Agent,
|
| 153 |
context=[router_task],
|
| 154 |
)
|
| 155 |
|
| 156 |
grader_task = Task(
|
| 157 |
+
description=(
|
| 158 |
+
"Based on the response from the retriever task for the question {question} evaluate whether the retrieved content is relevant to the question."
|
| 159 |
+
),
|
| 160 |
+
expected_output=(
|
| 161 |
+
"Binary score 'yes' or 'no' score to indicate whether the document is relevant to the question. "
|
| 162 |
+
"You must answer 'yes' if the response from the 'retriever_task' is in alignment with the question asked. "
|
| 163 |
+
"You must answer 'no' if the response from the 'retriever_task' is not in alignment with the question asked. "
|
| 164 |
+
"Do not provide any preamble or explanations except for 'yes' or 'no'."
|
| 165 |
),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
agent=Grader_agent,
|
| 167 |
context=[retriever_task],
|
| 168 |
)
|
| 169 |
|
| 170 |
hallucination_task = Task(
|
| 171 |
+
description=(
|
| 172 |
+
"Based on the response from the grader task for the question {question} evaluate whether the answer is grounded in / supported by a set of facts."
|
| 173 |
+
),
|
| 174 |
+
expected_output=(
|
| 175 |
+
"Binary score 'yes' or 'no' score to indicate whether the answer is sync with the question asked. "
|
| 176 |
+
"Respond 'yes' if the answer is useful and contains fact about the question asked. "
|
| 177 |
+
"Respond 'no' if the answer is not useful and does not contains fact about the question asked. "
|
| 178 |
+
"Do not provide any preamble or explanations except for 'yes' or 'no'."
|
| 179 |
+
),
|
| 180 |
agent=hallucination_grader,
|
| 181 |
context=[grader_task],
|
| 182 |
)
|
| 183 |
|
| 184 |
answer_task = Task(
|
| 185 |
+
description=(
|
| 186 |
+
"Based on the response from the hallucination task for the question {question} evaluate whether the answer is useful to resolve the question. "
|
| 187 |
+
"If the answer is 'yes' return a clear and concise answer. "
|
| 188 |
+
"If the answer is 'no' then perform a 'websearch' and return the response."
|
| 189 |
+
),
|
| 190 |
+
expected_output=(
|
| 191 |
+
"Return a clear and concise response if the response from 'hallucination_task' is 'yes'. "
|
| 192 |
+
"Perform a web search using 'web_search_tool' and return a clear and concise response only if the response from 'hallucination_task' is 'no'. "
|
| 193 |
+
"Otherwise respond as 'Sorry! unable to find a valid response'."
|
| 194 |
+
),
|
| 195 |
context=[hallucination_task],
|
| 196 |
agent=answer_grader,
|
| 197 |
)
|
| 198 |
|
| 199 |
+
# === Crew Definition ===
|
| 200 |
rag_crew = Crew(
|
| 201 |
agents=[Router_Agent, Retriever_Agent, Grader_agent, hallucination_grader, answer_grader],
|
| 202 |
tasks=[router_task, retriever_task, grader_task, hallucination_task, answer_task],
|
|
|
|
| 204 |
)
|
| 205 |
|
| 206 |
def respond(
|
| 207 |
+
message: str,
|
| 208 |
+
history: List[Tuple[str, str]],
|
| 209 |
+
system_message: str,
|
| 210 |
+
max_tokens: int,
|
| 211 |
+
temperature: float,
|
| 212 |
+
top_p: float,
|
| 213 |
):
|
| 214 |
+
"""Main response function for Gradio chat interface."""
|
| 215 |
messages = [{"role": "system", "content": system_message}]
|
| 216 |
|
| 217 |
for val in history:
|
|
|
|
| 237 |
response += token
|
| 238 |
yield response
|
| 239 |
|
| 240 |
+
# === Gradio Interface ===
|
| 241 |
demo = gr.ChatInterface(
|
| 242 |
respond,
|
| 243 |
additional_inputs=[
|
|
|
|
| 255 |
)
|
| 256 |
|
| 257 |
if __name__ == "__main__":
|
| 258 |
+
demo.launch()
|