Generate Multilingual Text Embeddings with adirik/multilingual-e5-small Actions

24 Apr 2025
Generate Multilingual Text Embeddings with adirik/multilingual-e5-small Actions

In today's globalized world, building applications that can understand and process multiple languages is more important than ever. The adirik/multilingual-e5-small API offers a powerful solution for developers looking to integrate multilingual capabilities into their applications. This API provides a set of Cognitive Actions that allow you to generate multilingual text embeddings along with relevancy scores, enabling more nuanced text analysis and understanding across different languages.

Using these pre-built actions simplifies the integration process, allowing developers to focus on building their applications rather than dealing with complex language models.

Prerequisites

Before you can start using the Cognitive Actions, ensure you have the following:

  • API Key: You will need an API key to authenticate your requests to the Cognitive Actions platform. This key should be included in the headers of your requests.
  • Setup: Familiarity with making HTTP requests and handling JSON payloads is essential.

Authentication typically involves passing the API key in the request headers, like so:

Authorization: Bearer YOUR_API_KEY
Content-Type: application/json

Cognitive Actions Overview

Generate Multilingual Text Embeddings with Relevancy Score

The Generate Multilingual Text Embeddings with Relevancy Score action utilizes the Multilingual E5-Small model, based on the XLM-RoBERTa architecture. This action generates text embeddings and relevancy scores for both a query and a passage, providing multilingual capabilities.

  • Category: Text Embedding

Input

The input for this action requires the following fields:

  • inputPassage (required): The passage of text that contains information relevant to the input query.
  • queryText (optional): The input text query that needs to be answered.
  • outputNormalizedEmbeddings (optional): A boolean indicating whether the output embeddings should be normalized (defaults to false).

Example Input:

{
  "queryText": "What is the recommended sugar intake for women",
  "inputPassage": "As a general guideline, the CDC’s average recommended sugar intake for women ages 19 to 70 is 20 grams per day.",
  "outputNormalizedEmbeddings": false
}

Output

The action typically returns a JSON object containing:

  • score: A relevancy score indicating how closely the passage relates to the query.
  • embeddings: An array of numerical values representing the text embeddings.

Example Output:

{
  "score": 91.8125,
  "embeddings": [
    [0.267822265625, -0.05438232421875, ...], 
    [-0.046630859375, 0.052398681640625, ...]
  ]
}

Conceptual Usage Example (Python)

Here’s a conceptual example of how you might call 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 = "9c808acb-d3f5-4c49-9952-c2430b3365db"  # Action ID for Generate Multilingual Text Embeddings with Relevancy Score

# Construct the input payload based on the action's requirements
payload = {
    "queryText": "What is the recommended sugar intake for women",
    "inputPassage": "As a general guideline, the CDC’s average recommended sugar intake for women ages 19 to 70 is 20 grams per day.",
    "outputNormalizedEmbeddings": false
}

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 code snippet, you replace the COGNITIVE_ACTIONS_API_KEY and endpoint URL with your actual API key and endpoint. The action ID and input payload are structured as per the requirements of the Generate Multilingual Text Embeddings with Relevancy Score action.

Conclusion

The adirik/multilingual-e5-small Cognitive Actions provide a robust framework for generating text embeddings and relevancy scores in multiple languages. By leveraging these actions, developers can enhance their applications with powerful multilingual capabilities, allowing for greater user engagement and content understanding.

As you explore these actions, consider how they can be integrated into your specific use cases—whether for content recommendations, search optimization, or any other applications requiring multilingual text analysis. Happy coding!