- Agent Server OpenAI 协议:AG-Kit Agent 可暴露兼容 OpenAI 的端点
- Client OpenAI 协议:AG-Kit 可消费来自其他服务的兼容 OpenAI 的应用程序接口
AG-Kit OpenAI 支持
Agent Server OpenAI 协议
AG-Kit Server 在/chat/completions(云环境中为 /v1/aibot/bots/{AgentId}/chat/completions)提供兼容 OpenAI 的端点:
AG-Kit 对 OpenAI 应用程序接口的兼容性支持
/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
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.'
});
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);
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);
}
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;
}
}
此页面对您有帮助吗?