OpenAI ChatKit Integration for Microsoft Agent Framework

raw JSON →
1.0.0b260409 verified Thu Apr 16 auth: no en

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.

pip install agent-framework-chatkit
error ModuleNotFoundError: No module named 'agent_framework_chatkit'
cause Attempted to import library components using the PyPI package name (`agent_framework_chatkit`) instead of the correct import root (`chatkit`).
fix
Correct the import statement. For example, change from agent_framework_chatkit import ChatKitClient to from chatkit import ChatKitClient.
error openai.AuthenticationError: Incorrect API key provided
cause The `OPENAI_API_KEY` environment variable is either missing, empty, or contains an invalid key.
fix
Verify that your OPENAI_API_KEY environment variable is correctly set with a valid OpenAI API key. This key is used by the underlying language models.
error ChatKit API returned an error: Unauthorized
cause The `CHATKIT_API_KEY` environment variable is either missing, empty, or contains an invalid key, preventing authentication with the ChatKit service.
fix
Ensure your CHATKIT_API_KEY environment variable is correctly set with a valid ChatKit API key. This key is for authenticating with the ChatKit service itself.
error AttributeError: 'ChatKitClient' object has no attribute 'run_agent_message' (or similar method not found)
cause The API method being called may have changed or been removed due to the library's beta status, or an outdated version of the library is installed.
fix
Update to the latest beta version (pip install --upgrade agent-framework-chatkit) and consult the official documentation for the current API methods and usage patterns.
breaking The `agent-framework-chatkit` library is currently in beta. This means breaking changes to APIs, class structures, and behavior are frequent and may occur without major version bumps (e.g., within `1.0.0bX` releases).
fix Regularly check the official documentation and GitHub releases for updates and adjust your code accordingly. For stability, consider pinning specific beta versions (`pip install agent-framework-chatkit==1.0.0bYYYYMMDD`) but be prepared to upgrade.
gotcha The PyPI package name is `agent-framework-chatkit`, but the top-level import for components is `chatkit` (e.g., `from chatkit import ChatKitClient`). Directly importing from `agent_framework_chatkit` will result in a `ModuleNotFoundError`.
fix Always use `from chatkit import ...` for importing library components, not `from agent_framework_chatkit import ...`.
gotcha Both `CHATKIT_API_KEY` (for ChatKit service) and `OPENAI_API_KEY` (for underlying OpenAI models) are required for full functionality. Misconfigured or missing keys will lead to authentication failures.
fix Ensure both environment variables `CHATKIT_API_KEY` and `OPENAI_API_KEY` are correctly set with valid keys obtained from OpenAI and ChatKit, respectively.

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())