Assistants API je vyššia abstrakcia nad Chat Completions, ktorá rieši bežné problémy: pamäť, nástroje, súbory.

Prečo Assistants API?

Pred Assistants API

# Manuálna správa histórie
conversation_history = []

def chat(user_message):
    conversation_history.append({"role": "user", "content": user_message})

    response = client.chat.completions.create(
        model="gpt-4",
        messages=conversation_history
    )

    assistant_message = response.choices[0].message.content
    conversation_history.append({"role": "assistant", "content": assistant_message})

    return assistant_message

S Assistants API

# Automatická správa konverzácií
message = client.beta.threads.messages.create(
    thread_id=thread.id,
    role="user",
    content="Analyzuj tento súbor"
)

run = client.beta.threads.runs.create(
    thread_id=thread.id,
    assistant_id=assistant.id
)

Základné koncepty

Assistant

Konfigurácia AI s inštrukciami a nástrojmi:

assistant = client.beta.assistants.create(
    name="Data Analyst",
    instructions="""Si expert na dátovú analýzu.
    Používaj Python pre výpočty.
    Vždy vysvetli svoje závery.""",
    model="gpt-4o",
    tools=[
        {"type": "code_interpreter"},
        {"type": "file_search"}
    ]
)

Thread

Konverzácia s pamäťou:

thread = client.beta.threads.create()

# Vlákno si pamätá celú históriu
message1 = client.beta.threads.messages.create(
    thread_id=thread.id,
    role="user",
    content="Volám sa Peter"
)

# ... neskôr v konverzácii ...

message2 = client.beta.threads.messages.create(
    thread_id=thread.id,
    role="user",
    content="Ako sa volám?"  # Assistant si pamätá
)

Run

Spustenie asistenta na vlákne:

run = client.beta.threads.runs.create(
    thread_id=thread.id,
    assistant_id=assistant.id
)

# Čakanie na dokončenie
while run.status in ["queued", "in_progress"]:
    time.sleep(1)
    run = client.beta.threads.runs.retrieve(
        thread_id=thread.id,
        run_id=run.id
    )

Vstavané nástroje

Code Interpreter

Spúšťa Python kód v sandboxe:

assistant = client.beta.assistants.create(
    name="Calculator",
    tools=[{"type": "code_interpreter"}],
    model="gpt-4o"
)

# "Vypočítaj priemer z [1, 2, 3, 4, 5]"
# -> Assistant napíše a spustí Python kód

File Search (RAG)

Automatický RAG nad uploadnutými súbormi:

# Upload súboru
file = client.files.create(
    file=open("report.pdf", "rb"),
    purpose="assistants"
)

# Vytvorenie vector store
vector_store = client.beta.vector_stores.create(
    name="Company Docs"
)

client.beta.vector_stores.files.create(
    vector_store_id=vector_store.id,
    file_id=file.id
)

# Pripojenie k asistentovi
assistant = client.beta.assistants.update(
    assistant_id=assistant.id,
    tool_resources={
        "file_search": {
            "vector_store_ids": [vector_store.id]
        }
    }
)

Function Calling

Vlastné nástroje:

assistant = client.beta.assistants.create(
    name="Weather Bot",
    tools=[{
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Získa počasie pre lokalitu",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {"type": "string"}
                },
                "required": ["location"]
            }
        }
    }],
    model="gpt-4o"
)

Streaming

Real-time odpovede:

with client.beta.threads.runs.stream(
    thread_id=thread.id,
    assistant_id=assistant.id
) as stream:
    for text in stream.text_deltas:
        print(text, end="", flush=True)

Best Practices

  1. Jeden asistent per use case - nemiešajte zodpovednosti
  2. Thread per user/session - izolácia konverzácií
  3. Cleanup - mažte staré threads a files
  4. Error handling - runs môžu zlyhať

Záver

Assistants API výrazne zjednodušuje vývoj AI aplikácií. Menej boilerplate kódu, viac času na produkt.