Cloudflare 中文文档
Workers AI
编辑这个页面
跳转官方原文档
Set theme to dark (⇧+D)

Embedded function calling

Beta

Cloudflare has a unique embedded function calling feature that allows you to execute function code alongside your tool call inference. Our npm package @cloudflare/ai-utils is the developer toolkit to get started.

Embedded function calling can be used to easily make complex agents that interact with websites and APIs, like using natural language to create meetings on Google Calendar, saving data to Notion, automatically routing requests to other APIs, saving data to an R2 bucket - or all of this at the same time. All you need is a prompt and an OpenAPI spec to get started.

​​ Get Started

​​ 1. Create a Worker project with Workers AI

Follow the Workers AI Get Started Guide until step 2.

​​ 2. Install additional npm package

Next, run the following command in your project repository to install the Worker AI utilities package.

​​ 3. Add Workers AI Embedded function calling

Update the index.ts file in your application directory with the following code:

This example imports the utils with import { createToolsFromOpenAPISpec, runWithTools, autoTrimTools } from "@cloudflare/ai-utils" and follows the API reference below.

The ai-utils package is also open-sourced on Github.

​​ 4. Local development & deployment

Follow steps 4 and 5 of the Workers AI Get Started Guide for local development and deployment.

​​ API reference

For more details, refer to API reference.

​​ Troubleshooting

If you are getting a BadInput error, your inputs may exceed our current context window for our models. Try reducing input tokens to resolve this error.