Skip to content

Commit d1b5143

Browse files
committed
feat: optimize ai output
1 parent 84bc5d6 commit d1b5143

File tree

3 files changed

+45
-21
lines changed

3 files changed

+45
-21
lines changed

app/server.ts

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,57 @@ const MAX_SIZE = 6;
1313
export async function getAnswer(
1414
prompt: string,
1515
guaMark: string,
16-
guaName: string,
16+
guaTitle: string,
17+
guaResult: string,
1718
guaChange: string,
1819
) {
19-
console.log(prompt, guaName, guaChange);
20+
console.log(prompt, guaTitle, guaResult, guaChange);
2021
const stream = createStreamableValue();
2122
try {
22-
// const res = await fetch(
23-
// `https://raw.githubusercontent.com/sunls2/zhouyi/main/docs/${guaMark}/index.md`,
24-
// );
25-
// const guaDetail = await res.text();
23+
const res = await fetch(
24+
`https://raw.githubusercontent.com/sunls2/zhouyi/main/docs/${guaMark}/index.md`,
25+
);
26+
const guaDetail = await res.text();
27+
const explain = guaDetail
28+
.match(/(\*\*[\s\S]*?)(?=\d+)/)?.[1]
29+
.replaceAll("\n\n", "\n");
30+
31+
const changeList: string[] = [];
32+
if (guaChange !== "无变爻") {
33+
guaChange
34+
.split(":")[1]
35+
.trim()
36+
.split(",")
37+
.forEach((change) => {
38+
const detail = guaDetail
39+
.match(`(\\*\\*${change}變卦[\\s\\S]*?)(?=${guaTitle}|$)`)?.[1]
40+
.replaceAll("\n\n", "\n");
41+
if (detail) {
42+
changeList.push(detail.trim());
43+
}
44+
});
45+
}
2646

2747
const { fullStream } = streamText({
2848
temperature: 0.5,
2949
model: openai(model),
3050
messages: [
3151
{
3252
role: "system",
33-
content:
34-
"你是精通易经64卦, 擅长解读卦象的AI助手\n1.首先对卦象整体情况进行解读\n2.再重点结合要算的事情和变爻情况进行详细分析",
53+
content: `你是一位精通《周易》的AI助手,根据用户提供的卦象和问题,提供准确的卦象解读和实用建议
54+
任务要求:逻辑清晰,语气得当
55+
1. 解读卦象:分析主卦、变爻及变卦,解读整体趋势和吉凶
56+
2. 关联问题:针对用户问题,结合卦象信息,提供具体分析
57+
3. 提供建议:根据卦象启示,给出切实可行的建议,帮助用户解决实际问题`,
3558
},
3659
{
3760
role: "user",
38-
content: `我想要算的事情是:\`${prompt}\`\n请帮我解读此卦象:\`${guaName}\`\n${guaChange}`,
61+
content: `我摇到的卦象:${guaTitle} ${guaResult} ${guaChange}
62+
我的问题:${prompt}
63+
64+
${explain}
65+
${changeList.join("\n")}`,
3966
},
40-
// {
41-
// role: "user",
42-
// content: `此卦象的详细解释:\n\`\`\`\n${guaDetail}\n\`\`\``,
43-
// },
4467
],
4568
maxRetries: 0,
4669
});

components/divination.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ function Divination() {
3030
const { data, error } = await getAnswer(
3131
question,
3232
resultObj!.guaMark,
33+
resultObj!.guaTitle,
3334
resultObj!.guaResult,
3435
resultObj!.guaChange,
3536
);
@@ -175,12 +176,11 @@ function Divination() {
175176
setResultObj({
176177
// 例:26.山天大畜
177178
guaMark: `${(guaIndex + 1).toString().padStart(2, "0")}.${guaName2}`,
178-
// 例:周易第26卦_大畜卦(山天大畜)_艮上乾下
179-
guaResult: `周易第${
180-
guaIndex + 1
181-
}卦_${guaName1}卦(${guaName2}_${guaDesc})`,
179+
guaTitle: `周易第${guaIndex + 1}卦`,
180+
// 例:大畜卦(山天大畜)_艮上乾下
181+
guaResult: `${guaName1}卦(${guaName2})_${guaDesc}`,
182182
guaChange:
183-
changeList.length === 0 ? "无变爻" : `变爻${changeList.toString()}`,
183+
changeList.length === 0 ? "无变爻" : `变爻: ${changeList.toString()}`,
184184
});
185185
}
186186

components/result.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from "react";
22

33
export interface ResultObj {
4+
guaTitle: string;
45
guaMark: string;
56
guaResult: string;
67
guaChange: string;
@@ -9,14 +10,14 @@ export interface ResultObj {
910
function Result(props: ResultObj) {
1011
return (
1112
<div className="flex flex-col items-start justify-center gap-2 sm:gap-3">
12-
{props.guaResult.split("_")[0]}
13+
{props.guaTitle}
1314
<a
1415
className="group flex items-center gap-1 font-medium text-primary/80 underline underline-offset-4 transition-colors hover:text-primary/100"
1516
href={`https://zhouyi.sunls.de/${props.guaMark}/`}
1617
target="_blank"
1718
>
18-
<div className="mt-1 h-[90%] w-1.5 rounded bg-blue-400/80 transition-colors group-hover:bg-blue-400/100" />
19-
<span>{props.guaResult.split("_").slice(1).join("_")}</span>
19+
<div className="mt-1 h-[90%] w-1.5 bg-blue-400/80 transition-colors group-hover:bg-blue-400/100" />
20+
<span>{props.guaResult}</span>
2021
</a>
2122
<span className="text-sm italic text-muted-foreground">
2223
{props.guaChange}

0 commit comments

Comments
 (0)