Location>code7788 >text

AI Agent development, how to call the three-party API Function, is it used to initiate the call through prompt words?

Popularity:136 ℃/2025-03-17 08:42:16

Recently, I am studying how AI Agent calls three-party APIs and have compiled an article to share with you.

Calling the three-party API (Function Calling)noIt is achieved through the prompt word (Prompt), but throughFunction Calling mechanismThe specific process is as follows:

1. Model identification call requirements

When a big model parses user input, it will determine whether an API needs to be called. If needed, it does not generate a text reply directly, but calls a registered oneFunction (Function)

2. API method registration

When the developer calls the API,Predefined available API functions, and provide:

  • Function name(likeget_weather
  • Parameter definition(Use JSON Schema to describe the parameter structure)
  • Description Information(Help big models understand the purpose of functions)
{
  "name": "get_weather",
  "description": "Get weather information for a city",
  "parameters": {
    "type": "object",
    "properties": {
      "city": {
        "type": "string",
        "description": "City Name"
      }
    },
    "required": ["city"]
  }
}

3. Large modelGenerate a function call request

When the user enters something like:

"What's the weather like in Beijing today?"

The big model will parse:

{
  "name": "get_weather",
  "arguments": {
    "city": "Beijing"
  }
}

 

4. AI Agent/ProgramCall the three-party API and return the result

In AI Agent/program, you can:

  • Calling the actual API (such as the Weather Services API)
  • Get data
  • Append data to Prompt and return it to the big model to continue processing

Example:

{
  "temperature": "15°C",
  "weather": "sunny"
}

5. Large modelparse the result and return the final text

The big model processes the data returned by the API and generates natural language reply to the user:

"It's sunny today in Beijing, with a temperature of 15°C."

Summarize

  • Instead of calling the API through the prompt word (Prompt), the function call mechanism
  • Developers need to pre-register API functions
  • After the big model recognizes the requirements, it automatically constructs API call parameters.
  • The developer is responsible for executing API requests and returning the result to GPT
  • Big model parses API results and generates end-user-readable reply

Zhou Guoqing

2025/03/16