The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
add retrieval agent (#13317)
Harrison Chase5f6043922123
diff --git a/templates/retrieval-agent/.gitignore b/templates/retrieval-agent/.gitignorenew file mode 100644index 0000000000..bee8a64b79--- /dev/null+++ b/templates/retrieval-agent/.gitignore@@ -0,0 +1 @@+__pycache__diff --git a/templates/retrieval-agent/LICENSE b/templates/retrieval-agent/LICENSEnew file mode 100644index 0000000000..426b650903--- /dev/null+++ b/templates/retrieval-agent/LICENSE@@ -0,0 +1,21 @@+MIT License++Copyright (c) 2023 LangChain, Inc.++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all+copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE+SOFTWARE.diff --git a/templates/retrieval-agent/README.md b/templates/retrieval-agent/README.mdnew file mode 100644index 0000000000..1e4fae3b9e--- /dev/null+++ b/templates/retrieval-agent/README.md@@ -0,0 +1,74 @@+# retrieval-agent++This package uses Azure OpenAI to do retrieval using an agent architecture.+By default, this does retrieval over Arxiv.++## Environment Setup++Since we are using Azure OpenAI, we will need to set the following environment variables:++```shell+export AZURE_OPENAI_API_BASE=...+export AZURE_OPENAI_API_VERSION=...+export AZURE_OPENAI_API_KEY=...+export AZURE_OPENAI_DEPLOYMENT_NAME=...+```++## Usage++To use this package, you should first have the LangChain CLI installed:++```shell+pip install -U langchain-cli+```++To create a new LangChain project and install this as the only package, you can do:++```shell+langchain app new my-app --package retrieval-agent+```++If you want to add this to an existing project, you can just run:++```shell+langchain app add retrieval-agent+```++And add the following code to your `server.py` file:+```python+from retrieval_agent import chain as retrieval_agent_chain++add_routes(app, retrieval_agent_chain, path="/retrieval-agent")+```++(Optional) Let's now configure LangSmith.+LangSmith will help us trace, monitor and debug LangChain applications.+LangSmith is currently in private beta, you can sign up [here](https://smith.langchain.com/).+If you don't have access, you can skip this section+++```shell+export LANGCHAIN_TRACING_V2=true+export LANGCHAIN_API_KEY=<your-api-key>+export LANGCHAIN_PROJECT=<your-project> # if not specified, defaults to "default"+```++If you are inside this directory, then you can spin up a LangServe instance directly by:++```shell+langchain serve+```++This will start the FastAPI app with a server is running locally at+[http://localhost:8000](http://localhost:8000)++We can see all templates at [http://127.0.0.1:8000/docs](http://127.0.0.1:8000/docs)+We can access the playground at [http://127.0.0.1:8000/retrieval-agent/playground](http://127.0.0.1:8000/retrieval-agent/playground)++We can access the template from code with:++```python+from langserve.client import RemoteRunnable++runnable = RemoteRunnable("http://localhost:8000/retrieval-agent")+```\ No newline at end of filediff --git a/templates/retrieval-agent/pyproject.toml b/templates/retrieval-agent/pyproject.tomlnew file mode 100644index 0000000000..263a27a060--- /dev/null+++ b/templates/retrieval-agent/pyproject.toml@@ -0,0 +1,25 @@+[tool.poetry]+name = "retrieval-agent"+version = "0.0.1"+description = ""+authors = []+readme = "README.md"++[tool.poetry.dependencies]+python = ">=3.8.1,<4.0"+langchain = ">=0.0.313, <0.1"+openai = "^0.28.1"+arxiv = "^2.0.0"++[tool.poetry.group.dev.dependencies]+langchain-cli = ">=0.0.4"+fastapi = "^0.104.0"+sse-starlette = "^1.6.5"++[tool.langserve]+export_module = "retrieval_agent"+export_attr = "agent_executor"++[build-system]+requires = ["poetry-core"]+build-backend = "poetry.core.masonry.api"diff --git a/templates/retrieval-agent/retrieval_agent/__init__.py b/templates/retrieval-agent/retrieval_agent/__init__.pynew file mode 100644index 0000000000..543e3fd855--- /dev/null+++ b/templates/retrieval-agent/retrieval_agent/__init__.py@@ -0,0 +1,3 @@+from retrieval_agent.chain import agent_executor++__all__ = ["agent_executor"]diff --git a/templates/retrieval-agent/retrieval_agent/chain.py b/templates/retrieval-agent/retrieval_agent/chain.pynew file mode 100644index 0000000000..fb3ab979c8--- /dev/null+++ b/templates/retrieval-agent/retrieval_agent/chain.py@@ -0,0 +1,75 @@+import os+from typing import List, Tuple++from langchain.agents import AgentExecutor+from langchain.agents.format_scratchpad import format_to_openai_function_messages+from langchain.agents.output_parsers import OpenAIFunctionsAgentOutputParser+from langchain.chat_models import AzureChatOpenAI+from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder+from langchain.pydantic_v1 import BaseModel, Field+from langchain.schema.messages import AIMessage, HumanMessage+from langchain.tools import ArxivQueryRun+from langchain.tools.render import format_tool_to_openai_function+from langchain.utilities import ArxivAPIWrapper+++class ArxivInput(BaseModel):+ query: str = Field(description="search query to look up")+++# Create the tool+arxiv_tool = ArxivQueryRun(api_wrapper=ArxivAPIWrapper(), args_schema=ArxivInput)+tools = [arxiv_tool]+llm = AzureChatOpenAI(+ temperature=0,+ deployment_name=os.environ["AZURE_OPENAI_DEPLOYMENT_NAME"],+ openai_api_base=os.environ["AZURE_OPENAI_API_BASE"],+ openai_api_version=os.environ["AZURE_OPENAI_API_VERSION"],+ openai_api_key=os.environ["AZURE_OPENAI_API_KEY"],+)+assistant_system_message = """You are a helpful research assistant. \+Lookup relevant information as needed."""+prompt = ChatPromptTemplate.from_messages(+ [+ ("system", assistant_system_message),+ MessagesPlaceholder(variable_name="chat_history"),+ ("user", "{input}"),+ MessagesPlaceholder(variable_name="agent_scratchpad"),+ ]+)++llm_with_tools = llm.bind(functions=[format_tool_to_openai_function(t) for t in tools])+++def _format_chat_history(chat_history: List[Tuple[str, str]]):+ buffer = []+ for human, ai in chat_history:+ buffer.append(HumanMessage(content=human))+ buffer.append(AIMessage(content=ai))+ return buffer+++agent = (+ {+ "input": lambda x: x["input"],+ "chat_history": lambda x: _format_chat_history(x["chat_history"]),+ "agent_scratchpad": lambda x: format_to_openai_function_messages(+ x["intermediate_steps"]+ ),+ }+ | prompt+ | llm_with_tools+ | OpenAIFunctionsAgentOutputParser()+)+++class AgentInput(BaseModel):+ input: str+ chat_history: List[Tuple[str, str]] = Field(+ ..., extra={"widget": {"type": "chat", "input": "input", "output": "output"}}+ )+++agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True).with_types(+ input_type=AgentInput+)diff --git a/templates/retrieval-agent/tests/__init__.py b/templates/retrieval-agent/tests/__init__.pynew file mode 100644index 0000000000..e69de29bb2