You need an API key to access the Writer API. Get an API key by following the steps in the API quickstart.We recommend setting the API key as an environment variable in a
.env file with the name WRITER_API_KEY.Tool structure
The translation tool allows you to translate text during a chat completion. To use the translation tool, add it to thetools array in your chat-completion endpoint request.
The translation tool object has the following structure:
You can only pass one prebuilt tool in the
tools array at a time. However, you can pass multiple custom tools in the same request.Prebuilt tools are:- Translation tool
- Knowledge Graph tool
- LLM tool
- Web search tool
Response format
For non-streaming responses, the translated text is in thechoices[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:
See the chat completion endpoint for more information on the response fields.
Usage example
This example usespalmyra-translate to translate a message during a chat completion.
Create a tools array containing a translation tool
First, create atools array that specifies the translation tool you want to use.
Send the request using chat completions
Add the tools array to the chat endpoint call along with your array of messages. Settingtool_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.