MinAI - Về trang chủ
Lý thuyết
9/1350 phút
Đang tải...

Function Calling & Tool Use

Cho AI khả năng gọi functions, query databases, và sử dụng external tools

0

🎯 Mục tiêu bài học

TB5 min

Function Calling biến LLM từ "text generator" thành "agent" — có thể gọi APIs, query databases, và thực hiện actions.

Sau bài này, bạn sẽ:

✅ Hiểu Function Calling architecture ✅ Implement tools cho OpenAI & Claude ✅ Build AI agent với multiple tools ✅ Xử lý structured outputs

1

🔍 Function Calling Explained

TB5 min

1.1 Tại sao cần Function Calling?

Ví dụ
1Without tools:
2User: "Thời tiết Hà Nội hôm nay?"
3AI: "Tôi không biết thời tiết hiện tại." ← Chỉ biết training data
4
5With tools:
6User: "Thời tiết Hà Nội hôm nay?"
7AI: [calls get_weather("Hanoi")] → "Hà Nội hôm nay 28°C, nắng."

1.2 Architecture

Ví dụ
11. User sends message
22. LLM decides which function to call (if any)
33. LLM returns function name + arguments (JSON)
44. YOUR CODE executes the function
55. Function result sent back to LLM
66. LLM generates final response using result

1.3 Key Concept

Ví dụ
1LLM KHÔNG thực sự chạy code.
2LLM chỉ QUYẾT ĐỊNH gọi function nào + arguments.
3CODE CỦA BẠN thực sự execute function.

Checkpoint

Bạn đã hiểu kiến trúc Function Calling và vai trò của LLM vs code của bạn chưa?

2

💻 OpenAI Function Calling

TB5 min

2.1 Define Tools

python.py
1from openai import OpenAI
2
3client = OpenAI()
4
5# Define available tools
6tools = [
7 {
8 "type": "function",
9 "function": {
10 "name": "get_weather",
11 "description": "Lấy thông tin thời tiết hiện tại của một thành phố",
12 "parameters": {
13 "type": "object",
14 "properties": {
15 "city": {
16 "type": "string",
17 "description": "Tên thành phố, e.g. 'Hanoi', 'Ho Chi Minh City'"
18 },
19 "unit": {
20 "type": "string",
21 "enum": ["celsius", "fahrenheit"],
22 "description": "Đơn vị nhiệt độ"
23 }
24 },
25 "required": ["city"]
26 }
27 }
28 },
29 {
30 "type": "function",
31 "function": {
32 "name": "search_products",
33 "description": "Tìm kiếm sản phẩm trong database",
34 "parameters": {
35 "type": "object",
36 "properties": {
37 "query": {"type": "string", "description": "Search query"},
38 "category": {"type": "string", "description": "Product category"},
39 "max_price": {"type": "number", "description": "Maximum price in VND"}
40 },
41 "required": ["query"]
42 }
43 }
44 }
45]

2.2 Implement Functions

python.py
1import json
2
3# Your actual function implementations
4def get_weather(city, unit="celsius"):
5 """Simulate weather API call."""
6 # In production: call real weather API
7 weather_data = {
8 "Hanoi": {"temp": 28, "condition": "Nắng", "humidity": 75},
9 "Ho Chi Minh City": {"temp": 33, "condition": "Mưa rào", "humidity": 85},
10 }
11 data = weather_data.get(city, {"temp": 25, "condition": "Unknown", "humidity": 60})
12 return json.dumps(data, ensure_ascii=False)
13
14def search_products(query, category=None, max_price=None):
15 """Simulate product search."""
16 products = [
17 {"name": "Laptop Dell XPS 13", "price": 35000000, "category": "laptop"},
18 {"name": "MacBook Air M3", "price": 28000000, "category": "laptop"},
19 {"name": "iPhone 16 Pro", "price": 32000000, "category": "phone"},
20 ]
21 results = [p for p in products if query.lower() in p["name"].lower()]
22 if max_price:
23 results = [p for p in results if p["price"] <= max_price]
24 return json.dumps(results, ensure_ascii=False)
25
26# Map function names to implementations
27FUNCTIONS = {
28 "get_weather": get_weather,
29 "search_products": search_products,
30}

2.3 Complete Flow

python.py
1def chat_with_tools(user_message):
2 messages = [
3 {"role": "system", "content": "Bạn là shopping assistant. Trả lời bằng tiếng Việt."},
4 {"role": "user", "content": user_message}
5 ]
6
7 # Step 1: First API call
8 response = client.chat.completions.create(
9 model="gpt-4-turbo",
10 messages=messages,
11 tools=tools,
12 tool_choice="auto" # Let model decide
13 )
14
15 msg = response.choices[0].message
16
17 # Step 2: Check if model wants to call functions
18 if msg.tool_calls:
19 messages.append(msg) # Add assistant's tool_calls message
20
21 # Step 3: Execute each function
22 for tool_call in msg.tool_calls:
23 func_name = tool_call.function.name
24 func_args = json.loads(tool_call.function.arguments)
25
26 print(f"🔧 Calling {func_name}({func_args})")
27
28 # Execute function
29 result = FUNCTIONS[func_name](**func_args)
30
31 # Add function result to messages
32 messages.append({
33 "role": "tool",
34 "tool_call_id": tool_call.id,
35 "content": result
36 })
37
38 # Step 4: Get final response with function results
39 final_response = client.chat.completions.create(
40 model="gpt-4-turbo",
41 messages=messages,
42 tools=tools
43 )
44 return final_response.choices[0].message.content
45
46 # No function calls needed
47 return msg.content
48
49# Test
50print(chat_with_tools("Thời tiết Hà Nội thế nào?"))
51print(chat_with_tools("Tìm laptop dưới 30 triệu"))
52print(chat_with_tools("Hello!")) # No function call needed

Checkpoint

Bạn đã thực hành implement function calling với OpenAI API chưa?

3

💻 Claude Tool Use

TB5 min

3.1 Define & Use Tools

python.py
1import anthropic
2
3client = anthropic.Anthropic()
4
5tools = [
6 {
7 "name": "get_weather",
8 "description": "Lấy thông tin thời tiết hiện tại",
9 "input_schema": {
10 "type": "object",
11 "properties": {
12 "city": {"type": "string", "description": "Tên thành phố"}
13 },
14 "required": ["city"]
15 }
16 }
17]
18
19response = client.messages.create(
20 model="claude-3-5-sonnet-20241022",
21 max_tokens=1024,
22 tools=tools,
23 messages=[{"role": "user", "content": "Thời tiết Hà Nội?"}]
24)
25
26# Check for tool use
27for block in response.content:
28 if block.type == "tool_use":
29 print(f"Tool: {block.name}, Input: {block.input}")
30
31 # Execute and return result
32 result = get_weather(**block.input)
33
34 # Continue conversation with result
35 final = client.messages.create(
36 model="claude-3-5-sonnet-20241022",
37 max_tokens=1024,
38 tools=tools,
39 messages=[
40 {"role": "user", "content": "Thời tiết Hà Nội?"},
41 {"role": "assistant", "content": response.content},
42 {"role": "user", "content": [
43 {"type": "tool_result", "tool_use_id": block.id, "content": result}
44 ]}
45 ]
46 )
47 print(final.content[0].text)

Checkpoint

Bạn có thể implement tool use với Anthropic Claude API không?

4

📝 Structured Outputs

TB5 min

4.1 JSON Mode (OpenAI)

python.py
1response = client.chat.completions.create(
2 model="gpt-4-turbo",
3 messages=[{
4 "role": "user",
5 "content": "Phân tích sentiment của review: 'Sản phẩm tốt nhưng giao hàng chậm'"
6 }],
7 response_format={"type": "json_object"} # Force JSON
8)
9
10import json
11result = json.loads(response.choices[0].message.content)
12# {"sentiment": "mixed", "positive": ["sản phẩm tốt"], "negative": ["giao hàng chậm"]}

4.2 Strict Structured Output

python.py
1from pydantic import BaseModel
2
3class SentimentAnalysis(BaseModel):
4 sentiment: str # positive, negative, mixed
5 score: float # -1.0 to 1.0
6 positive_aspects: list[str]
7 negative_aspects: list[str]
8 summary: str
9
10response = client.beta.chat.completions.parse(
11 model="gpt-4-turbo",
12 messages=[{
13 "role": "user",
14 "content": "Phân tích: 'Quán coffee đẹp, nhân viên thân thiện, nhưng giá hơi cao'"
15 }],
16 response_format=SentimentAnalysis
17)
18
19result = response.choices[0].message.parsed
20print(f"Sentiment: {result.sentiment}")
21print(f"Score: {result.score}")
22print(f"Positive: {result.positive_aspects}")

Checkpoint

Bạn đã hiểu cách sử dụng JSON mode và Pydantic models cho structured output chưa?

5

🛠️ Build Multi-Tool Agent

TB5 min

5.1 Complete Agent

python.py
1import json
2from datetime import datetime
3
4# Define all tools
5def get_current_time():
6 return json.dumps({"time": datetime.now().strftime("%H:%M %d/%m/%Y")})
7
8def calculate(expression):
9 try:
10 result = eval(expression) # Caution: sanitize in production!
11 return json.dumps({"result": result})
12 except:
13 return json.dumps({"error": "Invalid expression"})
14
15def get_exchange_rate(from_currency, to_currency):
16 rates = {"USD_VND": 25400, "EUR_VND": 27500, "JPY_VND": 168}
17 key = f"{from_currency}_{to_currency}"
18 rate = rates.get(key, None)
19 return json.dumps({"rate": rate, "pair": key})
20
21TOOLS = {
22 "get_current_time": get_current_time,
23 "calculate": calculate,
24 "get_exchange_rate": get_exchange_rate,
25 "get_weather": get_weather,
26 "search_products": search_products,
27}
28
29tools_schema = [
30 {"type": "function", "function": {
31 "name": "get_current_time",
32 "description": "Lấy thời gian hiện tại",
33 "parameters": {"type": "object", "properties": {}}
34 }},
35 {"type": "function", "function": {
36 "name": "calculate",
37 "description": "Tính toán biểu thức toán học",
38 "parameters": {"type": "object", "properties": {
39 "expression": {"type": "string", "description": "Math expression, e.g. '2+2'"}
40 }, "required": ["expression"]}
41 }},
42 {"type": "function", "function": {
43 "name": "get_exchange_rate",
44 "description": "Lấy tỷ giá ngoại tệ",
45 "parameters": {"type": "object", "properties": {
46 "from_currency": {"type": "string"},
47 "to_currency": {"type": "string"}
48 }, "required": ["from_currency", "to_currency"]}
49 }},
50 # ... add weather and products schemas
51]

5.2 Agent Loop

python.py
1def agent_loop(user_message, max_iterations=5):
2 """Agent that can call multiple tools in sequence."""
3 messages = [
4 {"role": "system", "content": """
5 Bn là AI assistant có nhiu tools.
6 S dng tools khi cn thông tin real-time.
7 Có th gi nhiu tools nếu cn.
8 Tr li bng tiếng Vit.
9 """},
10 {"role": "user", "content": user_message}
11 ]
12
13 for i in range(max_iterations):
14 response = client.chat.completions.create(
15 model="gpt-4-turbo",
16 messages=messages,
17 tools=tools_schema,
18 tool_choice="auto"
19 )
20
21 msg = response.choices[0].message
22
23 if not msg.tool_calls:
24 return msg.content # Final answer
25
26 messages.append(msg)
27
28 for tool_call in msg.tool_calls:
29 func_name = tool_call.function.name
30 func_args = json.loads(tool_call.function.arguments)
31
32 print(f" 🔧 [{i+1}] {func_name}({func_args})")
33 result = TOOLS[func_name](**func_args)
34
35 messages.append({
36 "role": "tool",
37 "tool_call_id": tool_call.id,
38 "content": result
39 })
40
41 return "Exceeded max iterations."
42
43# Test multi-tool queries
44print(agent_loop("100 USD bằng bao nhiêu VND? Tính luôn 100 USD * 2"))
45print(agent_loop("Bây giờ mấy giờ? Thời tiết HN thế nào?"))

Checkpoint

Bạn đã xây dựng được agent loop với multiple tool calls chưa?

6

💻 Hands-on Lab

TB5 min

Lab 1: Personal Assistant Agent

Build agent với tools:

  • get_weather — Weather info
  • calculate — Math calculator
  • search_web — Simulated web search
  • take_note — Save notes to file
  • get_notes — Retrieve saved notes

Lab 2: Data Analyst Agent

Build agent có thể:

  • Load CSV file
  • Run pandas queries
  • Generate charts (matplotlib)
  • Summarize findings

Lab 3: Customer Support Bot

Build chatbot có tools:

  • lookup_order — Tra cứu đơn hàng
  • check_inventory — Kiểm tra tồn kho
  • create_ticket — Tạo support ticket
  • get_faq — Tìm FAQ answers

Checkpoint

Bạn đã thực hành xây dựng Personal Assistant và Data Analyst agents chưa?

7

🎯 Tổng kết

TB5 min

📝 Quiz

  1. LLM thực sự execute function không?

    • Có, LLM chạy code trực tiếp
    • Không, LLM chỉ quyết định gọi function nào
    • Tùy model
    • Chỉ với GPT-4
  2. tool_choice="auto" nghĩa là?

    • Luôn gọi tool
    • Không bao giờ gọi tool
    • LLM tự quyết định có gọi tool hay không
    • Random
  3. Structured output dùng response_format để?

    • Giảm cost
    • Force LLM trả về đúng JSON format
    • Tăng speed
    • Improve accuracy

Những điểm quan trọng

  1. Function Calling = LLM + Actions — Biến text model thành agent
  2. LLM decides, YOU execute — LLM chọn function, code chạy function
  3. Agent loop — Cho phép multiple tool calls liên tiếp
  4. Structured output — Pydantic models cho type-safe responses
  5. Tool design — Clear descriptions giúp LLM chọn đúng tool

Câu hỏi tự kiểm tra

  1. LLM có thực sự execute function không? Vai trò thực tế của LLM trong function calling là gì?
  2. Agent loop hoạt động như thế nào khi cần gọi nhiều tools liên tiếp?
  3. tool_choice parameter có các giá trị nào và ảnh hưởng thế nào đến hành vi của LLM?
  4. Structured output với Pydantic models giúp ích gì so với để LLM trả về free-form text?

🎉 Tuyệt vời! Bạn đã hoàn thành bài học Function Calling & Tool Use!

Tiếp theo: Chúng ta sẽ học cách quản lý tokens, chọn model phù hợp và tối ưu chi phí!


🚀 Bài tiếp theo

Cost Optimization — Quản lý tokens, chọn model phù hợp, và giảm chi phí!