Requesting more advanced models

This guide uses a more advanced model, GPT-4o, and also explains how to use various chat model capabilities:

  • streaming mode

  • calling tools

  • uploading images to the model for analysis

  • uploading files to the model for analysis

  • web search

circle-info

If you need help with API keys or environment configuration, go back to the previous step and follow the detailed quickstart guide for the Gemma 3 model.


Making an API Call

The chat model used in this example is more advanced. In addition to regular user messages, it supports the system role in the messages parameter, which can be used to define global instructions that affect the model’s overall behavior, for example:

messages: [
    {
      role: "system",
      content: "You are a travel agent. Be descriptive and helpful.",
    },
    {
      role: "user",
      content: "Tell me about San Francisco",
    },
],

Here’s the complete code you can use right away in a cURL, Python, or Node.js program. You only need to replace <YOUR_AIMLAPI_KEY> with your AIML API key from your account, provide your behavior instructions in the system prompt, and place your request to the model in the user prompt.


Using Streaming Mode

Streaming lets the model send partial responses as they’re generated instead of waiting for the full output — useful for real‑time feedback.

Full Streaming Response (Raw Events)

This example shows how to consume the streaming response as-is, without abstraction. Each chunk is processed in real time, exposing the full event structure returned by the API.

Use this approach if you need:

  • access to all event types

  • fine-grained control over parsing

  • debugging or logging of raw responses

  • support for metadata beyond plain text

chevron-rightExample raw streaming responsehashtag

Streaming Response Processing (Text Extraction)

This example shows how to process the streaming response to extract only the generated text. Instead of handling all event types, the code filters incoming chunks and prints the content as it arrives. Use this approach if you only need the generated text.

chevron-rightExample processed clean streaming responsehashtag

Tool calling

GPT‑4o can call functions/tools you define in the API request to extend behavior (e.g., performing calculations, retrieving structured data).

chevron-rightHow it workshashtag
  1. Initial request — The model receives the user prompt and the registered tool, and generates a tool_calls object indicating which function it wants to execute.

  2. Extract and run the tool — Parse the arguments from the tool_calls object and execute the function locally.

  3. Send back the result — Return the computed result to the model using the tool role and the content field.

  4. Final response — The model incorporates the tool’s output and generates a complete answer for the user.

chevron-rightExample responsehashtag

Image upload

GPT‑4o supports vision inputs: you can send an image URL in the messages request to let the model analyze or describe it.

chevron-rightExample responsehashtag

Web search integration

With search‑preview models, you can perform live web search queries in combination with the model to get up‑to‑date results and grounded responses.

chevron-rightExample responsehashtag

Future Steps

Last updated

Was this helpful?