iuiaoin / wechat-gptbot

A wechat robot based on ChatGPT with no risk, very stable! 🚀
MIT License
578 stars 113 forks source link

[Feat]: 如何添加prompt呢,有很多prompt很有意思不知道怎么加上去 #116

Open ic4y opened 5 months ago

ic4y commented 5 months ago

Search for answers in existing issues

Feature description

https://github.com/linexjlin/GPTs 这个项目里就提供了很多有意思的prompt

Motivation

No response

QAbot-zh commented 5 months ago

我介绍下我的做法,首先是类似config.json,搞了一个prompts.json,用来存不同的prompts,格式大体如下:

  {
    "context_enable":false,
    "act": "中英文翻译官",
    "prompt":"请担任中英文翻译官,请检查信息是否准确,翻译需要自然、流畅和地道, 使用优美和高雅的表达方式。无论对方回复什么,你只需要将内容翻译为中文或英文。您应该只回复翻译后的内容,而不应回复其他任何多余内容,也不用写解释。",
    "few-shot":[
      {"role":"user","content":"hello world"},
      {"role":"assistant","content":"你好,世界"}
    ]
  }

然后我给context类型加了一个role属性,用来存当前对话的角色,session也就是对话上下文根据role进行区分,如果是第一次对话,就从prompts里加载预设:

''' session.py 部分代码 '''
...
        # 上下文管理
        if context.role not in sessions:
            sessions[context.role] = []
        session = sessions.get(context.role, [])
        few_shot_prompts = []
        if context.role in prompts():
            item = prompts().get(context.role)
            role_prompt = {"role": "system", "content": item.get("prompt")}
            few_shot_prompts = item.get("few-shot",[])
        else:
            role_prompt = {"role": "system", "content": context.system_prompt}

        user_item = {"role": "user", "content": context.query}   # 用户提问
        if session and prompts().get(context.role).get("context_enable"):
            session.append(user_item)
        else:
            session = [role_prompt]  # 系统预设
            session.extend(few_shot_prompts)  # few-shot 提示
            session.append(user_item) # 用户提问
...
ic4y commented 5 months ago

实现很不错啊,提个pr贡献给这个项目呗 @QAbot-zh