Unlocking Text Generation with Yi-6B Cognitive Actions

24 Apr 2025
Unlocking Text Generation with Yi-6B Cognitive Actions

In the world of AI, the ability to generate coherent and contextually relevant text is invaluable. The Yi-6B model, developed by 01.AI, provides developers with a powerful tool for text generation through its various Cognitive Actions. This article will guide you through leveraging the Generate Text with Yi-6B action, explaining its functionalities, input/output requirements, and providing a conceptual example to help you integrate it into your applications seamlessly.

Prerequisites

Before diving into the implementation, ensure you have the following:

  • An API key for the Cognitive Actions platform.
  • Basic familiarity with JSON payload structures.
  • A Python environment with the requests library installed for making API calls.

Authentication typically involves passing your API key in the request headers, allowing you to access the Cognitive Actions service.

Cognitive Actions Overview

Generate Text with Yi-6B

The Generate Text with Yi-6B action utilizes the Yi-6B model to generate text based on a prompt you provide. It offers control over various parameters such as output randomness, token limits, and redundancy reduction, making it a versatile choice for developers looking to create dynamic text outputs.

Input

The input for this action follows a specific schema, which includes several configurable parameters:

  • prompt (required): The main input text for which the model generates a response.
  • topK (optional): An integer that specifies the number of highest probability tokens to consider. Default is 50.
  • topP (optional): A probability threshold for token selection, defaulting to 0.8.
  • temperature (optional): Adjusts the randomness of the output, with a default value of 0.3.
  • maxNewTokens (optional): The maximum number of tokens to generate, defaulting to 1024.
  • promptTemplate (optional): A template for formatting the prompt, defaulting to a system-user-assistant structure.
  • repetitionPenalty (optional): A penalty to reduce redundancy in the output, defaulting to 1.2.
Example Input

Here's an example JSON payload to invoke the action:

{
  "topK": 50,
  "topP": 0.8,
  "prompt": "Write a script to download the images for the top 10 posts of all time from /r/pics using the PRAW library",
  "temperature": 0.3,
  "maxNewTokens": 1024,
  "promptTemplate": "<|im_start|>system\nYou are a helpful assistant<|im_end|>\n<|im_start|>user\n{prompt}<|im_end|>\n<|im_start|>assistant\n",
  "repetitionPenalty": 1.2
}

Output

The output from this action typically consists of a text array where each element represents a token generated by the model. The output will vary based on the prompt and parameters provided.

Example Output

For example, the output might look like this:

[
  "",
  "",
  "Certainly! ",
  "Below ",
  "is ",
  "a ",
  "Python ",
  "script ",
  "that ",
  "uses ",
  "`praw` ",
  "to ",
  "fetch ",
  "and ",
  "save ",
  "the ",
  "image ",
  "files ",
  "for ",
  "the ",
  "top ",
  "10 ",
  "posts ",
  "on ",
  "`/r/pics` ",
  "subreddit."
]

Conceptual Usage Example (Python)

Here’s a conceptual example of how you might call the Yi-6B action in 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 = "c4f8f3bb-7a9c-4fcb-a870-90c15e5b81e6"  # Action ID for Generate Text with Yi-6B

# Construct the input payload based on the action's requirements
payload = {
    "topK": 50,
    "topP": 0.8,
    "prompt": "Write a script to download the images for the top 10 posts of all time from /r/pics using the PRAW library",
    "temperature": 0.3,
    "maxNewTokens": 1024,
    "promptTemplate": "<|im_start|>system\nYou are a helpful assistant<|im_end|>\n<|im_start|>user\n{prompt}<|im_end|>\n<|im_start|>assistant\n",
    "repetitionPenalty": 1.2
}

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}
    )
    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 the code snippet above, replace the placeholder values for the API key and endpoint with your actual credentials. The action ID corresponds to the Generate Text with Yi-6B function. The input payload is structured according to the action's schema.

Conclusion

The Generate Text with Yi-6B action opens up new possibilities for creating dynamic content in your applications. By understanding its parameters and how to structure your requests, you can easily integrate this powerful text generation capability into your projects. Consider experimenting with different prompts and settings to explore the full potential of the Yi-6B model!