An advanced agent with 7 tools including percentage and simple interest.
1# study_buddy_pro.py - 7 Tools
2from openai import OpenAI
3import json
4client = OpenAI()
5
6# --- 7 Tool Functions ---
7def add(a, b): return a + b
8def subtract(a, b): return a - b
9def multiply(a, b): return a * b
10def divide(a, b): return a / b if b != 0 else "Cannot divide by zero!"
11def percentage(part, total): return (part / total) * 100 if total != 0 else "Total cannot be zero!"
12def simple_interest(principal, rate, time): return (principal * rate * time) / 100
13def lookup(query):
14 text = open("study_buddy_notes.txt").read()
15 for line in text.splitlines():
16 if query.lower() in line.lower():
17 return line
18 return "Sorry, I don't know that."
19
20# --- Tool descriptions for the LLM ({...} = parameters shortened) ---
21tools = [
22 {"type": "function", "function": {"name": "add", "description": "Add two numbers.", "parameters": {...}}},
23 {"type": "function", "function": {"name": "subtract", "description": "Subtract b from a.", "parameters": {...}}},
24 {"type": "function", "function": {"name": "multiply", "description": "Multiply two numbers.", "parameters": {...}}},
25 {"type": "function", "function": {"name": "divide", "description": "Divide a by b.", "parameters": {...}}},
26 {"type": "function", "function": {"name": "percentage", "description": "Calculate percentage as (part/total)*100.", "parameters": {...}}},
27 {"type": "function", "function": {
28 "name": "simple_interest",
29 "description": "Calculate simple interest using (principal * rate * time) / 100.",
30 "parameters": {"type": "object", "properties": {
31 "principal": {"type": "number"},
32 "rate": {"type": "number"},
33 "time": {"type": "number"}
34 }, "required": ["principal", "rate", "time"]}
35 }},
36 {"type": "function", "function": {"name": "lookup", "description": "Search for a concept or term in the notes file.", "parameters": {...}}},
37]
38
39system_prompt = """You are StudyBuddy Pro - a smart and friendly AI tutor.
40You can solve math problems (add, subtract, multiply, divide, percentages, simple interest)
41and explain topics using the lookup tool.
42Always choose the correct function based on the user's request."""
43user_query = "Find the simple interest on 1000 at 5% for 2 years"
44
45messages = [
46 {"role": "system", "content": system_prompt},
47 {"role": "user", "content": user_query}
48]
49
50response = client.chat.completions.create(
51 model="gpt-4o-mini", messages=messages, tools=tools
52)
53assistant_message = response.choices[0].message
54tool_call = assistant_message.tool_calls[0]
55tool_name = tool_call.function.name
56args = json.loads(tool_call.function.arguments)
57
58# --- Route to the correct function (a lookup table instead of 7 if/elifs) ---
59available_functions = {
60 "add": add, "subtract": subtract, "multiply": multiply,
61 "divide": divide, "percentage": percentage,
62 "simple_interest": simple_interest, "lookup": lookup,
63}
64function_to_call = available_functions[tool_name]
65result = function_to_call(**args)
66print("Tool result:", result)
67
68# --- Send the result back for a final answer ---
69messages.append(assistant_message)
70messages.append({
71 "role": "tool",
72 "tool_call_id": tool_call.id,
73 "content": str(result)
74})
75
76response_final = client.chat.completions.create(
77 model="gpt-4o-mini", messages=messages
78)
79final_answer = response_final.choices[0].message.content
80print("StudyBuddy Pro:", final_answer)No output yet...