Skip to content

Commit

Permalink
feat: better prompt (#183)
Browse files Browse the repository at this point in the history
* feat: better prompt
  • Loading branch information
yihong0618 committed Mar 28, 2023
1 parent ca38719 commit 47d3e3d
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/CI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ on:
branches: [ main ]
workflow_dispatch:

env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}

jobs:
testing:
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ python3 xiaogpt.py
| edge_tts_voice | Edge TTS 的嗓音 | `zh-CN-XiaoxiaoNeural` |
| prompt | 自定义prompt | `请用100字以内回答` |
| keyword | 自定义请求词列表 | `["请问"]` |
| change_prompt_keyword | 更改提示词触发列表 | `["更改提示词"]` |
| start_conversation | 开始持续对话关键词 | `开始持续对话` |
| end_conversation | 结束持续对话关键词 | `结束持续对话` |
| stream | 使用流式响应,获得更快的响应 | `false` |
Expand Down
6 changes: 4 additions & 2 deletions xiaogpt/bot/chatgptapi_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ async def ask(self, query, **options):
)
self.history.append([f"{query}", message])
# only keep 5 history
self.history = self.history[-5:]
first_history = self.history.pop(0)
self.history = [first_history] + self.history[-5:]
print(message)
return message

Expand Down Expand Up @@ -60,4 +61,5 @@ async def text_gen():
finally:
print()
self.history.append([f"{query}", message])
self.history = self.history[-5:]
first_history = self.history.pop(0)
self.history = [first_history] + self.history[-5:]
1 change: 1 addition & 0 deletions xiaogpt/bot/gpt3_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def __init__(self, openai_key, api_base=None):
openai.api_key = openai_key
if api_base:
openai.api_base = api_base
self.history = []

async def ask(self, query, **options):
data = {
Expand Down
4 changes: 3 additions & 1 deletion xiaogpt/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
DEFAULT_COMMAND = ("5-1", "5-5")

KEY_WORD = ("帮我", "请回答")
PROMPT = "请用100字以内回答"
CHANGE_PROMPT_KEY_WORD = ("更改提示词",)
PROMPT = "以下请用100字以内回答"
# simulate_xiaoai_question
MI_ASK_SIMULATE_DATA = {
"code": 0,
Expand All @@ -56,6 +57,7 @@ class Config:
openai_key: str = os.getenv("OPENAI_API_KEY", "")
mi_did: str = os.getenv("MI_DID", "")
keyword: Iterable[str] = KEY_WORD
change_prompt_keyword: Iterable[str] = CHANGE_PROMPT_KEY_WORD
prompt: str = PROMPT
mute_xiaoai: bool = False
bot: str = "chatgpt"
Expand Down
33 changes: 30 additions & 3 deletions xiaogpt/xiaogpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ async def _init_data_hardware(self):
self.device_id = h.get("deviceID")
break
else:
raise Exception(f"we have no hardware: {self.config.hardware} please check")
raise Exception(
f"we have no hardware: {self.config.hardware} please use `micli mina` to check"
)
if not self.config.mi_did:
devices = await self.miio_service.device_list()
try:
Expand Down Expand Up @@ -195,6 +197,27 @@ def need_ask_gpt(self, record):
or query.startswith(tuple(self.config.keyword))
)

def need_change_prompt(self, record):
if self.config.bot == "gpt3":
return False
query = record.get("query", "")
return (
self.in_conversation
and not query.startswith(WAKEUP_KEYWORD)
or query.startswith(tuple(self.config.change_prompt_keyword))
)

def _change_prompt(self, new_prompt):
new_prompt = re.sub(
rf"^({'|'.join(self.config.change_prompt_keyword)})", "", new_prompt
)
new_prompt = "以下都" + new_prompt
print(f"Prompt from {self.config.prompt} change to {new_prompt}")
self.config.prompt = new_prompt
if self.chatbot.history:
print(self.chatbot.history)
self.chatbot.history[0][0] = new_prompt

async def get_latest_ask_from_xiaoai(self, session):
retries = 2
for _ in range(retries):
Expand Down Expand Up @@ -406,6 +429,10 @@ async def run_forever(self):
await self.stop_if_xiaoai_is_playing()
continue

# we can change prompt
if self.need_change_prompt(new_record):
self._change_prompt(new_record.get("query", ""))

if not self.need_ask_gpt(new_record):
self.log.debug("No new xiao ai record")
continue
Expand All @@ -415,8 +442,8 @@ async def run_forever(self):

print("-" * 20)
print("问题:" + query + "?")

query = f"{query}{self.config.prompt}"
if not self.chatbot.history:
query = f"{query}{self.config.prompt}"
if self.config.mute_xiaoai:
await self.stop_if_xiaoai_is_playing()
else:
Expand Down

0 comments on commit 47d3e3d

Please sign in to comment.