DocumentaciónEl endpoint del asistente
Tus asistentes
El endpoint del asistente
Una sola solicitud en formato OpenAI. Tu asistente responde con sus instrucciones y su memoria, en streaming.
Llamar al asistente
POST /v1/agents/chat/completions Un turno de conversación completo.
Indica el asistente con assistantId. Para continuar la misma conversación, envía threadId en su lugar.
curl -N https://api.learnya.ai/v1/agents/chat/completions \
-H "Authorization: Bearer $LEARNYA_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"assistantId": "ASSISTANT_ID",
"stream": true,
"messages": [
{"role": "user", "content": "Where is my invoice?"}
]
}'with requests.post(
f"{API}/agents/chat/completions",
headers=HEADERS,
json={
"assistantId": assistant["id"],
"stream": True,
"messages": [
{"role": "user", "content": "Where is my invoice?"}
],
},
stream=True,
) as response:
for line in response.iter_lines(decode_unicode=True):
if line.startswith("data: ") and line != "data: [DONE]":
chunk = json.loads(line[6:])
delta = chunk["choices"][0]["delta"].get("content")
print(delta or "", end="", flush=True)const response = await fetch(`${API}/agents/chat/completions`, {
method: "POST",
headers,
body: JSON.stringify({
assistantId: assistant.id,
stream: true,
messages: [
{ role: "user", content: "Where is my invoice?" },
],
}),
})
const decoder = new TextDecoder()
for await (const part of response.body) {
for (const line of decoder.decode(part).split("\n")) {
if (!line.startsWith("data: ")) continue
if (line === "data: [DONE]") continue
const chunk = JSON.parse(line.slice(6))
process.stdout.write(chunk.choices[0]?.delta?.content ?? "")
}
}interface Chunk {
choices: { delta: { content?: string } }[]
}
const response = await fetch(`${API}/agents/chat/completions`, {
method: "POST",
headers,
body: JSON.stringify({
assistantId: assistant.id,
stream: true,
messages: [
{ role: "user", content: "Where is my invoice?" },
],
}),
})
const text = response.body!.pipeThrough(new TextDecoderStream())
for await (const part of text) {
for (const line of part.split("\n")) {
if (!line.startsWith("data: ")) continue
if (line === "data: [DONE]") continue
const chunk = JSON.parse(line.slice(6)) as Chunk
process.stdout.write(chunk.choices[0]?.delta?.content ?? "")
}
}La respuesta
| Modo | Lo que recibes |
|---|---|
| En streaming | Fragmentos chat.completion.chunk en formato OpenAI, después [DONE] |
| Sin streaming | Una respuesta inmediata sin texto, con finish_reason en queued y los identificadores del hilo, del mensaje y de la ejecución que seguir |
Qué cambia respecto a un modelo solo
| Asunto | Con un asistente |
|---|---|
| El campo model | Elige el modelo de esta respuesta, no el asistente |
| Mensajes tool e imágenes | Rechazados: el asistente llama él mismo a sus herramientas |
| Recuento de tokens | Una estimación, y completion_tokens vale 0 |
| Acceso | El token de acceso de una persona, no una clave de API |
Para un modelo sin asistente, quédate con la conversación simple y una clave de API.