AutoGen 0.7 : AgentChat チュートリアル – モデル

エージェントは OpenAI, Azure OpenAI や ローカルモデルのような LLM モデルサービスへのアクセスが必要です。autogen-core はモデルクライアント用のプロトコルを実装し、autogen-ext はポピュラーなモデルサービス用のモデルクライアントのセットを実装しています。AgentChat はこれらのモデルクライアントを使用してモデルサービスと相互作用できます。

AutoGen 0.7 : AgentChat チュートリアル – モデル

作成 : クラスキャット・セールスインフォメーション
作成日時 : 08/27/2025
バージョン : python-v0.7.4

* 本記事は microsoft.github.io/autogen の以下のページを独自に翻訳した上でまとめ直し、補足説明を加えています :

* サンプルコードの動作確認はしておりますが、必要な場合には適宜、追加改変しています。
* ご自由にリンクを張って頂いてかまいませんが、sales-info@classcat.com までご一報いただけると嬉しいです。

 

 

AutoGen 0.7 : AgentChat チュートリアル – モデル

多くの場合、エージェントは OpenAI, Azure OpenAI や ローカルモデルのような LLM モデルサービスへのアクセスが必要です。異なる API を備える多くの様々なプロバイダーがありますので、autogen-core はモデルクライアント用のプロトコルを実装し、autogen-ext はポピュラーなモデルサービス用のモデルクライアントのセットを実装しています。AgentChat はこれらのモデルクライアントを使用してモデルサービスと相互作用できます。

このセクションは利用可能なモデルクライアントの簡潔な概要を提供します。For more details on how to use them directly, please refer to Model Clients in the Core API documentation.

 

モデル呼び出しのログ記録

AutoGen は標準 Python ロギング・モジュールを使用してモデル呼び出しや応答のようなイベントをログ記録します。ロガー名は autogen_core.EVENT_LOGGER_NAME で、イベントタイプは LLMCall です。

import logging

from autogen_core import EVENT_LOGGER_NAME

logging.basicConfig(level=logging.WARNING)
logger = logging.getLogger(EVENT_LOGGER_NAME)
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.INFO)

 

OpenAI

OpenAI モデルにアクセスするには、openai 拡張をインストールします、これは OpenAIChatCompletionClient を使用可能にします。

pip install "autogen-ext[openai]"

OpenAI から API キー を取得することも必要です。

from autogen_ext.models.openai import OpenAIChatCompletionClient

openai_model_client = OpenAIChatCompletionClient(
    model="gpt-4o-2024-08-06",
    # api_key="sk-...", # Optional if you have an OPENAI_API_KEY environment variable set.
)

モデルクライアントをテストするには、以下のコードを使用できます :

from autogen_core.models import UserMessage
from autogen_ext.models.openai import OpenAIChatCompletionClient

import asyncio

async def main():
    openai_model_client = OpenAIChatCompletionClient(
        model="gpt-4o-2024-08-06",
        # api_key="sk-...", # Optional if you have an OPENAI_API_KEY environment variable set.
    )

    result = await openai_model_client.create([UserMessage(content="What is the capital of France?", source="user")])
    print(result)
    await openai_model_client.close()

asyncio.run(main())
finish_reason='stop' content='The capital of France is Paris.' usage=RequestUsage(prompt_tokens=15, completion_tokens=7) cached=False logprobs=None thought=None

 

Anthropic (experimental)

AnthropicChatCompletionClient を使用するには、anthropic extra をインストールする必要があります。下部では、anthropic python sdk を使用してモデルにアクセスしています。
Anthropic から API キー を取得する必要もあります。

pip install -U "autogen-ext[anthropic]"
from autogen_core.models import UserMessage
from autogen_ext.models.anthropic import AnthropicChatCompletionClient

import asyncio

async def main():
    anthropic_client = AnthropicChatCompletionClient(
        model="claude-3-7-sonnet-20250219",
        #api_key=...
        )
    result = await anthropic_client.create([UserMessage(content="What is the capital of France?", source="user")])
    print(result)
    await anthropic_client.close()

asyncio.run(main())
finish_reason='stop' content="The capital of France is Paris. It's the country's largest city and a global center for art, fashion, gastronomy, and culture." usage=RequestUsage(prompt_tokens=14, completion_tokens=33) cached=False logprobs=None thought=None

 

Ollama (experimental)

Ollama はマシンでローカルにモデルを実行できるローカルモデルサーバです。

Ollama を使用するには、ollama 拡張をインストールして OllamaChatCompletionClient を使用します。

pip install -U "autogen-ext[ollama]"
from autogen_core.models import UserMessage
from autogen_ext.models.ollama import OllamaChatCompletionClient

# Assuming your Ollama server is running locally on port 11434.
ollama_model_client = OllamaChatCompletionClient(model="llama3.2")

response = await ollama_model_client.create([UserMessage(content="What is the capital of France?", source="user")])
print(response)
await ollama_model_client.close()
finish_reason='unknown' content='The capital of France is Paris.' usage=RequestUsage(prompt_tokens=32, completion_tokens=8) cached=False logprobs=None thought=None

 

Gemini (experimental)

Gemini は現在、OpenAI 互換 API (beta) を提供しています。そのため、Gemini API で OpenAIChatCompletionClient を使用できます。

from autogen_core.models import UserMessage
from autogen_ext.models.openai import OpenAIChatCompletionClient

model_client = OpenAIChatCompletionClient(
    model="gemini-1.5-flash-8b",
    # api_key="GEMINI_API_KEY",
)

response = await model_client.create([UserMessage(content="What is the capital of France?", source="user")])
print(response)
await model_client.close()
finish_reason='stop' content='Paris\n' usage=RequestUsage(prompt_tokens=7, completion_tokens=2) cached=False logprobs=None thought=None

また、Gemini が新しいモデルを追加するとき、model_info フィールドでモデルの機能を定義する必要があるかもしれません。例えば、gemini-2.0-flash-lite や同様の新しいモデルを使用するために、以下のコードが使用できます :

from autogen_core.models import UserMessage
from autogen_ext.models.openai import OpenAIChatCompletionClient
from autogen_core.models import ModelInfo

model_client = OpenAIChatCompletionClient(
    model="gemini-2.0-flash-lite",
    model_info=ModelInfo(vision=True, function_calling=True, json_output=True, family="unknown", structured_output=True)
    # api_key="GEMINI_API_KEY",
)

response = await model_client.create([UserMessage(content="What is the capital of France?", source="user")])
print(response)
await model_client.close()

 

以上