Skip to main content

Complete Chat

Basic and advanced examples for non-streaming chat completions.

Basic Completion

import { Gateway } from "@adaline/gateway";
import { OpenAI } from "@adaline/openai";
import { Config } from "@adaline/types";

const gateway = new Gateway();
const openai = new OpenAI();
const gpt4o = openai.chatModel({ modelName: "gpt-4o", apiKey: process.env.OPENAI_API_KEY });

const response = await gateway.completeChat({
  model: gpt4o,
  config: Config().parse({ temperature: 0.7, maxTokens: 500 }),
  messages: [
    { role: "system", content: [{ modality: "text", value: "You are a helpful assistant." }] },
    { role: "user", content: [{ modality: "text", value: "What is TypeScript?" }] },
  ],
  tools: [],
});

console.log(response.response);
console.log(`Tokens used: ${response.usage.totalTokens}`);

Multi-turn Conversation

const messages = [
  { role: "system", content: [{ modality: "text", value: "You are a cooking assistant." }] },
  { role: "user", content: [{ modality: "text", value: "How do I make pasta?" }] },
];

// First turn
const turn1 = await gateway.completeChat({
  model: gpt4o,
  config: Config().parse({ temperature: 0.7 }),
  messages,
  tools: [],
});

// Add assistant response and user follow-up
messages.push(turn1.response);
messages.push({
  role: "user",
  content: [{ modality: "text", value: "What sauce goes well with it?" }],
});

// Second turn
const turn2 = await gateway.completeChat({
  model: gpt4o,
  config: Config().parse({ temperature: 0.7 }),
  messages,
  tools: [],
});

Switching Providers

The same code works across any provider — just swap the model:
import { Anthropic } from "@adaline/anthropic";
import { Google } from "@adaline/google";

// Anthropic
const anthropic = new Anthropic();
const claude = anthropic.chatModel({ modelName: "claude-3-5-sonnet-20241022", apiKey: process.env.ANTHROPIC_API_KEY });

// Google
const google = new Google();
const gemini = google.chatModel({ modelName: "gemini-1.5-pro", apiKey: process.env.GOOGLE_API_KEY });

// Same gateway, same interface
const response1 = await gateway.completeChat({ model: claude, config, messages, tools: [] });
const response2 = await gateway.completeChat({ model: gemini, config, messages, tools: [] });