Coverage for src\agents_sdk_models\anthropic.py: 79%
24 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-06-04 17:38 +0900
« prev ^ index » next coverage.py v7.8.0, created at 2025-06-04 17:38 +0900
1"""
2Anthropic model implementation for OpenAI Agents
3OpenAI AgentsのためのAnthropic Claude モデル実装
4"""
5import os
6from typing import Any, Dict, List, Optional, Union
7from agents.models.openai_chatcompletions import OpenAIChatCompletionsModel
8from openai import AsyncOpenAI
11class ClaudeModel(OpenAIChatCompletionsModel):
12 """
13 Anthropic Claude model implementation that extends OpenAI's chat completions model
14 OpenAIのチャット補完モデルを拡張したAnthropic Claudeモデルの実装
15 """
17 def __init__(
18 self,
19 model: str = "claude-3-5-sonnet-latest",
20 temperature: float = 0.3,
21 api_key: str = None,
22 base_url: str = "https://api.anthropic.com/v1/",
23 thinking: bool = False,
24 **kwargs: Any,
25 ) -> None:
26 """
27 Initialize the Anthropic Claude model with OpenAI compatible interface
28 OpenAI互換インターフェースでAnthropic Claudeモデルを初期化する
30 Args:
31 model (str): Name of the Claude model to use (e.g. "claude-3-5-sonnet-latest")
32 使用するClaudeモデルの名前(例:"claude-3-5-sonnet-latest")
33 temperature (float): Sampling temperature between 0 and 1
34 サンプリング温度(0から1の間)
35 api_key (str): Anthropic API key
36 Anthropic APIキー
37 base_url (str): Base URL for the Anthropic OpenAI compatibility API
38 Anthropic OpenAI互換APIのベースURL
39 thinking (bool): Enable extended thinking for complex reasoning
40 複雑な推論のための拡張思考を有効にする
41 **kwargs: Additional arguments to pass to the OpenAI API
42 OpenAI APIに渡す追加の引数
43 """
44 # get_llm経由で base_url が None の場合はデフォルトの URL を設定
45 if base_url == None:
46 base_url = "https://api.anthropic.com/v1/"
48 # api_key が None の場合は環境変数から取得
49 if api_key is None:
50 api_key = os.environ.get("ANTHROPIC_API_KEY")
51 if api_key is None:
52 raise ValueError("Anthropic API key is required. Get one from https://console.anthropic.com/")
54 # Create AsyncOpenAI client with Anthropic base URL
55 # AnthropicのベースURLでAsyncOpenAIクライアントを作成
56 openai_client = AsyncOpenAI(base_url=base_url, api_key=api_key)
58 # Store the AsyncOpenAI client on the instance for direct access
59 # テストで参照できるよう AsyncOpenAI クライアントをインスタンスに保存する
60 self.openai_client = openai_client
62 # Store parameters for later use in API calls
63 # 後でAPIコールで使用するためにパラメータを保存
64 self.temperature = temperature
65 self.thinking = thinking
66 self.kwargs = kwargs
68 # Initialize the parent class with our custom client
69 # カスタムクライアントで親クラスを初期化
70 super().__init__(
71 model=model,
72 openai_client=openai_client
73 )
75 # Override methods that make API calls to include our parameters
76 # APIコールを行うメソッドをオーバーライドして、パラメータを含める
77 async def _create_chat_completion(self, *args, **kwargs):
78 """Override to include temperature, thinking and other parameters"""
79 kwargs["temperature"] = self.temperature
81 # Add thinking parameter if enabled
82 # 拡張思考が有効な場合はthinkingパラメータを追加
83 if self.thinking:
84 kwargs["thinking"] = True
86 # Add any other custom parameters
87 # その他のカスタムパラメータを追加
88 kwargs.update(self.kwargs)
90 return await super()._create_chat_completion(*args, **kwargs)