66# このソースコードは MITライセンス の下でライセンスされています。
77# ライセンスの詳細については、このプロジェクトのLICENSEファイルを参照してください。
88
9+ import json
910import os
11+ from datetime import datetime
1012from typing import Tuple
1113
1214from openai import OpenAI
1315from openai import AzureOpenAI
1416
1517# チャット基底クラス
1618class Chat :
17- def __init__ (self , client , model : str , instruction : str , bad_response : str , history_size : int ):
19+ def __init__ (self , client , model : str , instruction : str , bad_response : str , history_size : int , log_folder : str ):
1820 self .messages = []
1921 self .client = client
2022 self .model = model
2123 self .instruction = instruction
2224 self .bad_response = bad_response
2325 self .history_size = history_size
26+ self .log_folder = log_folder
27+ self .chat_start_time = datetime .now ()
2428
2529 # メッセージを送信して回答を得る
2630 def send_message (self , text : str ) -> Tuple [str , int ]:
@@ -32,13 +36,27 @@ def send_message(self, text: str) -> Tuple[str, int]:
3236 content = response .choices [0 ].message .content
3337 if content :
3438 self .messages .append ({"role" : role , "content" : content })
39+ self .write_chat_log ()
3540 return content , response .usage .total_tokens
3641 else :
3742 return self .bad_response , 0
43+
44+ # チャットのログを保存する
45+ def write_chat_log (self ):
46+ if self .log_folder == "" :
47+ return
48+
49+ if not os .path .exists (self .log_folder ):
50+ os .mkdir (self .log_folder )
51+
52+ filename = self .chat_start_time .strftime ("chatlog-%Y%m%d-%H%M%S.txt" )
53+ path = os .path .join (self .log_folder , filename )
54+ with open (path , "w" , encoding = "utf-8" ) as file :
55+ json .dump (self .messages , file , ensure_ascii = False , indent = 4 )
3856
3957# OpenAI チャットクラス
4058class ChatOpenAI (Chat ):
41- def __init__ (self , model : str , instruction : str , bad_response : str , history_size : int ):
59+ def __init__ (self , model : str , instruction : str , bad_response : str , history_size : int , log_folder : str ):
4260 api_key = os .environ .get ("OPENAI_API_KEY" )
4361 if api_key is None :
4462 raise ValueError ("環境変数 OPENAI_API_KEY が設定されていません。" )
@@ -49,12 +67,13 @@ def __init__(self, model: str, instruction: str, bad_response: str, history_size
4967 model = model ,
5068 instruction = instruction ,
5169 bad_response = bad_response ,
52- history_size = history_size
70+ history_size = history_size ,
71+ log_folder = log_folder
5372 )
5473
5574# Azure OpenAI チャットクラス
5675class ChatAzureOpenAI (Chat ):
57- def __init__ (self , model : str , instruction : str , bad_response : str , history_size : int ):
76+ def __init__ (self , model : str , instruction : str , bad_response : str , history_size : int , log_folder : str ):
5877 endpoint = os .environ .get ("AZURE_OPENAI_ENDPOINT" )
5978 if endpoint is None :
6079 raise ValueError ("環境変数 AZURE_OPENAI_ENDPOINT が設定されていません。" )
@@ -69,17 +88,18 @@ def __init__(self, model: str, instruction: str, bad_response: str, history_size
6988 model = model ,
7089 instruction = instruction ,
7190 bad_response = bad_response ,
72- history_size = history_size
91+ history_size = history_size ,
92+ log_folder = log_folder
7393 )
7494
7595# チャットファクトリー
7696class ChatFactory :
7797 # api_idに基づいてChatオブジェクトを作成する
7898 @classmethod
79- def create (cls , api_id : str , model : str , instruction : str , bad_response : str , history_size : int ) -> Chat :
99+ def create (cls , api_id : str , model : str , instruction : str , bad_response : str , history_size : int , log_folder : str ) -> Chat :
80100 if api_id == "OpenAI" :
81- return ChatOpenAI (model , instruction , bad_response , history_size )
101+ return ChatOpenAI (model , instruction , bad_response , history_size , log_folder )
82102 elif api_id == "AzureOpenAI" :
83- return ChatAzureOpenAI (model , instruction , bad_response , history_size )
103+ return ChatAzureOpenAI (model , instruction , bad_response , history_size , log_folder )
84104 else :
85105 raise ValueError ("API IDが間違っています。" )
0 commit comments