Tailwinds - The UI for AI
  • Welcome to Tailwinds
    • Chatflows
      • LangChain
        • Agents
          • Airtable Agent
          • AutoGPT
          • BabyAGI
          • CSV Agent
          • Conversational Agent
          • OpenAI Assistant
            • Threads
          • ReAct Agent Chat
          • ReAct Agent LLM
          • Tool Agent
          • XML Agent
        • Cache
          • InMemory Cache
          • InMemory Embedding Cache
          • Momento Cache
          • Redis Cache
          • Redis Embeddings Cache
          • Upstash Redis Cache
        • Chains
          • GET API Chain
          • OpenAPI Chain
          • POST API Chain
          • Conversation Chain
          • Conversational Retrieval QA Chain
          • LLM Chain
          • Multi Prompt Chain
          • Multi Retrieval QA Chain
          • Retrieval QA Chain
          • Sql Database Chain
          • Vectara QA Chain
          • VectorDB QA Chain
        • Chat Models
          • AWS ChatBedrock
          • Azure ChatOpenAI
          • NIBittensorChat
          • ChatAnthropic
          • ChatCohere
          • Chat Fireworks
          • ChatGoogleGenerativeAI
          • ChatGooglePaLM
          • Google VertexAI
          • ChatHuggingFace
          • ChatMistralAI
          • ChatOllama
          • ChatOllama Funtion
          • ChatOpenAI
          • ChatOpenAI Custom
          • ChatTogetherAI
          • GroqChat
        • Document Loaders
          • API Loader
          • Airtable
          • Apify Website Content Crawler
          • Cheerio Web Scraper
          • Confluence
          • Csv File
          • Custom Document Loader
          • Document Store
          • Docx File
          • Figma
          • FireCrawl
          • Folder with Files
          • GitBook
          • Github
          • Json File
          • Json Lines File
          • Notion Database
          • Notion Folder
          • Notion Page
          • PDF Files
          • Plain Text
          • Playwright Web Scraper
          • Puppeteer Web Scraper
          • AWS S3 File Loader
          • SearchApi For Web Search
          • SerpApi For Web Search
          • Spider Web Scraper/Crawler
          • Text File
          • Unstructured File Loader
          • Unstructured Folder Loader
          • VectorStore To Document
        • Embeddings
          • AWS Bedrock Embeddings
          • Azure OpenAI Embeddings
          • Cohere Embeddings
          • Google GenerativeAI Embeddings
          • Google PaLM Embeddings
          • Google VertexAI Embeddings
          • HuggingFace Inference Embeddings
          • MistralAI Embeddings
          • Ollama Embeddings
          • OpenAI Embeddings
          • OpenAI Embeddings Custom
          • TogetherAI Embedding
          • VoyageAI Embeddings
        • LLMs
          • AWS Bedrock
          • Azure OpenAI
          • NIBittensorLLM
          • Cohere
          • GooglePaLM
          • GoogleVertex AI
          • HuggingFace Inference
          • Ollama
          • OpenAI
          • Replicate
        • Memory
          • Buffer Memory
          • Buffer Window Memory
          • Conversation Summary Memory
          • Conversation Summary Buffer Memory
          • DynamoDB Chat Memory
          • MongoDB Atlas Chat Memory
          • Redis-Backed Chat Memory
          • Upstash Redis-Backed Chat Memory
        • Moderation
          • OpenAI Moderation
          • Simple Prompt Moderation
        • Output Parsers
          • CSV Output Parser
          • Custom List Output Parser
          • Structured Output Parser
          • Advanced Structured Output Parser
        • Prompts
          • Chat Prompt Template
          • Few Shot Prompt Template
          • Prompt Template
        • Record Managers
        • Retrievers
          • Cohere Rerank Retriever
          • Embeddings Filter Retriever
          • HyDE Retriever
          • LLM Filter Retriever
          • Multi Query Retriever
          • Prompt Retriever
          • Reciprocal Rank Fusion Retriever
          • Similarity Score Threshold Retriever
          • Vector Store Retriever
          • Voyage AI Rerank Retriever
        • Text Splitters
          • Character Text Splitter
          • Code Text Splitter
          • Html-To-Markdown Text Splitter
          • Markdown Text Splitter
          • Recursive Character Text Splitter
          • Token Text Splitter
        • Tools
          • BraveSearch API
          • Calculator
          • Chain Tool
          • Chatflow Tool
          • Custom Tool
          • Exa Search
          • Google Custom Search
          • OpenAPI Toolkit
          • Python Interpreter
          • Read File
          • Request Get
          • Request Post
          • Retriever Tool
          • SearchApi
          • SearXNG
          • Serp API
          • Serper
          • Web Browser
          • Write File
        • Vector Stores
          • AstraDB
          • Chroma
          • Elastic
          • Faiss
          • In-Memory Vector Store
          • Milvus
          • MongoDB Atlas
          • OpenSearch
          • Pinecone
          • Postgres
          • Qdrant
          • Redis
          • SingleStore
          • Supabase
          • Upstash Vector
          • Vectara
          • Weaviate
          • Zep Collection - Open Source
          • Zep Collection - Cloud
      • LlamaIndex
        • Agents
          • OpenAI Tool Agent
          • Anthropic Tool Agent
        • Chat Models
          • AzureChatOpenAI
          • ChatAnthropic
          • ChatMistral
          • ChatOllama
          • ChatOpenAI
          • ChatTogetherAI
          • ChatGroq
        • Embeddings
          • Azure OpenAI Embeddings
          • OpenAI Embedding
        • Engine
          • Query Engine
          • Simple Chat Engine
          • Context Chat Engine
          • Sub-Question Query Engine
        • Response Synthesizer
          • Refine
          • Compact And Refine
          • Simple Response Builder
          • Tree Summarize
        • Tools
          • Query Engine Tool
        • Vector Stores
          • Pinecone
          • SimpleStore
    • Agentflows
      • Multi-Agents (Supervisor/Worker)
      • Sequential Agents
    • API
      • Chatflows and APIs
    • Document Stores
    • Embed
      • Rate Limit
    • API Streaming
    • Analytics
    • Credentials
      • Amazon Bedrock Credential Setup
      • IBM Watsonx.AI Credential Setup
    • Variables
    • Utilities
      • Custom JS Function
      • Set/Get Variable
      • If Else
      • Sticky Note
    • Example Flows
      • Calling Children Flows
      • Calling Webhook
      • Interacting with API
      • Multiple Documents QnA
      • SQL QnA
      • Upserting Data
      • Web Scrape QnA
    • Monitoring & Auditing
      • Configuring Monitoring and Traces
    • Tailwinds Security and Deployment
  • Release Notes
    • 12/17/2024 - v2.2.1
    • 10/11/2024 - v2.1.2
    • 9/27/2024- v2.1
    • 8/16/2024 - v2.0.5
  • Demos and Use-cases
    • Create a Basic Chatbot
    • Build an AI-Powered Translator
    • Create research-powered call scripts
    • Extract information from Medical Documents
    • Identify ICD10 medical codes
  • GenAI University
    • Syllabus
    • 101-Prompt Engineering
    • 101-System Prompts
    • 101-Human (User) Prompts
    • 101-Context Window
    • 101-Prompt Chains
    • 201-Documents and Vector Databases (RAG)
    • 301-AI Agents
    • 301-Agent Tools
    • 401-Multi-Agent
Powered by GitBook
On this page
  • Problem
  • Tutorial
  • Create Tools
  • Create Chatflow
  • Additional
  • Pass Session ID to Function
  • Pass variables to Function
  • Override Custom Tool

Was this helpful?

  1. Welcome to Tailwinds
  2. Chatflows
  3. LangChain
  4. Tools

Custom Tool

PreviousChatflow ToolNextExa Search

Last updated 9 months ago

Was this helpful?

Problem

Function usually takes in structured input data. Let's say you want the LLM to be able to call Airtable Create Record , the body parameters has to be structured in a specific way. For example:

"records": [
  {
    "fields": {
      "Address": "some address",
      "Name": "some name",
      "Visited": true
    }
  }
]

Ideally, we want LLM to return a proper structured data like this:

{
  "Address": "some address",
  "Name": "some name",
  "Visited": true
}

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 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.

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.

    • Return the result at the end.

The final code should be:

const fetch = require('node-fetch');
const url = 'https://morning-star.p.rapidapi.com/market/v2/get-movers';
const options = {
	method: 'GET',
	headers: {
		'X-RapidAPI-Key': 'replace with your api key',
		'X-RapidAPI-Host': 'morning-star.p.rapidapi.com'
	}
};

try {
	const response = await fetch(url, options);
	const result = await response.text();
	console.log(result);
	return result;
} catch (error) {
	console.error(error);
	return '';
}

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:

{ "performanceId": "SOME TICKER" }
    • 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:

const fetch = require('node-fetch');
const url = 'https://morning-star.p.rapidapi.com/news/list?performanceId=' + $performanceId;
const options = {
	method: 'GET',
	headers: {
		'X-RapidAPI-Key': 'replace with your api key',
		'X-RapidAPI-Host': 'morning-star.p.rapidapi.com'
	}
};

try {
	const response = await fetch(url, options);
	const result = await response.text();
	console.log(result);
	return result;
} catch (error) {
	console.error(error);
	return '';
}

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:

{ "stock": "SOME TICKER", "move": "20%", "news_summary": "Some summary" }

Final code should looks like below. Note how we pass in $stock, $move and $news_summary as variables:

const fetch = require('node-fetch');
const baseId = 'your-base-id';
const tableId = 'your-table-id';
const token = 'your-token';

const body = {
	"records": [
		{
			"fields": {
				"stock": $stock,
				"move": $move,
				"news_summary": $news_summary,
			}
		}
	]
};

const options = {
	method: 'POST',
	headers: {
		'Authorization': `Bearer ${token}`,
		'Content-Type': 'application/json'
	},
	body: JSON.stringify(body)
};

const url = `https://api.airtable.com/v0/${baseId}/${tableId}`

try {
	const response = await fetch(url, options);
	const text = await response.text();
	return text;
} catch (error) {
	console.error(error);
	return '';
}

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:

  1. 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.

  2. 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.

Additional

Pass Session ID to Function

By default, Function in custom tool has access to the following flow configurations:

$flow.sessionId 
$flow.chatId
$flow.chatflowId
$flow.input

Below is an example of sending the sessionId to Discord webhook:

const fetch = require('node-fetch');
const webhookUrl = "https://discord.com/api/webhooks/1124783587267";
const content = $content; // captured from input schema
const sessionId = $flow.sessionId;

const body = {
	"content": `${mycontent} and the sessionid is ${sessionId}`
};

const options = {
	method: 'POST',
	headers: {
		'Content-Type': 'application/json'
	},
	body: JSON.stringify(body)
};

const url = `${webhookUrl}?wait=true`

try {
	const response = await fetch(url, options);
	const text = await response.text();
	return text;
} catch (error) {
	console.error(error);
	return '';
}

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:

$vars.<variable-name>

Example of how to pass variables in Tailwinds using API and Embedded:

async function query(data) {
    const response = await fetch(
        "http://localhost:3000/api/v1/prediction/<chatflow-id>",
        {
            method: "POST",
            headers: {
                "Content-Type": "application/json"
            },
            body: JSON.stringify(data)
        }
    );
    const result = await response.json();
    return result;
}

query({
    "question": "Hey, how are you?",
    "overrideConfig": {
        "vars": {
            "apiKey": "abc"
        }
    }
}).then((response) => {
    console.log(response);
});
<script type="module">
    import Chatbot from 'https://cdn.jsdelivr.net/npm/flowise-embed/dist/web.js';
    Chatbot.init({
        chatflowid: 'chatflow-id',
        apiHost: 'http://localhost:3000',
        chatflowConfig: {
          vars: {
            apiKey: 'def'
          }
        }
    });
</script>

Example of how to receive the variables in custom tool:

const fetch = require('node-fetch');
const webhookUrl = "https://discord.com/api/webhooks/1124783587267";
const content = $content; // captured from input schema
const sessionId = $flow.sessionId;
const apiKey = $vars.apiKey;

const body = {
	"content": `${mycontent} and the sessionid is ${sessionId}`
};

const options = {
	method: 'POST',
	headers: {
		'Content-Type': 'application/json',
		'Authorization': `Bearer ${apiKey}`
	},
	body: JSON.stringify(body)
};

const url = `${webhookUrl}?wait=true`

try {
	const response = await fetch(url, options);
	const text = await response.text();
	return text;
} catch (error) {
	console.error(error);
	return '';
}

Override Custom Tool

Parameters below can be overriden

Parameter
Description

customToolName

tool name

customToolDesc

tool description

customToolSchema

tool schema

customToolFunc

tool function

Example of an API call to override custom tool parameters:

async function query(data) {
    const response = await fetch(
        "http://localhost:3000/api/v1/prediction/<chatflow-id>",
        {
            method: "POST",
            headers: {
                "Content-Type": "application/json"
            },
            body: JSON.stringify(data)
        }
    );
    const result = await response.json();
    return result;
}

query({
    "question": "Hey, how are you?",
    "overrideConfig": {
        "customToolName": "example_tool",
        "customToolSchema": "z.object({title: z.string()})"
    }
}).then((response) => {
    console.log(response);
});

Let's get started

JavaScript Function: We are going to use /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 and .

JavaScript Function: We are going to use /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 and .

JavaScript Function: We are going to use to create a new record to an existing table. You can find the tableId and baseId from . You'll also need to create a personal access token, find how to do it .

Voila! That's how you can create your own custom tool and use it with the OpenAI Function Agent!

API
OpenAI Function Calling
🚀
Morning Star
modules
external libraries
Morning Star
modules
external libraries
Airtable Create Record API
here
here
🎉
🎉