Coverage for src\agents_sdk_models\ollama.py: 85%
20 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"""
2Ollama model implementation for OpenAI Agents
3OpenAI AgentsのためのOllamaモデル実装
4"""
5import os
6from typing import Any, Dict, List, Optional, Union
7from agents.models.openai_chatcompletions import OpenAIChatCompletionsModel
8from openai import AsyncOpenAI
10class OllamaModel(OpenAIChatCompletionsModel):
11 """
12 Ollama model implementation that extends OpenAI's chat completions model
13 OpenAIのチャット補完モデルを拡張したOllamaモデルの実装
14 """
16 def __init__(
17 self,
18 model: str = "phi4-mini:latest",
19 temperature: float = 0.3,
20 base_url: str = None, # デフォルトのURL
21 **kwargs: Any,
22 ) -> None:
23 """
24 Initialize the Ollama model with OpenAI compatible interface
25 OpenAI互換インターフェースでOllamaモデルを初期化する
27 Args:
28 model (str): Name of the Ollama model to use (e.g. "phi4-mini")
29 使用するOllamaモデルの名前(例:"phi4-mini")
30 temperature (float): Sampling temperature between 0 and 1
31 サンプリング温度(0から1の間)
32 base_url (str): Base URL for the Ollama API
33 Ollama APIのベースURL
34 **kwargs: Additional arguments to pass to the OpenAI API
35 OpenAI APIに渡す追加の引数
36 """
37 # get_llm経由で base_url が None の場合はデフォルトの URL を設定
38 if base_url == None:
39 base_url = os.environ.get("OLLAMA_BASE_URL", "http://localhost:11434")
41 base_url = base_url.rstrip("/")
42 if not base_url.endswith("v1"):
43 base_url = base_url + "/v1"
45 # Create AsyncOpenAI client with Ollama base URL
46 # OllamaのベースURLでAsyncOpenAIクライアントを作成
47 openai_client = AsyncOpenAI(base_url=base_url, api_key="ollama")
49 # Store the AsyncOpenAI client on the instance for direct access
50 # テストで参照できるよう AsyncOpenAI クライアントをインスタンスに保存する
51 self.openai_client = openai_client
53 # Store parameters for later use in API calls
54 # 後でAPIコールで使用するためにパラメータを保存
55 self.temperature = temperature
56 self.kwargs = kwargs
58 # Initialize the parent class with our custom client
59 # カスタムクライアントで親クラスを初期化
60 super().__init__(
61 model=model,
62 openai_client=openai_client
63 )
65 # Override methods that make API calls to include our parameters
66 # APIコールを行うメソッドをオーバーライドして、パラメータを含める
67 async def _create_chat_completion(self, *args, **kwargs):
68 """Override to include temperature and other parameters"""
69 kwargs["temperature"] = self.temperature
70 kwargs.update(self.kwargs)
71 return await super()._create_chat_completion(*args, **kwargs)