Generate Text Embeddings with the E5-large-v2 Cognitive Action

Cognitive Actions provide powerful capabilities for developers to easily integrate advanced machine learning functionalities into their applications. The 1m4nt0/e5-large-v2 specification focuses specifically on generating text embeddings using the E5-large-v2 model, which leverages a weakly-supervised contrastive pre-training approach. This model outputs 1024-dimensional embeddings from English text, making it suitable for various applications like semantic search, clustering, and more.
In this article, we will explore the key action available—Generate Text Embeddings—and provide developers with the necessary information and code snippets to implement this functionality effectively.
Prerequisites
To start using the Cognitive Actions, you'll need:
- An API key for the Cognitive Actions platform, which you will use for authentication when making requests.
- Basic knowledge of making HTTP requests in your preferred programming language.
Authentication typically involves passing your API key in the request headers.
Cognitive Actions Overview
Generate Text Embeddings
This action generates embeddings from English text using the E5-large-v2 model. It produces 1024-dimensional embeddings, and long texts are truncated to a maximum of 512 tokens.
Input
The input for this action requires the following fields:
- text (required): The English text for which embeddings will be generated.
Here’s the input schema:
{
"text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit..."
}
Example Input:
{
"text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam posuere ipsum finibus laoreet pretium. Fusce commodo arcu sapien, et vestibulum lacus aliquam ac..."
}
Output
The action returns an array of floating-point numbers representing the 1024-dimensional embeddings generated from the input text.
Example Output:
[
0.002524025971069932,
-0.056985631585121155,
0.031711943447589874,
...
]
Conceptual Usage Example (Python)
Below is a conceptual example of how to call the Generate Text Embeddings action using Python. This code constructs the input payload and sends it to a hypothetical Cognitive Actions execution endpoint.
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 = "bf93405e-6d37-4bff-bd45-ece5a053fcd7" # Action ID for Generate Text Embeddings
# Construct the input payload based on the action's requirements
payload = {
"text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam posuere ipsum finibus laoreet pretium. Fusce commodo arcu sapien..."
}
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 provided code snippet:
- Replace
YOUR_COGNITIVE_ACTIONS_API_KEYwith your actual API key. - The
action_idcorresponds to the Generate Text Embeddings action. - The input payload is structured according to the required schema.
Conclusion
The Generate Text Embeddings action from the 1m4nt0/e5-large-v2 specification allows developers to easily generate meaningful embeddings from English text. This functionality can be leveraged for various applications such as natural language processing, information retrieval, and more.
To get started, simply follow the guidelines provided, and you'll be able to integrate this powerful action into your applications seamlessly. Explore further use cases and combine multiple Cognitive Actions to enhance your applications' capabilities!