graph LR A([Start]) --> B[User Query] B --> C[AI Receives Query] C --> D{Check Prompt & Functions} D -->|No Function| E[Direct Response] D -->|Need Function| F[Function Handling] subgraph Function Calls F -.-> G[Function 1] F -.-> H[Function 2] F -.-> I[Function 3] F -.-> J[Function N] end G & H & I & J -.-> K[Collect Results] K --> L[Generate Response] E --> L L --> M[Send to User] M --> N([End]) %% Layout adjustments A --- C J --- N classDef default fill:#f0f4f8,stroke:#506480,stroke-width:2px; classDef process fill:#d0e3ff,stroke:#3474eb,stroke-width:2px; classDef decision fill:#ffeeba,stroke:#d39e00,stroke-width:2px; classDef start_end fill:#d4edda,stroke:#28a745,stroke-width:2px; classDef function fill:#e8d8ff,stroke:#6f42c1,stroke-width:2px; class A,N start_end; class B,C,E,K,L,M process; class D decision; class F,G,H,I,J function;
Functions act as tools or micro-agents that perform specific tasks. They are autonomous and can be guided using your base prompt.
Benefits
-
Chained Execution: AI can execute a chain of functions to answer complex queries. For example, for the query โweather of India & current time,โ the AI would first call a function to get the weather in India and then another function to get the current time in India.
-
Easy Setup: Functions are simple to set up. You only need to enable the function and, if needed, guide when the function should execute by defining it in your base prompt settings. For example, โUse Websearch function to answer any stock price-related questions and do not use web search for any other purpose.โ
Cons
-
Sequence Limitations: Long sequences that require input across multiple messages may not work effectively.
-
Customization Constraints: There are limitations on how much you can customize the functions.
System Functions
-
Web Search: This function performs web searches to fetch real-time information from the web, such as the latest news, weather updates, or stock prices.
-
Change Language: This function changes the language of the session, and maintains the context of the conversation in the new language.
-
Unable To Answer Hook: This function triggers when the AI cannot provide an answer, helping to handle cases where more information is needed or suggesting alternative actions.
-
Get Current Datetime: This function provides the current date and time, useful for answering questions based on time.
Adding an API Function
In addition to the pre-configured system functions, you can also create custom API functions to extend the functionality of your AI. API functions allow you to call external APIs and integrate their data or services into your AI responses.
Step-by-step guide on how to add an API function:
-
Name: Enter a descriptive name for your API function.
-
Description: Provide a brief description of what the function does.
-
API Endpoint: Specify the HTTP method (e.g., GET, POST) and the endpoint URL for the API you want to call.
-
Headers: Add any necessary headers required by the API. Click the green plus button to add more headers if needed.
-
Parameters (JSON): Define the JSON schema for the parameters your API function requires. This typically includes specifying the type and properties of the parameters. For example:
-
Add API Function: Once all the necessary details are filled in, click the โAdd API Functionโ button to save and enable the function.
Example
If you want to add a function that retrieves weather information based on geographic coordinates, you might set it up as follows:
-
Name: Get Weather
-
Description: Retrieves the current weather information based on longitude and latitude.
-
API Endpoint:
GET
https://api.weather.com/v3/wx/conditions/current
-
Headers:
Authorization: Bearer YOUR_API_KEY
-
Parameters (JSON):
3. Function Usage:
-
Once the function is added, your chatbot can access the weather API and retrieve relevant weather information based on the userโs query.
-
Users can trigger the function by using a specific keyword or phrase like โwhatโs the weather like in [location]โ. The chatbot will then use the location to determine the latitude and longitude, which are passed as parameters to the function.
Letโs say you want to create a function to find the cheapest flight for a given destination from a specific origin.
1. Function Setup:
- Name:
cheapest_flight_api
- Description:
API to find the cheapest flight from origin to destination
- API Endpoint:
https://your_flight_api.com/cheapest
2. Parameters Definition:
-
The function accepts two parameters,
origin
anddestination
, both of which arestrings
representing the departure and arrival airports respectively. -
Parameters (JSON):
3. Function Usage:
-
Once the function is added, your chatbot can access the flight search API and retrieve relevant results based on the userโs query.
-
The chatbot can execute the function when a user inputs a phrase like โfind cheapest flights from [origin] to [destination]โ, The chatbot can then display the cheapest flight options to the user, including details such as flight numbers, departure and arrival times, and prices.