|
4 | 4 | from crewai.flow.flow import Flow, listen, start, router |
5 | 5 | from crewai.llm import LLM |
6 | 6 | from tasks.hivemind.classify_question import ClassifyQuestion |
7 | | -from tasks.hivemind.query_data_sources import RAGPipelineTool |
8 | | -from crewai.process import Process |
| 7 | +from tasks.hivemind.query_data_sources import make_rag_tool |
9 | 8 | from pydantic import BaseModel |
10 | 9 | from crewai.tools import tool |
11 | 10 | from openai import OpenAI |
12 | 11 | from typing import Optional |
13 | 12 | from tasks.mongo_persistence import MongoPersistence |
| 13 | +from langchain_openai import ChatOpenAI |
| 14 | +from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder |
| 15 | +from langchain.agents import AgentExecutor, create_openai_functions_agent |
14 | 16 |
|
15 | 17 |
|
16 | 18 | class AgenticFlowState(BaseModel): |
@@ -154,50 +156,76 @@ def detect_question_type(self) -> str: |
154 | 156 |
|
155 | 157 | @router("rag") |
156 | 158 | def do_rag_query(self) -> str: |
157 | | - query_data_source_tool = RAGPipelineTool.setup_tools( |
158 | | - community_id=self.community_id, |
159 | | - enable_answer_skipping=self.enable_answer_skipping, |
160 | | - workflow_id=self.workflow_id, |
161 | | - ) |
| 159 | + # query_data_source_tool = RAGPipelineTool.setup_tools( |
| 160 | + # community_id=self.community_id, |
| 161 | + # enable_answer_skipping=self.enable_answer_skipping, |
| 162 | + # workflow_id=self.workflow_id, |
| 163 | + # ) |
| 164 | + |
| 165 | + # q_a_bot_agent = Agent( |
| 166 | + # role="Q&A Bot", |
| 167 | + # goal=( |
| 168 | + # "You decide when to rely on your internal knowledge and when to retrieve real-time data. " |
| 169 | + # "For queries that are not specific to community data, answer using your own LLM knowledge. " |
| 170 | + # "Your final response must not exceed 250 words." |
| 171 | + # ), |
| 172 | + # backstory=( |
| 173 | + # "You are an intelligent agent capable of giving concise answers to questions." |
| 174 | + # ), |
| 175 | + # allow_delegation=True, |
| 176 | + # llm=LLM(model="gpt-4o-mini-2024-07-18"), |
| 177 | + # ) |
| 178 | + # rag_task = Task( |
| 179 | + # description=( |
| 180 | + # "Answer the following query using a maximum of 250 words. " |
| 181 | + # "If the query is specific to community data, use the tool to retrieve updated information; " |
| 182 | + # f"otherwise, answer using your internal knowledge.\n\nQuery: {self.state.user_query}" |
| 183 | + # ), |
| 184 | + # expected_output="A clear, well-structured answer under 250 words that directly addresses the query using appropriate information sources", |
| 185 | + # agent=q_a_bot_agent, |
| 186 | + # tools=[ |
| 187 | + # query_data_source_tool(result_as_answer=True), |
| 188 | + # ], |
| 189 | + # ) |
| 190 | + |
| 191 | + # crew = Crew( |
| 192 | + # agents=[q_a_bot_agent], |
| 193 | + # tasks=[rag_task], |
| 194 | + # process=Process.hierarchical, |
| 195 | + # manager_llm=LLM(model="gpt-4o-mini-2024-07-18"), |
| 196 | + # verbose=True, |
| 197 | + # ) |
| 198 | + |
| 199 | + # crew_output = crew.kickoff() |
162 | 200 |
|
163 | | - q_a_bot_agent = Agent( |
164 | | - role="Q&A Bot", |
165 | | - goal=( |
166 | | - "You decide when to rely on your internal knowledge and when to retrieve real-time data. " |
167 | | - "For queries that are not specific to community data, answer using your own LLM knowledge. " |
168 | | - "Your final response must not exceed 250 words." |
169 | | - ), |
170 | | - backstory=( |
171 | | - "You are an intelligent agent capable of giving concise answers to questions." |
172 | | - ), |
173 | | - allow_delegation=True, |
174 | | - llm=LLM(model="gpt-4o-mini-2024-07-18"), |
175 | | - ) |
176 | | - rag_task = Task( |
177 | | - description=( |
178 | | - "Answer the following query using a maximum of 250 words. " |
179 | | - "If the query is specific to community data, use the tool to retrieve updated information; " |
180 | | - f"otherwise, answer using your internal knowledge.\n\nQuery: {self.state.user_query}" |
181 | | - ), |
182 | | - expected_output="A clear, well-structured answer under 250 words that directly addresses the query using appropriate information sources", |
183 | | - agent=q_a_bot_agent, |
184 | | - tools=[ |
185 | | - query_data_source_tool(result_as_answer=True), |
186 | | - ], |
187 | | - ) |
| 201 | + # Store the latest crew output and increment retry count |
| 202 | + # self.state.last_answer = crew_output |
| 203 | + |
| 204 | + llm = ChatOpenAI(model="gpt-4o-mini-2024-07-18") |
| 205 | + rag_tool = make_rag_tool(self.enable_answer_skipping, self.community_id, self.workflow_id) |
| 206 | + tools = [rag_tool] |
188 | 207 |
|
189 | | - crew = Crew( |
190 | | - agents=[q_a_bot_agent], |
191 | | - tasks=[rag_task], |
192 | | - process=Process.hierarchical, |
193 | | - manager_llm=LLM(model="gpt-4o-mini-2024-07-18"), |
194 | | - verbose=True, |
| 208 | + SYSTEM_INSTRUCTIONS = """\ |
| 209 | + You are a helpful assistant. |
| 210 | + """ |
| 211 | + |
| 212 | + prompt = ChatPromptTemplate.from_messages( |
| 213 | + [ |
| 214 | + ("system", SYSTEM_INSTRUCTIONS), |
| 215 | + MessagesPlaceholder("chat_history", optional=True), |
| 216 | + ("human", "{input}"), |
| 217 | + MessagesPlaceholder("agent_scratchpad"), |
| 218 | + ] |
195 | 219 | ) |
| 220 | + agent = create_openai_functions_agent(llm, tools, prompt) |
196 | 221 |
|
197 | | - crew_output = crew.kickoff() |
| 222 | + # Run the agent |
| 223 | + agent_executor = AgentExecutor( |
| 224 | + agent=agent, tools=tools, verbose=True, return_intermediate_steps=False |
| 225 | + ) |
198 | 226 |
|
199 | | - # Store the latest crew output and increment retry count |
200 | | - self.state.last_answer = crew_output |
| 227 | + result = agent_executor.invoke({"input": self.state.user_query}) |
| 228 | + self.state.last_answer = result["output"] |
201 | 229 | self.state.retry_count += 1 |
202 | 230 |
|
203 | 231 | return "stop" |
|
0 commit comments