-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
138 lines (119 loc) · 4.93 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import gen_plot
import clock
import dialogue_abstract
import favorability
import stimulate
from oneesan import Oneesan # 修改导入方式
import memory
import similarity
import chardet
import os
# 全局变量
api_key = "API填在这里"
base_url = "https://internlm-chat.intern-ai.org.cn/puyu/api/v1"
class GameState:
def __init__(self):
self.state_instance = None
self.clock_instance = None
self.favorability_instance = None
self.oneesan = None
self.similarity_instance = None
self.custom_prompts = None
game_state = GameState()
def user_select_screenplay(screenplay):
game_state.clock_instance = clock.CallCountClock() # 初始为1
game_state.similarity_instance = similarity.TextSimilarity(
api_key="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",
history_dir="history" # 指定历史记录目录
)
match screenplay:
case "A screenplay":
game_state.state_instance = gen_plot.State("history")
game_state.oneesan = Oneesan(api_key=api_key, base_url=base_url)
plot = gen_plot.generator(game_state.state_instance, base_url, api_key)
if plot is None:
print("生成剧情失败")
return
#第一组对话
file_path = 'history/stage_1.txt'
if os.path.exists(file_path):
# 使用二进制模式读取UTF-8编码的文件
with open(file_path, 'rb') as f:
text = f.read().decode('utf-8')
game_state.custom_prompts = text
game_state.oneesan = Oneesan(api_key=api_key, base_url=base_url, custom_prompt={"情景": game_state.custom_prompts})
result = game_state.oneesan.chat("请你根据{custom_prompt},对对方倾诉你的想法,引诱对方和你交流")
print(result["句子"])
print(game_state.oneesan.get_mood())
mood = game_state.oneesan.get_mood()
print(mood)
game_state.favorability_instance = favorability.Favorability(mood) # 初始为50
def user_chat(input):
game_state.clock_instance.increment()
current_state = game_state.state_instance.get_state()
result = game_state.oneesan.chat(input)
print(result["句子"])
mood = game_state.oneesan.get_mood()
print(mood)
game_state.favorability_instance.add_change(mood)
if game_state.clock_instance.get_time() > 30:
abstract_text = dialogue_abstract.dialogue_abstract(
base_url,
api_key,
'Oneesan/memory.txt',
current_state
)
game_state.oneesan.remove_earliest_memory() # 改用删除最早记忆的方法
mood_dict = {
'Valence': mood[0],
'Arousal': mood[1],
'Dominance': mood[2]
}
stimulate_calculator = stimulate.TopicActivationCalculator(mood_dict)
should_activate = stimulate_calculator.calculate_activation()
if should_activate:
similar_texts = game_state.similarity_instance.find_similar_in_history(
source_text=abstract_text,
stage=current_state, # 会在stage 3到stage 1的内容中查找
top_k=1,
threshold=0.8
)
if similar_texts:
game_state.custom_prompts = {"情景": game_state.custom_prompts["情景"] + "以下是你想起的回忆,你需要向对方诉说" + similar_texts}
def next():
"""
更新游戏状态并生成新的剧情
"""
# 更新时钟
game_state.clock_instance.increment()
game_state.clock_instance.get_time()
# 获取好感度并更新状态
fav = game_state.favorability_instance.get_favorability()
if 50 <= fav < 60:
game_state.state_instance.update(1)
elif 60 <= fav < 70:
game_state.state_instance.update(2)
elif 70 <= fav < 80:
game_state.state_instance.update(3)
elif 80 <= fav < 90:
game_state.state_instance.update(4)
elif 90 <= fav < 100:
game_state.state_instance.update(5)
elif fav >= 100:
game_state.state_instance.update(6)
# 生成新剧情
next_scene = gen_plot.generator(game_state.state_instance, base_url, api_key)
if game_state.state_instance.get_state()%3 == 0:
gen_plot.generator(game_state.state_instance, base_url, api_key)
game_state.custom_prompts = next_scene
game_state.oneesan = Oneesan(api_key=api_key, base_url=base_url, custom_prompt={"情景": game_state.custom_prompts})
result = game_state.oneesan.chat("请你根据前面的剧情喝对话,合理地预测接下来的剧情,并且月芙主动向用户说话。要求:200字左右,请保留之前的人设和记忆、文笔。")
print(result)
return result["句子"]
def test_state():
# 初始化剧本
user_select_screenplay("A screenplay")
# 检查初始状态
print(f"当前状态: {game_state.state_instance.get_state()}")
print(f"时钟: {game_state.clock_instance.get_time()}")
print(f"好感度: {game_state.favorability_instance.get_favorability()}")