Generate text from a prompt - Writer AI Studio
Text generation endpoint
You can use the text generation endpoint to generate text with an LLM.
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.
Text generation vs. chat completion
The text generation endpoint is appropriate when you need to generate a single text response based on a given prompt, or when you want to ask a specific LLM a question. The chat completion endpoint can generate single messages, or create more complex conversations between a user and a general-purpose LLM. Additionally, the chat completion endpoint offers tool calling, which you can use to access other LLMs, Knowledge Graphs, and custom functions.
Endpoint overview
URL: POST https://api.writer.com/v1/completions
Using the /completions endpoint results in charges for model usage. See the pricing page for more information.
cURL
curl --location 'https://api.writer.com/v1/completions' \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $WRITER_API_KEY" \
--data '{
"model": "palmyra-x5",
"prompt": "Tell me a story",
"max_tokens": 1000,
"temperature": 0.7,
"stream": true
}'
Python
from writerai import Writer
# Initialize the client. If you don't pass the `api_key` parameter,
# the client looks for the `WRITER_API_KEY` environment variable.
client = Writer()
text_generation = client.completions.create(
model="palmyra-x5",
prompt="Tell me a story",
max_tokens=1000,
temperature=0.7,
stream=True
)
for chunk in text_generation:
print(chunk.value, end="", flush=True)
JavaScript
import { Writer } from 'writer-sdk';
// Initialize the client. If you don't pass the `apiKey` parameter,
// the client looks for the `WRITER_API_KEY` environment variable.
const client = new Writer();
const text_generation = await client.completions.create({
model: 'palmyra-x5',
prompt: 'Tell me a story',
max_tokens: 1000,
temperature: 0.7,
stream: true
});
for await (const chunk of text_generation) {
process.stdout.write(chunk.value);
}
Request body
Below are the required and commonly used optional parameters for the text generation endpoint.
| Parameter | Type | Description |
|---|---|---|
model |
string | Required. The ID of the model to use for text generation. |
prompt |
string | Required. The prompt to generate text from. |
max_tokens |
int | The maximum number of tokens to generate for the response. Defaults to 100. |
temperature |
float | Temperature influences the randomness in generated text. Defaults to 1. Increase the value for more creative responses, and decrease the value for more predictable responses. |
stream |
Boolean | A Boolean value that indicates whether to stream the response. Defaults to false. |
See the full list of available parameters in the text generation endpoint reference.
Response parameters
Non-streaming response
If you set the stream parameter to false, the response is a single JSON object with the following parameters:
| Parameter | Type | Description |
|---|---|---|
model |
string | The ID of the model used to generate the response. |
choices |
array | An array of choices objects. |
choices[0].text |
string | The generated text. |
choices[0].log_probs |
object | The log probabilities of the tokens in the generated text. |
{
"choices": [
{
"text": "Camping Gear: The Ultimate Guide\n\nCamping is a great way to get outdoors and enjoy nature",
"log_probs": null
}
],
"model": "palmyra-x5"
}
Streaming response
If you set the stream parameter to true, the response is delivered as server-sent events with the following parameters:
| Parameter | Description |
|---|---|
value |
The content of the chunk. |
data: {"value":"Camping Gear: The Ultimate Guide\n\nCamping is a great way to get outdoors and enjoy nature"}
Generate streaming and non-streaming responses
The examples below generate a single message from the palmyra-x5 model, using the prompt “How can I treat a cold?”
Streaming response
The text generation endpoint supports streaming responses. The response comes in chunks until the entire response finishes. Streaming responses are useful when you want to display the generated text in real-time, or when you want to stream the response to a client, rather than waiting for the entire response to finish.
Example requests
cURL
curl --location 'https://api.writer.com/v1/completions' \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $WRITER_API_KEY" \
--data '{
"model": "palmyra-x5",
"prompt": "How can I treat a cold?",
"stream": true
}'
Python
from writerai import Writer
# Initialize the client. If you don't pass the `api_key` parameter,
# the client looks for the `WRITER_API_KEY` environment variable.
client = Writer()
text_generation = client.completions.create(
model="palmyra-x5",
prompt="How can I treat a cold?",
stream=True
)
for chunk in text_generation:
print(chunk.value, end="", flush=True)
JavaScript
import { Writer } from 'writer-sdk';
const text_generation = await client.completions.create({
model: 'palmyra-x5',
prompt: 'How can I treat a cold?',
stream: true
});
for await (const chunk of text_generation) {
process.stdout.write(chunk.value);
}
Non-streaming response
For non-streaming responses, the response returns as a single JSON object after the entire response is complete. The text is in the choices[0].text field.
Example requests
cURL
curl --location 'https://api.writer.com/v1/completions' \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $WRITER_API_KEY" \
--data '{
"model": "palmyra-x5",
"prompt": "How can I treat a cold?",
"stream": false
}'
Python
from writerai import Writer
# Initialize the client. If you don't pass the `api_key` parameter,
# the client looks for the `WRITER_API_KEY` environment variable.
client = Writer()
text_generation = client.completions.create(
model="palmyra-x5",
prompt="How can I treat a cold?",
stream=False
)
print(text_generation.choices[0].text)
JavaScript
import { Writer } from 'writer-sdk';
const text_generation = await client.completions.create({
model: 'palmyra-x5',
prompt: 'How can I treat a cold?',
stream: false
});
console.log(text_generation.choices[0].text);