jupiter0913 commited on
Commit ·
aea9671
1
Parent(s): 64ef2ba
feature(#98) create manage-gmail module for reading email...
Browse files
Brain/src/rising_plugin/gmail/manage_gmail.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain import OpenAI
|
| 2 |
+
from langchain.agents import initialize_agent, AgentType
|
| 3 |
+
from langchain.tools.gmail.utils import build_resource_service, get_gmail_credentials
|
| 4 |
+
from langchain.agents.agent_toolkits import GmailToolkit
|
| 5 |
+
from langchain.agents.agent import AgentExecutor
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def get_agent() -> AgentExecutor:
|
| 9 |
+
# Can review scopes here https://developers.google.com/gmail/api/auth/scopes
|
| 10 |
+
# For instance, readonly scope is 'https://www.googleapis.com/auth/gmail.readonly'
|
| 11 |
+
credentials = get_gmail_credentials(
|
| 12 |
+
token_file="token.json",
|
| 13 |
+
scopes=["https://mail.google.com/"],
|
| 14 |
+
client_secrets_file="credentials.json",
|
| 15 |
+
)
|
| 16 |
+
api_resource = build_resource_service(credentials=credentials)
|
| 17 |
+
toolkit = GmailToolkit(api_resource=api_resource)
|
| 18 |
+
|
| 19 |
+
llm = OpenAI(temperature=0)
|
| 20 |
+
agent = initialize_agent(
|
| 21 |
+
tools=toolkit.get_tools(),
|
| 22 |
+
llm=llm,
|
| 23 |
+
agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,
|
| 24 |
+
)
|
| 25 |
+
return agent
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def read_emails() -> str:
|
| 29 |
+
agent = get_agent()
|
| 30 |
+
result = agent.run(
|
| 31 |
+
"Could you search in my inbox for the latest email?"
|
| 32 |
+
)
|
| 33 |
+
return result
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def write_email(query: str) -> str:
|
| 37 |
+
agent = get_agent()
|
| 38 |
+
result = agent.run(query)
|
| 39 |
+
return result
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def send_email(query: str) -> str:
|
| 43 |
+
agent = get_agent()
|
| 44 |
+
result = agent.run(query)
|
| 45 |
+
return result
|