OpenAI ChatKit Integration for Microsoft Agent Framework

1.0.0b260409 · active · verified Thu Apr 16

The `agent-framework-chatkit` library provides an integration with OpenAI's ChatKit (beta) for the Microsoft Agent Framework, enabling developers to leverage ChatKit's agent capabilities within their Agent Framework applications. It is currently in active beta development (version 1.0.0b260409) with frequent updates and potential breaking changes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `ChatKitClient`, define and create a simple `BasicChatKitAgent`, and interact with it using text messages. It requires `CHATKIT_API_KEY` and `OPENAI_API_KEY` to be set as environment variables for authentication.

import os
import asyncio
from chatkit import ChatKitClient
from chatkit.agents import BasicChatKitAgent
from chatkit.messages import TextMessage

async def main():
    # Ensure CHATKIT_API_KEY and OPENAI_API_KEY are set as environment variables
    chatkit_api_key = os.environ.get("CHATKIT_API_KEY", "")
    openai_api_key = os.environ.get("OPENAI_API_KEY", "")

    if not chatkit_api_key or not openai_api_key:
        print("Please set CHATKIT_API_KEY and OPENAI_API_KEY environment variables.")
        print("You can obtain these from your respective dashboards (OpenAI and ChatKit). Exiting.")
        return

    client = ChatKitClient(api_key=chatkit_api_key, openai_api_key=openai_api_key)
    agent_name = "my-chatkit-quickstart-agent"

    # Create or update a simple agent definition
    agent = BasicChatKitAgent(
        name=agent_name,
        model="gpt-4o", # Recommended to use a recent model
        instructions="You are a helpful, concise assistant. Respond briefly to questions."
    )
    # This method handles creation if the agent doesn't exist, or updates it if it does.
    await client.create_agent(agent)
    print(f"Agent '{agent_name}' is ready.")

    user_message = TextMessage(content="What is the capital of France?")
    print(f"\nUser: {user_message.content}")

    # Interact with the agent
    async for response_message in client.run_agent_message(
        agent_name=agent_name,
        message=user_message
    ):
        if isinstance(response_message, TextMessage):
            print(f"Agent: {response_message.content}")
        else:
            # Handle other message types like FunctionCallMessage if needed
            print(f"Agent sent a non-text message: {type(response_message).__name__}")

if __name__ == "__main__":
    asyncio.run(main())

view raw JSON →