Generate Efficient Code with Evolved Seeker's Cognitive Actions

Cognitive Actions from the tomasmcm/evolved-seeker-1.3b spec empower developers to harness the capabilities of advanced machine learning models for tasks like code generation. With the EvolvedSeeker action, you can automate the process of generating and evaluating code, significantly enhancing productivity while utilizing minimal resources. This action is particularly beneficial for curating code datasets efficiently and effectively.
Prerequisites
Before diving into using the Cognitive Actions, ensure you have the following:
- An API key for the Cognitive Actions platform.
- Basic knowledge of making HTTP requests and handling JSON data in your applications.
For authentication, you will typically include your API key in the request headers. This provides secure access to the Cognitive Actions.
Cognitive Actions Overview
Generate Code with EvolvedSeeker
The Generate Code with EvolvedSeeker action leverages the evolvedSeeker_1_3 model to generate high-quality code based on given prompts. This action is categorized under code-generation and is designed for efficiency, utilizing a fine-tuned model that can handle various coding tasks.
Input
The action requires a structured input object. Here’s a breakdown of the input schema:
- prompt: (string, required) The main input text that the model will process. This is essential for generating the desired output.
- stop: (string, optional) A string that signals where the generation should stop.
- topK: (integer, optional) The number of top tokens to consider for generation. Setting this to -1 includes all tokens.
- topP: (number, optional) A cumulative probability threshold for token selection, ranging from 0.01 to 1.0.
- maxTokens: (integer, optional) The maximum number of tokens to generate in each sequence, defaulting to 128.
- temperature: (number, optional) Controls randomness in the output. Values lower than 1.0 yield more focused responses.
- presencePenalty: (number, optional) Penalty for token presence to discourage or encourage repetition.
- frequencyPenalty: (number, optional) Penalty based on token frequency.
Here’s an example of the input JSON payload:
{
"stop": "<|im_end|>",
"topK": 40,
"topP": 0.95,
"prompt": "<|im_start|>system\nYou are a helpful coding assistant.<|im_end|>\n<|im_start|>user\nwrite a javascript program to reverse letters in each word in a sentence without reversing order of words in the sentence.<|im_end|>\n<|im_start|>assistant",
"maxTokens": 512,
"temperature": 0.7,
"presencePenalty": 0,
"frequencyPenalty": 0
}
Output
The action typically returns a code snippet along with an explanation. Here’s a sample output:
Here is a simple JavaScript program that reverses each word in a sentence. This program does not reverse the order of words in the sentence.
```javascript
function reverseWords(sentence) {
return sentence.split(' ').map(word => word.split('').reverse().join('')).join(' ');
}
console.log(reverseWords('Hello world')); // prints: 'olleH dlrow'
This function works by first splitting the sentence into words with the split(' ') method. Then for each word, it reverses the letters with the split('').reverse().join('') method and joins them back together with the join(' ') method. Finally, it joins all the words back together with a space in between.
#### Conceptual Usage Example (Python)
Here’s a conceptual Python code snippet demonstrating how to call this action using a hypothetical Cognitive Actions endpoint:
```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 = "5ccfedeb-0e46-48a0-8a69-bde262e74279" # Action ID for Generate Code with EvolvedSeeker
# Construct the input payload based on the action's requirements
payload = {
"stop": "<|im_end|>",
"topK": 40,
"topP": 0.95,
"prompt": "<|im_start|>system\nYou are a helpful coding assistant.<|im_end|>\n<|im_start|>user\nwrite a javascript program to reverse letters in each word in a sentence without reversing order of words in the sentence.<|im_end|>\n<|im_start|>assistant",
"maxTokens": 512,
"temperature": 0.7,
"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 this Python snippet, replace YOUR_COGNITIVE_ACTIONS_API_KEY with your actual API key. The action ID for Generate Code with EvolvedSeeker is specified, and the input payload is structured according to the action's requirements.
Conclusion
The EvolvedSeeker Cognitive Action presents an efficient solution for code generation tasks, allowing developers to streamline their coding processes. By leveraging this action, you can automate code creation and evaluation, saving time and resources. Explore various prompts and parameters to tailor the output to your specific needs and enhance your development workflow!