DocumentaçãoConversa
Construir
Conversa
Instruções, uma pergunta, uma resposta. Depois, a continuação da conversa, reenviando o histórico em cada interação.
As mensagens
Uma conversa é uma lista de mensagens, cada uma com uma função.
| Função | Quem fala |
|---|---|
system | As tuas instruções: o tom, a língua, o que o modelo deve fazer ou evitar |
user | A pessoa ou a tua aplicação |
assistant | As respostas anteriores do modelo |
tool | O resultado de uma das tuas funções |
Várias interações
O modelo não guarda nada de uma chamada para a outra. Para continuar, reenvia a conversa inteira com a nova mensagem.
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)Os parâmetros úteis
temperaturenúmero- Mais baixo, respostas mais constantes. Mais alto, mais variadas. Sem definição, o modelo recebe o valor que escolhemos para ele.
max_tokensinteiro- O comprimento máximo da resposta, reduzido ao limite do modelo se o ultrapassar.
response_formatobjeto- Pede uma resposta em JSON, para a ler diretamente no teu código.
stoplista- Sequências de caracteres que interrompem a resposta.
Raciocínio
Para uma pergunta difícil, pede ao modelo que pense antes de responder. O esforço vai de none a high. O raciocínio chega à parte, em 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."}
]
}'