Harnessing Text Generation with the halevi/sandbox1 Cognitive Actions

22 Apr 2025
Harnessing Text Generation with the halevi/sandbox1 Cognitive Actions

In the evolving landscape of AI-driven applications, the integration of text generation capabilities can significantly enhance user experience and automate content creation. The halevi/sandbox1 Cognitive Actions provide developers with robust tools to generate text outputs through configurable parameters, allowing for tailored and dynamic interactions. These pre-built actions streamline the process, enabling quick and efficient integration into your applications.

Prerequisites

Before diving into the Cognitive Actions, ensure you have the following:

  • An API key for the Cognitive Actions platform to authenticate your requests.
  • Basic familiarity with making HTTP requests and handling JSON data.

Authentication typically involves passing the API key in the request headers, which is essential for securely accessing the Cognitive Actions.

Cognitive Actions Overview

Generate Text with Seed Control

The Generate Text with Seed Control action allows developers to create text outputs using a variety of configurable parameters. This action supports detailed debugging and fine-tuned control over randomness and predictability, making it ideal for applications requiring tailored text generation.

Input

The input for this action follows a structured schema. Here are the key fields:

  • prompt (required): The initial input prompt for generating responses.
  • seed (optional): A random seed to control the generation randomness. Leave blank for a random seed.
  • topK (optional): Limits the sampling to the top K most probable tokens.
  • topP (optional): Samples from the top P percentage of probable tokens.
  • debug (optional): Enables debug output in logs when set to true.
  • temperature (optional): Controls the randomness of the output, with higher values leading to more varied results.
  • maxNewTokens (optional): Defines the maximum number of tokens to generate.
  • minNewTokens (optional): Sets the minimum number of tokens to generate, or -1 to disable.
  • stopPatterns (optional): A list of sequences where generation should halt.
  • systemPrompt (optional): A guiding prompt that shapes the model's responses.
  • fineTunedWeights (optional): Path to fine-tuned model weights.

Example Input:

{
  "topK": 50,
  "topP": 0.9,
  "debug": false,
  "prompt": "hi",
  "temperature": 0.75,
  "maxNewTokens": 128,
  "minNewTokens": -1,
  "systemPrompt": "You are a Spanish-speaking companion to enhance my language skills in a fun and engaging way. You should act as a new friend eager to chat, with the ability to subtly gauge and adapt to my Spanish level. Never use English, except when you use a difficult Spanish word, then add an English translation in brackets. Always use short replies, and try to keep the conversation going."
}

Output

The output will typically consist of a generated text response based on the provided prompt and other parameters. For instance:

Example Output:

¡Hola! ¿Cómo estás? (Hi! How are you?) I'm so glad we can practice speaking Spanish together. What do you like to do for fun? Do you have any pets?

Conceptual Usage Example (Python)

Here’s how you might structure a call to this action using Python:

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 = "9db21c5a-d911-43ee-aae6-9b736a04c461"  # Action ID for Generate Text with Seed Control

# Construct the input payload based on the action's requirements
payload = {
    "topK": 50,
    "topP": 0.9,
    "debug": False,
    "prompt": "hi",
    "temperature": 0.75,
    "maxNewTokens": 128,
    "minNewTokens": -1,
    "systemPrompt": "You are a Spanish-speaking companion to enhance my language skills in a fun and engaging way. You should act as a new friend eager to chat, with the ability to subtly gauge and adapt to my Spanish level. Never use English, except when you use a difficult Spanish word, then add an English translation in brackets. Always use short replies, and try to keep the conversation going."
}

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, you’ll notice that the action ID and the input payload are clearly structured to match the requirements of the Cognitive Action. This code snippet serves as a conceptual guide to help you understand how to interact with the Cognitive Actions API.

Conclusion

The halevi/sandbox1 Cognitive Actions provide a powerful means to integrate sophisticated text generation capabilities into your applications. By utilizing the Generate Text with Seed Control action, developers can create personalized and contextually relevant text outputs that enhance user interactions. As you explore these actions, consider how they can be utilized in various scenarios, such as chatbots, content creation, or educational tools. Happy coding!