♻️Refactor: 清除 mermaid

This commit is contained in:
2026-03-28 20:00:08 +08:00
parent 06a4486a4f
commit 45c668e970
10 changed files with 17 additions and 1435 deletions
+10 -8
View File
@@ -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
}
+2 -9
View File
@@ -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
}
-8
View File
@@ -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()
}
-6
View File
@@ -1,6 +0,0 @@
请为"{{.Knowledge}}"生成一个Mermaid思维导图或流程图,要求:
1. 零未定义节点:所有节点(A[文本]、B(文本)等)必须在关系语句前先定义。
2. 严格缩进与换行:使用2个空格缩进,每条关系语句独立成行。
3. 括号与引号成对:所有()、[]、<>、""必须严格配对,无多余空格。
4. 箭头方向明确:使用标准箭头 -->、---、-.-> 等,禁止使用 ->(不完整)或 =>(错误)。
5. 避免中文直接嵌入:若需中文,确保用双引号包裹(如 A["开始"]),或建议用户使用英文标签。
-6
View File
@@ -1,6 +0,0 @@
package response_struct
type Mermaid struct {
Type string `json:"type"`
Content string `json:"content"`
}
-26
View File
@@ -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
}