Skip to content
Documentation menu

Examples

Each example assumes you finished the Quickstart and have your key in ACG_API_KEY. Use the official OpenAI SDKs; no custom SDK is needed.

Python

import os
from openai import OpenAI

client = OpenAI(
    base_url="https://api.aicloudgateway.com/v1",
    api_key=os.environ["ACG_API_KEY"],
)
reply = client.chat.completions.create(
    model="auto",  # or "auto:cost", "auto:quality", or a model id
    messages=[{"role": "user", "content": "What is a sports car?"}],
)
print(reply.choices[0].message.content)

JavaScript / TypeScript

import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.aicloudgateway.com/v1",
  apiKey: process.env.ACG_API_KEY,
});
const reply = await client.chat.completions.create({
  model: "auto",
  messages: [{ role: "user", content: "What is a sports car?" }],
});
console.log(reply.choices[0].message.content);

Stream the answer

Use streaming for long answers: a non-streamed request must send its first byte within 60 seconds.

stream = client.chat.completions.create(
    model="auto",
    messages=[{"role": "user", "content": "Explain how a turbocharger works."}],
    stream=True,
)
for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)

More in the full guide

The GitHub guide also covers LangChain, seeing which model answered, tool calls, JSON output, images, tags, error handling, and an agent on Amazon Bedrock AgentCore.

Full guide: examples.md on GitHub