Skip to main content

OpenAI ChatKit integration for Microsoft Agent Framework.

Project description

Agent Framework and ChatKit Integration

This package provides an integration layer between Microsoft Agent Framework and OpenAI ChatKit (Python). Specifically, it mirrors the Agent SDK integration, and provides the following helpers:

  • stream_agent_response: A helper to convert a streamed AgentRunResponseUpdate from a Microsoft Agent Framework agent that implements AgentProtocol to ChatKit events.
  • ThreadItemConverter: A extendable helper class to convert ChatKit thread items to ChatMessage objects that can be consumed by an Agent Framework agent.
  • simple_to_agent_input: A helper function that uses the default implementation of ThreadItemConverter to convert a ChatKit thread to a list of ChatMessage, useful for getting started quickly.

Installation

pip install agent-framework-chatkit --pre

This will install agent-framework-core and openai-chatkit as dependencies.

Example Usage

Here's a minimal example showing how to integrate Agent Framework with ChatKit:

from collections.abc import AsyncIterator
from typing import Any

from azure.identity import AzureCliCredential
from fastapi import FastAPI, Request
from fastapi.responses import Response, StreamingResponse

from agent_framework import ChatAgent
from agent_framework.azure import AzureOpenAIChatClient
from agent_framework.chatkit import simple_to_agent_input, stream_agent_response

from chatkit.server import ChatKitServer
from chatkit.types import ThreadMetadata, UserMessageItem, ThreadStreamEvent

# You'll need to implement a Store - see the sample for a SQLiteStore implementation
from your_store import YourStore  # type: ignore[import-not-found]  # Replace with your Store implementation

# Define your agent with tools
agent = ChatAgent(
    chat_client=AzureOpenAIChatClient(credential=AzureCliCredential()),
    instructions="You are a helpful assistant.",
    tools=[],  # Add your tools here
)

# Create a ChatKit server that uses your agent
class MyChatKitServer(ChatKitServer[dict[str, Any]]):
    async def respond(
        self,
        thread: ThreadMetadata,
        input_user_message: UserMessageItem | None,
        context: dict[str, Any],
    ) -> AsyncIterator[ThreadStreamEvent]:
        if input_user_message is None:
            return

        # Convert ChatKit message to Agent Framework format
        agent_messages = await simple_to_agent_input(input_user_message)

        # Run the agent and stream responses
        response_stream = agent.run_stream(agent_messages)

        # Convert agent responses back to ChatKit events
        async for event in stream_agent_response(response_stream, thread.id):
            yield event

# Set up FastAPI endpoint
app = FastAPI()
chatkit_server = MyChatKitServer(YourStore())  # type: ignore[misc]

@app.post("/chatkit")
async def chatkit_endpoint(request: Request):
    result = await chatkit_server.process(await request.body(), {"request": request})

    if hasattr(result, '__aiter__'):  # Streaming
        return StreamingResponse(result, media_type="text/event-stream")  # type: ignore[arg-type]
    else:  # Non-streaming
        return Response(content=result.json, media_type="application/json")  # type: ignore[union-attr]

For a complete end-to-end example with a full frontend, see the weather agent sample.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

agent_framework_chatkit-1.0.0b251112.tar.gz (13.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

agent_framework_chatkit-1.0.0b251112-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file agent_framework_chatkit-1.0.0b251112.tar.gz.

File metadata

File hashes

Hashes for agent_framework_chatkit-1.0.0b251112.tar.gz
Algorithm Hash digest
SHA256 4be6de4cff26f96412e3976ed5b3a01ead8212d6bfad063ba3d80694ee0646f8
MD5 821b91faba62133eb42db6547ca0c1a4
BLAKE2b-256 e7b185b45eb303baf1e5f3181ba8dbacd783d61d21f109ac3f7ba08a02c6606e

See more details on using hashes here.

File details

Details for the file agent_framework_chatkit-1.0.0b251112-py3-none-any.whl.

File metadata

File hashes

Hashes for agent_framework_chatkit-1.0.0b251112-py3-none-any.whl
Algorithm Hash digest
SHA256 d1d25eaabc1a8ce422e2c81828f9334261483e1602e8035521ce5b0fa716f0e4
MD5 67b5644f6ba139703757370340fde4cc
BLAKE2b-256 37d74898ac152b1730cd9a4f7c2168dfb95e9541bba0a1bf3a7242c1fb6dc9ed

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page