from Agents.llms.LlmBase import LlmBase # Import the new base class
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_google_genai.chat_models import ChatGoogleGenerativeAIError
from langchain_core.language_models.chat_models import BaseChatModel # SystemMessage を追加
from langchain_core.messages import HumanMessage, AIMessage, BaseMessage, SystemMessage, AIMessageChunk
from typing import List, Optional, Any # Any を追加
from tools.exception import InterruptedException as InterruptedException
class GeminiLLM(LlmBase): # Inherit from LlmBase
def __init__(self, model_identifier: str = "gemini-1.5-flash", temperature: float = 0, **kwargs):
super().__init__(model_identifier, temperature, **kwargs) # Call base class constructor
# Specific GeminiLLM initialization
# self.llm is already initialized in LlmBase.__init__ via _initialize_llm()
# Easy image support detection (more accurate detection needed)
if "vision" in self.model_name or "gemini-1.5-pro" in self.model_name:
self._supports_images = True
print(f"GeminiLLM: モデル '{self.model_name}' は画像対応の可能性があります。")
else:
self._supports_images = False
def _initialize_llm(self) -> BaseChatModel:
try:
self.llm = ChatGoogleGenerativeAI(
model=self.model_name,
temperature=self.temperature,
**self.llm_kwargs # kwargsではなくself.llm_kwargsを使用
)
return self.llm
except Exception as e:
print(f"GeminiLLM初期化エラー: {e}. モデル名: {self.model_name}")
# フォールバックやデフォルトモデルでの再試行などを検討
raise
@property
def supports_images(self) -> bool:
return self._supports_images
AIによる説明
GeminiLLM クラスのリファレンスマニュアル
このクラスは、Google Gemini をベースとした大規模言語モデル (LLM) を Langchain で利用するためのラッパーです。LlmBase クラスを継承しており、Gemini の様々な機能を簡潔に利用できるように設計されています。
継承: LlmBase
コンストラクタ: __init__(self, model_identifier: str = "gemini-1.5-flash", temperature: float = 0, **kwargs)
model_identifier: str: 使用する Gemini モデルの識別子です。デフォルトは"gemini-1.5-flash"です。 モデル名によって機能が異なるため、適切なモデルを選択する必要があります。temperature: float: 生成テキストのランダム性を制御するパラメータです。0 に近いほど予測可能性が高くなり、1 に近いほどランダム性が高くなります。デフォルトは 0 です。**kwargs:ChatGoogleGenerativeAIに渡される追加のキーワード引数です。 例えば、verboseなどを設定できます。
メソッド:
_initialize_llm(self) -> BaseChatModel:ChatGoogleGenerativeAIオブジェクトを初期化します。self.llm属性に初期化されたChatGoogleGenerativeAIオブジェクトを格納します。 エラー処理を含み、初期化に失敗した場合は例外を発生させます。
プロパティ:
supports_images(self) -> bool: この Gemini モデルが画像処理に対応しているかどうかを示すブール値を返します。"vision"がモデル名に含まれているか、"gemini-1.5-pro"である場合、Trueを返します。 これは簡易的な判定であり、より正確な判定が必要な場合は、追加のロジックを実装する必要があります。
例外処理:
_initialize_llm メソッドは、ChatGoogleGenerativeAI の初期化に失敗した場合、例外を発生させます。 エラーメッセージには、エラーの内容と使用しようとしたモデル名が含まれます。 より堅牢なシステムを構築するには、この例外をキャッチして、フォールバック処理(別のモデルを使用するなど)を実装する必要があります。
依存関係:
Agents.llms.LlmBase: このクラスの基底クラス。langchain_google_genai: Google Gemini API とのインタフェースを提供する Langchain パッケージ。langchain_core.language_models.chat_models: Langchain のチャットモデルの基底クラス。langchain_core.messages: Langchain のメッセージオブジェクト。typing: 型ヒントを提供する標準ライブラリ。tools.exception:InterruptedException例外を定義するカスタムモジュール。
このドキュメントは、GeminiLLM クラスの機能を理解し、使用するためのリファレンスとして役立つことを目的としています。 より詳細な情報が必要な場合は、LlmBase クラスや ChatGoogleGenerativeAI クラスのドキュメントを参照してください。
