DocumentationChat
Build
Chat
Instructions, a question, an answer. Then the rest of the exchange, sending the history back on every turn.
On this page
Messages
A conversation is a list of messages, each with a role.
| Role | Who is speaking |
|---|---|
system | Your instructions: tone, language, what the model should do or avoid |
user | The person or your application |
assistant | The model’s previous responses |
tool | The result of one of your functions |
Multiple turns
The model keeps nothing between calls. To continue an exchange, send the whole conversation again with the new message.
messages = [
{
"role": "system",
"content": "Answer in French, in plain words.",
},
{
"role": "user",
"content": "What must a privacy notice contain?",
},
]
first = client.chat.completions.create(
model="flash", messages=messages
)
# No memory between calls: send the whole conversation again.
messages.append(first.choices[0].message)
messages.append(
{
"role": "user",
"content": "Write one for a bakery with a web shop.",
}
)
second = client.chat.completions.create(
model="flash", messages=messages, temperature=0.4
)
print(second.choices[0].message.content)const messages = [
{
role: "system",
content: "Answer in French, in plain words.",
},
{
role: "user",
content: "What must a privacy notice contain?",
},
]
const first = await client.chat.completions.create({
model: "flash",
messages,
})
// No memory between calls: send the whole conversation again.
messages.push(first.choices[0].message)
messages.push({
role: "user",
content: "Write one for a bakery with a web shop.",
})
const second = await client.chat.completions.create({
model: "flash",
messages,
temperature: 0.4,
})
console.log(second.choices[0].message.content)import type OpenAI from "openai"
const messages: OpenAI.ChatCompletionMessageParam[] = [
{
role: "system",
content: "Answer in French, in plain words.",
},
{
role: "user",
content: "What must a privacy notice contain?",
},
]
const first = await client.chat.completions.create({
model: "flash",
messages,
})
// No memory between calls: send the whole conversation again.
messages.push(first.choices[0].message)
messages.push({
role: "user",
content: "Write one for a bakery with a web shop.",
})
const second = await client.chat.completions.create({
model: "flash",
messages,
temperature: 0.4,
})
console.log(second.choices[0].message.content)Useful parameters
temperaturenumber- Lower gives more consistent responses, higher gives more varied ones. If unset, the model receives the value we chose for it.
max_tokensinteger- The maximum length of the response, capped at the model’s limit if it exceeds it.
response_formatobject- Asks for a response in JSON, so your code can read it directly.
stoparray- Character sequences that stop the response.
Reasoning
For a hard question, ask the model to think before answering. Effort ranges from none to high. The reasoning arrives separately, in reasoning_content.
response = client.chat.completions.create(
model="max",
reasoning_effort="high",
messages=[
{
"role": "user",
"content": "Compare these contracts.",
}
],
)
message = response.choices[0].message
# How the model got there, then the answer.
print(getattr(message, "reasoning_content", None))
print(message.content)const response = await client.chat.completions.create({
model: "max",
reasoning_effort: "high",
messages: [
{ role: "user", content: "Compare these contracts." },
],
})
const message = response.choices[0].message
// How the model got there, then the answer.
console.log(message.reasoning_content)
console.log(message.content)import type { ChatCompletionMessage } from "openai/resources"
// Learnya adds the reasoning next to the answer,
// a field the OpenAI types do not declare.
type LearnyaMessage = ChatCompletionMessage & {
reasoning_content?: string
}
const response = await client.chat.completions.create({
model: "max",
reasoning_effort: "high",
messages: [
{ role: "user", content: "Compare these contracts." },
],
})
const message: LearnyaMessage = response.choices[0].message
// How the model got there, then the answer.
console.log(message.reasoning_content)
console.log(message.content)curl https://api.learnya.ai/v1/chat/completions \
-H "Authorization: Bearer $LEARNYA_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "max",
"reasoning_effort": "high",
"messages": [
{"role": "user", "content": "Compare these contracts."}
]
}'