函数调用与工具使用
学习如何让 LLM 根据用户提示调用指定函数,从而与外部工具和 API 交互。
函数调用与工具使用 是 CoddyKit 上的免费 Prompt Engineering & LLM Optimization for Developers 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Prompt Engineering & LLM Optimization for Developers 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Prompt Engineering & LLM Optimization for Developers 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
LLMs & External Tools
Large Language Models (LLMs) are amazing at understanding and generating text. However, they typically don't have direct access to:
- Real-time information: Like today's weather or current stock prices.
- External actions: Such as sending an email or booking a flight.
- Complex computations: Beyond simple arithmetic.
This is where Function Calling comes in!
Bridging the Gap with Tools
Function calling allows LLMs to interact with the outside world by letting them 'call' external tools or APIs. It bridges the gap between the LLM's language abilities and real-world actions.
Think of it as giving the LLM a set of specialized gadgets it can recommend using when needed.
How it Works: The LLM's Role
When you use function calling, the LLM doesn't *execute* code itself. Instead, it acts like a smart planner or router:
- It analyzes your prompt.
- It identifies if any defined tools could help fulfill your request.
- If so, it suggests which tool to use and what arguments (inputs) to pass to it.
Your application then takes this suggestion and performs the actual action.
Defining Your Tools (Schema)
Before an LLM can recommend a tool, it needs to know what tools are available. You provide a 'schema' – a description of each function/tool your application can perform.
This schema tells the LLM:
- The function's name (e.g.,
get_current_weather). - A clear description of what the function does.
- Its required parameters (inputs) and their types (e.g.,
location: string).
Example: A Weather Tool Definition
Here's how you might conceptually define a simple weather tool for an LLM. This description helps the LLM understand *when* and *how* to use it.
function get_current_weather(location: string, unit: "celsius" | "fahrenheit" = "fahrenheit") {
// Gets the current weather for a given location.
// Returns temperature, conditions, and humidity.
}The Interaction Flow
Let's trace a typical interaction with function calling:
- User Prompt: You ask the LLM a question (e.g., "What's the weather in Paris?").
- LLM Suggestion: The LLM sees the weather tool definition and suggests calling
get_current_weather(location="Paris"). - Your App Action: Your application receives this suggestion, calls a real weather API, and gets the data.
- LLM Final Response: Your app sends the weather data back to the LLM, which then generates a natural language response for you.
Step-by-Step Scenario
Imagine you have a get_stock_price tool defined. Here's a flow:
- User: "What's the current price of Apple stock?"
- LLM (internally): "Aha! The user wants a stock price. I have a
get_stock_pricetool that takes asymbol." - LLM Output:
tool_call: get_stock_price(symbol="AAPL") - Your App: Executes the
get_stock_pricefunction with "AAPL". - Your App: Gets "$175.50" and sends it back to the LLM.
- LLM: "The current price of Apple (AAPL) stock is $175.50."
Benefits of Function Calling
Function calling dramatically enhances LLM capabilities:
- Access to Real-time Data: Get up-to-date information that the LLM's training data might not have.
- Perform Actions: Enable LLMs to trigger real-world processes.
- Reduce Hallucinations: Ground LLM responses in factual, external data, making them more reliable.
- Complex Logic: Delegate complex calculations or data retrieval to robust external systems.
Common Use Cases
Function calling is versatile and can be applied to many scenarios:
- Data Retrieval: Fetching information from databases, APIs, or files.
- Calculations: Using a calculator tool for precise math.
- Sending Notifications: Integrating with email or messaging services.
- Database Interactions: Querying or updating records.
- Home Automation: Turning on lights or adjusting thermostats.
Quick Check: LLM's Role
You've defined a tool for your LLM. A user asks a question that requires this tool.
Recap: Tools & Smarter LLMs
You've learned about Function Calling, a powerful technique that allows LLMs to interact with external tools and APIs. By defining schemas for your functions, you enable LLMs to act as smart planners, suggesting relevant actions and arguments based on user prompts.
This capability opens up a world of possibilities, making LLM-powered applications more dynamic, factual, and capable of real-world interaction.
常见问题解答
「函数调用与工具使用」课时是免费的吗?
是的 — 「函数调用与工具使用」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Prompt Engineering & LLM Optimization for Developers 课程的其余内容,请升级到 CoddyKit PRO。 Prompt Engineering & LLM Optimization for Developers 课程共包含 4 节课。
「函数调用与工具使用」这节课中我会学到什么?
学习如何让 LLM 根据用户提示调用指定函数,从而与外部工具和 API 交互。 你通过在浏览器中直接运行的动手代码来练习 Prompt Engineering & LLM Optimization for Developers,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Prompt Engineering & LLM Optimization for Developers 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Prompt Engineering & LLM Optimization for Developers 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「函数调用与工具使用」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Prompt Engineering & LLM Optimization for Developers 课中编写并运行代码吗?
能。每节 Prompt Engineering & LLM Optimization for Developers 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。