Integrating Intelligent Conversations with lucataco/glaive-function-calling-v1 Actions

The lucataco/glaive-function-calling-v1 API provides developers with a powerful toolset to create engaging and intelligent multi-turn conversations within their applications. Leveraging Glaive’s synthetic data platform, developers can execute functions based on conversational context, similar to advanced models like GPT-3.5 and GPT-4. By using these pre-built Cognitive Actions, you can enhance your applications with capabilities such as text generation, user assistance, and function execution, all while maintaining control over the randomness and diversity of the generated responses.
Prerequisites
Before diving into the integration of these Cognitive Actions, ensure you have the following:
- An API key for the Cognitive Actions platform.
- Basic understanding of JSON structure for input and output.
- Familiarity with making HTTP requests in your programming environment.
To authenticate your requests, you will need to pass your API key in the request headers. This typically involves adding an Authorization header with a Bearer token format.
Cognitive Actions Overview
Execute Function Using Glaive-Function-Calling-V1
Description:
This action utilizes the glaive-function-calling-v1 model to initiate multi-turn conversations and intelligently execute functions based on user inputs. It allows for customizable text generation with parameters like temperature and token limit.
Category:
Text Generation
Input
The input for this action is a JSON object that includes the following fields:
- topP (number): The probability threshold for sampling the next word in text generation. Ranges from 0 (less diverse) to 1 (more diverse). Default is
0.95. - prompt (string): The user's input prompt, guiding the AI's response. Example:
"I am thinking of having a 10 day long vacation in Greece, can you help me plan it?" - newTokens (integer): Maximum number of new tokens the model can generate in response, ranging from
0to1024. Default is128. - temperature (number): Controls the randomness of outputs. A value of
0means deterministic, while values closer to1increase randomness. Default is0.5. - systemPrompt (string): Provides system-level context to the AI, detailing available functions to assist user requests.
Example Input:
{
"topP": 0.95,
"prompt": "I am thinking of having a 10 day long vacation in Greece, can you help me plan it?",
"newTokens": 128,
"temperature": 0.5,
"systemPrompt": "You are an helpful assistant who has access to the following functions to help the user, you can use the functions if needed- \n{\n\t\"name\": \"plan_holiday\",\n\t\"description\": \"Plan a holiday based on user's interests\",\n\t\"parameters\": {\n\t\t\"type\": \"object\",\n\t\t\"properties\": {\n\t\t\t\"destination\": {\n\t\t\t\t\"type\": \"string\",\n\t\t\t\t\"description\": \"The destination of the holiday\"\n\t\t\t},\n\t\t\t\"duration\": {\n\t\t\t\t\"type\": \"integer\",\n\t\t\t\t\"description\": \"The duration of the trip in holiday\"\n\t\t\t}\n\t\t},\n\t\t\"required\": [\n\t\t\t\"destination\",\n\t\t\t\"duration\"\n\t\t]\n\t}\n}"
}
Output
Typically, the action returns a structured response indicating the execution result, including the conversation context and any relevant function calls. An example output might look like this:
SYSTEM: You are an helpful assistant who has access to the following functions to help the user, you can use the functions if needed-
{
"name": "plan_holiday",
"description": "Plan a holiday based on user's interests",
"parameters": {
"type": "object",
"properties": {
"destination": {
"type": "string",
"description": "The destination of the holiday"
},
"duration": {
"type": "integer",
"description": "The duration of the trip in holiday"
}
},
"required": [
"destination",
"duration"
]
}
}. USER: I am thinking of having a 10 day long vacation in Greece, can you help me plan it?
ASSISTANT: <functioncall> {"name": "plan_holiday", "arguments": '{\n "destination": "Greece",\n "duration": 10\n}'}
FUNCTION RESPONSE: {"status": "success", "message": "Your vacation in Greece is planned for 10 days."}
ASSISTANT: Your vacation in Greece is planned for 10 days.
USER: That sounds great! Thank you for your help.
ASSISTANT: You're welcome! If you need any more
Conceptual Usage Example (Python)
Here’s a conceptual Python code snippet demonstrating how to call this action using the hypothetical Cognitive Actions execution endpoint:
import requests
import json
# Replace with your Cognitive Actions API key and endpoint
COGNITIVE_ACTIONS_API_KEY = "YOUR_COGNITIVE_ACTIONS_API_KEY"
COGNITIVE_ACTIONS_EXECUTE_URL = "https://api.cognitiveactions.com/actions/execute" # Hypothetical endpoint
action_id = "451c5f61-4b6d-4ab0-bd8c-0043351fcf0b" # Action ID for Execute Function Using Glaive-Function-Calling-V1
# Construct the input payload based on the action's requirements
payload = {
"topP": 0.95,
"prompt": "I am thinking of having a 10 day long vacation in Greece, can you help me plan it?",
"newTokens": 128,
"temperature": 0.5,
"systemPrompt": "You are an helpful assistant who has access to the following functions to help the user, you can use the functions if needed- \n{\n\t\"name\": \"plan_holiday\",\n\t\"description\": \"Plan a holiday based on user's interests\",\n\t\"parameters\": {\n\t\t\"type\": \"object\",\n\t\t\"properties\": {\n\t\t\t\"destination\": {\n\t\t\t\t\"type\": \"string\",\n\t\t\t\t\"description\": \"The destination of the holiday\"\n\t\t\t},\n\t\t\t\"duration\": {\n\t\t\t\t\"type\": \"integer\",\n\t\t\t\t\"description\": \"The duration of the trip in holiday\"\n\t\t\t}\n\t\t},\n\t\t\"required\": [\n\t\t\t\"destination\",\n\t\t\t\"duration\"\n\t\t]\n\t}\n}"
}
headers = {
"Authorization": f"Bearer {COGNITIVE_ACTIONS_API_KEY}",
"Content-Type": "application/json"
}
try:
response = requests.post(
COGNITIVE_ACTIONS_EXECUTE_URL,
headers=headers,
json={"action_id": action_id, "inputs": payload} # Hypothetical structure
)
response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)
result = response.json()
print("Action executed successfully:")
print(json.dumps(result, indent=2))
except requests.exceptions.RequestException as e:
print(f"Error executing action {action_id}: {e}")
if e.response is not None:
print(f"Response status: {e.response.status_code}")
try:
print(f"Response body: {e.response.json()}")
except json.JSONDecodeError:
print(f"Response body: {e.response.text}")
In this example, replace the COGNITIVE_ACTIONS_API_KEY and Cognitive Actions API endpoint with your actual credentials. The action ID corresponds to the specific action you want to execute. The payload is constructed according to the required input schema, and the request is sent to the hypothetical endpoint.
Conclusion
By integrating the lucataco/glaive-function-calling-v1 actions into your applications, you can create engaging user experiences that intelligently respond to conversational inputs. The flexibility in configuring parameters such as temperature and token limits allows you to tailor the interaction style to match your application's needs. As a next step, consider implementing additional functions that can further enhance user interactions, or explore other Cognitive Actions to expand your application's capabilities.