Skip to content

Commit

Permalink
Merge pull request #5 from ZiTao-Li/zitao/game_dev
Browse files Browse the repository at this point in the history
adding a POV style story generation based on conversation
  • Loading branch information
ZiTao-Li authored Jan 16, 2024
2 parents 38113e6 + 94718ec commit 404a2a5
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
18 changes: 18 additions & 0 deletions examples/game/config/game_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@
基于你的分析:{analysis}
和旧的人物背景:{background}
为{name}生成新的人物背景。
"pov_story": >
你扮演的人物名字是{name}。根据下面的对话,生成一段第一人称的故事,不少于400字。
你自己是这个故事的主角。故事内容不要超过背景和对话。
在故事中加入你的内心活动,内心活动的描写要符合你的人物设定,内心活动的描写要符合提供的对话。
例子:
背景:小明是一名学生,他聪明好学。
对话:
小刚:小明,放学要不要一起去踢球?\n 小明:我想想,我还是不去了吧。我要回家写作业。
生成故事:
下午刚上课,小刚就来找我:“小明,放学要不要一起去踢球?”说实话,我好久没和小刚他们踢球了,
但是今天老师课上讲的那个知识点我没太吃透,可能还要花点时间去复习一下;不然明天的课程就跟不上了。
想到这里,我遗憾地摇摇头说:“我想想,我还是不去了吧。我要回家写作业。” 小刚也一声叹气,回到自己的座位上了。
\n\n
参照以上例子和要求,生成故事,故事中一定要包含对话和心理活动。
背景:{background}
对话:{conversation}
生成故事:
"plots":
- ["王老板", "阿炳"]
23 changes: 23 additions & 0 deletions examples/game/customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,26 @@ def _validated_history_messages(self, recent_n: int = 10):
if len(hist_mem) > 0:
hist_mem[0]["role"], hist_mem[-1]["role"] = "user", "user"
return hist_mem

def generate_pov_story(self, recent_n: int = 20):
related_mem = self._validated_history_messages(recent_n)
conversation = ""
for mem in related_mem:
if "name" in mem:
conversation += mem["name"] + ": " + mem["content"]
else:
conversation += "背景" + ": " + mem["content"]
background = self.background
if self.plot_stage == CustomerPlot.ACTIVE:
background += self.config["character_setting"]["hidden_plot"]

pov_prompt = self.game_config["pov_story"].format_map({
"name": self.name,
"background": background,
"conversation": conversation,
})
msg = Msg(name="system", role="user", content=pov_prompt)
pov_story = self.model(messages=[msg])
print("*" * 20)
logger.info(pov_story)
print("*" * 20)
12 changes: 11 additions & 1 deletion examples/game/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import argparse

import agentscope
from agentscope.models import read_model_configs
from agentscope.message import Msg
from agentscope.msghub import msghub
from customer import Customer
Expand Down Expand Up @@ -54,6 +53,17 @@ def invited_group_chat(invited_customer, player, cur_plot):
correct_names.sort()
if invited_names == correct_names:
print("===== successfully unlock a plot =======")
questions = [
inquirer.List(
"ans",
message="【系统】:需要以哪位角色的视角生成一段完整故事吗?",
choices=invited_names + ["跳过"]
),
]
answer = inquirer.prompt(questions)["ans"]
for c in invited_customer:
if c.name == answer:
c.generate_pov_story()
cur_plot += 1 # move to next plot
for c in invited_customer:
c.refine_background()
Expand Down
3 changes: 2 additions & 1 deletion examples/game/ruled_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def reply(
while True:
try:
content = input(f"{self.name}: ")
if not hasattr(self, "model"):

if not hasattr(self, "model") or len(content) == 0:
break

ruler_res = self.is_content_valid(content)
Expand Down

0 comments on commit 404a2a5

Please sign in to comment.