An agent with calculator + knowledge lookup tools.
1# study_buddy.py - Multi-Tool Agent
2from openai import OpenAI
3import json
4client = OpenAI()
5
6# --- Tools (real Python functions) ---
7def add(a, b):
8 return a + b
9
10def lookup(query):
11 text = open("study_buddy_notes.txt").read()
12 for line in text.splitlines():
13 if query.lower() in line.lower():
14 return line
15 return "Sorry, I don't know that."
16
17# --- Tool descriptions (the menu for the AI) ---
18tools = [
19 {"type": "function", "function": {
20 "name": "add",
21 "description": "Add two numbers together.",
22 "parameters": {"type": "object", "properties": {
23 "a": {"type": "number"}, "b": {"type": "number"}
24 }, "required": ["a", "b"]}
25 }},
26 {"type": "function", "function": {
27 "name": "lookup",
28 "description": "Search for a concept or term in the notes file.",
29 "parameters": {"type": "object", "properties": {
30 "query": {"type": "string"}
31 }, "required": ["query"]}
32 }}
33]
34
35system_prompt = """You are StudyBuddy - a friendly AI assistant that helps students.
36If the user asks a math question, use the calculator.
37If the user asks a concept question, use the lookup function."""
38user_query = "What is LangChain?"
39
40messages = [
41 {"role": "system", "content": system_prompt},
42 {"role": "user", "content": user_query}
43]
44
45# --- Let GPT decide which tool to use ---
46response = client.chat.completions.create(
47 model="gpt-4o-mini", messages=messages, tools=tools
48)
49assistant_message = response.choices[0].message
50
51# --- Execute the chosen tool ---
52tool_call = assistant_message.tool_calls[0]
53tool_name = tool_call.function.name
54args = json.loads(tool_call.function.arguments)
55
56if tool_name == "add":
57 result = add(**args)
58elif tool_name == "lookup":
59 result = lookup(**args)
60else:
61 result = "Tool not found."
62print("Tool result:", result)
63
64# --- Send result back for a final answer ---
65messages.append(assistant_message)
66messages.append({
67 "role": "tool",
68 "tool_call_id": tool_call.id,
69 "content": str(result)
70})
71
72response_final = client.chat.completions.create(
73 model="gpt-4o-mini", messages=messages
74)
75final_answer = response_final.choices[0].message.content
76print("StudyBuddy:", final_answer)No output yet...