跳转到主要内容
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 的端点:
from ag_kit_py.server import AGKitAPIApp
from ag_kit_py.agents import LangGraphAgent
from langgraph.graph import StateGraph, MessagesState

def create_agent():
    # Use the actual chat workflow from examples
    from python_sdk.examples.langgraph import build_chat_workflow
    
    workflow = build_chat_workflow()
    agent = LangGraphAgent(
        name='openai-compatible-agent',
        description='You are a helpful assistant.',
        graph=workflow
    )
    return {'agent': agent}

AGKitAPIApp().run(create_agent, port=3000)
# OpenAI API available at http://localhost:3000/chat/completions

Client OpenAI 协议

AG-Kit 也可消费来自其他服务的兼容 OpenAI 的应用程序接口:
from ag_kit_py.agents import LangGraphAgent
from langgraph.graph import StateGraph, MessagesState

# Use the actual chat workflow from examples
from python_sdk.examples.langgraph import build_chat_workflow

workflow = build_chat_workflow()
# Note: External API integration would be configured in the LangGraph workflow
agent = LangGraphAgent(
    name='openai-client-agent',
    description='You are an agent using external OpenAI API.',
    graph=workflow
)

客户端集成

OpenAI AG-Kit集成

将 AG-Kit Agent 与官方 OpenAI AG-Kit结合使用:
import openai

client = openai.OpenAI(
    api_key='your-api-key',
    base_url='http://localhost:3000'
)

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

print(response.choices[0].message.content)

函数调用支持

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

response = client.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:
    function_call = response.choices[0].message.function_call
    print('Function called:', function_call.name)

智能硬件集成

IoT 设备集成

将智能硬件设备连接至 AG-Kit Agent: