-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
50 lines (40 loc) · 1.63 KB
/
main.py
File metadata and controls
50 lines (40 loc) · 1.63 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
from dotenv import load_dotenv
from langchain.agents import AgentExecutor, OpenAIFunctionsAgent
from langchain.chat_models import ChatOpenAI
from langchain.memory import ConversationBufferMemory
from langchain.prompts import (
ChatPromptTemplate,
HumanMessagePromptTemplate,
MessagesPlaceholder,
)
from langchain.schema import SystemMessage
from handlers.chat_model_start_handler import ChatModelStartHandler
from tools.report import write_report_tool
from tools.sql import describe_tables_tool, list_tables, run_query_tool
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. "
f"The database has tables of: {tables}"
"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=False, tools=tools, memory=memory)
agent_executor("How many orders are there? Write an html report.")
agent_executor("Repeat the same process for users")