-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
88 lines (74 loc) · 2.38 KB
/
Copy pathmain.py
File metadata and controls
88 lines (74 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# Agent
"""
A chain that knows how to use tools
WIll take that list of tools and convert them into JSON functions description
Still has input variable, memory, prompts, etc all the normal things a chain has
"""
# AgentExecutor
"""
Takes an agent and runs it until the response is not a function call
Essentially a fancy while loop
"""
# Important
"""
The docs show several different ways of creating an Agent + AgentExecutor
They are all doing the same thing behind the scenes!
"""
from langchain.chat_models import ChatOpenAI
from langchain.prompts import (
ChatPromptTemplate,
HumanMessagePromptTemplate,
MessagesPlaceholder
)
from langchain.schema import SystemMessage
from langchain.agents import OpenAIFunctionsAgent, AgentExecutor
from langchain.memory import ConversationBufferMemory
from dotenv import load_dotenv
from tools.sql import run_query_tool, list_tables, describe_tables_tool
from tools.report import write_report_tool
from handlers.chat_model_start_handler import ChatModelStartHandler
load_dotenv()
handler = ChatModelStartHandler()
chat = ChatOpenAI(
callbacks=[handler]
)
tables = list_tables()
prompt = ChatPromptTemplate(
messages=[
SystemMessage(content=(
"You are an AI that has access to a SQLite database.\n"
f"The database has tables of: {tables}\n"
"Do not make any assumptions about what tables exist "
"or what columns exist. Instead, use the 'describe_tables' function"
)),
MessagesPlaceholder(variable_name="chat_history"),
HumanMessagePromptTemplate.from_template("{input}"),
MessagesPlaceholder(variable_name="agent_scratchpad")
]
)
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
tools = [
run_query_tool,
describe_tables_tool,
write_report_tool
]
agent = OpenAIFunctionsAgent(
llm=chat,
prompt=prompt,
tools=tools
)
agent_executor = AgentExecutor(
agent=agent,
verbose=True,
tools=tools,
memory=memory
)
agent_executor(
"How many orders are there? Write the result to an html report."
)
# agent_executor(
# "Repeat the exact same process for users."
# )
# agent_executor("Summarize the top 5 most popular products. Write the results to a report file.")
# agent_executor("How many users have provided a shipping address?")
# agent_executor("how many users are there?")