♻️Refactor: 清除 mermaid
This commit is contained in:
@@ -44,7 +44,7 @@ func CallWithMessage(llm llms.Model, ctx context.Context, prompt string) (respon
|
||||
return response
|
||||
|
||||
}
|
||||
func CallWithAgent(llm llms.Model, ctx context.Context, topic string, promptPath string,) (response string) {
|
||||
func CallWithAgent(llm llms.Model, ctx context.Context, topic string, promptPath string) (response string) {
|
||||
content, err := os.ReadFile(promptPath)
|
||||
if err != nil {
|
||||
fmt.Printf("File err: %v", err)
|
||||
@@ -67,13 +67,13 @@ func CallWithAgent(llm llms.Model, ctx context.Context, topic string, promptPath
|
||||
}
|
||||
|
||||
func CallWithAgentDefined[T any](llm llms.Model, ctx context.Context, topic string, promptPath string, responseType T) (response T) {
|
||||
|
||||
|
||||
parse, err := outputparser.NewDefined(responseType)
|
||||
if err != nil {
|
||||
fmt.Printf("CreateParse err: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
content, err := os.ReadFile(promptPath)
|
||||
if err != nil {
|
||||
fmt.Printf("File err: %v", err)
|
||||
@@ -93,25 +93,25 @@ func CallWithAgentDefined[T any](llm llms.Model, ctx context.Context, topic stri
|
||||
if err != nil {
|
||||
fmt.Printf("Prompt err: %v", err)
|
||||
}
|
||||
|
||||
|
||||
responseString, err := llms.GenerateFromSinglePrompt(
|
||||
ctx,
|
||||
llm,
|
||||
"用户正在学习Go语言和React框架,你是用户的助手。\n"+prompt,
|
||||
)
|
||||
|
||||
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
responseString = strings.TrimPrefix(responseString, "\n")
|
||||
responseString = strings.TrimSuffix(responseString, "\n")
|
||||
|
||||
if !strings.Contains(responseString, "```json") {
|
||||
responseString = "```json\n" + responseString + "```"
|
||||
responseString = "```json\n" + responseString + "\n```"
|
||||
}
|
||||
|
||||
fmt.Printf("AI: %s\n\n",responseString)
|
||||
// fmt.Printf("AI: %s\n\n", responseString)
|
||||
|
||||
response, err = parse.Parse(responseString)
|
||||
if err != nil {
|
||||
@@ -119,5 +119,7 @@ func CallWithAgentDefined[T any](llm llms.Model, ctx context.Context, topic stri
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("API:\n%v", response)
|
||||
|
||||
return response
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
var files = []string{
|
||||
".\\prompt\\card.txt",
|
||||
".\\prompt\\guodegang.txt",
|
||||
".\\prompt\\mermaid.txt",
|
||||
".\\prompt\\quiz.txt",
|
||||
".\\prompt\\scenario.txt",
|
||||
}
|
||||
@@ -28,20 +27,14 @@ func GetGuodegang(llm llms.Model, ctx context.Context, topic string) (response r
|
||||
return
|
||||
}
|
||||
|
||||
func GetMermaid(llm llms.Model, ctx context.Context, topic string) (response response_struct.Mermaid) {
|
||||
file := files[2]
|
||||
response = agent.CallWithAgentDefined(llm, ctx, topic, file, response_struct.Mermaid{})
|
||||
return
|
||||
}
|
||||
|
||||
func GetQuiz(llm llms.Model, ctx context.Context, topic string) (response response_struct.Quiz) {
|
||||
file := files[3]
|
||||
file := files[2]
|
||||
response = agent.CallWithAgentDefined(llm, ctx, topic, file, response_struct.Quiz{})
|
||||
return
|
||||
}
|
||||
|
||||
func GetScenario(llm llms.Model, ctx context.Context, topic string) (response response_struct.Scenario) {
|
||||
file := files[4]
|
||||
file := files[3]
|
||||
response = agent.CallWithAgentDefined(llm, ctx, topic, file, response_struct.Scenario{})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -69,13 +69,6 @@ func main() {
|
||||
"message": message,
|
||||
})
|
||||
})
|
||||
route.GET("/mermaid/:topic", func(c *gin.Context) {
|
||||
topic := c.Param("topic")
|
||||
message := handle.GetMermaid(llm, ctx, topic)
|
||||
c.JSON(200, gin.H{
|
||||
"message": message,
|
||||
})
|
||||
})
|
||||
route.GET("/quiz/:topic", func(c *gin.Context) {
|
||||
topic := c.Param("topic")
|
||||
message := handle.GetQuiz(llm, ctx, topic)
|
||||
@@ -93,5 +86,4 @@ func main() {
|
||||
|
||||
route.Run()
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
请为"{{.Knowledge}}"生成一个Mermaid思维导图或流程图,要求:
|
||||
1. 零未定义节点:所有节点(A[文本]、B(文本)等)必须在关系语句前先定义。
|
||||
2. 严格缩进与换行:使用2个空格缩进,每条关系语句独立成行。
|
||||
3. 括号与引号成对:所有()、[]、<>、""必须严格配对,无多余空格。
|
||||
4. 箭头方向明确:使用标准箭头 -->、---、-.-> 等,禁止使用 ->(不完整)或 =>(错误)。
|
||||
5. 避免中文直接嵌入:若需中文,确保用双引号包裹(如 A["开始"]),或建议用户使用英文标签。
|
||||
@@ -1,6 +0,0 @@
|
||||
package response_struct
|
||||
|
||||
type Mermaid struct {
|
||||
Type string `json:"type"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/tmc/langchaingo/outputparser"
|
||||
)
|
||||
|
||||
// Parse parses the output of an LLM call.
|
||||
func (p outputparser.Defined[T)(T) Parse(text string) (T, error) {
|
||||
var target T
|
||||
|
||||
// Removes '```json' and '```' from the start and end of the text.
|
||||
const opening = "```json"
|
||||
const closing = "```"
|
||||
if text[:len(opening)] != opening || text[len(text)-len(closing):] != closing {
|
||||
return target, nil
|
||||
// return target, fmt.Errorf("input text should start with %s and end with %s", opening, closing)
|
||||
}
|
||||
parseableJSON := text[len(opening) : len(text)-len(closing)]
|
||||
if err := json.Unmarshal([]byte(parseableJSON), &target); err != nil {
|
||||
return target, fmt.Errorf("could not parse generated JSON: %w", err)
|
||||
}
|
||||
return target, nil
|
||||
}
|
||||
Generated
+1
-1240
File diff suppressed because it is too large
Load Diff
@@ -12,7 +12,6 @@
|
||||
"dependencies": {
|
||||
"@heroui/react": "^3.0.1",
|
||||
"@heroui/styles": "^3.0.1",
|
||||
"mermaid": "^11.13.0",
|
||||
"react": "^19.2.4",
|
||||
"react-dom": "^19.2.4"
|
||||
},
|
||||
|
||||
+4
-37
@@ -2,7 +2,6 @@ import { useState } from "react";
|
||||
import { ProgressBar, Label, Tabs } from "@heroui/react";
|
||||
import Card from "./components/Card";
|
||||
import Guodegang from "./components/Guodegang";
|
||||
import Mermaid from "./components/Mermaid";
|
||||
import Topic from "./components/Topic";
|
||||
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
@@ -14,21 +13,15 @@ function App() {
|
||||
const [guodegangData, setGuodegangData]: any = useState(null);
|
||||
const [guodegangLoading, setGuodegangLoading] = useState(false);
|
||||
const [guodegangError, setGuodegangError] = useState<string | null>(null);
|
||||
const [mermaidData, setMermaidData]: any = useState(null);
|
||||
const [mermaidLoading, setMermaidLoading] = useState(false);
|
||||
const [mermaidError, setMermaidError] = useState<string | null>(null);
|
||||
|
||||
const fetchData = async (topicToFetch: string) => {
|
||||
setCardLoading(true);
|
||||
setGuodegangLoading(true);
|
||||
setMermaidLoading(true);
|
||||
setCardError(null);
|
||||
setGuodegangError(null);
|
||||
setMermaidError(null);
|
||||
|
||||
const cardUrl = `http://localhost:8080/card/${encodeURIComponent(topicToFetch)}`;
|
||||
const guodegangUrl = `http://localhost:8080/guodegang/${encodeURIComponent(topicToFetch)}`;
|
||||
const mermaidUrl = `http://localhost:8080/mermaid/${encodeURIComponent(topicToFetch)}`;
|
||||
|
||||
const abortController = new AbortController();
|
||||
const timeoutId = setTimeout(() => abortController.abort(), 300000);
|
||||
@@ -61,21 +54,7 @@ function App() {
|
||||
}
|
||||
};
|
||||
|
||||
const fetchMermaid = async () => {
|
||||
try {
|
||||
const response = await fetch(mermaidUrl, { signal: abortController.signal });
|
||||
if (!response.ok) throw new Error(`Mermaid API failed: ${response.status}`);
|
||||
const result = await response.json();
|
||||
setMermaidData(result.message);
|
||||
} catch (error) {
|
||||
console.error("Error fetching mermaid data:", error);
|
||||
setMermaidError("Failed to load mermaid data");
|
||||
} finally {
|
||||
setMermaidLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
Promise.allSettled([fetchCard(), fetchGuodegang(), fetchMermaid()]).finally(() => {
|
||||
Promise.allSettled([fetchCard(), fetchGuodegang()]).finally(() => {
|
||||
clearTimeout(timeoutId);
|
||||
});
|
||||
};
|
||||
@@ -89,15 +68,11 @@ function App() {
|
||||
|
||||
const cardLoaded = !cardLoading && !!cardData;
|
||||
const guodegangLoaded = !guodegangLoading && !!guodegangData;
|
||||
const mermaidLoaded = !mermaidLoading && !!mermaidData;
|
||||
|
||||
const progress = (() => {
|
||||
if (!topic) return 0;
|
||||
if (cardLoaded && guodegangLoaded && mermaidLoaded) return 100;
|
||||
if (cardLoaded && guodegangLoaded) return 67;
|
||||
if (cardLoaded && mermaidLoaded) return 67;
|
||||
if (guodegangLoaded && mermaidLoaded) return 67;
|
||||
if (cardLoaded || guodegangLoaded || mermaidLoaded) return 33;
|
||||
if (cardLoaded && guodegangLoaded) return 100;
|
||||
if (cardLoaded || guodegangLoaded) return 50;
|
||||
return 10;
|
||||
})();
|
||||
|
||||
@@ -115,7 +90,7 @@ function App() {
|
||||
</ProgressBar>
|
||||
</div>
|
||||
)}
|
||||
<div className="w-full max-w-6xl">
|
||||
<div className="w-full max-w-8xl">
|
||||
<Tabs className="w-full">
|
||||
<Tabs.ListContainer>
|
||||
<Tabs.List aria-label="View options">
|
||||
@@ -129,11 +104,6 @@ function App() {
|
||||
<Tabs.Indicator />
|
||||
🎭 德云社讲解
|
||||
</Tabs.Tab>
|
||||
<Tabs.Tab id="mermaid">
|
||||
<Tabs.Separator />
|
||||
<Tabs.Indicator />
|
||||
📊 Mermaid 图表
|
||||
</Tabs.Tab>
|
||||
</Tabs.List>
|
||||
</Tabs.ListContainer>
|
||||
<Tabs.Panel id="card">
|
||||
@@ -142,9 +112,6 @@ function App() {
|
||||
<Tabs.Panel id="guodegang">
|
||||
<Guodegang topic={topic} data={guodegangData} loading={guodegangLoading} error={guodegangError} />
|
||||
</Tabs.Panel>
|
||||
<Tabs.Panel id="mermaid">
|
||||
<Mermaid topic={topic} data={mermaidData} loading={mermaidLoading} error={mermaidError} />
|
||||
</Tabs.Panel>
|
||||
</Tabs>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,94 +0,0 @@
|
||||
import { Surface } from "@heroui/react";
|
||||
import { useEffect, useRef } from "react";
|
||||
import mermaid from "mermaid";
|
||||
|
||||
interface MermaidData {
|
||||
type: string;
|
||||
content: string;
|
||||
}
|
||||
|
||||
interface Props {
|
||||
topic: string;
|
||||
data: MermaidData | null;
|
||||
loading: boolean;
|
||||
error: string | null;
|
||||
}
|
||||
|
||||
export default function Default({ topic, data, loading, error }: Props) {
|
||||
const mermaidRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
useEffect(() => {
|
||||
mermaid.initialize({ startOnLoad: true });
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (data && mermaidRef.current) {
|
||||
const id = `mermaid-diagram-${Date.now()}`;
|
||||
mermaid.render(id, data.content).then((result) => {
|
||||
if (mermaidRef.current) {
|
||||
mermaidRef.current.innerHTML = result.svg;
|
||||
}
|
||||
});
|
||||
}
|
||||
}, [data]);
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<div className="w-full p-4 sm:p-6 md:max-w-2xl lg:max-w-4xl mx-auto">
|
||||
<Surface variant="secondary" className="relative w-full max-w-2xl mx-auto bg-white shadow-xl rounded-2xl overflow-hidden border border-gray-100 p-8">
|
||||
<div className="text-center text-gray-600">Loading...</div>
|
||||
</Surface>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (error) {
|
||||
return (
|
||||
<div className="w-full p-4 sm:p-6 md:max-w-2xl lg:max-w-4xl mx-auto">
|
||||
<Surface variant="secondary" className="relative w-full max-w-2xl mx-auto bg-white shadow-xl rounded-2xl overflow-hidden border border-gray-100 p-8">
|
||||
<div className="text-center text-red-600">{error}</div>
|
||||
</Surface>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (!topic || !data) {
|
||||
return (
|
||||
<div className="w-full p-4 sm:p-6 md:max-w-2xl lg:max-w-4xl mx-auto">
|
||||
<Surface variant="secondary" className="relative w-full max-w-2xl mx-auto bg-white shadow-xl rounded-2xl overflow-hidden border border-gray-100 p-8">
|
||||
<div className="absolute upper-0 right-0 -mr-2 -mb-2 pointer-events-none select-none">
|
||||
<span className="text-[10rem] opacity-80 blur-none">📊</span>
|
||||
</div>
|
||||
<div className="text-center text-gray-600">
|
||||
<div className="text-3xl mb-4">🔷</div>
|
||||
<div className="text-lg font-medium mb-2">Mermaid 图表组件</div>
|
||||
<div className="text-sm text-gray-500">输入主题后,这里将展示相关的 Mermaid 流程图、时序图等可视化内容</div>
|
||||
</div>
|
||||
</Surface>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="w-full p-4 sm:p-6 md:max-w-2xl lg:max-w-4xl mx-auto">
|
||||
<Surface variant="secondary" className="relative w-full max-w-2xl mx-auto bg-white shadow-xl rounded-2xl overflow-hidden border border-gray-100 p-6">
|
||||
<div className="absolute upper-0 right-0 -mr-2 -mb-2 pointer-events-none select-none">
|
||||
<span className="text-[10rem] opacity-80 blur-none">📊</span>
|
||||
</div>
|
||||
|
||||
<div className="relative z-5">
|
||||
<div className="px-4 pt-4 pb-4">
|
||||
<h2 className="text-2xl font-bold text-gray-800 mb-2">
|
||||
{data!.type.charAt(0).toUpperCase() + data!.type.slice(1)} Diagram
|
||||
</h2>
|
||||
<div className="h-1 w-20 bg-gradient-to-r from-blue-500 to-purple-500 rounded"></div>
|
||||
</div>
|
||||
|
||||
<div className="px-4 pb-4 flex justify-center items-center overflow-x-auto">
|
||||
<div ref={mermaidRef} className="mermaid-container"></div>
|
||||
</div>
|
||||
</div>
|
||||
</Surface>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user