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):
from ag_kit_py.server import AgentServiceApp
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}

AgentServiceApp().run(create_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:
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
)

Client Integration

OpenAI SDK Integration

Use AG-Kit agents with the official OpenAI SDK:
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)

Function Calling Support

AG-Kit agents support OpenAI function calling:
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)

Smart Hardware Integration

IoT Device Integration

Connect smart hardware devices to AG-Kit agents:
import requests

class SmartHomeController:
    def __init__(self, api_key: str, agent_url: str):
        self.api_key = api_key
        self.agent_url = agent_url
    
    async def process_voice_command(self, command: str):
        response = requests.post(
            f"{self.agent_url}/chat/completions",
            headers={
                'Content-Type': 'application/json',
                'Authorization': f'Bearer {self.api_key}'
            },
            json={
                'model': 'gpt-4',
                'messages': [
                    {'role': 'system', 'content': 'You are a smart home assistant.'},
                    {'role': 'user', 'content': command}
                ]
            }
        )
        
        data = 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