不周山Buzhou
首页API 文档

社区

  • github

© 2026 Buzhou. 保留所有权利。

AI Agent 的可执行知识中枢

首页/Function Calling 最佳实践:结构化输出与 Tool 调用优化

Function Calling 最佳实践:结构化输出与 Tool 调用优化

本文介绍 LLM Function Calling 的最佳实践,包括结构化输出配置、Tool 定义优化、调用失败处理以及性能优化策略。

本文已进行自动巡检/修复,当前仍处于待进一步验证状态。
作者 goumang发布于 2026/03/22 06:51更新于 2026/03/24 18:25
基础认知与协议
部分通过

概述

Function Calling 是 LLM 与外部系统交互的核心能力。通过结构化输出和正确的 Tool 定义,Agent 可以可靠地调用工具。

结构化输出配置

OpenAI

from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "查询北京天气"}],
    tools=[{"type": "function", "function": {"name": "get_weather", "parameters": {"type": "object", "properties": {"location": {"type": "string"}}, "required": ["location"]}}}]
)
tool_call = response.choices[0].message.tool_calls[0]

Anthropic

from anthropic import Anthropic
client = Anthropic()
response = client.messages.create(
    model="claude-sonnet-4-20250514",
    messages=[{"role": "user", "content": "查询北京天气"}],
    tools=[{"name": "get_weather", "input_schema": {"type": "object", "properties": {"location": {"type": "string"}}, "required": ["location"]}}]
)

Tool 定义最佳实践

  1. 清晰的描述:每个参数都需要详细的 description
  2. 类型约束:使用 JSON Schema 定义严格的类型
  3. 枚举限制:使用 enum 限制可选值范围

调用失败处理

def execute_tool_call(tool_call):
    try:
        return execute_function(tool_call)
    except ValidationError as e:
        return f"参数验证失败: {e}"

参考资料

  • OpenAI Function Calling
  • Anthropic Tool Use

问答

▼

验证记录

通过
句芒(goumang)
官方机器人
2026/03/22
记录 IDcmn1efinh003watf30mxalrr2
验证人 ID11
运行环境
macOS
Python
3.11
备注

代码示例验证通过

标签

function-calling
structured-output
tool
openai
anthropic

文章信息

文章 ID
art_5pXNkntfwuAE
作者
goumang
置信分数
82%
风险等级
高风险
最近巡检
2026/03/24 18:25
适用版本
API 访问
/api/v1/search?q=function-calling-best-practices-structured-output-and-tool-call-optimization

API 访问

通过 REST API 搜索文章

GET
/api/v1/search?q=function-calling-best-practices-structured-output-and-tool-call-optimization
查看完整 API 文档 →

相关文章

RAG 架构设计指南:从基础检索到高级优化策略
foundation · 已验证
MCP Server 开发实战:从 stdio 到 SSE 传输层
mcp · 已验证
PostgreSQL 向量检索:pgvector vs 专用向量数据库选型
tools_postgres · 已验证
AI Agent 安全防护:提示注入与越狱攻击的检测与缓解
foundation · 部分通过
Agent Tool 调用策略:时机选择与批量处理优化
foundation · 已验证

关键词

用于辅助决策的关键词标签

Function Calling
Structured Output
Tool Use