sirochild commited on
Commit
b00de58
·
verified ·
1 Parent(s): defafed

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -16
app.py CHANGED
@@ -51,18 +51,37 @@ DEFAULT_SCENE_PARAMS = {
51
 
52
  # --- 2. 機能定義 ---
53
 
 
54
  def detect_scene_change(history, message):
55
- history_text = "\n".join([f"ユーザー: {u}\n麻理: {m}" for u, m in history[-4:]])
 
 
 
 
 
56
  prompt = f"""
57
- あなたは会話の流れを分析するエキスパートです。以下のタスクを厳密に実行してください。
58
  # タスク
59
- 直近の会話履歴と最後のユーザー発言を分析し、**会話の結果として登場人物がどこか特定の場所へ行くことに合意したか**を判断してください。
 
60
  # 判断基準
61
- 1. 会話の中で具体的な場所が提案されているか?
62
- 2. 最後のユーザー発言が、その提案に対する明確な同意(例:「行こう」「いいね」「そうしよう」など)を示しているか?
 
 
 
 
 
 
 
63
  # 出力形式
64
- - 合意が成立した場合:その場所の英語キーワード(例: "aquarium_night")を一つだけ出力。
65
- - 合意に至らなかった場合:「none」とだけ出力。
 
 
 
 
 
66
  ---
67
  # 分析対象の会話
68
  {history_text}
@@ -72,18 +91,23 @@ def detect_scene_change(history, message):
72
  """
73
  try:
74
  response = gemini_model.generate_content(prompt, generation_config={"temperature": 0.0})
75
- # ★ 安全性チェックを追加
76
  if not response.candidates or response.candidates[0].finish_reason not in {1, 'STOP'}:
77
  print(f"シーン検出LLMで応答がブロックされました: {response.prompt_feedback}")
78
  return None
 
79
  scene_name = response.text.strip().lower()
80
- if scene_name != "none" and re.match(r'^[a-z0-9_]+$', scene_name):
 
 
 
81
  return scene_name
 
82
  return None
83
  except Exception as e:
84
  print(f"シーン検出LLMエラー: {e}")
85
  return None
86
 
 
87
  def generate_scene_instruction_with_groq(affection, stage_name, scene, previous_topic):
88
  print(f"Groqに指示書生成をリクエスト (シーン: {scene})")
89
  prompt_template = f"""
@@ -108,7 +132,6 @@ def generate_scene_instruction_with_groq(affection, stage_name, scene, previous_
108
  print(f"指示書生成エラー(Groq): {e}")
109
  return None
110
 
111
- # ★★★★★ ここが重要な修正点 ★★★★★
112
  def generate_dialogue_with_gemini(history, message, affection, stage_name, scene_params, instruction=None):
113
  history_text = "\n".join([f"ユーザー: {u}\n麻理: {m}" for u, m in history])
114
  task_prompt = f"指示: {instruction}" if instruction else f"ユーザー: {message}"
@@ -130,7 +153,6 @@ def generate_dialogue_with_gemini(history, message, affection, stage_name, scene
130
  """
131
  print(f"Geminiに応答生成をリクエストします (モード: {'シーン遷移' if instruction else '通常会話'})")
132
 
133
- # ★ ぶっきらぼうなキャラ設定のため、安全設定を調整
134
  safety_settings = {
135
  HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
136
  HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE,
@@ -143,14 +165,12 @@ def generate_dialogue_with_gemini(history, message, affection, stage_name, scene
143
  response = gemini_model.generate_content(
144
  system_prompt,
145
  generation_config=generation_config,
146
- safety_settings=safety_settings # ★ 安全設定を適用
147
  )
148
 
149
- # ★ 応答が正常に生成されたかを確認してから .text にアクセスする
150
  if response.candidates and response.candidates[0].finish_reason in {1, 'STOP'}:
151
  return response.text.strip()
152
  else:
153
- # 安全フィルターなどでブロックされた場合のフォールバック
154
  print(f"応答生成が途中で終了しました。理由: {response.candidates[0].finish_reason if response.candidates else 'N/A'}")
155
  print(f"Prompt Feedback: {response.prompt_feedback}")
156
  return "(……何か言おうとしたけど、言葉に詰まった)"
@@ -180,12 +200,16 @@ def update_affection(message, affection):
180
  def respond(message, chat_history, affection, history, scene_params):
181
  new_affection = update_affection(message, affection)
182
  stage_name = get_relationship_stage(new_affection)
 
 
 
183
  new_scene_name = detect_scene_change(history, message)
184
 
185
  final_scene_params = scene_params
186
 
187
- if new_scene_name and new_scene_name in THEME_URLS:
188
- print(f"シーンチェンジを検出: {new_scene_name}")
 
189
  new_params_base = generate_scene_instruction_with_groq(new_affection, stage_name, new_scene_name, message)
190
  if new_params_base:
191
  final_scene_params = {**DEFAULT_SCENE_PARAMS, **new_params_base}
 
51
 
52
  # --- 2. 機能定義 ---
53
 
54
+ # ★★★★★ ここが重要な修正点 ★★★★★
55
  def detect_scene_change(history, message):
56
+ history_text = "\n".join([f"ユーザー: {u}\n麻理: {m}" for u, m in history[-5:]]) # 少し履歴を長く見る
57
+
58
+ # 利用可能なキーワードリストを作成
59
+ available_keywords = ", ".join(THEME_URLS.keys())
60
+
61
+ # ★ 判定基準を緩和し、具体例を追加したプロンプト
62
  prompt = f"""
63
+ あなたは会話の流れから、登場人物たちの次の行動を読み取るエキスパートです。
64
  # タスク
65
+ 直近の会話履歴を分析し、**会話の結果、登場人物たちがどこか特定の場所へ行く流れになっているか**を判断してください。
66
+
67
  # 判断基準
68
+ 1. 会話の中で、これから向かう可能性のある具体的な場所(例:水族館、カフェ、お祭り、神社など)について言及されていますか?
69
+ 2. その場所へ行くことについて、**双方が合意している、あるいは肯定的な雰囲気**になっていますか? どちらか一方の提案に対し、もう一方が明確に否定していなければ、合意とみなして構いません。
70
+
71
+ # 判断の例
72
+ - ユーザー「水族館とかどうかな?」 麻理「…別に、いいけど」 → 合意とみなし「aquarium_night」を出力
73
+ - 麻理「お祭り、やってるみたいだけど」 ユーザー「へぇ、いいね!」 → 合意とみなし「festival_night」を出力
74
+ - ユーザー「どこか行きたいね」 麻理「…そうだね」 → 具体的な場所がないため「none」を出力
75
+ - ユーザー「海に行かない?」 麻理「気分じゃない」 → 合意不成立。「none」を出力
76
+
77
  # 出力形式
78
+ - 合意が成立した場合:以下のリストの中から最も合致する場所のキーワードを一つだけ出力してください。
79
+ - 合意に至らなかった場合:「none」とだけ出力してください。
80
+ - **リストにない場所や、判断に迷う場合は「none」と出力してください。**
81
+
82
+ # 利用可能なキーワード
83
+ `{available_keywords}`
84
+
85
  ---
86
  # 分析対象の会話
87
  {history_text}
 
91
  """
92
  try:
93
  response = gemini_model.generate_content(prompt, generation_config={"temperature": 0.0})
 
94
  if not response.candidates or response.candidates[0].finish_reason not in {1, 'STOP'}:
95
  print(f"シーン検出LLMで応答がブロックされました: {response.prompt_feedback}")
96
  return None
97
+
98
  scene_name = response.text.strip().lower()
99
+
100
+ # 判定されたシーンが実際に利用可能なものか確認
101
+ if scene_name in THEME_URLS:
102
+ print(f"シーン変更を検出(候補): {scene_name}")
103
  return scene_name
104
+
105
  return None
106
  except Exception as e:
107
  print(f"シーン検出LLMエラー: {e}")
108
  return None
109
 
110
+
111
  def generate_scene_instruction_with_groq(affection, stage_name, scene, previous_topic):
112
  print(f"Groqに指示書生成をリクエスト (シーン: {scene})")
113
  prompt_template = f"""
 
132
  print(f"指示書生成エラー(Groq): {e}")
133
  return None
134
 
 
135
  def generate_dialogue_with_gemini(history, message, affection, stage_name, scene_params, instruction=None):
136
  history_text = "\n".join([f"ユーザー: {u}\n麻理: {m}" for u, m in history])
137
  task_prompt = f"指示: {instruction}" if instruction else f"ユーザー: {message}"
 
153
  """
154
  print(f"Geminiに応答生成をリクエストします (モード: {'シーン遷移' if instruction else '通常会話'})")
155
 
 
156
  safety_settings = {
157
  HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
158
  HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE,
 
165
  response = gemini_model.generate_content(
166
  system_prompt,
167
  generation_config=generation_config,
168
+ safety_settings=safety_settings
169
  )
170
 
 
171
  if response.candidates and response.candidates[0].finish_reason in {1, 'STOP'}:
172
  return response.text.strip()
173
  else:
 
174
  print(f"応答生成が途中で終了しました。理由: {response.candidates[0].finish_reason if response.candidates else 'N/A'}")
175
  print(f"Prompt Feedback: {response.prompt_feedback}")
176
  return "(……何か言おうとしたけど、言葉に詰まった)"
 
200
  def respond(message, chat_history, affection, history, scene_params):
201
  new_affection = update_affection(message, affection)
202
  stage_name = get_relationship_stage(new_affection)
203
+
204
+ # 現在のテーマを保持しておく
205
+ current_theme = scene_params.get("theme", "default")
206
  new_scene_name = detect_scene_change(history, message)
207
 
208
  final_scene_params = scene_params
209
 
210
+ # 新しいシーンが検出され、かつ現在のシーンと異なる場合のみ遷移処理を行う
211
+ if new_scene_name and new_scene_name != current_theme:
212
+ print(f"シーンチェンジを実行: {current_theme} -> {new_scene_name}")
213
  new_params_base = generate_scene_instruction_with_groq(new_affection, stage_name, new_scene_name, message)
214
  if new_params_base:
215
  final_scene_params = {**DEFAULT_SCENE_PARAMS, **new_params_base}