找回密码
 立即注册
首页 业界区 业界 我不允许谁还不清楚function call在AI-Agent领域中打手 ...

我不允许谁还不清楚function call在AI-Agent领域中打手的地位

峰襞副 4 天前
前文提要:还有比ollama更傻瓜式的大模型本地部署方式吗 ?
1.function calling 底层工作原理

大模型重塑了我们与软件应用的交互方式, 其中最重要的特性就是 function calling 。
一种利用结构化输入/输出在LLM和编程应用之间建立桥梁的方式。
不管是当前火热的AI-agent还是MCP,了解function calling底层工作原理都至关重要,特别是request和response payload。
回顾上文我们与qwen大模型的对话:
what is the temperature in the capital of china today?
1.png

一个由LLM驱动的应用, 回答这个问题,应用与LLM经历了三次对话。
① 第一次请求payload:

  • message:标准的历史对话, 提供上下文context
  • tools:一系列的工具函数定义, 提供给LLM来选择
LLM的响应payload:

  • tool_calls.function.name: LLM选中的工具函数get_currentDate
  • tool_calls.id: 由LLM为这个函数指定的id,后面会用到
② 第二次request:

  • messages.role.assistant: 告诉LLM我们这次请求包含了上次function calling的结果
  • messages.role.tool: 上次function calling执行的结果2026-01-24
LLM的响应payload:
如第一次类似:
本例也有tool_calls:包含LLM选中的函数get_temperature() 和所有的参数beijing 2026-01-24。
③ 应用最后一次请求,包含所有信息
LLM推理认为不再需要外部工具,不再返回tool_calls,给出结合外部工具的对话结果。
从三次请求对话来看, LLM在三次对话的响应中体现了它的思考和逻辑步骤,应用持续被LLM引导做出行动,同时LLM也持续对应用的行为做出进一步观察和思考。
2. agent的实现原理

大模型是 AI 的大脑,其核心是理解自然语言,并做出回应,(文本)大模型本身只能接收一段文本,然后输出一段文本。
而当你希望大模型能使用一些工具自行获取所需的信息、执行一些动作,就需要使用 Tool 来实现了,拥有了 Tool 的大模型就像是拥有了手脚,可以和当下已有的 IT 基础设施进行交互。
RAG给LLM装上实时知识外挂, 通过将信息检索与文本生成结合,让模型能引用外部权威信息生成回答,既保证了时效性,又提升了准确性。
字节开源的Eino标榜的优势在于:

  • LangChain,LlamaIndex等主流框架虽然起源自python强大的AI生态,但是也继承了python“弱类型检查”和“长期维护成本高”的诟病。 Eino作为golang下的开源agent开发框架,规避了这一问题。
  • 另一方面, 借助字节系在agent领域的工程化实践,Eino既封装了领域内不变的通用核心和最佳实践,也能敏捷的反映业内技术动向。
Eino框架结构图:
2.png

Eino 有三大稳定内核: compose编排、components组件,common公共库。
组件一抹多,是原子能力的最小单位, 编排对这些组件进行组合、串连。
3.  中国首都今天的天气怎么样? 我母鸡啊

我们使用Eino框架来实现本文的题目:
what is  the temperature in the capital of china today。
按照我们的分析,
从LLM的视角,要回答这个问题,经历了“思考-行动-观察-思考” 循环, 这是一个ReAct模式的agent。
下面基于阿里百炼千问大模型,实现了天气对话,请自行从阿里百炼平台申请的apiKey替换到54行。
  1. package main
  2. import (
  3.         "context"
  4.         "fmt"
  5.         "log"
  6.         "os"
  7.         "time"
  8.         "github.com/cloudwego/eino-ext/components/model/qwen"
  9.         "github.com/cloudwego/eino/components/tool"
  10.         "github.com/cloudwego/eino/components/tool/utils"
  11.         "github.com/cloudwego/eino/compose"
  12.         "github.com/cloudwego/eino/schema"
  13. )
  14. // what is  the temperature in the capital of china today
  15. type weatherReqParam struct {
  16.         City string `json:"city"  jsonschema:"description=the name of the city"`
  17.         Date string `json:"date"  jsonschema:"description=the date in the format of YYYY-MM-DD"`
  18. }
  19. func GetTemperatureFunc(_ context.Context, p weatherReqParam) (float64, error) {
  20.         // 这里直接mock一个温度值,实际应用中应该替换为真实的API调用
  21.         return 32, nil
  22. }
  23. func GetCurrentDateFunc(_ context.Context, _ struct{}) (string, error) {
  24.         return time.Now().Format("2006-01-02"), nil
  25. }
  26. func of[T any](t T) *T {
  27.         return &t
  28. }
  29. func main() {
  30.         getDateTool, err := utils.InferTool("get_currentDate", "Get the current date", GetCurrentDateFunc)
  31.         if err != nil {
  32.                 panic(err)
  33.         }
  34.         getTemperatureTool, err := utils.InferTool("get_temperature", "Get the temperature in the capital of the city", GetTemperatureFunc)
  35.         if err != nil {
  36.                 panic(err)
  37.         }
  38.         // 初始化 tools
  39.         weatherTools := []tool.BaseTool{
  40.                 getDateTool,
  41.                 getTemperatureTool,
  42.         }
  43.         apiKey := os.Getenv("DASHSCOPE_API_KEY")
  44.         apiKey = "{}"  // 在阿里百炼平台申请签名
  45.         modelName := os.Getenv("MODEL_NAME")
  46.         modelName = "qwen3-max"
  47.         chatModel, err := qwen.NewChatModel(context.Background(), &qwen.ChatModelConfig{
  48.                 BaseURL:     "https://dashscope.aliyuncs.com/compatible-mode/v1",
  49.                 APIKey:      apiKey,
  50.                 Timeout:     0,
  51.                 Model:       modelName,
  52.                 MaxTokens:   of(2048),
  53.                 Temperature: of(float32(0.7)),
  54.                 TopP:        of(float32(0.7)),
  55.         })
  56.         if err != nil {
  57.                 log.Fatalf("NewChatModel of qwen failed, err=%v", err)
  58.         }
  59.         var ctx = context.Background()
  60.         // 获取工具信息并绑定到 ChatModel
  61.         toolInfos := make([]*schema.ToolInfo, 0, len(weatherTools))
  62.         for _, tool := range weatherTools {
  63.                 info, err := tool.Info(ctx)
  64.                 if err != nil {
  65.                         log.Fatal(err)
  66.                 }
  67.                 toolInfos = append(toolInfos, info)
  68.         }
  69.         err = chatModel.BindTools(toolInfos)
  70.         if err != nil {
  71.                 log.Fatal(err)
  72.         }
  73.         // 创建 tools 节点
  74.         weatherToolsNode, err := compose.NewToolNode(context.Background(), &compose.ToolsNodeConfig{
  75.                 Tools: weatherTools,
  76.         })
  77.         if err != nil {
  78.                 log.Fatal(err)
  79.         }
  80.         // 构建基于 Graph 的 Agent,实现 ReAct 模式(自动执行工具并生成自然语言回复)
  81.         // 定义状态,用于保存对话历史
  82.         type appState struct {
  83.                 messages []*schema.Message
  84.         }
  85.         graph := compose.NewGraph[[]*schema.Message, *schema.Message](
  86.                 compose.WithGenLocalState(func(ctx context.Context) *appState {
  87.                         return &appState{}
  88.                 }),
  89.         )
  90.         // 添加 ChatModel 节点
  91.         err = graph.AddChatModelNode("qwen_chat_model", chatModel,
  92.                 compose.WithStatePreHandler(func(ctx context.Context, input []*schema.Message, state *appState) ([]*schema.Message, error) {
  93.                         // 如果是第一次进入(从 Start),将输入(用户问题)添加到历史
  94.                         if len(state.messages) == 0 {
  95.                                 state.messages = append(state.messages, input...)
  96.                         }
  97.                         // 始终将完整的历史记录作为 ChatModel 的输入
  98.                         return state.messages, nil
  99.                 }),
  100.                 compose.WithStatePostHandler(func(ctx context.Context, output *schema.Message, state *appState) (*schema.Message, error) {
  101.                         // 将 ChatModel 的输出(可能是工具调用或最终回复)添加到历史
  102.                         state.messages = append(state.messages, output)
  103.                         return output, nil
  104.                 }),
  105.         )
  106.         if err != nil {
  107.                 log.Fatal(err)
  108.         }
  109.         // 添加 Tools 节点
  110.         err = graph.AddToolsNode("agent_tools", weatherToolsNode,
  111.                 compose.WithStatePostHandler(func(ctx context.Context, output []*schema.Message, state *appState) ([]*schema.Message, error) {
  112.                         // 将工具执行结果添加到历史
  113.                         state.messages = append(state.messages, output...)
  114.                         return output, nil
  115.                 }),
  116.         )
  117.         if err != nil {
  118.                 log.Fatal(err)
  119.         }
  120.         // 添加边和分支
  121.         _ = graph.AddEdge(compose.START, "qwen_chat_model")
  122.         // 如果有工具调用,流转到 tools;否则结束
  123.         _ = graph.AddBranch("qwen_chat_model", compose.NewGraphBranch(func(ctx context.Context, msg *schema.Message) (string, error) {
  124.                 if len(msg.ToolCalls) > 0 {
  125.                         return "agent_tools", nil
  126.                 }
  127.                 return compose.END, nil
  128.         }, map[string]bool{"agent_tools": true, compose.END: true}))
  129.         // 工具执行完后,回流到 chat_model 生成回复
  130.         _ = graph.AddEdge("agent_tools", "qwen_chat_model")
  131.         // 编译运行
  132.         agent, err := graph.Compile(ctx)
  133.         if err != nil {
  134.                 log.Fatal(err)
  135.         }
  136.         // 运行示例
  137.         resp, err := agent.Invoke(ctx, []*schema.Message{
  138.                 {
  139.                         Role:    schema.User,
  140.                         Content: "what is the temperature in the capital of china today? please answer in a humam like way.",
  141.                 },
  142.         })
  143.         if err != nil {
  144.                 log.Fatal(err)
  145.         }
  146.         // 输出结果
  147.         fmt.Println(resp.Content)
  148. }
复制代码
一开始我参照的官网的chain编排Eino组件,但是得到的结果是数字“32”, 并不是LLM对话的类人语言。
使用Trae的编码agent,20s就帮我改成了Graph形式的正确编码, 输出:
  1. The temperature in Beijing, the capital of China, today (January 28, 2026) is a warm 32°C! That’s quite hot for this time of year—make sure to stay hydrated and cool if you’re out and about!
复制代码
本例实际也可以使用Chain来完成,chain是一种特殊的、简化的graph,chain不能回头,本例需要手动串起来,Chain更适合确定性的编排。
Graph就像一个自动化的流水线(loop),工人是 chat_model 和 tools :
本例的Graph图如下,读者可以结合源代码理解。
3.png


  • START -> chat_model

    • ChatModel 拿到用户问题,思考后说:“我需要查日期。”(输出包含 ToolCall)

  • chat_model -> tools (由 Branch 判断)

    • Tools 听到指令,去查日期,得到 "2026-01-24"。

  • tools -> chat_model (由 AddEdge 强制流转)

    • ChatModel 再次被唤醒。这次它的输入包含了之前的历史(用户问题 + 刚才的 ToolCall + 刚才的结果)。
    • 它再次思考:“我知道日期了,现在我要查北京的温度。”(输出包含 ToolCall)

  • chat_model -> tools (再次循环)

    • Tools 去查温度,得到 "32度"。

  • tools -> chat_model (再次循环)

    • ChatModel 再次被唤醒。现在的输入更丰富了(所有历史)。
    • 它再次思考:“日期有了,温度也有了,我可以直接回答用户了。”(输出 不包含 ToolCall)

  • chat_model -> END (由 Branch 判断)

    • 循环结束。

最后我们重温全文,function calling(function tools)在agent中的作用:
在由LLM驱动的agent应用中,function calling(function tools)作为LLM的手脚,让LLM具备使用工具从外部获取最新信息并指导应用行为的能力,这一过程由结构化的输入输出参数来传递。

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

昨天 03:41

举报

懂技术并乐意极积无私分享的人越来越少。珍惜
您需要登录后才可以回帖 登录 | 立即注册