Custom Tool
Problem
Function usually takes in structured input data. Let's say you want the LLM to be able to call Airtable Create Record API, the body parameters has to be structured in a specific way. For example:
Ideally, we want LLM to return a proper structured data like this:
So we can extract the value and parse it into the body needed for API. However, instructing LLM to output the exact pattern is difficult.
With the new OpenAI Function Calling models, it is now possible. gpt-4-0613
and gpt-3.5-turbo-0613
are specifically trained to return structured data. The model will intelligently choose to output a JSON object containing arguments to call those functions.
Tutorial
Goal: Have the agent automatically get the stock price movement, retrieve related stock news, and add a new record to Airtable.
Let's get started🚀
Create Tools
We need 3 tools to achieve the goal:
Get Stock Price Movement
Get Stock News
Add Airtable Record
Get Stock Price Movement
Create a new Tool with the following details (you can change as you want):
Name: get_stock_movers
Description: Get the stocks that has biggest price/volume moves, e.g. actives, gainers, losers, etc.
Description is an important piece as ChatGPT is relying on this to decide when to use this tool.
JavaScript Function: We are going to use Morning Star
/market/v2/get-movers
API to get data. First you have to click Subscribe to Test if you haven't already, then copy the code and paste it into JavaScript Function.Add
const fetch = require('node-fetch');
at the top to import the library. You can import any built-in NodeJS modules and external libraries.Return the
result
at the end.
The final code should be:
You can now save it.
Get Stock news
Create a new Tool with the following details (you can change as you want):
Name: get_stock_news
Description: Get latest news for a stock
Input Schema:
Property: performanceId
Type: string
Description: id of the stock, which is referred as performanceID in the API
Required: true
Input Schema tells LLM what to return as a JSON object. In this case, we are expecting a JSON object like below:
JavaScript Function: We are going to use Morning Star
/news/list
API to get the data. First you have to click Subscribe to Test if you haven't already, then copy the code and paste it into JavaScript Function.Add
const fetch = require('node-fetch');
at the top to import the library. You can import any built-in NodeJS modules and external libraries.Return the
result
at the end.
Next, replace the hard-coded url query parameter performanceId:
0P0000OQN8
to the property variable specified in Input Schema:$performanceId
You can use any properties specified in Input Schema as variables in the JavaScript Function by appending a prefix
$
at the front of the variable name.
Final code:
You can now save it.
Add Airtable Record
Create a new Tool with the following details (you can change as you want):
Name: add_airtable
Description: Add the stock, news summary & price move to Airtable
Input Schema:
Property: stock
Type: string
Description: stock ticker
Required: true
Property: move
Type: string
Description: price move in %
Required: true
Property: news_summary
Type: string
Description: news summary of the stock
Required: true
ChatGPT will returns a JSON object like this:
JavaScript Function: We are going to use Airtable Create Record API to create a new record to an existing table. You can find the tableId and baseId from here. You'll also need to create a personal access token, find how to do it here.
Final code should looks like below. Note how we pass in $stock
, $move
and $news_summary
as variables:
You can now save it.
You should see 3 tools created:
Create Chatflow
You can use the template OpenAI Function Agent from marketplace, and replace the tools with Custom Tool. Select the tool you have created.
Note: OpenAI Function Agent only supports 0613 models currently.
Save the chatflow and start testing it. For starter, you can try asking:
What is the stock that has the biggest price movement today?
The stock that has the biggest price movement today is Overstock.com (OSTK) with a price move of 17.47%.
You can then follow up with another question to get the news of that particular stock:
What are the latest news about this stock that might cause the price movement?
Here are the latest news about Overstock.com (OSTK) that might have caused the price movement:
Title: "Overstock's shares soar on successful 21.5 million bid for Bed Bath & Beyond assets" Source: MarketWatch Published Date: June 22, 2023 Summary: Overstock.com's shares experienced a significant increase after successfully winning a bid for Bed Bath & Beyond assets worth 21.5 million.
Title: "Thinking about trading options or stock in Meta Platforms, Overstock.com, Walmart, Home Depot, or United Parcel Service?" Source: PR Newswire Published Date: June 22, 2023 Summary: This news article discusses the potential trading options and stocks, including Overstock.com, that investors may consider.
Please note that these news articles are for informational purposes only and may not be the sole reason for the price movement. It is always recommended to conduct thorough research and analysis before making any investment decisions.
Lastly, you can ask ChatGPT to add a new record to Airtable:
Can you add a record to Airtable with the stock ticker, price move and news summary?
I have added a record to Airtable with the following details:
Stock Ticker: OSTK Price Move: 17.47% News Summary: Overstock.com's shares experienced a significant increase after successfully winning a bid for Bed Bath & Beyond assets worth $21.5 million.
🎉🎉Voila! That's how you can create your own custom tool and use it with the OpenAI Function Agent!
Additional
Pass Session ID to Function
By default, Function in custom tool has access to the following flow configurations:
Below is an example of sending the sessionId to Discord webhook:
Pass variables to Function
In some cases, you would like to pass variables to custom tool function.
For example, you are creating a chatbot that uses a custom tool. The custom tool is executing a HTTP POST call and API key is needed for successful authenticated request. You can pass it as a variable.
By default, Function in custom tool has access to variables:
Example of how to pass variables in Tailwinds using API and Embedded:
Example of how to receive the variables in custom tool:
Override Custom Tool
Parameters below can be overriden
Example of an API call to override custom tool parameters:
Last updated