├── .gitignore ├── LICENSE ├── README.md ├── agents ├── executor.py └── planner.py ├── graph.py ├── main.py └── requirements.txt /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .nox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | *.py,cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | cover/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | .pybuilder/ 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | # For a library or package, you might want to ignore these files since the code is 87 | # intended to run in multiple environments; otherwise, check them in: 88 | # .python-version 89 | 90 | # pipenv 91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 94 | # install all needed dependencies. 95 | #Pipfile.lock 96 | 97 | # poetry 98 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 99 | # This is especially recommended for binary packages to ensure reproducibility, and is more 100 | # commonly ignored for libraries. 101 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 102 | #poetry.lock 103 | 104 | # pdm 105 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 106 | #pdm.lock 107 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 108 | # in version control. 109 | # https://pdm.fming.dev/#use-with-ide 110 | .pdm.toml 111 | 112 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 113 | __pypackages__/ 114 | 115 | # Celery stuff 116 | celerybeat-schedule 117 | celerybeat.pid 118 | 119 | # SageMath parsed files 120 | *.sage.py 121 | 122 | # Environments 123 | .env 124 | .venv 125 | env/ 126 | venv/ 127 | ENV/ 128 | env.bak/ 129 | venv.bak/ 130 | 131 | # Spyder project settings 132 | .spyderproject 133 | .spyproject 134 | 135 | # Rope project settings 136 | .ropeproject 137 | 138 | # mkdocs documentation 139 | /site 140 | 141 | # mypy 142 | .mypy_cache/ 143 | .dmypy.json 144 | dmypy.json 145 | 146 | # Pyre type checker 147 | .pyre/ 148 | 149 | # pytype static type analyzer 150 | .pytype/ 151 | 152 | # Cython debug symbols 153 | cython_debug/ 154 | 155 | # PyCharm 156 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 157 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 158 | # and can be added to the global gitignore or merged into this file. For a more nuclear 159 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 160 | #.idea/ 161 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 alidhl 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Multi-Agent Chatbot 2 | 3 | This project presents a multi-agent chatbot system integrated with a search engine, designed to handle complex user queries with a systematic approach. It leverages the capabilities of LangChain and LangGraph libraries, and Tavily for the search engine functionality. The chatbot uses the "plan-and-execute" coordination strategy among its agents to ensure effective and efficient responses. 4 | 5 | ## Demo 6 | 7 | Check out the live demo of the multi-agent chatbot system: [Multi-Agent Chatbot Demo](https://multi-agent-chatbot.streamlit.app/) 8 | 9 | ## Features 10 | 11 | - **Plan-and-Execute Agent Coordination:** The system incorporates three specialized agents: 12 | - **Planner:** Begins by formulating a structured plan based on the user's query. 13 | - **Executor:** Follows the defined plan, executing the necessary steps one by one. 14 | - **Replanner:** Monitors the execution process, evaluates its effectiveness, and decides whether the current plan suffices or needs adjustments to better address the user's query. 15 | 16 | - **Integration with LangChain and LangGraph:** Utilizes the advanced capabilities of these libraries to understand and navigate complex language constructs. 17 | 18 | - **Search Engine Integration:** Powered by Tavily, the system includes a robust search engine to fetch relevant information and support the chatbot's responses. 19 | 20 | - **Streamlit Web Interface:** The project uses Streamlit for designing and deploying an intuitive web interface, enhancing user interaction and experience. 21 | 22 | ## How to Use Locally 23 | 24 | 1. **Clone Repo** 25 | 26 | 2. **Install requirements.txt** 27 | ```bash 28 | pip install -r requirements.txt 29 | ``` 30 | 31 | 3. **Run main.py via Streamlit** 32 | ```bash 33 | streamlit run main.py 34 | ``` 35 | 36 | ## Built With 37 | 38 | - [LangChain](https://langchain.com) 39 | - [LangGraph](https://langgraph.com) 40 | - [Tavily](https://tavily.com) 41 | - [OpenAI](https://openai.com) 42 | - [Streamlit](https://streamlit.io) 43 | 44 | ## License 45 | 46 | This project is licensed under the MIT License. 47 | -------------------------------------------------------------------------------- /agents/executor.py: -------------------------------------------------------------------------------- 1 | from langchain import hub 2 | from langchain.agents import create_openai_functions_agent 3 | from langchain_openai import ChatOpenAI 4 | from langchain_community.tools.tavily_search import TavilySearchResults 5 | from langgraph.prebuilt import create_agent_executor 6 | 7 | def get_executor(): 8 | tools = [TavilySearchResults(max_results=3)] 9 | # Basic prompt for the executer 10 | prompt = hub.pull("hwchase17/openai-functions-agent") 11 | print(prompt) 12 | llm = ChatOpenAI(model = "gpt-3.5-turbo") 13 | agent = create_openai_functions_agent(llm, tools, prompt) 14 | agent_executor = create_agent_executor(agent, tools=tools) 15 | return agent_executor 16 | # Test the agent executor 17 | if __name__ == "__main__": 18 | executor = get_executor() 19 | query = "When was Saudi Arabia The Line project announced?" 20 | print(query + "\n\n") 21 | print(executor.invoke({"input" : query , "chat_history" : []})) 22 | -------------------------------------------------------------------------------- /agents/planner.py: -------------------------------------------------------------------------------- 1 | from langchain_core.pydantic_v1 import BaseModel, Field 2 | from langchain_core.prompts import ChatPromptTemplate 3 | from langchain.chains.openai_functions import create_structured_output_runnable 4 | from langchain.chains.openai_functions import create_openai_fn_runnable 5 | from langchain_openai import ChatOpenAI 6 | 7 | class Planner(BaseModel): 8 | """Plan the execution of the agent""" 9 | 10 | steps: list[str] = Field( 11 | description="The steps to execute the agent, should be in sorted order" 12 | ) 13 | 14 | def get_planner(): 15 | planner_prompt = ChatPromptTemplate.from_template( 16 | """Given the objective, devise a simple and concise step-by-step plan that involves using a search engine to find the answer. \ 17 | This plan should consist of individual search queries that, if executed correctly, will yield the correct answer. \ 18 | Avoid unnecessary steps and aim to make the plan as short as possible. The result of the final step should be the final answer. \ 19 | 20 | {objective}""" 21 | ) 22 | return create_structured_output_runnable( 23 | output_schema= Planner, 24 | llm = ChatOpenAI(model = "gpt-4-0125-preview"), 25 | prompt = planner_prompt 26 | ) 27 | 28 | # Replanner for updating the plan or returning a response 29 | 30 | class Response(BaseModel): 31 | """Response to user""" 32 | 33 | response: str 34 | 35 | 36 | # Inside the state will be four fields: input , plan , past_steps , response 37 | 38 | def get_replanner(): 39 | replanner_prompt = ChatPromptTemplate.from_template( 40 | """Your task is to revise the current plan based on the executed steps. Remove any steps that have been completed and ensure the remaining steps will lead to the complete answer for the objective. Remember, the objective should be fully answered, not just partially. If the answer is already found in the executed steps, return the answer to the objective. 41 | 42 | Objective: 43 | {input} 44 | 45 | Current Plan: 46 | {plan} 47 | 48 | Executed Steps: 49 | {past_steps} 50 | 51 | """ 52 | ) 53 | return create_openai_fn_runnable( 54 | functions= [Planner, Response], 55 | llm = ChatOpenAI(model = "gpt-4-0125-preview"), 56 | prompt = replanner_prompt 57 | ) 58 | 59 | 60 | # Testing 61 | if __name__ == "__main__": 62 | query = "How do I get a passport in saudi arabia" 63 | planner = get_planner() 64 | replanner = get_replanner() 65 | 66 | print(query + "\n\n") 67 | print(planner.invoke({"objective" : query}).steps) 68 | 69 | -------------------------------------------------------------------------------- /graph.py: -------------------------------------------------------------------------------- 1 | from typing import List, Tuple, Annotated, TypedDict 2 | import operator 3 | from agents.planner import get_planner, get_replanner 4 | from agents.executor import get_executor 5 | from agents.planner import Response 6 | from langgraph.graph import StateGraph, END 7 | 8 | import os 9 | from uuid import uuid4 10 | from langsmith import Client 11 | 12 | unique_id = uuid4().hex[0:8] 13 | os.environ["LANGCHAIN_TRACING_V2"] = "true" 14 | os.environ["LANGCHAIN_PROJECT"] = "multi-agent-search" 15 | os.environ["LANGCHAIN_ENDPOINT"] = "https://api.smith.langchain.com" 16 | client = Client() 17 | # Define the State 18 | class State(TypedDict): 19 | input: str 20 | plan: List[str] 21 | past_steps: Annotated[List[Tuple], operator.add] 22 | response: str 23 | 24 | 25 | # Create the graph 26 | 27 | def get_graph(): 28 | # Create agents 29 | executor = get_executor() 30 | planner = get_planner() 31 | replanner = get_replanner() 32 | 33 | def execute(state: State): 34 | task = state['plan'][0] 35 | output = executor.invoke({'input': task, "chat_history" : []}) 36 | return {"past_steps" : (task, output['agent_outcome'].return_values['output'])} 37 | 38 | def plan(state: State): 39 | plan = planner.invoke({'objective': state['input']}) 40 | return {"plan" : plan.steps} 41 | 42 | def replan(state: State): 43 | output = replanner.invoke(state) 44 | # If the output is a response (the plan is complete), then return the response else update the plan 45 | if isinstance(output, Response): 46 | return {"response" : output.response} 47 | else: 48 | return {"plan" : output.steps} 49 | 50 | def should_end(state: State): 51 | if (state['response']): 52 | return True 53 | else: 54 | return False 55 | 56 | graph = StateGraph(State) 57 | graph.add_node("planner", plan) 58 | graph.add_node("executor", execute) 59 | graph.add_node("replanner", replan) 60 | 61 | graph.set_entry_point("planner") 62 | graph.add_edge("planner", "executor") 63 | graph.add_edge("executor", "replanner") 64 | graph.add_conditional_edges( 65 | 'replanner', 66 | should_end, 67 | { 68 | True: END, 69 | False: "executor" 70 | }) 71 | return graph.compile() 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | from graph import get_graph 2 | import streamlit as st 3 | import os 4 | 5 | with st.sidebar: 6 | st.sidebar.title("API Keys") 7 | openai_api_key = st.sidebar.text_input("OpenAI API Key", key= "chatbot_api_key" , type="password") 8 | #taveliy_api_key = st.sidebar.text_input("Taveliy API Key", type="password") 9 | 10 | 11 | 12 | st.title("💬Multi-Agent Search Engine Chatbot") 13 | if "messages" not in st.session_state: 14 | st.session_state["messages"] = [{"role": "assistant", "content": "How can I help you?"}] 15 | 16 | for msg in st.session_state.messages: 17 | st.chat_message(msg["role"]).write(msg["content"]) 18 | 19 | if prompt := st.chat_input(): 20 | if not openai_api_key: 21 | st.info("Please add your OpenAI API to continue.") 22 | st.stop() 23 | os.environ['OPENAI_API_KEY'] = openai_api_key 24 | # Will be used in the future for now will use my own API key 25 | #os.environ['TAVELIY_API_KEY'] = taveliy_api_key 26 | graph = get_graph() 27 | st.session_state.messages.append({"role": "user", "content": prompt}) 28 | st.chat_message("user").write(prompt) 29 | response = graph.invoke({"input": st.session_state.messages[-1]}) 30 | msg = response["response"] 31 | st.session_state.messages.append({"role": "assistant", "content": msg}) 32 | st.chat_message("assistant").write(msg) 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alidhl/multi-agent-chatbot/43c80bcc2ccace0df16caf5be8c27b774db7a2eb/requirements.txt --------------------------------------------------------------------------------