forked from humanlayer/humanlayer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
43 lines (33 loc) · 971 Bytes
/
agent.py
File metadata and controls
43 lines (33 loc) · 971 Bytes
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
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain.agents import AgentType, initialize_agent
from langchain.tools import tool
from humanlayer.core.approval import HumanLayer
load_dotenv()
hl = HumanLayer(
verbose=True,
run_id="flask-langchain-math",
)
# add can be called without approval
@tool
def add(x: int, y: int) -> int:
"""Add two numbers together."""
return x + y
# multiply requires approval
@tool
@hl.require_approval()
def multiply(x: int, y: int) -> int:
"""multiply two numbers"""
return x * y
def run_agent(prompt: str) -> str:
"""Run the agent with the given prompt and return the result"""
tools = [add.as_tool(), multiply.as_tool()]
llm = ChatOpenAI(model="gpt-4", temperature=0)
agent = initialize_agent(
tools,
llm,
agent=AgentType.OPENAI_FUNCTIONS,
verbose=True,
handle_parsing_errors=True,
)
return agent.run(prompt)