Enhance Multilingual Search Relevance with AI Embeddings

25 Apr 2025
Enhance Multilingual Search Relevance with AI Embeddings

The Multilingual E5 Base is a powerful service designed to help developers create applications that can understand and process multiple languages with remarkable efficiency. At the core of this service is a set of Cognitive Actions that leverage advanced machine learning techniques to generate multilingual text embeddings, allowing applications to better grasp the context and relevance of textual data across languages. The result is a more intuitive and responsive user experience, especially in applications that require understanding user queries in various languages.

By utilizing the Multilingual E5 Base, developers can expect to improve the speed and accuracy of their applications, facilitating better search results, content recommendations, and data analysis. This service is particularly beneficial for e-commerce platforms, content management systems, and any application that serves a diverse, multilingual audience.

Prerequisites

Before diving into the integration of the Multilingual E5 Base, ensure you have a Cognitive Actions API key and a basic understanding of making API calls. This foundational knowledge will help you seamlessly connect to the service and make the most of its capabilities.

Generate Multilingual Embeddings with Relevancy Score

The "Generate Multilingual Embeddings with Relevancy Score" action is designed to create multilingual text embeddings while providing a relevancy score. This action utilizes the Multilingual E5-Base model, based on the XLM-RoBERTa architecture, making it ideal for handling diverse multilingual datasets. By inputting a query and a passage, developers can assess how relevant the passage is to the query, enabling applications to deliver more accurate information to users.

Input Requirements

To use this action, you need to provide:

  • Query: A string that represents the search query (e.g., "What is the recommended sugar intake for women").
  • Passage: A string containing the relevant information you wish to analyze (e.g., "As a general guideline, the CDC’s average recommended sugar intake for women ages 19 to 70 is 20 grams per day.").
  • Output Normalized Embeddings (optional): A boolean flag to indicate whether the output embeddings should be normalized. Defaults to false.

Expected Output

The expected output includes:

  • Score: A relevancy score indicating the degree of relevance between the query and the passage.
  • Embeddings: A multidimensional array representing the generated embeddings for both the query and the passage.

Use Cases for this Specific Action

This action is particularly useful in scenarios such as:

  • Search Engines: Enhancing search results by providing more relevant content based on user queries in multiple languages.
  • Chatbots and Virtual Assistants: Enabling smarter responses that understand user intent across different languages.
  • Content Recommendation Systems: Improving content suggestions by matching user queries with relevant content in various languages.

By integrating this action, developers can significantly enhance the relevance and quality of user interactions with their applications.

import requests
import json

# Replace with your actual Cognitive Actions API key and endpoint
# Ensure your environment securely handles the API key
COGNITIVE_ACTIONS_API_KEY = "YOUR_COGNITIVE_ACTIONS_API_KEY"
# This endpoint URL is hypothetical and should be documented for users
COGNITIVE_ACTIONS_EXECUTE_URL = "https://api.cognitiveactions.com/actions/execute"

action_id = "9e683164-4c58-4ddb-968a-e12e4e527396" # Action ID for: Generate Multilingual Embeddings with Relevancy Score

# Construct the exact input payload based on the action's requirements
# This example uses the predefined example_input for this action:
payload = {
  "query": "What is the recommended sugar intake for women",
  "passage": "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",
    # Add any other required headers for the Cognitive Actions API
}

# Prepare the request body for the hypothetical execution endpoint
request_body = {
    "action_id": action_id,
    "inputs": payload
}

print(f"--- Calling Cognitive Action: {action.name or action_id} ---")
print(f"Endpoint: {COGNITIVE_ACTIONS_EXECUTE_URL}")
print(f"Action ID: {action_id}")
print("Payload being sent:")
print(json.dumps(request_body, indent=2))
print("------------------------------------------------")

try:
    response = requests.post(
        COGNITIVE_ACTIONS_EXECUTE_URL,
        headers=headers,
        json=request_body
    )
    response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)

    result = response.json()
    print("Action executed successfully. Result:")
    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 (non-JSON): {e.response.text}")
    print("------------------------------------------------")

Conclusion

The Multilingual E5 Base offers a robust solution for developers looking to enhance their applications' multilingual capabilities. By leveraging the "Generate Multilingual Embeddings with Relevancy Score" action, developers can improve search relevance, enrich user interactions, and deliver more personalized content across languages. As you explore this service, consider the various applications and scenarios where improved multilingual understanding can lead to enhanced user experiences and increased engagement. Start integrating today to unlock the full potential of multilingual AI!