-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04-tool_agent.py
216 lines (175 loc) · 5.18 KB
/
04-tool_agent.py
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "marimo",
# "python-dotenv==1.0.1",
# "langchain==0.3.17",
# "langchain-openai==0.3.4",
# "langchain-core==0.3.34",
# ]
# ///
import marimo
__generated_with = "0.11.0"
app = marimo.App(width="medium")
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
# Building a Tool-Calling Agent with LangChain
- Define simple math tools (addition, multiplication, exponentiation).
- Configure a language model and prompt template.
- Create and execute a tool-calling agent that performs a complex arithmetic operation.
"""
)
return
@app.cell
def _():
import marimo as mo
import os
from dotenv import load_dotenv, find_dotenv
from langchain import hub
from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain_openai import ChatOpenAI
from langchain_core.tools import tool
from langchain.prompts import (
ChatPromptTemplate,
SystemMessagePromptTemplate,
HumanMessagePromptTemplate,
MessagesPlaceholder
)
return (
AgentExecutor,
ChatOpenAI,
ChatPromptTemplate,
HumanMessagePromptTemplate,
MessagesPlaceholder,
SystemMessagePromptTemplate,
create_tool_calling_agent,
find_dotenv,
hub,
load_dotenv,
mo,
os,
tool,
)
@app.cell
def _(os):
# Delete or modify these lines if you are not behind the UTSA proxy.
os.environ["http_proxy"] = "http://xa-proxy.utsarr.net:80"
os.environ["https_proxy"] = "http://xa-proxy.utsarr.net:80"
return
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
## Load Environment Variables
Create a `.env` file in the `AgenticAISystems/` folder and add your key:
```OPENAI_API_KEY=<your-openai-key>```
"""
)
return
@app.cell
def _(find_dotenv, load_dotenv, os):
# Ensure you have a .env file with your OPENAI_API_KEY.
working_dir = os.getcwd()
status = load_dotenv(
find_dotenv(
filename=f'{working_dir}/AgenticAISystems/.env',
raise_error_if_not_found=True
)
)
return status, working_dir
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
## Define Tools
Define three simple math tools using the `@tool` decorator:
- **multiply**: Multiplies two integers.
- **add**: Adds two integers.
- **exponentiate**: Raises a base to a given exponent.
These tools are later made available for the agent to call.
"""
)
return
@app.cell
def _(tool):
@tool
def multiply(first_int: int, second_int: int) -> int:
"""Multiply two integers together."""
return first_int * second_int
@tool
def add(first_int: int, second_int: int) -> int:
"Add two integers."
return first_int + second_int
@tool
def exponentiate(base: int, exponent: int) -> int:
"Exponentiate the base to the exponent power."
return base**exponent
return add, exponentiate, multiply
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
## Configure the Language Model and Prompt Template
- Initialize a `ChatOpenAI` model (using GPT-3.5-turbo-0125).
- Create a chat prompt template with:
- A system message defining the assistant as "helpful".
- A human message that takes user input.
- A placeholder (`agent_scratchpad`) for the intermediate steps after each tool is called.
"""
)
return
@app.cell
def _(
ChatOpenAI,
ChatPromptTemplate,
HumanMessagePromptTemplate,
MessagesPlaceholder,
SystemMessagePromptTemplate,
):
llm = ChatOpenAI(model="gpt-3.5-turbo-0125")
chat_prompt = ChatPromptTemplate(
messages=[
SystemMessagePromptTemplate.from_template("You are a helpful assistant."),
HumanMessagePromptTemplate.from_template("{input}"),
MessagesPlaceholder(variable_name="agent_scratchpad"),
]
)
chat_prompt.pretty_print()
return chat_prompt, llm
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
## Build and Execute the Tool-Calling Agent
- Compile our defined math tools into a list.
- Create a tool-calling agent by combining the language model, tools, and prompt.
- An `AgentExecutor` is used to run the agent with verbose output.
"""
)
return
@app.cell
def _(
AgentExecutor,
add,
chat_prompt,
create_tool_calling_agent,
exponentiate,
llm,
multiply,
):
_tools = [multiply, add, exponentiate]
# Construct the tool calling agent
_agent = create_tool_calling_agent(llm, _tools, chat_prompt)
# Create an agent executor by passing in the agent and tools
_agent_executor = AgentExecutor(agent=_agent, tools=_tools, verbose=True)
_results = _agent_executor.invoke(
{
"input": "Take 3 to the fifth power and multiply that by the sum of twelve and three, then square the whole result"
}
)
_results
return
if __name__ == "__main__":
app.run()