Python client to interact with Aleph Alpha API endpoints
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Translate text between languages using Aleph Alpha's translation models. Provides high-quality translations with quality scoring and segment-level analysis.
Configure translation operations with source text and target language specification.
class TranslationRequest:
model: str
source: str
target_language: str
def __init__(self, model: str, source: str, target_language: str):
"""
Request for translation.
Parameters:
- model: The name of the model to be used for the translation
- source: The input text to be translated
- target_language: The desired target language using ISO 639 language codes
(e.g., "en" for English, "de" for German, "fr" for French)
"""
def to_json(self) -> dict:
"""Convert the request to a JSON-serializable dictionary."""Structured response containing translated text with quality metrics and optional segment analysis.
class TranslationResponse:
translation: str
score: float
segments: Optional[List[TranslationSegment]]
num_tokens_prompt_total: int
num_tokens_generated: int
def __init__(
self,
translation: str,
score: float,
segments: Optional[List[TranslationSegment]],
num_tokens_prompt_total: int,
num_tokens_generated: int
):
"""
Response from a translation request.
Attributes:
- translation: The complete translated output text
- score: Overall quality estimate on a scale of 0 to 1
- segments: List of translated segments (may be None)
- num_tokens_prompt_total: Total tokens in prompt (may be zero)
- num_tokens_generated: Total tokens generated (may be zero)
"""
@classmethod
def from_json(cls, json: dict) -> TranslationResponse:
"""Create a TranslationResponse from a JSON dictionary."""Individual segments of translated text with quality scoring.
class TranslationSegment:
source: str
translation: str
score: float
def __init__(self, source: str, translation: str, score: float):
"""
A segment of translated text with its source and quality score.
Parameters:
- source: The input text to be translated
- translation: The translated output text of the segment
- score: Quality estimate for this segment on a scale of 0 to 1
"""Translate text using synchronous and asynchronous clients.
def translate(self, request: TranslationRequest) -> TranslationResponse:
"""
Translate text to target language.
Parameters:
- request: Translation configuration
Returns:
TranslationResponse with translated text and quality metrics
"""
async def translate(self, request: TranslationRequest) -> TranslationResponse:
"""
Translate text to target language (async).
Parameters:
- request: Translation configuration
Returns:
TranslationResponse with translated text and quality metrics
"""Basic translation with quality assessment:
from aleph_alpha_client import Client, TranslationRequest
client = Client(token="your-api-token")
# Simple translation
request = TranslationRequest(
model="luminous-extended",
source="Hello, how are you today?",
target_language="de" # German
)
response = client.translate(request)
print(f"Translation: {response.translation}")
print(f"Quality Score: {response.score:.2f}")
# Translation with segment analysis
request = TranslationRequest(
model="luminous-extended",
source="Good morning! I hope you're having a wonderful day. The weather is beautiful today.",
target_language="fr" # French
)
response = client.translate(request)
print(f"Full Translation: {response.translation}")
print(f"Overall Quality: {response.score:.2f}")
if response.segments:
print("\nSegment Analysis:")
for i, segment in enumerate(response.segments):
print(f"Segment {i+1}:")
print(f" Source: {segment.source}")
print(f" Translation: {segment.translation}")
print(f" Quality: {segment.score:.2f}")
# Async translation
import asyncio
async def translate_async():
async with AsyncClient(token="your-api-token") as client:
request = TranslationRequest(
model="luminous-extended",
source="Artificial intelligence is transforming our world.",
target_language="es" # Spanish
)
response = await client.translate(request)
print(f"Async Translation: {response.translation}")
return response
# Run async translation
response = asyncio.run(translate_async())
# Multiple language translation
languages = {
"de": "German",
"fr": "French",
"es": "Spanish",
"it": "Italian"
}
source_text = "Technology connects people across the globe."
for lang_code, lang_name in languages.items():
request = TranslationRequest(
model="luminous-extended",
source=source_text,
target_language=lang_code
)
response = client.translate(request)
print(f"{lang_name}: {response.translation} (Quality: {response.score:.2f})")Install with Tessl CLI
npx tessl i tessl/pypi-aleph-alpha-client