Skip to main content
AG-Kit provides OpenAI API compatibility through two main approaches:
  1. Agent Server OpenAI Protocol: AG-Kit agents can expose OpenAI-compatible endpoints
  2. Client OpenAI Protocol: AG-Kit can consume OpenAI-compatible APIs from other services

AG-Kit OpenAI Support

Agent Server OpenAI Protocol

AG-Kit Server provides OpenAI-compatible endpoints at /chat/completions (or /v1/aibot/bots/{AgentId}/chat/completions in cloud environments):
import { run } from '@ag-kit/server';
import { Agent } from '@ag-kit/agents';
import { OpenAIProvider } from '@ag-kit/providers/openai';

const provider = new OpenAIProvider({
  apiKey: process.env.OPENAI_API_KEY,
  defaultModel: 'gpt-4'
});

const agent = new Agent({
  name: 'openai-compatible-agent',
  model: provider,
  instructions: 'You are a helpful assistant.'
});

run({
  createAgent: () => ({ agent }),
  port: 3000
});
// OpenAI API available at http://localhost:3000/chat/completions

Client OpenAI Protocol

AG-Kit can also consume OpenAI-compatible APIs from other services:
import { Agent } from '@ag-kit/agents';
import { OpenAIProvider } from '@ag-kit/providers/openai';

// Use external OpenAI-compatible service
const provider = new OpenAIProvider({
  apiKey: 'your-api-key',
  baseURL: 'https://api.openai.com/v1' // or any OpenAI-compatible endpoint
});

const agent = new Agent({
  name: 'openai-client-agent',
  model: provider,
  instructions: 'You are an agent using external OpenAI API.'
});

Client Integration

OpenAI SDK Integration

Use AG-Kit agents with the official OpenAI SDK:
import OpenAI from 'openai';

const openai = new OpenAI({
  apiKey: 'your-api-key',
  baseURL: 'http://localhost:3000'
});

const response = await openai.chat.completions.create({
  model: 'gpt-4', // Model name (can be any string)
  messages: [
    { role: 'user', content: 'Hello, Agent!' }
  ]
});

console.log(response.choices[0].message.content);

Function Calling Support

AG-Kit agents support OpenAI function calling:
const functions = [
  {
    name: 'get_weather',
    description: 'Get weather information for a location',
    parameters: {
      type: 'object',
      properties: {
        location: {
          type: 'string',
          description: 'The city and state, e.g. San Francisco, CA'
        }
      },
      required: ['location']
    }
  }
];

const response = await openai.chat.completions.create({
  model: 'gpt-4',
  messages: [
    { role: 'user', content: 'What is the weather in New York?' }
  ],
  functions: functions,
  function_call: 'auto'
});

if (response.choices[0].message.function_call) {
  const functionCall = response.choices[0].message.function_call;
  console.log('Function called:', functionCall.name);
}

Smart Hardware Integration

IoT Device Integration

Connect smart hardware devices to AG-Kit agents:
class SmartHomeController {
  constructor(private apiKey: string, private agentUrl: string) {}
  
  async processVoiceCommand(command: string) {
    const response = await fetch(`${this.agentUrl}/chat/completions`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${this.apiKey}`
      },
      body: JSON.stringify({
        model: 'gpt-4',
        messages: [
          { role: 'system', content: 'You are a smart home assistant.' },
          { role: 'user', content: command }
        ]
      })
    });
    
    const data = await response.json();
    return data.choices[0].message.content;
  }
}

Key Features

  • Chat Completions: Full support for chat completion endpoints
  • Function Calling: Complete function calling and tool use support
  • Streaming: Real-time streaming responses
  • Error Handling: OpenAI-compatible error codes and messages
  • Authentication: API key authentication support

Compatibility Matrix

FeatureOpenAI APIAG-Kit SupportStatus
Chat Completionsv1Full supportStable
Function Callingv1Full supportStable
Streamingv1Full supportStable
Error Handlingv1Full supportStable
Authenticationv1Full supportStable

Next Steps

OpenAI SDK Integration

Learn how to integrate with OpenAI SDK

Smart Hardware Examples

Explore smart hardware integration examples