Generate Engaging Text with the Yi Series Cognitive Actions

In the world of AI-driven applications, the "01-ai/yi-34b-200k" spec brings a powerful toolset for developers looking to leverage advanced text generation capabilities. This specification includes a unique Cognitive Action designed to utilize the Yi Series large language models from 01.AI, enabling you to generate coherent and contextually relevant text based on user-defined prompts. By integrating these pre-built actions into your applications, you can enhance user engagement, automate content creation, and provide dynamic responses tailored to specific inputs.
Prerequisites
Before you dive into using the Cognitive Actions, ensure you have the necessary prerequisites in place:
- An API key for the Cognitive Actions platform.
- Basic understanding of making HTTP requests and handling JSON data.
For authentication, you'll typically pass your API key in the headers of your requests, allowing you to securely access the Cognitive Actions endpoints.
Cognitive Actions Overview
Generate Text with Yi Series Model
The "Generate Text with Yi Series Model" action is a powerful text generation tool that allows you to create text based on a given prompt. This action offers customizable parameters such as top-k, top-p, temperature, and penalties to fine-tune the output quality.
- Category: Text Generation
- Purpose: Generate text with customizable parameters based on a user-defined prompt.
Input
The input for this action is structured as a JSON object. Here's a breakdown of the required and optional fields:
- Required:
prompt: The initial text input you want the model to expand upon.
- Optional:
topK: (integer) The number of top tokens to consider for generating the output, defaulting to 50.topP: (number) A probability threshold for token selection, defaulting to 0.95.temperature: (number) Controls the randomness of output, with a default of 0.8.maxNewTokens: (integer) The maximum number of tokens to generate, defaulting to 512.promptTemplate: (string) A template for formatting the prompt, defaulting to{prompt}.presencePenalty: (number) A penalty for introducing new concepts, defaulting to 0.frequencyPenalty: (number) A penalty for token frequency, defaulting to 0.
Example Input:
{
"topK": 50,
"topP": 0.95,
"prompt": "Here is the itinerary for my dog's birthday party:\n",
"temperature": 0.8,
"maxNewTokens": 512,
"promptTemplate": "{prompt}",
"presencePenalty": 0,
"frequencyPenalty": 0
}
Output
The output of the action typically consists of a JSON array of tokens representing the generated text. Each token corresponds to a part of the generated response based on the input prompt.
Example Output:
[
"\n",
1,
0,
":",
0,
0,
" AM",
" -",
" Guests",
" arrive",
...
"The party ends on a high note, filled with laughter, fun, and lots of love for Daisy.",
""
]
Conceptual Usage Example (Python)
Here’s a conceptual example of how you might invoke the "Generate Text with Yi Series Model" 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 = "04d83e65-7420-4032-818b-f460627bf615" # Action ID for Generate Text with Yi Series Model
# Construct the input payload based on the action's requirements
payload = {
"topK": 50,
"topP": 0.95,
"prompt": "Here is the itinerary for my dog's birthday party:\n",
"temperature": 0.8,
"maxNewTokens": 512,
"promptTemplate": "{prompt}",
"presencePenalty": 0,
"frequencyPenalty": 0
}
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 the above code snippet, replace YOUR_COGNITIVE_ACTIONS_API_KEY with your actual API key. The payload is structured based on the action's input schema, allowing you to specify how you want the text to be generated.
Conclusion
The "Generate Text with Yi Series Model" action offers developers a robust way to integrate advanced text generation capabilities into their applications. By customizing parameters like temperature and token selection, you can generate high-quality, contextually relevant text outputs tailored to user needs. As you explore this action further, consider experimenting with different prompts and configurations to discover the full potential of the Yi Series models in your projects. Happy coding!