AI Model Wiki

    Skip to main content
    50 min
    Lesson 4 of 6

    Assistants API & Tools

    From the course OpenAI API Masterclass

    The Assistants API

    The Assistants API provides stateful, multi-turn conversations with built-in tools for code execution, file search, and function calling.

    Creating an Assistant

    const assistant = await openai.beta.assistants.create({
      name: "Data Analyst",
      instructions: "You analyze data and create visualizations.",
      model: "gpt-4o",
      tools: [
        { type: "code_interpreter" },
        { type: "file_search" }
      ],
    });
    

    Threads & Messages

    Conversations happen in threads that persist across sessions:

    const thread = await openai.beta.threads.create();
    
    await openai.beta.threads.messages.create(thread.id, {
      role: "user",
      content: "Analyze the uploaded sales data",
    });
    
    const run = await openai.beta.threads.runs.create(thread.id, {
      assistant_id: assistant.id,
    });
    

    Built-in Tools

    • Code Interpreter: Executes Python code in a sandbox. Processes files, generates charts, performs calculations
    • File Search: Indexes uploaded files with vector search for retrieval
    • Function Calling: Define custom functions the assistant can invoke

    Function Calling Pattern

    const assistant = await openai.beta.assistants.create({
      model: "gpt-4o",
      tools: [{
        type: "function",
        function: {
          name: "get_weather",
          description: "Get current weather for a city",
          parameters: {
            type: "object",
            properties: {
              city: { type: "string" },
              unit: { type: "string", enum: ["celsius", "fahrenheit"] }
            },
            required: ["city"]
          }
        }
      }]
    });
    

    Run Lifecycle

    Runs go through states: queued → in_progress → requires_action → completed. Poll or use streaming to track progress and handle tool calls.

    Key Takeaways

    • •Assistants maintain stateful conversations across sessions via threads
    • •Code Interpreter executes Python for data analysis and visualization
    • •File Search enables RAG over uploaded documents automatically
    • •Function calling connects assistants to external APIs and data
    0 of 6 lessons marked complete

    Progress is kept in this browser only.