-
Notifications
You must be signed in to change notification settings - Fork 2
/
06 - basic agent.py
53 lines (32 loc) · 1.34 KB
/
06 - basic 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
import os
import llama_index.core
from llama_index.core.agent import ReActAgent
from llama_index.llms.openai import OpenAI
from llama_index.core.tools import FunctionTool
from dotenv import load_dotenv
load_dotenv()
PHOENIX_API_KEY = os.getenv("PHOENIX_API_KEY")
os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = f"api_key={PHOENIX_API_KEY}"
llama_index.core.set_global_handler(
"arize_phoenix", endpoint="https://llamatrace.com/v1/traces"
)
llm = OpenAI(model="gpt-4o", temperature=0)
def multiply(a: float, b: float) -> float:
"""Multiply two numbers and returns the product"""
return a * b
multiply_tool = FunctionTool.from_defaults(fn=multiply)
def add(a: float, b: float) -> float:
"""Add two numbers and returns the sum"""
return a + b
add_tool = FunctionTool.from_defaults(fn=add)
def subtract(a: float, b: float) -> float:
"""Subtract two numbers and returns the difference"""
return a - b
sub_tool = FunctionTool.from_defaults(fn=subtract)
def divide(a: float, b: float) -> float:
"""Divide two numbers and returns the quotient"""
return a / b
divide_tool = FunctionTool.from_defaults(fn=divide)
agent = ReActAgent.from_tools([multiply_tool, add_tool, sub_tool, divide_tool], llm=llm, verbose=True)
response = agent.chat("What is 20+(2*4) / (5 - 1)? Use a tool to calculate every step.")
print(response)