DokumentationThreads und Runs
Ihre Assistenten
Threads und Runs
Ein Thread hält eine Konversation mit einem Assistenten fest. Jede Frage startet darin einen Run, den Sie bis zur Antwort verfolgen.
Auf dieser Seite
Eine Konversationsrunde
Einen Thread eröffnen
POST /v1/threadMit der ID des Assistenten.Die Frage stellen
POST /v1/thread/{id}/messagesDie Nachricht der Person.Den Run starten
POST /v1/thread/{id}/runsDer Assistent bearbeitet diese Nachricht. Die Antwort enthält die ID des Runs.
# Open a thread with the assistant.
curl https://api.learnya.ai/v1/thread \
-H "Authorization: Bearer $LEARNYA_TOKEN" \
-H "Content-Type: application/json" \
-d '{"assistantId": "ASSISTANT_ID", "title": "Ticket 42"}'
# Post the question.
curl https://api.learnya.ai/v1/thread/THREAD_ID/messages \
-H "Authorization: Bearer $LEARNYA_TOKEN" \
-H "Content-Type: application/json" \
-d '{"content": "Where is my invoice?"}'
# Start the run on it. The answer carries the run id.
curl https://api.learnya.ai/v1/thread/THREAD_ID/runs \
-H "Authorization: Bearer $LEARNYA_TOKEN" \
-H "Content-Type: application/json" \
-d '{"triggerMessageId": "MESSAGE_ID"}'def post(path, body):
reply = requests.post(
f"{API}{path}", headers=HEADERS, json=body
)
reply.raise_for_status()
return reply.json()
thread = post("/thread", {"assistantId": assistant["id"]})
question = post(
f"/thread/{thread['id']}/messages",
{"content": "Where is my invoice?"},
)
run = post(
f"/thread/{thread['id']}/runs",
{"triggerMessageId": question["id"]},
)
run_id = run["id"]async function post(path, body) {
const r = await fetch(`${API}${path}`, {
method: "POST",
headers,
body: JSON.stringify(body),
})
if (!r.ok) throw new Error(`${r.status} on ${path}`)
return r.json()
}
const thread = await post("/thread", {
assistantId: assistant.id,
})
const question = await post(`/thread/${thread.id}/messages`, {
content: "Where is my invoice?",
})
const run = await post(`/thread/${thread.id}/runs`, {
triggerMessageId: question.id,
})
const runId = run.idasync function post<T>(path: string, body: unknown) {
const r = await fetch(`${API}${path}`, {
method: "POST",
headers,
body: JSON.stringify(body),
})
if (!r.ok) throw new Error(`${r.status} on ${path}`)
return (await r.json()) as T
}
const thread = await post<{ id: string }>("/thread", {
assistantId: assistant.id,
})
const question = await post<{ id: string }>(
`/thread/${thread.id}/messages`,
{ content: "Where is my invoice?" },
)
const run = await post<{ id: string; status: string }>(
`/thread/${thread.id}/runs`,
{ triggerMessageId: question.id },
)
const runId = run.idDen Run verfolgen
Fragen Sie den Run ab, bis er endet. Ist er abgeschlossen, steht die Antwort in output.
| Status | Bedeutung |
|---|---|
queued | Wartet auf einen freien Worker |
in_progress | Der Assistent arbeitet |
completed | Abgeschlossen, die Antwort ist bereit |
failed | Durch einen Fehler abgebrochen, beschrieben in error |
canceled | Vor dem Ende gestoppt |
import time
while True:
reply = requests.get(f"{API}/run/{run_id}", headers=HEADERS)
run = reply.json()
if run["status"] in ("completed", "failed", "canceled"):
break
time.sleep(1)
if run["status"] == "completed":
print(run["output"][0]["text"])
else:
print(run["status"], run.get("error"))const DONE = ["completed", "failed", "canceled"]
let run
do {
await new Promise((done) => setTimeout(done, 1000))
run = await fetch(`${API}/run/${runId}`, { headers }).then(
(r) => r.json(),
)
} while (!DONE.includes(run.status))
const done = run.status === "completed"
console.log(done ? run.output[0].text : run.error)type RunStatus =
| "queued"
| "in_progress"
| "completed"
| "failed"
| "canceled"
interface Run {
id: string
status: RunStatus
output?: { type: "text"; text: string }[] | null
error?: string | null
}
const DONE: RunStatus[] = ["completed", "failed", "canceled"]
let run: Run
do {
await new Promise((done) => setTimeout(done, 1000))
const r = await fetch(`${API}/run/${runId}`, { headers })
run = (await r.json()) as Run
} while (!DONE.includes(run.status))
const done = run.status === "completed"
console.log(done ? run.output?.[0].text : run.error)Antwort
{
"id": "c2a7e9d4-1b3f-4e6a-8c5d-9f0b2e4a6d18",
"threadId": "8e4b1d6f-2c9a-4f7e-b3d5-0a6c8e2f4b91",
"assistantId": "6f1c2b0e-3a4d-4c8e-9b7a-2d5e8f1a0c34",
"status": "completed",
"output": [
{ "type": "text", "text": "Your invoice 2026-114 was sent on 2 October." }
],
"modelUsed": "learnya-flash",
"error": null
}Ebenfalls verfügbar
GET /v1/thread/{id}/messages Die Nachrichten des Threads, bis zu 400 pro Seite.
GET /v1/run/{id}/stream Die Antwort als Server-Sent Events, während sie geschrieben wird.
POST /v1/runs/{id}/stop Stoppt einen Run.
GET /v1/threads Die Threads der Person, filterbar nach Assistent.
Einen Run einstellen
modelOverrideText- Das Modell nur für diesen Run: flash oder max.
reasoning_effortText- Der Aufwand fürs Schlussfolgern: none, low, medium oder high.
maxStepsGanzzahl- Die maximale Anzahl Schritte des Runs, von 1 bis 500.
Limits
- 20 Runs pro Minute und Person
- 600 Anfragen pro Minute für die übrigen Routen
- Die Anzahl Runs pro Tag richtet sich nach dem Plan des Arbeitsbereichs