Skip to content

Commit

Permalink
v0.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
led-mirage committed Dec 29, 2023
1 parent 4120a8c commit 437409e
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 64 deletions.
19 changes: 14 additions & 5 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ A.I.VOICE … https://aivoice.jp/

以下のリンクから ZundaGPT.ZIP をダウンロードして、作成したフォルダに展開するのだ。

https://github.com/led-mirage/ZundaGPT/releases/tag/v0.4.0
https://github.com/led-mirage/ZundaGPT/releases/tag/v0.5.0

#### 3. 実行

Expand Down Expand Up @@ -300,11 +300,15 @@ A.I.VOICEのDLLのパスを記載するのだ。この項目がない場合は

チャットのログはlogファルダに出力されるのだ。出力先はsettings.jsonファイルで変更することもできるのだ。アプリが起動してから終了するまでを1ファイルで出力するのだよ。

### 🗒️ OpenAI API利用量確認ファイル
### 🗒️ <s>OpenAI API利用量確認ファイル</s>

`monthly_token_usage.json`ファイルは、このアプリで使用したOpenAI APIの利用量(トークン数)を月別に記録するファイルなのだ。利用料はトークン数(会話の量)で算出されるので、この値を参考にするといいのだ。
<s>`monthly_token_usage.json`ファイルは、このアプリで使用したOpenAI APIの利用量(トークン数)を月別に記録するファイルなのだ。利用料はトークン数(会話の量)で算出されるので、この値を参考にするといいのだ。</s>

ただ、モデルによる利用料金の違いや、質問と回答に対する利用料金が異なるなどの細かい点は考慮していないので、あくまで参考までとして欲しいのだ。正確な使用量はOpenAIのサイトで確認してほしいのだ。
<s>ただ、モデルによる利用料金の違いや、質問と回答に対する利用料金が異なるなどの細かい点は考慮していないので、あくまで参考までとして欲しいのだ。正確な使用量はOpenAIのサイトで確認してほしいのだ。</s>

OpenAI APIの使い方を変更したのに伴って、v0.5.0で廃止したのだ。申し訳ないのだ。

使用料はOpenAIのサイトで確認してほしいのだ。

## 注意事項

Expand Down Expand Up @@ -366,7 +370,7 @@ WindowsのIME設定で「以前のバージョンのMicrosoft IMEを使う」と
## バージョン履歴

### 0.1.0 (2023/12/16)

- ファーストリリース

### 0.2.0 (2023/12/18)
Expand All @@ -390,6 +394,11 @@ WindowsのIME設定で「以前のバージョンのMicrosoft IMEを使う」と
- コマンドの追加
- 設定ファイル再編

### 0.5.0 (2023/12/29)

- レスポンス性の向上(OpenAI APIのレスポンスをチャンク処理するように変更)
- 利用トークン記録機能の廃止

## さいごに

楽しいと感じてくれると嬉しいのだ。また、使った感想などをissueなどに書き込んでくれるともっと嬉しいのだ。不定期でバージョンアップしていく予定なのでよろしくお願いしますなのだ。
Expand Down
34 changes: 27 additions & 7 deletions chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import json
import os
from datetime import datetime
from typing import Tuple
from typing import Callable

from openai import OpenAI
from openai import AzureOpenAI
Expand All @@ -27,19 +27,39 @@ def __init__(self, client, model: str, instruction: str, bad_response: str, hist
self.chat_start_time = datetime.now()

# メッセージを送信して回答を得る
def send_message(self, text: str) -> Tuple[str, int]:
def send_message(self, text: str, outputChunk: Callable[[str], None], outputSentence: Callable[[str], None]) -> str:
self.messages.append({"role": "user", "content": text})
messages = self.messages[-self.history_size:]
messages.insert(0, {"role": "system", "content": self.instruction})
response = self.client.chat.completions.create(model=self.model, messages=messages)
role = response.choices[0].message.role
content = response.choices[0].message.content
stream = self.client.chat.completions.create(model=self.model, messages=messages, stream=True)

content = ""
sentence = ""
role = ""
for chunk in stream:
if chunk.choices[0].delta.role is not None:
role = chunk.choices[0].delta.role

if chunk.choices[0].delta.content is not None:
chunk_content = chunk.choices[0].delta.content

content += chunk_content
sentence += chunk_content
outputChunk(chunk_content)

if sentence.endswith(("。", "\n", "?", "!")):
outputSentence(sentence)
sentence = ""

if sentence != "":
outputSentence(sentence)

if content:
self.messages.append({"role": role, "content": content})
self.write_chat_log()
return content, response.usage.total_tokens
return content
else:
return self.bad_response, 0
return self.bad_response

# チャットのログを保存する
def write_chat_log(self):
Expand Down
102 changes: 50 additions & 52 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@
from voicevox_api import VoicevoxAPI

APP_NAME = "ずんだGPT"
APP_VERSION = "0.4.0"
APP_VERSION = "0.5.0"
COPYRIGHT = "Copyright 2023 led-mirage"
SETTING_FILE = "settings.json"
MONTHLY_USAGE_FILE = "monthly_token_usage.json"

settings = None
assistant_character = None
user_character = None

# メイン
def main():
global assistant_character, user_character
global settings, assistant_character, user_character

print_apptitle()

Expand All @@ -53,13 +53,10 @@ def main():
assistant_character = CharacterFactory.create_assistant_character(settings)
user_character = CharacterFactory.create_user_character(settings)

monthly_usage = MonthlyUsage(MONTHLY_USAGE_FILE)
monthly_usage.load()

while True:
message = input("あなた > ")
if message[0] == "@" or message[0] == "+" or message[0] == "-":
if exec_command(settings, message, chat):
if exec_command(message, chat):
print()
continue

Expand All @@ -73,7 +70,8 @@ def main():

print(f"{settings.get_chat_character_name()} > ")
try:
response, tokens = chat.send_message(message)
chat.send_message(message, outputChunk, outputSentence)
print()
except openai.AuthenticationError as err:
print("APIの認証に失敗しました")
print(err.message)
Expand All @@ -83,18 +81,18 @@ def main():
print(err.message)
sys.exit()

monthly_usage.add_token(tokens)

response = response.replace("。", "。;")
sentences = response.split(";")
for text in sentences:
if text != "":
print(text)
if settings.get_assistant_echo_enable() and assistant_character is not None:
try:
assistant_character.talk(text)
except:
pass
# チャット応答のチャンク出力用コールバック関数
def outputChunk(chunk):
print(chunk, end="", flush=True)

# チャット応答のセンテンス読み上げ用コールバック関数
def outputSentence(sentence):
if settings.get_assistant_echo_enable() and assistant_character is not None:
try:
assistant_character.talk(sentence)
except:
pass
if not sentence.endswith("\n"):
print()

# タイトルを表示する
Expand All @@ -107,52 +105,52 @@ def print_apptitle():
print(f"")

# コマンドの実行
def exec_command(settings, command, chat):
def exec_command(command, chat):
words = command.lower().split()

if words[0] == "@assistant":
exec_command_assistant(settings, words)
exec_command_assistant()
elif words[0] == "@assistant_echo" or words[0] == "@echo":
exec_command_assistant_echo(settings, words)
exec_command_assistant_echo(words)
elif words[0] == "@assistant_tts_software" or words[0] == "@tts_software":
exec_command_assistant_tts_software(settings, words)
exec_command_assistant_tts_software(words)
elif words[0] == "@assistant_speaker_id" or words[0] == "@speaker_id":
exec_command_assistant_speaker_id(settings, words)
exec_command_assistant_speaker_id(words)
elif words[0] == "@assistant_speed_scale" or words[0] == "@speed_scale":
exec_command_assistant_speed_scale(settings, words)
exec_command_assistant_speed_scale(words)
elif words[0] == "@assistant_pitch_scale" or words[0] == "@pitch_scale":
exec_command_assistant_pitch_scale(settings, words)
exec_command_assistant_pitch_scale(words)
elif words[0] == "@user":
exec_command_user(settings, words)
exec_command_user()
elif words[0] == "@user_echo":
exec_command_user_echo(settings, words)
exec_command_user_echo(words)
elif words[0] == "@user_tts_software":
exec_command_user_tts_software(settings, words)
exec_command_user_tts_software(words)
elif words[0] == "@user_speaker_id":
exec_command_user_speaker_id(settings, words)
exec_command_user_speaker_id(words)
elif words[0] == "@user_speed_scale":
exec_command_user_speed_scale(settings, words)
exec_command_user_speed_scale(words)
elif words[0] == "@user_pitch_scale":
exec_command_user_pitch_scale(settings, words)
exec_command_user_pitch_scale(words)
elif words[0] == "@prev" or words[0] == "-":
exec_command_prev(settings, chat)
exec_command_prev(chat)
elif words[0] == "@next" or words[0] == "+":
exec_command_next(settings, chat)
exec_command_next(chat)
else:
return False

return True

# assistant情報の表示
def exec_command_assistant(settings: Settings, words: List[str]):
def exec_command_assistant():
print(f"echo : {"on" if settings.get_assistant_echo_enable() else "off"}")
print(f"tts_software : {settings.get_assistant_tts_software()}")
print(f"speaker_id : {settings.get_assistant_speaker_id()}")
print(f"speed_scale : {settings.get_assistant_speed_scale()}")
print(f"pitch_scale : {settings.get_assistant_pitch_scale()}")

# assistant_echoの設定
def exec_command_assistant_echo(settings: Settings, words: List[str]):
def exec_command_assistant_echo(words: List[str]):
if len(words) == 1:
if settings.get_assistant_echo_enable():
state = "有効"
Expand All @@ -179,7 +177,7 @@ def exec_command_assistant_echo(settings: Settings, words: List[str]):
print("無効なコマンドなのだ")

# assistant_tts_softwareの設定
def exec_command_assistant_tts_software(settings: Settings, words: List[str]):
def exec_command_assistant_tts_software(words: List[str]):
global assistant_character

if len(words) == 1:
Expand All @@ -195,7 +193,7 @@ def exec_command_assistant_tts_software(settings: Settings, words: List[str]):
print("無効なコマンドなのだ")

# assistant_speaker_idの設定
def exec_command_assistant_speaker_id(settings: Settings, words: List[str]):
def exec_command_assistant_speaker_id(words: List[str]):
global assistant_character

if len(words) == 1:
Expand All @@ -208,7 +206,7 @@ def exec_command_assistant_speaker_id(settings: Settings, words: List[str]):
print(f"assistant_speaker_idを '{val}' に変更したのだ")

# assistant_speed_scaleの設定
def exec_command_assistant_speed_scale(settings: Settings, words: List[str]):
def exec_command_assistant_speed_scale(words: List[str]):
global assistant_character

if len(words) == 1:
Expand All @@ -224,7 +222,7 @@ def exec_command_assistant_speed_scale(settings: Settings, words: List[str]):
print("無効なコマンドなのだ")

# assistant_pitch_scaleの設定
def exec_command_assistant_pitch_scale(settings: Settings, words: List[str]):
def exec_command_assistant_pitch_scale(words: List[str]):
global assistant_character

if len(words) == 1:
Expand All @@ -240,15 +238,15 @@ def exec_command_assistant_pitch_scale(settings: Settings, words: List[str]):
print("無効なコマンドなのだ")

# user情報の表示
def exec_command_user(settings: Settings, words: List[str]):
def exec_command_user():
print(f"echo : {"on" if settings.get_user_echo_enable() else "off"}")
print(f"tts_software : {settings.get_user_tts_software()}")
print(f"speaker_id : {settings.get_user_speaker_id()}")
print(f"speed_scale : {settings.get_user_speed_scale()}")
print(f"pitch_scale : {settings.get_user_pitch_scale()}")

# user_echoの設定
def exec_command_user_echo(settings: Settings, words: List[str]):
def exec_command_user_echo(words: List[str]):
if len(words) == 1:
if settings.get_user_echo_enable():
state = "有効"
Expand All @@ -275,7 +273,7 @@ def exec_command_user_echo(settings: Settings, words: List[str]):
print("無効なコマンドなのだ")

# user_tts_softwareの設定
def exec_command_user_tts_software(settings: Settings, words: List[str]):
def exec_command_user_tts_software(words: List[str]):
global user_character

if len(words) == 1:
Expand All @@ -291,7 +289,7 @@ def exec_command_user_tts_software(settings: Settings, words: List[str]):
print("無効なコマンドなのだ")

# user_speaker_idの設定
def exec_command_user_speaker_id(settings: Settings, words: List[str]):
def exec_command_user_speaker_id(words: List[str]):
global user_character

if len(words) == 1:
Expand All @@ -304,7 +302,7 @@ def exec_command_user_speaker_id(settings: Settings, words: List[str]):
print(f"user_speaker_idを '{val}' に変更したのだ")

# user_speed_scaleの設定
def exec_command_user_speed_scale(settings: Settings, words: List[str]):
def exec_command_user_speed_scale(words: List[str]):
global user_character

if len(words) == 1:
Expand All @@ -320,7 +318,7 @@ def exec_command_user_speed_scale(settings: Settings, words: List[str]):
print("無効なコマンドなのだ")

# user_pitch_scaleの設定
def exec_command_user_pitch_scale(settings: Settings, words: List[str]):
def exec_command_user_pitch_scale(words: List[str]):
global user_character

if len(words) == 1:
Expand All @@ -336,17 +334,17 @@ def exec_command_user_pitch_scale(settings: Settings, words: List[str]):
print("無効なコマンドなのだ")

# ひとつ前のチャット記録を読み込む
def exec_command_prev(settings: Settings, chat: Chat):
def exec_command_prev(chat: Chat):
chat.load_prev()
print_chat_messages(settings, chat)
print_chat_messages(chat)

# ひとつ後のチャット記録を読み込む
def exec_command_next(settings: Settings, chat: Chat):
def exec_command_next(chat: Chat):
chat.load_next()
print_chat_messages(settings, chat)
print_chat_messages(chat)

# チャットメッセージ全体の再表示
def print_chat_messages(settings: Settings, chat: Chat):
def print_chat_messages(chat: Chat):
os.system('cls' if os.name == 'nt' else 'clear')
print_apptitle()
for message in chat.messages:
Expand Down

0 comments on commit 437409e

Please sign in to comment.