-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlangchain_example.py
More file actions
103 lines (75 loc) · 3.16 KB
/
langchain_example.py
File metadata and controls
103 lines (75 loc) · 3.16 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"""
Example: LangChain agent with agentopt.
Prerequisites:
1. pip install langchain-classic langchain-openai agentopt-py
2. Set OPENAI_API_KEY environment variable
"""
from dotenv import load_dotenv
load_dotenv()
from langchain_classic.agents import AgentExecutor, create_tool_calling_agent
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI
from agentopt import ModelSelector
@tool
def search(query: str) -> str:
"""Search for information about a topic."""
return f"Search results for: {query}"
TOOLS = [search]
PROMPT = ChatPromptTemplate.from_messages(
[
(
"system",
"You are a helpful assistant. Use tools when needed to answer questions concisely.",
),
("human", "{input}"),
("placeholder", "{agent_scratchpad}"),
]
)
# ---------------------------------------------------------------------------
# Step 1: Define your agent class.
# __init__(models) receives a dict like {"agent": "gpt-4o-mini"}.
# run(input_data) runs the agent on a single datapoint and returns the output.
# ---------------------------------------------------------------------------
class MyAgent:
"""LangChain tool-calling agent."""
def __init__(self, models):
llm = ChatOpenAI(model=models["agent"], disable_streaming=True)
agent = create_tool_calling_agent(llm, TOOLS, PROMPT)
self.executor = AgentExecutor(agent=agent, tools=TOOLS, verbose=False)
def run(self, input_data):
result = self.executor.invoke({"input": input_data})
return result["output"]
# ---------------------------------------------------------------------------
# Step 2: Evaluation dataset — (input_data, expected_output) pairs.
# ---------------------------------------------------------------------------
dataset = [
("What is the capital of France?", "Paris"),
("What is 2 + 2?", "4"),
("What color is the sky on a clear day?", "blue"),
("What is the largest planet in our solar system?", "Jupiter"),
("What is H2O commonly known as?", "water"),
]
# ---------------------------------------------------------------------------
# Step 3: Evaluation function — score agent output against expected answer.
# ---------------------------------------------------------------------------
def eval_fn(expected, actual):
return 1.0 if expected.lower() in str(actual).lower() else 0.0
# ---------------------------------------------------------------------------
# Step 4: Run model selection.
# This agent has a single LLM step ("agent"), so we search over 3 models.
# ---------------------------------------------------------------------------
if __name__ == "__main__":
selector = ModelSelector(
agent=MyAgent,
models={"agent": ["gpt-4o", "gpt-4o-mini", "gpt-4.1-nano"],},
eval_fn=eval_fn,
dataset=dataset,
method="brute_force", # or "auto" for smarter selection algorithms
)
results = selector.select_best(parallel=True)
results.print_summary()
results.plot_pareto()
best = results.get_best_combo()
if best:
print(f"\nBest combination: {best}")