跳转到主要内容
AG-Kit 通过两种主要方式提供 OpenAI 应用程序接口兼容性:
  1. Agent Server OpenAI 协议:AG-Kit Agent 可暴露兼容 OpenAI 的端点
  2. Client OpenAI 协议:AG-Kit 可消费来自其他服务的兼容 OpenAI 的应用程序接口

AG-Kit OpenAI 支持

Agent Server OpenAI 协议

AG-Kit Server 在 /chat/completions(云环境中为 /v1/aibot/bots/{AgentId}/chat/completions)提供兼容 OpenAI 的端点:
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 协议

AG-Kit 也可消费来自其他服务的兼容 OpenAI 的应用程序接口:
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.'
});

客户端集成

OpenAI AG-Kit集成

将 AG-Kit Agent 与官方 OpenAI AG-Kit结合使用:
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);

函数调用支持

AG-Kit Agent 支持 OpenAI 函数调用:
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);
}

智能硬件集成

IoT 设备集成

将智能硬件设备连接至 AG-Kit Agent:
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;
  }
}