OpenAI GPT function calling with JavaScript and Cloudflare Workers
In this tutorial, you will build a project that leverages OpenAI’s function calling ↗ feature, available in OpenAI’s latest Chat Completions API models.
The function calling feature allows the AI model to intelligently decide when to call a function based on the input, and respond in JSON format to match the function’s signature. You will use the function calling feature to request for the model to determine a website URL which contains information relevant to a message from the user, retrieve the text content of the site, and, finally, return a final response from the model informed by real-time web data.
- How to use OpenAI’s function calling feature.
- Integrating OpenAI’s API in a Cloudflare Worker.
- Fetching and processing website content using Cheerio.
- Handling API responses and function calls in JavaScript.
- Storing API keys as secrets with Wrangler.
All of the tutorials assume you have already completed the Get started guide, which gets you set up with a Cloudflare Workers account, C3 ↗, and Wrangler.
Create a Worker project in the command line:
For setup, select the following options:
- For What would you like to start with?, choose
Hello World example
. - For Which template would you like to use?, choose
Hello World Worker
. - For Which language do you want to use?, choose
JavaScript
. - For Do you want to use git for version control?, choose
Yes
. - For Do you want to deploy your application?, choose
No
(we will be making some changes before deploying).
Go to your new openai-function-calling-workers
Worker project:
Inside of your new openai-function-calling-workers
directory, find the src/index.js
file. You will configure this file for most of the tutorial.
You will also need an OpenAI account and API key for this tutorial. If you do not have one, create a new OpenAI account ↗ and create an API key ↗ to continue with this tutorial. Make sure to store you API key somewhere safe so you can use it later.
With your Worker project created, make your first request to OpenAI. You will use the OpenAI node library to interact with the OpenAI API. In this project, you will also use the Cheerio library to handle processing the HTML content of websites
Now, define the structure of your Worker in index.js
:
Above export default
, add the imports for openai
and cheerio
:
Within your fetch
function, instantiate your OpenAI
client:
Use wrangler secret put
to set OPENAI_API_KEY
. This secret’s value is the API key you created earlier in the OpenAI dashboard:
For local development, create a new file .dev.vars
in your Worker project and add this line. Make sure to replace OPENAI_API_KEY
with your own OpenAI API key:
Now, make a request to the OpenAI Chat Completions API ↗:
Review the arguments you are passing to OpenAI:
- model: This is the model you want OpenAI to use for your request. In this case, you are using
gpt-4o-mini
. - messages: This is an array containing all messages that are part of the conversation. Initially you provide a message from the user, and we later add the response from the model. The content of the user message is either the
message
query parameter from the request URL or the default “What’s in the news today?”. - tools: An array containing the actions available to the AI model. In this example you only have one tool,
read_website_content
, which reads the content on a given website.- name: The name of your function. In this case, it is
read_website_content
. - description: A short description that lets the model know the purpose of the function. This is optional but helps the model know when to select the tool.
- parameters: A JSON Schema object which describes the function. In this case we request a response containing an object with the required property
url
.
- name: The name of your function. In this case, it is
- tool_choice: This argument is technically optional as
auto
is the default. This argument indicates that either a function call or a normal message response can be returned by OpenAI.
You will now need to define the read_website_content
function, which is referenced in the tools
array. The read_website_content
function fetches the content of a given URL and extracts the text from <p>
tags using the cheerio
library:
Add this code above the export default
block in your index.js
file:
In this function, you take the URL that you received from OpenAI and use JavaScript’s Fetch API
↗ to pull the content of the website and extract the paragraph text. Now we need to determine when to call this function.
Next, we need to process the response from the OpenAI API to check if it includes any function calls. If a function call is present, you should execute the corresponding function in your Worker. Note that the assistant may request multiple function calls.
Modify the fetch method within the export default
block as follows:
Check if the assistant message contains any function calls by checking for the tool_calls
property. Because the AI model can call multiple functions by default, you need to loop through any potential function calls and add them to the messages
array. Each read_website_content
call will invoke the read_website_content
function you defined earlier and pass the URL generated by OpenAI as an argument. `
The secondChatCompletion
is needed to provide a response informed by the data you retrieved from each function call. Now, the last step is to deploy your Worker.
Test your code by running npx wrangler dev
and open the provided url in your browser. This will now show you OpenAI’s response using real-time information from the retrieved web data.
To deploy your application, run the npx wrangler deploy
command to deploy your Worker application:
You can now preview your Worker at <YOUR_WORKER>.<YOUR_SUBDOMAIN>.workers.dev
. Going to this URL will display the response from OpenAI. Optionally, add the message
URL parameter to write a custom message: for example, https://<YOUR_WORKER>.<YOUR_SUBDOMAIN>.workers.dev/?message=What is the weather in NYC today?
.
Reference the finished code for this tutorial on GitHub ↗.
To continue working with Workers and AI, refer to the guide on using LangChain and Cloudflare Workers together ↗ or how to build a ChatGPT plugin with Cloudflare Workers ↗.
If you have any questions, need assistance, or would like to share your project, join the Cloudflare Developer community on Discord ↗ to connect with fellow developers and the Cloudflare team.