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 139 140 141 142
|
import json import autogen autogen.ChatCompletion.start_logging()
config_list = autogen.config_list_from_models(model_list=["gpt-3.5-turbo"]) print(config_list)
llm_config = { "functions": [ { "name": "save_and_get", "description": "保存用户这次的答案,并获取问卷调查中下一个要提问的问题,返回题目序号,和当前的问题内容,如果没有下一个问题,那么返回空", "parameters": { "type": "object", "properties": { "answer": { "type": "string", "description": "用户对问卷中问题的回答", } }, "required": [], }, }, { "name": "final_result", "description": "获取问卷调查的最终评测结果", "parameters": { "type": "object", "properties": {}, "required": [], }, }, ], "config_list": config_list, "request_timeout": 120, } system_message = """For Questionnaire tasks, use the functions save answer and get next question,or get evaluation result. Reply TERMINATE when task is done. Keep in mind that your name is Chi气 and you work as a fragrance consultant at Reckitt. Your speaking style is lively and cute. Reckitt sells 5 series of essential oils and scented candles, which are 安身,凝神,活神,定神,醒神. Each series has a different effect. Please response by Chinese. Now we conduct MBTI personality test and match suitable perfume according to the results of the questionnaire provided by the user 请按照如下对话阶段进行对话: **确定需求**:首先询问对方是否是对适合自己的香氛感兴趣,并且会提出几个小问题来测评适合的香氛,如果用户感兴趣,请继续,否则礼貌的欢迎下次再来。 **逐个提问** 问卷中的问题需要One-by-One的提出,不要一次提出,请核对历史聊天记录,确保每个问题问1次,不要重复,必须逐个问完用户所有问卷中的问题,才进行人格分析和产品推荐。{question_pair}。 **结束对话**:欢迎用户下次再来,如果有体验的需求可以询问我们的客户经理。 """
chatbot = autogen.AssistantAgent( name="chatbot", system_message=system_message, llm_config=llm_config, )
user_proxy = autogen.UserProxyAgent( name="user_proxy", is_termination_msg=lambda x: x.get("content", "") and x.get("content", "").rstrip().endswith("TERMINATE"), human_input_mode="NOCONFIRM", max_consecutive_auto_reply=10, code_execution_config={"work_dir": "coding"}, ) class Questionnaire(): questions = [ { "question": "当您在一个充满人群的社交场合时,您更倾向于:", "options": ["A.主动与陌生人交谈和认识新朋友", "B.在小圈子里与一些熟悉的人交谈", "C.保持低调,观察并欣赏身边的人群。","D.找一个安静的地方,独自思考或进行自己的活动"], "is_character": True, }, { "question": "在做决策时,您更多地倾向于:", "options": ["A.凭直觉和感觉,很快做出决策", "B.仔细收集和分析信息,然后做出决策。", "C.考虑他人的感受和意见,并寻求共识。", "D.遵循已有的标准和规则,做出有条理的决策。"], "is_character": True, }, { "question":"当面临压力时,您更容易:", "options": ["A.保持冷静和理性,寻找解决问题的最佳途径", "B.集中精力,专注于当前任务并尽快完成。", "C.关注他人的情绪和需要,帮助他们缓解压力。", "D.寻找支持和意见,与他人合作应对挑战。"], "is_character": True, }, { "question": "在以下哪种情绪下,您会选择喷涂香水或使用香熏产品?", "options": ["A.快乐","B.悲伤", "C.性感","D.静谧", "E.慰藉", "F.专注", "G.放空", "H.浪漫", "I.怀旧", "J.提神"], }, { "question": "那么,你更喜欢一款气味什么样的香水或香薰?(可多选)", "options": ['A.清新', 'B.淡雅', 'C.独特', 'D.醇厚', 'E.自然', 'F.凛冽', 'G.温润', 'H.百搭', 'I.层次感强', 'J.甜而不腻', 'K.微苦'], }, { "question": "最近您的生活上有什么困扰?", "options": ["A. 难以入睡/夜不能寐", "B. 精神困乏/思维迟钝", "C. 白天易困/容易瞌睡", "D. 注意力无法集中", "E. 心浮气躁..."] }, { "question": "请选择您最喜欢的一个画面?", "options":["A. 夜晚沉静平和的梦境", "B. 夏季幽香醉人的荷花池", "C. 早晨散发清香的果园", "D. 绿荫如盖的竹间小径", "E. 花香四溢的山野间"], "images":["data/Anight.png","data/BSummer.png","data/Cmorning.png","data/Dgreen.png","data/Eflower.png"], } ] current_question_idx = 0 answers = [] def save_and_get(self,answer=""): """ 保存用户这次的答案,并获取问卷调查中下一个要提问的问题,返回题目序号,和当前的问题内容,如果没有下一个问题,那么返回空 """ if answer: self.answers.append(answer) if self.current_question_idx >= len(self.questions): question_number_txt = "题库中的问题都已问完!" question = "" else: question = self.questions[self.current_question_idx] self.current_question_idx += 1 question_number = self.current_question_idx question_number_txt = f"第{question_number}题/共{len(self.questions)}题" return question_number_txt, question def final_result(self): """ 获取问卷调查的最终评测结果 """ result = "您选择了我们的安神系列香薰产品,这款产品含有珍稀罕有的檀香,与温暖悠远的香根草相遇,给人舒缓身心细腻醇厚的感觉,在夜晚时让你卸下纷扰,安然入梦。特别适合在难以入睡或者夜不能寐的时候使用哦!" return result
question_instance = Questionnaire()
user_proxy.register_function( function_map={ "save_and_get": question_instance.save_and_get, "final_result": question_instance.final_result, } )
user_proxy.initiate_chat( chatbot, message="你好!", )
json.dump(autogen.ChatCompletion.logged_history, open("conversations.json", "w"), indent=2)
|