AutoGen AgentChat : Tutorial : モデル

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

AutoGen AgentChat : Tutorial : モデル

作成 : クラスキャット・セールスインフォメーション
作成日時 : 04/18/2025

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

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

 

クラスキャット 人工知能 研究開発支援サービス ⭐️ リニューアルしました 😉

クラスキャット は人工知能に関する各種サービスを提供しています。お気軽にご相談ください :

  • 人工知能導入個別相談会(無償)実施中! [詳細]

  • 人工知能研究開発支援 [詳細]
    1. 自社特有情報を含むチャットボット構築支援
    2. 画像認識 (医療系含む) / 画像生成

  • PoC(概念実証)を失敗させないための支援 [詳細]

お問合せ : 下記までお願いします。

  • クラスキャット セールス・インフォメーション
  • sales-info@classcat.com
  • ClassCatJP

 

 

AutoGen AgentChat : Tutorial : モデル

多くの場合、エージェントは 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]"

You will also need to obtain an API key from OpenAI.

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

result = await openai_model_client.create([UserMessage(content="What is the capital of France?", source="user")])
print(result)
await openai_model_client.close()
CreateResult(finish_reason='stop', content='The capital of France is Paris.', usage=RequestUsage(prompt_tokens=15, completion_tokens=7), cached=False, logprobs=None)

 

Anthropic (experimental)

AnthropicChatCompletionClient を使用するには、anthropic extra をインストールする必要があります。下部では、anthropic python sdk を使用してモデルにアクセスしています。You will also need to obtain an API key from Anthropic.

Anthropicエクストラは、モデルへのアクセスにAnthropic Python SDKを使用します。また、AnthropicからAPIキーを取得する必要があります。

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

anthropic_client = AnthropicChatCompletionClient(model="claude-3-7-sonnet-20250219")
result = await anthropic_client.create([UserMessage(content="What is the capital of France?", source="user")])
print(result)
await anthropic_client.close()
finish_reason='stop' content="The capital of France is Paris. It's not only the political and administrative capital but also a major global center for art, fashion, gastronomy, and culture. Paris is known for landmarks such as the Eiffel Tower, the Louvre Museum, Notre-Dame Cathedral, and the Champs-Élysées." usage=RequestUsage(prompt_tokens=14, completion_tokens=73) 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()

 

以上