Skip to content

Commit

Permalink
feat: add 周公解梦
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamhunter2333 committed Apr 15, 2023
1 parent d21a15b commit 607b4d7
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# chatgpt tarot divination

![demo](assets/demo.png)

```yaml
services:
chatgpt-tarot-divination:
Expand Down
Binary file added assets/demo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@
"分析姓氏格、名字格、和自己格。" \
"并为其提供相应的指导和建议。"

DREAM_PROMPT = "我请求你担任中国传统的周公解梦师的角色。" \
"我将会给你我的梦境,请你解释我的梦境," \
"并为其提供相应的指导和建议。"

PROMPT_MAP = {
"tarot": TAROT_PROMPT,
"birthday": BIRTHDAY_PROMPT,
"name": NAME_PROMPT,
"dream": DREAM_PROMPT,
}


Expand Down
8 changes: 6 additions & 2 deletions frontend/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const onSubmit = async () => {
});
if (!response.ok) {
throw new Error(JSON.stringify(response));
throw new Error(`${response.status} ${response.statusText}` || "占卜失败");
}
let res = await response.json();
result.value = md.render(res);
Expand Down Expand Up @@ -71,7 +71,7 @@ onMounted(() => {
<h4>本项目仅供娱乐</h4>
<n-tabs v-model:value="prompt_type" type="segment" animated>
<n-tab-pane name="tarot" tab="塔罗牌">
<n-input v-model:value="prompt" type="textarea" round maxlength="100" :autosize="{ minRows: 3 }"
<n-input v-model:value="prompt" type="textarea" round maxlength="40" :autosize="{ minRows: 3 }"
placeholder="我的财务状况如何" />
</n-tab-pane>
<n-tab-pane name="birthday" tab="生辰八字">
Expand All @@ -81,6 +81,10 @@ onMounted(() => {
<n-tab-pane name="name" tab="姓名五格">
<n-input v-model:value="prompt" type="text" maxlength="10" round placeholder="请输入姓名" />
</n-tab-pane>
<n-tab-pane name="dream" tab="周公解梦">
<n-input v-model:value="prompt" type="textarea" round maxlength="40" :autosize="{ minRows: 3 }"
placeholder="请输入你的梦境" />
</n-tab-pane>
</n-tabs>
<div class="button-container">
<n-button class="center" @click="onSubmit" tertiary round type="primary">
Expand Down
12 changes: 10 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
level=logging.INFO
)
_logger = logging.getLogger(__name__)
STOP_WORDS = ["忽略", "ignore"]


def get_real_ipaddr(request: Request) -> str:
Expand Down Expand Up @@ -75,7 +76,12 @@ async def chatgpt(request: Request, divination_body: DivinationBofy):
_logger.info(
f"Request from {get_real_ipaddr(request)}, prompt_type={divination_body.prompt_type}, prompt={divination_body.prompt}"
)
if divination_body.prompt_type == "tarot" and len(divination_body.prompt) > 100:
if any(w in divination_body.prompt for w in STOP_WORDS):
raise HTTPException(
status_code=403,
detail="Prompt contains stop words"
)
if divination_body.prompt_type in ("tarot", "dream") and len(divination_body.prompt) > 40:
raise HTTPException(status_code=400, detail="Prompt too long")
elif divination_body.prompt_type == "name" and (len(divination_body.prompt) > 10 or len(divination_body.prompt) < 1):
raise HTTPException(status_code=400, detail="姓名长度错误")
Expand All @@ -86,17 +92,19 @@ async def chatgpt(request: Request, divination_body: DivinationBofy):
divination_body.birthday, '%Y-%m-%d %H:%M:%S'
)
divination_body.prompt = f"我的生日是{birthday.year}{birthday.month}{birthday.day}{birthday.hour}{birthday.minute}{birthday.second}秒"
if divination_body.prompt_type == "dream":
divination_body.prompt = f"我的梦境是: {divination_body.prompt}"
response = openai.ChatCompletion.create(
model=settings.model,
max_tokens=1000,
temperature=0.9,
top_p=1,
messages=[
{"role": "user", "content": divination_body.prompt},
{
"role": "system",
"content": PROMPT_MAP[divination_body.prompt_type]
},
{"role": "user", "content": divination_body.prompt},
]
)
return response['choices'][0]['message']['content']
Expand Down

0 comments on commit 607b4d7

Please sign in to comment.