Jiana commited on
Commit
8b2d483
·
1 Parent(s): 568f131

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -2
app.py CHANGED
@@ -30,19 +30,76 @@ history = {}
30
  # uid: 用户的唯一标识。例如`'bxuid-Aj8Spso8Xsp...'`。由平台生成并传递给机器人,以便机器人区分用户。可被用于实现多轮对话的功能。
31
  # 返回值:[type, content]
32
  # 详见 https://huggingface.co/spaces/baixing/hackathon_test/blob/main/bot-api.md
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  def chat(p, qid, uid):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  # 找出该 uid 对应的历史对话
35
  global history
36
  if uid in history:
37
  msgs = history[uid]
38
  else:
39
  msgs = []
40
-
41
  response = callapi(p, msgs)
42
  history[uid] = msgs + [[p, response]]
43
  logger.info(f"history: {msgs}")
44
  logger.info(f"p: {p}")
45
  logger.info(f"response: {response}")
 
46
  return ["text", response]
47
 
48
 
@@ -70,7 +127,7 @@ def callapi(p, msgs):
70
  iface = gr.Interface(fn=chat,
71
  inputs=["text", "text", "text"],
72
  outputs=["text", "text"],
73
- description="""是您的私人游规助理跟它交流一下法吧,它会帮你定制游计划哦~""")
74
 
75
  comments = """
76
  已添加多轮对话的极简示范,能将该 uid 的最近八条消息一起发给openai。本实现是内存中的,一旦重启即被清空。如需可持久的多轮对话,需要改用数据库等方式。
 
30
  # uid: 用户的唯一标识。例如`'bxuid-Aj8Spso8Xsp...'`。由平台生成并传递给机器人,以便机器人区分用户。可被用于实现多轮对话的功能。
31
  # 返回值:[type, content]
32
  # 详见 https://huggingface.co/spaces/baixing/hackathon_test/blob/main/bot-api.md
33
+
34
+
35
+ prompt_0 = "您好!我是您的智能旅行计划师。请告诉我你想去哪里旅行?您可以直接回复我目的地,例如日本,香港,或者西双版纳。"
36
+ prompt_1 = "好的。请问您计划什么时候出行?您可以回答我具体的日期,例如4月1日到4月5日;或者回答我模糊的时间,例如未来三个月,5天。"
37
+ prompt_2 = "收到。请问您计划多少人出行?请回答我具体的人数和类型,例如2位成人和1位儿童。"
38
+ prompt_3 = "您的出行目的是什么?您可以回答我例如亲子游、情侣/蜜月、休闲度假、文化体验、地标打卡、探险。"
39
+ prompt_4 = "您有没有什么需要打卡的地点或者特殊需求?您可以告诉我具体的景点,例如金阁寺,或者特定的活动,例如赏樱花。"
40
+
41
+
42
+ predefined_prompts = [
43
+ prompt_0,
44
+ prompt_1,
45
+ prompt_2,
46
+ prompt_3,
47
+ prompt_4
48
+ ]
49
+
50
+ user_response = ["pred def" for _ in range(6)]
51
+
52
+ count_i = 1
53
+
54
+ def get_info(p, qid, uid):
55
+ global predefined_prompts
56
+ global user_response
57
+ global count_i
58
+
59
+ user_response[count_i-1] = p
60
+
61
+ next_prompt = predefined_prompts[count_i]
62
+ count_i += 1
63
+ logger.info(f"In get info loop: get from user: {p}")
64
+ logger.info(f"In get info loop: Our next prompt: {next_prompt}, count_i={count_i}")
65
+ return ["text", next_prompt]
66
+
67
+
68
+
69
  def chat(p, qid, uid):
70
+ global user_response
71
+
72
+ if count_i <= 4:
73
+ ret = get_info(p, qid, uid)
74
+ return ret
75
+
76
+
77
+
78
+ if count_i == 5:
79
+ user_response[4] = p
80
+ prompt_0_response = user_response[0]
81
+ prompt_1_response = user_response[1]
82
+ prompt_2_response = user_response[2]
83
+ prompt_3_response = user_response[3]
84
+ prompt_4_response = user_response[4]
85
+ final_prompt = f"请假设自己是一名旅行计划师,给我设计一条去{prompt_0_response}旅行的{prompt_1_response}行程,包含{prompt_2_response}。我的出行目的是{prompt_3_response},希望包含 {prompt_4_response} 。请给我推荐一个详细的每日行程,以表格的方式呈现,包含有趣的景点、活动和推荐理由。请尽量考虑可执行性。"
86
+ logger.info(f"Our final prompt: {final_prompt}")
87
+ logger.info(f"count_i: {count_i}")
88
+
89
+
90
  # 找出该 uid 对应的历史对话
91
  global history
92
  if uid in history:
93
  msgs = history[uid]
94
  else:
95
  msgs = []
96
+
97
  response = callapi(p, msgs)
98
  history[uid] = msgs + [[p, response]]
99
  logger.info(f"history: {msgs}")
100
  logger.info(f"p: {p}")
101
  logger.info(f"response: {response}")
102
+ count_i += 1
103
  return ["text", response]
104
 
105
 
 
127
  iface = gr.Interface(fn=chat,
128
  inputs=["text", "text", "text"],
129
  outputs=["text", "text"],
130
+ description="""您好!我是您的智能行计请告诉我你想去哪里行?您可以直接回复我目的地,例如日本,香港,或者西双版纳。""")
131
 
132
  comments = """
133
  已添加多轮对话的极简示范,能将该 uid 的最近八条消息一起发给openai。本实现是内存中的,一旦重启即被清空。如需可持久的多轮对话,需要改用数据库等方式。