Optimize Function Calls with Nexusraven V2 13b

26 Apr 2025
Optimize Function Calls with Nexusraven V2 13b

In today’s fast-paced development environment, efficiency and performance are paramount. The Nexusraven V2 13b offers a powerful set of Cognitive Actions designed to streamline and optimize function calls, enhancing the way developers interact with APIs. One of the standout actions is the Execute Function Call Optimization, which leverages the capabilities of the NexusRaven V2-13B model to improve the speed and reliability of data fetching operations, such as retrieving weather information or geolocation data.

By using this action, developers can simplify their code and reduce the complexity involved in making multiple API calls. Whether you're building a weather app, a location-based service, or any application that relies on real-time data, this action can significantly enhance your workflow.

Prerequisites

To get started, you will need an API key for the Cognitive Actions service and a basic understanding of how to make API calls.

Execute Function Call Optimization

The Execute Function Call Optimization action is designed to enhance the performance of function calls by providing structured interactions for fetching data. This action addresses the common challenge of optimizing API calls, allowing developers to execute multiple functions efficiently.

Input Requirements

The input for this action is structured as a JSON object. It includes:

  • prompt: A string containing the functions to be executed and any user queries that need to be processed. For example, you might include functions that fetch weather data or geolocation based on city names.
  • thought: A boolean that indicates whether to return the language model's reasoning during function execution. The default is false.
  • maxTokens: An integer specifying the maximum number of tokens for the response, with a default of 1024.
  • temperature: A float that controls the randomness of the response, allowing for more or less variability based on the set value.

Example input might look like this:

{
  "prompt": "Function:\ndef get_weather_data(coordinates):\n    \"\"\"\n    Fetches weather data from the Open-Meteo API for the given latitude and longitude.\n    Args:\n    coordinates (tuple): The latitude of the location.\n    Returns:\n    float: The current temperature in the coordinates you've asked for\n    \"\"\"\nFunction:\ndef get_coordinates_from_city(city_name):\n    \"\"\"\n    Fetches the latitude and longitude of a given city name using the Maps.co Geocoding API.\n    Args:\n    city_name (str): The name of the city.\n    Returns:\n    tuple: The latitude and longitude of the city.\n    \"\"\"\n\"Setting: Allowed to issue multiple calls with semicolon\\n\"\nUser Query: \"What's the weather like in Seattle right now?<human_end>\n"
}

Expected Output

The output will be a structured response indicating the function calls that were executed. For instance, a typical output could be:

Call: get_weather_data(coordinates=get_coordinates_from_city(city_name='Seattle'))

Use Cases for this Action

  • Weather Applications: Quickly fetch current weather conditions based on user-provided locations without writing complex code.
  • Location Services: Efficiently retrieve geographic coordinates for cities, making it easier to build applications that rely on location data.
  • Data Aggregation: Streamline multiple API calls into a single, optimized function execution, reducing latency and improving user experience.
import requests
import json

# Replace with your actual Cognitive Actions API key and endpoint
# Ensure your environment securely handles the API key
COGNITIVE_ACTIONS_API_KEY = "YOUR_COGNITIVE_ACTIONS_API_KEY"
# This endpoint URL is hypothetical and should be documented for users
COGNITIVE_ACTIONS_EXECUTE_URL = "https://api.cognitiveactions.com/actions/execute"

action_id = "313f0592-f8c3-4691-88dc-8287fd342ce4" # Action ID for: Execute Function Call Optimization

# Construct the exact input payload based on the action's requirements
# This example uses the predefined example_input for this action:
payload = {
  "prompt": "Function:\ndef get_weather_data(coordinates):\n    \"\"\"\n    Fetches weather data from the Open-Meteo API for the given latitude and longitude.\n\n    Args:\n    coordinates (tuple): The latitude of the location.\n\n    Returns:\n    float: The current temperature in the coordinates you've asked for\n    \"\"\"\n\nFunction:\ndef get_coordinates_from_city(city_name):\n    \"\"\"\n    Fetches the latitude and longitude of a given city name using the Maps.co Geocoding API.\n\n    Args:\n    city_name (str): The name of the city.\n\n    Returns:\n    tuple: The latitude and longitude of the city.\n    \"\"\"\n\n\"Setting: Allowed to issue multiple calls with semicolon\\n\"\n\nUser Query: \"What's the weather like in Seattle right now?<human_end>\n\n"
}

headers = {
    "Authorization": f"Bearer {COGNITIVE_ACTIONS_API_KEY}",
    "Content-Type": "application/json",
    # Add any other required headers for the Cognitive Actions API
}

# Prepare the request body for the hypothetical execution endpoint
request_body = {
    "action_id": action_id,
    "inputs": payload
}

print(f"--- Calling Cognitive Action: {action.name or action_id} ---")
print(f"Endpoint: {COGNITIVE_ACTIONS_EXECUTE_URL}")
print(f"Action ID: {action_id}")
print("Payload being sent:")
print(json.dumps(request_body, indent=2))
print("------------------------------------------------")

try:
    response = requests.post(
        COGNITIVE_ACTIONS_EXECUTE_URL,
        headers=headers,
        json=request_body
    )
    response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)

    result = response.json()
    print("Action executed successfully. Result:")
    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 (non-JSON): {e.response.text}")
    print("------------------------------------------------")

Conclusion

The Execute Function Call Optimization action within the Nexusraven V2 13b service is an invaluable tool for developers looking to enhance their applications' efficiency and performance. By optimizing API calls and simplifying complex interactions, this action enables you to focus on building robust applications without getting bogged down in the intricacies of function handling.

As you explore the capabilities of this action, consider how it can be integrated into your existing projects or workflows to streamline data retrieval and enhance user experience. Start optimizing your function calls today!