translation
endpoint that you can use to translate text outside of a chat completion. See the translation API guide for more information.
This guide explains how to use the translation tool in a chat completion and provides an example of how to use it.
.env
file with the name WRITER_API_KEY
.tools
array in your chat-completion
endpoint request.
The translation tool object has the following structure:
Parameter | Type | Description |
---|---|---|
type | string | The type of tool, which is translation for the translation tool |
function | object | An object containing the tool’s description and model |
function.model | string | palmyra-translate |
function.formality | boolean | Whether the translation should be formal or informal, if the target language supports it. |
function.length_control | boolean | Whether to control the length of the translation, if the target language supports it. |
function.mask_profanity | boolean | Whether to mask profanity in the translation, if the target language supports it. |
function.source_language | string | (Optional) The language code of the text you want to translate. If you don’t provide a source language, the model automatically detects the language of the text you want to translate. If your message contains a different language than this value, this value overrides the language detection. |
function.target_language | string | (Optional) The language code you want to translate the text to. If you don’t provide a target language, the model automatically selects the most appropriate language based on the message you provide to the chat completion endpoint. If your message contains a different language than this value, this value overrides the language selection. |
tools
array at a time. However, you can pass multiple custom tools in the same request.Prebuilt tools are:choices[0].message.content
field. For streaming responses, the translated text is in the choices[0].delta.content
field.
The response also contains a translation_data
field that contains the following information:
Parameter | Type | Description |
---|---|---|
source_language_code | string | The language code of the text you provided to the translation tool. |
target_language_code | string | The language code of the translated text. |
source_text | string | The text the translation tool translated. |
choices[0].delta.content
field once the full translation is complete.If you are unfamiliar with the chat completions endpoint or streaming vs. non-streaming responses, learn more in the chat completion guide.palmyra-translate
to translate a message during a chat completion.
tools
array that specifies the translation tool you want to use.
tool_choice
to auto
allows the model to choose when to use the translation tool, based on the message provided in the messages
array.
The response contains the translated text in the choices[0].message.content
field.
translation_data
field to see the source text, source language code, and target language code that the translation tool used.
palmyra-translate
model translate a message during a chat completion.