0
# Translation
1
2
Translate text between languages using Aleph Alpha's translation models. Provides high-quality translations with quality scoring and segment-level analysis.
3
4
## Capabilities
5
6
### Translation Requests
7
8
Configure translation operations with source text and target language specification.
9
10
```python { .api }
11
class TranslationRequest:
12
model: str
13
source: str
14
target_language: str
15
16
def __init__(self, model: str, source: str, target_language: str):
17
"""
18
Request for translation.
19
20
Parameters:
21
- model: The name of the model to be used for the translation
22
- source: The input text to be translated
23
- target_language: The desired target language using ISO 639 language codes
24
(e.g., "en" for English, "de" for German, "fr" for French)
25
"""
26
27
def to_json(self) -> dict:
28
"""Convert the request to a JSON-serializable dictionary."""
29
```
30
31
### Translation Responses
32
33
Structured response containing translated text with quality metrics and optional segment analysis.
34
35
```python { .api }
36
class TranslationResponse:
37
translation: str
38
score: float
39
segments: Optional[List[TranslationSegment]]
40
num_tokens_prompt_total: int
41
num_tokens_generated: int
42
43
def __init__(
44
self,
45
translation: str,
46
score: float,
47
segments: Optional[List[TranslationSegment]],
48
num_tokens_prompt_total: int,
49
num_tokens_generated: int
50
):
51
"""
52
Response from a translation request.
53
54
Attributes:
55
- translation: The complete translated output text
56
- score: Overall quality estimate on a scale of 0 to 1
57
- segments: List of translated segments (may be None)
58
- num_tokens_prompt_total: Total tokens in prompt (may be zero)
59
- num_tokens_generated: Total tokens generated (may be zero)
60
"""
61
62
@classmethod
63
def from_json(cls, json: dict) -> TranslationResponse:
64
"""Create a TranslationResponse from a JSON dictionary."""
65
```
66
67
### Translation Segments
68
69
Individual segments of translated text with quality scoring.
70
71
```python { .api }
72
class TranslationSegment:
73
source: str
74
translation: str
75
score: float
76
77
def __init__(self, source: str, translation: str, score: float):
78
"""
79
A segment of translated text with its source and quality score.
80
81
Parameters:
82
- source: The input text to be translated
83
- translation: The translated output text of the segment
84
- score: Quality estimate for this segment on a scale of 0 to 1
85
"""
86
```
87
88
### Translation Methods
89
90
Translate text using synchronous and asynchronous clients.
91
92
```python { .api }
93
def translate(self, request: TranslationRequest) -> TranslationResponse:
94
"""
95
Translate text to target language.
96
97
Parameters:
98
- request: Translation configuration
99
100
Returns:
101
TranslationResponse with translated text and quality metrics
102
"""
103
104
async def translate(self, request: TranslationRequest) -> TranslationResponse:
105
"""
106
Translate text to target language (async).
107
108
Parameters:
109
- request: Translation configuration
110
111
Returns:
112
TranslationResponse with translated text and quality metrics
113
"""
114
```
115
116
### Usage Examples
117
118
Basic translation with quality assessment:
119
120
```python
121
from aleph_alpha_client import Client, TranslationRequest
122
123
client = Client(token="your-api-token")
124
125
# Simple translation
126
request = TranslationRequest(
127
model="luminous-extended",
128
source="Hello, how are you today?",
129
target_language="de" # German
130
)
131
132
response = client.translate(request)
133
print(f"Translation: {response.translation}")
134
print(f"Quality Score: {response.score:.2f}")
135
136
# Translation with segment analysis
137
request = TranslationRequest(
138
model="luminous-extended",
139
source="Good morning! I hope you're having a wonderful day. The weather is beautiful today.",
140
target_language="fr" # French
141
)
142
143
response = client.translate(request)
144
print(f"Full Translation: {response.translation}")
145
print(f"Overall Quality: {response.score:.2f}")
146
147
if response.segments:
148
print("\nSegment Analysis:")
149
for i, segment in enumerate(response.segments):
150
print(f"Segment {i+1}:")
151
print(f" Source: {segment.source}")
152
print(f" Translation: {segment.translation}")
153
print(f" Quality: {segment.score:.2f}")
154
155
# Async translation
156
import asyncio
157
158
async def translate_async():
159
async with AsyncClient(token="your-api-token") as client:
160
request = TranslationRequest(
161
model="luminous-extended",
162
source="Artificial intelligence is transforming our world.",
163
target_language="es" # Spanish
164
)
165
166
response = await client.translate(request)
167
print(f"Async Translation: {response.translation}")
168
return response
169
170
# Run async translation
171
response = asyncio.run(translate_async())
172
173
# Multiple language translation
174
languages = {
175
"de": "German",
176
"fr": "French",
177
"es": "Spanish",
178
"it": "Italian"
179
}
180
181
source_text = "Technology connects people across the globe."
182
183
for lang_code, lang_name in languages.items():
184
request = TranslationRequest(
185
model="luminous-extended",
186
source=source_text,
187
target_language=lang_code
188
)
189
190
response = client.translate(request)
191
print(f"{lang_name}: {response.translation} (Quality: {response.score:.2f})")
192
```