Skip to content

Commit c475528

Browse files
feat(api): add translation endpoint
1 parent 91598ac commit c475528

9 files changed

Lines changed: 497 additions & 3 deletions

File tree

.stats.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
configured_endpoints: 30
1+
configured_endpoints: 31
22
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/writerai%2Fwriter-a8e20e751972f7b8c948a833764b02d0835b380189eaf7393d63eb2250dbd598.yml
33
openapi_spec_hash: 6f27335468bc5bf01fbcd1454330fb07
4-
config_hash: ee2e5a914fd298a6f4c740f5ce187f87
4+
config_hash: 1f6d0bf7309d0007e28ab85b89a0de85

api.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,18 @@ Methods:
182182

183183
- <code title="post /v1/tools/comprehend/medical">client.tools.comprehend.<a href="./src/writerai/resources/tools/comprehend.py">medical</a>(\*\*<a href="src/writerai/types/tools/comprehend_medical_params.py">params</a>) -> <a href="./src/writerai/types/tools/comprehend_medical_response.py">ComprehendMedicalResponse</a></code>
184184

185+
# Translation
186+
187+
Types:
188+
189+
```python
190+
from writerai.types import TranslationRequest, TranslationResponse
191+
```
192+
193+
Methods:
194+
195+
- <code title="post /v1/translation">client.translation.<a href="./src/writerai/resources/translation.py">translate</a>(\*\*<a href="src/writerai/types/translation_translate_params.py">params</a>) -> <a href="./src/writerai/types/translation_response.py">TranslationResponse</a></code>
196+
185197
# Vision
186198

187199
Types:

src/writerai/_client.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
get_async_library,
2626
)
2727
from ._version import __version__
28-
from .resources import chat, files, graphs, models, vision, completions
28+
from .resources import chat, files, graphs, models, vision, completions, translation
2929
from ._streaming import Stream as Stream, AsyncStream as AsyncStream
3030
from ._exceptions import WriterError, APIStatusError
3131
from ._base_client import (
@@ -47,6 +47,7 @@ class Writer(SyncAPIClient):
4747
graphs: graphs.GraphsResource
4848
files: files.FilesResource
4949
tools: tools.ToolsResource
50+
translation: translation.TranslationResource
5051
vision: vision.VisionResource
5152
with_raw_response: WriterWithRawResponse
5253
with_streaming_response: WriterWithStreamedResponse
@@ -114,6 +115,7 @@ def __init__(
114115
self.graphs = graphs.GraphsResource(self)
115116
self.files = files.FilesResource(self)
116117
self.tools = tools.ToolsResource(self)
118+
self.translation = translation.TranslationResource(self)
117119
self.vision = vision.VisionResource(self)
118120
self.with_raw_response = WriterWithRawResponse(self)
119121
self.with_streaming_response = WriterWithStreamedResponse(self)
@@ -231,6 +233,7 @@ class AsyncWriter(AsyncAPIClient):
231233
graphs: graphs.AsyncGraphsResource
232234
files: files.AsyncFilesResource
233235
tools: tools.AsyncToolsResource
236+
translation: translation.AsyncTranslationResource
234237
vision: vision.AsyncVisionResource
235238
with_raw_response: AsyncWriterWithRawResponse
236239
with_streaming_response: AsyncWriterWithStreamedResponse
@@ -298,6 +301,7 @@ def __init__(
298301
self.graphs = graphs.AsyncGraphsResource(self)
299302
self.files = files.AsyncFilesResource(self)
300303
self.tools = tools.AsyncToolsResource(self)
304+
self.translation = translation.AsyncTranslationResource(self)
301305
self.vision = vision.AsyncVisionResource(self)
302306
self.with_raw_response = AsyncWriterWithRawResponse(self)
303307
self.with_streaming_response = AsyncWriterWithStreamedResponse(self)
@@ -416,6 +420,7 @@ def __init__(self, client: Writer) -> None:
416420
self.graphs = graphs.GraphsResourceWithRawResponse(client.graphs)
417421
self.files = files.FilesResourceWithRawResponse(client.files)
418422
self.tools = tools.ToolsResourceWithRawResponse(client.tools)
423+
self.translation = translation.TranslationResourceWithRawResponse(client.translation)
419424
self.vision = vision.VisionResourceWithRawResponse(client.vision)
420425

421426

@@ -428,6 +433,7 @@ def __init__(self, client: AsyncWriter) -> None:
428433
self.graphs = graphs.AsyncGraphsResourceWithRawResponse(client.graphs)
429434
self.files = files.AsyncFilesResourceWithRawResponse(client.files)
430435
self.tools = tools.AsyncToolsResourceWithRawResponse(client.tools)
436+
self.translation = translation.AsyncTranslationResourceWithRawResponse(client.translation)
431437
self.vision = vision.AsyncVisionResourceWithRawResponse(client.vision)
432438

433439

@@ -440,6 +446,7 @@ def __init__(self, client: Writer) -> None:
440446
self.graphs = graphs.GraphsResourceWithStreamingResponse(client.graphs)
441447
self.files = files.FilesResourceWithStreamingResponse(client.files)
442448
self.tools = tools.ToolsResourceWithStreamingResponse(client.tools)
449+
self.translation = translation.TranslationResourceWithStreamingResponse(client.translation)
443450
self.vision = vision.VisionResourceWithStreamingResponse(client.vision)
444451

445452

@@ -452,6 +459,7 @@ def __init__(self, client: AsyncWriter) -> None:
452459
self.graphs = graphs.AsyncGraphsResourceWithStreamingResponse(client.graphs)
453460
self.files = files.AsyncFilesResourceWithStreamingResponse(client.files)
454461
self.tools = tools.AsyncToolsResourceWithStreamingResponse(client.tools)
462+
self.translation = translation.AsyncTranslationResourceWithStreamingResponse(client.translation)
455463
self.vision = vision.AsyncVisionResourceWithStreamingResponse(client.vision)
456464

457465

src/writerai/resources/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@
5656
CompletionsResourceWithStreamingResponse,
5757
AsyncCompletionsResourceWithStreamingResponse,
5858
)
59+
from .translation import (
60+
TranslationResource,
61+
AsyncTranslationResource,
62+
TranslationResourceWithRawResponse,
63+
AsyncTranslationResourceWithRawResponse,
64+
TranslationResourceWithStreamingResponse,
65+
AsyncTranslationResourceWithStreamingResponse,
66+
)
5967
from .applications import (
6068
ApplicationsResource,
6169
AsyncApplicationsResource,
@@ -108,6 +116,12 @@
108116
"AsyncToolsResourceWithRawResponse",
109117
"ToolsResourceWithStreamingResponse",
110118
"AsyncToolsResourceWithStreamingResponse",
119+
"TranslationResource",
120+
"AsyncTranslationResource",
121+
"TranslationResourceWithRawResponse",
122+
"AsyncTranslationResourceWithRawResponse",
123+
"TranslationResourceWithStreamingResponse",
124+
"AsyncTranslationResourceWithStreamingResponse",
111125
"VisionResource",
112126
"AsyncVisionResource",
113127
"VisionResourceWithRawResponse",
Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2+
3+
from __future__ import annotations
4+
5+
from typing_extensions import Literal
6+
7+
import httpx
8+
9+
from ..types import translation_translate_params
10+
from .._types import NOT_GIVEN, Body, Query, Headers, NotGiven
11+
from .._utils import (
12+
maybe_transform,
13+
async_maybe_transform,
14+
)
15+
from .._compat import cached_property
16+
from .._resource import SyncAPIResource, AsyncAPIResource
17+
from .._response import (
18+
to_raw_response_wrapper,
19+
to_streamed_response_wrapper,
20+
async_to_raw_response_wrapper,
21+
async_to_streamed_response_wrapper,
22+
)
23+
from .._base_client import make_request_options
24+
from ..types.translation_response import TranslationResponse
25+
26+
__all__ = ["TranslationResource", "AsyncTranslationResource"]
27+
28+
29+
class TranslationResource(SyncAPIResource):
30+
@cached_property
31+
def with_raw_response(self) -> TranslationResourceWithRawResponse:
32+
"""
33+
This property can be used as a prefix for any HTTP method call to return
34+
the raw response object instead of the parsed content.
35+
36+
For more information, see https://www.github.com/writer/writer-python#accessing-raw-response-data-eg-headers
37+
"""
38+
return TranslationResourceWithRawResponse(self)
39+
40+
@cached_property
41+
def with_streaming_response(self) -> TranslationResourceWithStreamingResponse:
42+
"""
43+
An alternative to `.with_raw_response` that doesn't eagerly read the response body.
44+
45+
For more information, see https://www.github.com/writer/writer-python#with_streaming_response
46+
"""
47+
return TranslationResourceWithStreamingResponse(self)
48+
49+
def translate(
50+
self,
51+
*,
52+
formality: bool,
53+
length_control: bool,
54+
mask_profanity: bool,
55+
model: Literal["palmyra-translate"],
56+
source_language_code: str,
57+
target_language_code: str,
58+
text: str,
59+
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
60+
# The extra values given here take precedence over values defined on the client or passed to this method.
61+
extra_headers: Headers | None = None,
62+
extra_query: Query | None = None,
63+
extra_body: Body | None = None,
64+
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
65+
) -> TranslationResponse:
66+
"""
67+
Translate text from one language to another.
68+
69+
Args:
70+
formality: Whether to use formal or informal language in the translation. See the
71+
[list of languages that support formality](https://dev.writer.com/api-guides/api-reference/translation-api/language-support#formality).
72+
If the language does not support formality, this parameter is ignored.
73+
74+
length_control: Whether to control the length of the translated text. See the
75+
[list of languages that support length control](https://dev.writer.com/api-guides/api-reference/translation-api/language-support#length-control).
76+
If the language does not support length control, this parameter is ignored.
77+
78+
mask_profanity: Whether to mask profane words in the translated text. See the
79+
[list of languages that do not support profanity masking](https://dev.writer.com/api-guides/api-reference/translation-api/language-support#profanity-masking).
80+
If the language does not support profanity masking, this parameter is ignored.
81+
82+
model: The model to use for translation.
83+
84+
source_language_code: The [ISO-639-1](https://en.wikipedia.org/wiki/List_of_ISO_639_language_codes)
85+
language code of the original text to translate. For example, `en` for English,
86+
`zh` for Chinese, `fr` for French, `es` for Spanish. If the language has a
87+
variant, the code appends the two-digit
88+
[ISO-3166 country code](https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes).
89+
For example, Mexican Spanish is `es-MX`. See the
90+
[list of supported languages and language codes](https://dev.writer.com/api-guides/api-reference/translation-api/language-support).
91+
92+
target_language_code: The [ISO-639-1](https://en.wikipedia.org/wiki/List_of_ISO_639_language_codes)
93+
language code of the target language for the translation. For example, `en` for
94+
English, `zh` for Chinese, `fr` for French, `es` for Spanish. If the language
95+
has a variant, the code appends the two-digit
96+
[ISO-3166 country code](https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes).
97+
For example, Mexican Spanish is `es-MX`. See the
98+
[list of supported languages and language codes](https://dev.writer.com/api-guides/api-reference/translation-api/language-support).
99+
100+
text: The text to translate. Maximum of 100,000 words.
101+
102+
extra_headers: Send extra headers
103+
104+
extra_query: Add additional query parameters to the request
105+
106+
extra_body: Add additional JSON properties to the request
107+
108+
timeout: Override the client-level default timeout for this request, in seconds
109+
"""
110+
return self._post(
111+
"/v1/translation",
112+
body=maybe_transform(
113+
{
114+
"formality": formality,
115+
"length_control": length_control,
116+
"mask_profanity": mask_profanity,
117+
"model": model,
118+
"source_language_code": source_language_code,
119+
"target_language_code": target_language_code,
120+
"text": text,
121+
},
122+
translation_translate_params.TranslationTranslateParams,
123+
),
124+
options=make_request_options(
125+
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
126+
),
127+
cast_to=TranslationResponse,
128+
)
129+
130+
131+
class AsyncTranslationResource(AsyncAPIResource):
132+
@cached_property
133+
def with_raw_response(self) -> AsyncTranslationResourceWithRawResponse:
134+
"""
135+
This property can be used as a prefix for any HTTP method call to return
136+
the raw response object instead of the parsed content.
137+
138+
For more information, see https://www.github.com/writer/writer-python#accessing-raw-response-data-eg-headers
139+
"""
140+
return AsyncTranslationResourceWithRawResponse(self)
141+
142+
@cached_property
143+
def with_streaming_response(self) -> AsyncTranslationResourceWithStreamingResponse:
144+
"""
145+
An alternative to `.with_raw_response` that doesn't eagerly read the response body.
146+
147+
For more information, see https://www.github.com/writer/writer-python#with_streaming_response
148+
"""
149+
return AsyncTranslationResourceWithStreamingResponse(self)
150+
151+
async def translate(
152+
self,
153+
*,
154+
formality: bool,
155+
length_control: bool,
156+
mask_profanity: bool,
157+
model: Literal["palmyra-translate"],
158+
source_language_code: str,
159+
target_language_code: str,
160+
text: str,
161+
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
162+
# The extra values given here take precedence over values defined on the client or passed to this method.
163+
extra_headers: Headers | None = None,
164+
extra_query: Query | None = None,
165+
extra_body: Body | None = None,
166+
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
167+
) -> TranslationResponse:
168+
"""
169+
Translate text from one language to another.
170+
171+
Args:
172+
formality: Whether to use formal or informal language in the translation. See the
173+
[list of languages that support formality](https://dev.writer.com/api-guides/api-reference/translation-api/language-support#formality).
174+
If the language does not support formality, this parameter is ignored.
175+
176+
length_control: Whether to control the length of the translated text. See the
177+
[list of languages that support length control](https://dev.writer.com/api-guides/api-reference/translation-api/language-support#length-control).
178+
If the language does not support length control, this parameter is ignored.
179+
180+
mask_profanity: Whether to mask profane words in the translated text. See the
181+
[list of languages that do not support profanity masking](https://dev.writer.com/api-guides/api-reference/translation-api/language-support#profanity-masking).
182+
If the language does not support profanity masking, this parameter is ignored.
183+
184+
model: The model to use for translation.
185+
186+
source_language_code: The [ISO-639-1](https://en.wikipedia.org/wiki/List_of_ISO_639_language_codes)
187+
language code of the original text to translate. For example, `en` for English,
188+
`zh` for Chinese, `fr` for French, `es` for Spanish. If the language has a
189+
variant, the code appends the two-digit
190+
[ISO-3166 country code](https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes).
191+
For example, Mexican Spanish is `es-MX`. See the
192+
[list of supported languages and language codes](https://dev.writer.com/api-guides/api-reference/translation-api/language-support).
193+
194+
target_language_code: The [ISO-639-1](https://en.wikipedia.org/wiki/List_of_ISO_639_language_codes)
195+
language code of the target language for the translation. For example, `en` for
196+
English, `zh` for Chinese, `fr` for French, `es` for Spanish. If the language
197+
has a variant, the code appends the two-digit
198+
[ISO-3166 country code](https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes).
199+
For example, Mexican Spanish is `es-MX`. See the
200+
[list of supported languages and language codes](https://dev.writer.com/api-guides/api-reference/translation-api/language-support).
201+
202+
text: The text to translate. Maximum of 100,000 words.
203+
204+
extra_headers: Send extra headers
205+
206+
extra_query: Add additional query parameters to the request
207+
208+
extra_body: Add additional JSON properties to the request
209+
210+
timeout: Override the client-level default timeout for this request, in seconds
211+
"""
212+
return await self._post(
213+
"/v1/translation",
214+
body=await async_maybe_transform(
215+
{
216+
"formality": formality,
217+
"length_control": length_control,
218+
"mask_profanity": mask_profanity,
219+
"model": model,
220+
"source_language_code": source_language_code,
221+
"target_language_code": target_language_code,
222+
"text": text,
223+
},
224+
translation_translate_params.TranslationTranslateParams,
225+
),
226+
options=make_request_options(
227+
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
228+
),
229+
cast_to=TranslationResponse,
230+
)
231+
232+
233+
class TranslationResourceWithRawResponse:
234+
def __init__(self, translation: TranslationResource) -> None:
235+
self._translation = translation
236+
237+
self.translate = to_raw_response_wrapper(
238+
translation.translate,
239+
)
240+
241+
242+
class AsyncTranslationResourceWithRawResponse:
243+
def __init__(self, translation: AsyncTranslationResource) -> None:
244+
self._translation = translation
245+
246+
self.translate = async_to_raw_response_wrapper(
247+
translation.translate,
248+
)
249+
250+
251+
class TranslationResourceWithStreamingResponse:
252+
def __init__(self, translation: TranslationResource) -> None:
253+
self._translation = translation
254+
255+
self.translate = to_streamed_response_wrapper(
256+
translation.translate,
257+
)
258+
259+
260+
class AsyncTranslationResourceWithStreamingResponse:
261+
def __init__(self, translation: AsyncTranslationResource) -> None:
262+
self._translation = translation
263+
264+
self.translate = async_to_streamed_response_wrapper(
265+
translation.translate,
266+
)

0 commit comments

Comments
 (0)