|
| 1 | +""" |
| 2 | +2-Agent system where: |
| 3 | +- Assistant takes user's (complex) question, breaks it down into smaller pieces |
| 4 | + if needed |
| 5 | +- WebSearcher takes Assistant's question, uses the Search tool to search the web |
| 6 | + (using DuckDuckGo), and returns a coherent answer to the Assistant. |
| 7 | +
|
| 8 | +Once the Assistant thinks it has enough info to answer the user's question, it |
| 9 | +says DONE and presents the answer to the user. |
| 10 | +
|
| 11 | +Run like this: |
| 12 | +
|
| 13 | +python3 search-assistant.py |
| 14 | +""" |
| 15 | + |
| 16 | +from dotenv import load_dotenv |
| 17 | +from rich import print |
| 18 | +from rich.prompt import Prompt |
| 19 | +from fire import Fire |
| 20 | +import langroid as lr |
| 21 | +import langroid.language_models as lm |
| 22 | +from langroid.agent.tools.duckduckgo_search_tool import DuckduckgoSearchTool |
| 23 | + |
| 24 | +MODEL = "groq/llama3-70b-8192" |
| 25 | + |
| 26 | +def main() -> None: |
| 27 | + print( |
| 28 | + """ |
| 29 | + [blue]Welcome to the Web Search Assistant chatbot! |
| 30 | + I will try to answer your complex questions. |
| 31 | + |
| 32 | + Enter x or q to quit at any point. |
| 33 | + """ |
| 34 | + ) |
| 35 | + load_dotenv() |
| 36 | + |
| 37 | + llm_config = lm.OpenAIGPTConfig( |
| 38 | + chat_model=MODEL, # or lr.OpenAIChatModel.GPT4_TURBO |
| 39 | + chat_context_length=8192, |
| 40 | + temperature=0, |
| 41 | + max_output_tokens=200, |
| 42 | + timeout=45, |
| 43 | + ) |
| 44 | + |
| 45 | + assistant_config = lr.ChatAgentConfig( |
| 46 | + system_message=""" |
| 47 | + You are a resourceful assistant, able to think step by step to answer |
| 48 | + complex questions from the user. You must break down complex questions into |
| 49 | + simpler questions that can be answered by a web search. You must ask me |
| 50 | + (the user) each question ONE BY ONE, and I will do a web search and send you |
| 51 | + a brief answer. Once you have enough information to answer my original |
| 52 | + (complex) question, you MUST say DONE and present the answer to me. |
| 53 | + """, |
| 54 | + llm=llm_config, |
| 55 | + vecdb=None, |
| 56 | + ) |
| 57 | + assistant_agent = lr.ChatAgent(assistant_config) |
| 58 | + search_tool_handler_method = DuckduckgoSearchTool.default_value("request") |
| 59 | + |
| 60 | + search_agent_config = lr.ChatAgentConfig( |
| 61 | + llm=llm_config, |
| 62 | + vecdb=None, |
| 63 | + system_message=f""" |
| 64 | + You are a web-searcher. For any question you get, you must use the |
| 65 | + `{search_tool_handler_method}` tool/function-call to get up to 5 results. |
| 66 | + I WILL SEND YOU THE RESULTS; DO NOT MAKE UP THE RESULTS!! |
| 67 | + Once you receive the results, you must compose a CONCISE answer |
| 68 | + based on the search results and say DONE and show the answer to me, |
| 69 | + in this format: |
| 70 | + DONE [... your CONCISE answer here ...] |
| 71 | + IMPORTANT: YOU MUST WAIT FOR ME TO SEND YOU THE |
| 72 | + SEARCH RESULTS BEFORE saying you're DONE. |
| 73 | + """, |
| 74 | + ) |
| 75 | + search_agent = lr.ChatAgent(search_agent_config) |
| 76 | + search_agent.enable_message(DuckduckgoSearchTool) |
| 77 | + |
| 78 | + assistant_task = lr.Task( |
| 79 | + assistant_agent, |
| 80 | + name="Assistant", |
| 81 | + llm_delegate=True, |
| 82 | + single_round=False, |
| 83 | + interactive=False, |
| 84 | + ) |
| 85 | + search_task = lr.Task( |
| 86 | + search_agent, |
| 87 | + name="Searcher", |
| 88 | + llm_delegate=True, |
| 89 | + single_round=False, |
| 90 | + interactive=False, |
| 91 | + ) |
| 92 | + assistant_task.add_sub_task(search_task) |
| 93 | + question = Prompt.ask("What do you want to know?") |
| 94 | + assistant_task.run(question) |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + Fire(main) |
0 commit comments