2222class ChatClaude (Chat ):
2323 MAX_IMAGE_SIZE_MB = 4.0
2424
25- def __init__ (self , model : str , instruction : str , bad_response : str , history_size : int , history_char_limit : int ,
25+ def __init__ (self , model : str , temperature : float , instruction : str , bad_response : str , history_size : int , history_char_limit : int ,
2626 api_key_envvar : str = None , claude_options : dict = None ):
2727
28+ super ().__init__ (
29+ model = model ,
30+ temperature = temperature ,
31+ instruction = instruction ,
32+ bad_response = bad_response ,
33+ history_size = history_size ,
34+ history_char_limit = history_char_limit
35+ )
36+
2837 self .claude_options = claude_options
2938
3039 if api_key_envvar :
3140 api_key = os .environ .get (api_key_envvar )
3241 else :
3342 api_key = os .environ .get ("ANTHROPIC_API_KEY" )
3443
35- client = None
44+ self . _client = None
3645 if api_key :
37- client = anthropic .Anthropic (api_key = api_key )
38-
39- super ().__init__ (
40- client = client ,
41- model = model ,
42- instruction = instruction ,
43- bad_response = bad_response ,
44- history_size = history_size ,
45- history_char_limit = history_char_limit
46- )
46+ self ._client = anthropic .Anthropic (api_key = api_key )
4747
4848 if api_key is None :
4949 self .client_creation_error = get_text_resource ("ERROR_MISSING_ANTHROPIC_API_KEY" )
@@ -52,11 +52,13 @@ def __init__(self, model: str, instruction: str, bad_response: str, history_size
5252 def send_onetime_message (self , text :str ):
5353 messages = []
5454 messages .append ({"role" : "user" , "content" : text })
55- response = self .client .messages .create (
55+
56+ response = self ._client .messages .create (
5657 max_tokens = 4096 ,
57- system = self .instruction ,
58+ system = self ._instruction ,
5859 messages = messages ,
59- model = self .model )
60+ model = self ._model
61+ )
6062 return response .content [0 ].text
6163
6264 # メッセージを送信して回答を得る
@@ -67,10 +69,10 @@ def send_message(
6769 listener : SendMessageListener ) -> str :
6870
6971 try :
70- self .stop_send_event .clear ()
72+ self ._stop_send_event .clear ()
7173
7274 self .messages .append ({"role" : "user" , "content" : text })
73- messages = copy .deepcopy (self .get_history ())
75+ messages = copy .deepcopy (self ._get_history ())
7476
7577 if images and len (images ) > 0 :
7678 messages = messages [:- 1 ]
@@ -102,18 +104,23 @@ def send_message(
102104 "type" : "disabled"
103105 }
104106
105- with self .client .messages .stream (
107+ temperature = anthropic .omit
108+ if self ._temperature :
109+ temperature = self ._temperature
110+
111+ with self ._client .messages .stream (
106112 max_tokens = max_tokens ,
107113 thinking = thinking ,
108- system = self .instruction ,
114+ system = self ._instruction ,
109115 messages = messages ,
110- model = self .model ,
116+ model = self ._model ,
117+ temperature = temperature
111118 ) as stream :
112119 code_block = 0
113120 code_block_inside = False
114121
115122 for text in stream .text_stream :
116- if self .stop_send_event .is_set ():
123+ if self ._stop_send_event .is_set ():
117124 break
118125
119126 if text is not None :
@@ -154,8 +161,8 @@ def send_message(
154161 listener .on_end_response (content )
155162 return content
156163 else :
157- listener .on_end_response (self .bad_response )
158- return self .bad_response
164+ listener .on_end_response (self ._bad_response )
165+ return self ._bad_response
159166 except anthropic .APITimeoutError as e :
160167 listener .on_error (e , "Timeout" )
161168 except anthropic .APIConnectionError as e :
0 commit comments