mirror of https://github.com/hwchase17/langchain
Add Gmail Agent Example (#14567)
Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>pull/14606/head
parent
76905aa043
commit
ce61a8ca98
@ -0,0 +1,4 @@
|
||||
client_secret*.json
|
||||
credentials.json
|
||||
token.json
|
||||
|
@ -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.
|
@ -0,0 +1,91 @@
|
||||
# OpenAI Functions Agent - Gmail
|
||||
|
||||
This template implements a simple agent using OpenAI function calling imports directly from [langchain-core](https://pypi.org/project/langchain-core/) and [`langchain-community`](https://pypi.org/project/langchain-community/).
|
||||
|
||||
This template creates an agent that uses OpenAI function calling to communicate its decisions on what actions to take.
|
||||
|
||||
This example creates an agent that can optionally look up information on the internet using Tavily's search engine.
|
||||
|
||||
## Environment Setup
|
||||
|
||||
The following environment variables need to be set:
|
||||
|
||||
Set the `OPENAI_API_KEY` environment variable to access the OpenAI models.
|
||||
|
||||
Create a [`credentials.json`](https://developers.google.com/gmail/api/quickstart/python#authorize_credentials_for_a_desktop_application) file containing your OAuth client ID from Gmail. To customize authentication, see the [Customize Auth](#customize-auth) section below.
|
||||
|
||||
_*Note:* The first time you run this app, it will force you to go through a user authentication flow._
|
||||
|
||||
## 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 openai-functions-agent-gmail
|
||||
```
|
||||
|
||||
If you want to add this to an existing project, you can just run:
|
||||
|
||||
```shell
|
||||
langchain app add openai-functions-agent-gmail
|
||||
```
|
||||
|
||||
And add the following code to your `server.py` file:
|
||||
```python
|
||||
from openai_functions_agent import agent_executor as openai_functions_agent_chain
|
||||
|
||||
add_routes(app, openai_functions_agent_chain, path="/openai-functions-agent-gmail")
|
||||
```
|
||||
|
||||
(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/openai-functions-agent-gmail/playground](http://127.0.0.1:8000/openai-functions-agent/playground)
|
||||
|
||||
We can access the template from code with:
|
||||
|
||||
```python
|
||||
from langserve.client import RemoteRunnable
|
||||
|
||||
runnable = RemoteRunnable("http://localhost:8000/openai-functions-agent-gmail")
|
||||
```
|
||||
|
||||
## Customize Auth
|
||||
|
||||
```
|
||||
from langchain.tools.gmail.utils import build_resource_service, get_gmail_credentials
|
||||
|
||||
# Can review scopes here https://developers.google.com/gmail/api/auth/scopes
|
||||
# For instance, readonly scope is 'https://www.googleapis.com/auth/gmail.readonly'
|
||||
credentials = get_gmail_credentials(
|
||||
token_file="token.json",
|
||||
scopes=["https://mail.google.com/"],
|
||||
client_secrets_file="credentials.json",
|
||||
)
|
||||
api_resource = build_resource_service(credentials=credentials)
|
||||
toolkit = GmailToolkit(api_resource=api_resource)
|
||||
```
|
@ -0,0 +1,9 @@
|
||||
from openai_functions_agent.agent import agent_executor
|
||||
|
||||
if __name__ == "__main__":
|
||||
question = (
|
||||
"Write a draft response to LangChain's last email. "
|
||||
"First do background research on the sender and topics to make sure you"
|
||||
" understand the context, then write the draft."
|
||||
)
|
||||
print(agent_executor.invoke({"input": question, "chat_history": []}))
|
@ -0,0 +1,3 @@
|
||||
from openai_functions_agent.agent import agent_executor
|
||||
|
||||
__all__ = ["agent_executor"]
|
@ -0,0 +1,94 @@
|
||||
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.tools.render import format_tool_to_openai_function
|
||||
from langchain_community.chat_models import ChatOpenAI
|
||||
from langchain_community.tools.gmail import (
|
||||
GmailCreateDraft,
|
||||
GmailGetMessage,
|
||||
GmailGetThread,
|
||||
GmailSearch,
|
||||
GmailSendMessage,
|
||||
)
|
||||
from langchain_community.tools.gmail.utils import build_resource_service
|
||||
from langchain_community.utilities.tavily_search import TavilySearchAPIWrapper
|
||||
from langchain_core.messages import AIMessage, HumanMessage
|
||||
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
||||
from langchain_core.pydantic_v1 import BaseModel, Field
|
||||
from langchain_core.tools import tool
|
||||
|
||||
|
||||
@tool
|
||||
def search_engine(query: str, max_results: int = 5) -> str:
|
||||
""""A search engine optimized for comprehensive, accurate, \
|
||||
and trusted results. Useful for when you need to answer questions \
|
||||
about current events or about recent information. \
|
||||
Input should be a search query. \
|
||||
If the user is asking about something that you don't know about, \
|
||||
you should probably use this tool to see if that can provide any information."""
|
||||
return TavilySearchAPIWrapper().results(query, max_results=max_results)
|
||||
|
||||
|
||||
# Create the tools
|
||||
tools = [
|
||||
GmailCreateDraft(),
|
||||
GmailGetMessage(),
|
||||
GmailGetThread(),
|
||||
GmailSearch(),
|
||||
GmailSendMessage(),
|
||||
search_engine,
|
||||
]
|
||||
current_user = (
|
||||
build_resource_service().users().getProfile(userId="me").execute()["emailAddress"]
|
||||
)
|
||||
assistant_system_message = """You are a helpful assistant aiding a user with their \
|
||||
emails. Use tools (only if necessary) to best answer \
|
||||
the users questions.\n\nCurrent user: {user}"""
|
||||
prompt = ChatPromptTemplate.from_messages(
|
||||
[
|
||||
("system", assistant_system_message),
|
||||
MessagesPlaceholder(variable_name="chat_history"),
|
||||
("user", "{input}"),
|
||||
MessagesPlaceholder(variable_name="agent_scratchpad"),
|
||||
]
|
||||
).partial(user=current_user)
|
||||
|
||||
|
||||
llm = ChatOpenAI(model="gpt-4-1106-preview", temperature=0)
|
||||
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
|
||||
)
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,38 @@
|
||||
[tool.poetry]
|
||||
name = "openai-functions-agent-gmail"
|
||||
version = "0.1.0"
|
||||
description = "Agent using OpenAI function calling to execute functions, including search"
|
||||
authors = [
|
||||
"Lance Martin <lance@langchain.dev>",
|
||||
]
|
||||
readme = "README.md"
|
||||
|
||||
[tool.poetry.dependencies]
|
||||
python = ">=3.8.1,<4.0"
|
||||
langchain = ">=0.0.349,<0.1.0"
|
||||
openai = "<2"
|
||||
langchain-core = ">=0.0.13,<0.1.0"
|
||||
langchain-community = ">=0.0.1,<0.1.0"
|
||||
google-api-python-client = "^2.110.0"
|
||||
google-auth-oauthlib = "^1.1.0"
|
||||
google-auth-httplib2 = "^0.1.1"
|
||||
bs4 = "^0.0.1"
|
||||
|
||||
[tool.poetry.group.dev.dependencies]
|
||||
langchain-cli = ">=0.0.15"
|
||||
|
||||
[tool.langserve]
|
||||
export_module = "openai_functions_agent"
|
||||
export_attr = "agent_executor"
|
||||
|
||||
[tool.templates-hub]
|
||||
use-case = "research"
|
||||
author = "LangChain"
|
||||
integrations = ["OpenAI", "Tavily"]
|
||||
tags = ["search", "agents", "function-calling"]
|
||||
|
||||
[build-system]
|
||||
requires = [
|
||||
"poetry-core",
|
||||
]
|
||||
build-backend = "poetry.core.masonry.api"
|
Loading…
Reference in New Issue