✨Feat: 增添结构体解析函数

This commit is contained in:
hhs
2026-03-27 19:30:04 +08:00
parent 4b9670967f
commit 18040d00c4
11 changed files with 179 additions and 37 deletions
+48 -1
View File
@@ -8,6 +8,7 @@ import (
"github.com/tmc/langchaingo/llms"
"github.com/tmc/langchaingo/prompts"
"github.com/tmc/langchaingo/outputparser"
)
func Call(llm llms.Model, ctx context.Context) (response string) {
@@ -42,7 +43,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)
@@ -63,3 +64,49 @@ func CallWithAgent(llm llms.Model, ctx context.Context, topic string, promptPath
response = CallWithMessage(llm, ctx, prompt)
return response
}
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)
return
}
text := string(content)
textWithFormatInstruction := text + "\n" + parse.GetFormatInstructions()
template := prompts.NewPromptTemplate(textWithFormatInstruction, []string{".Knowledge"})
prompt, err := template.Format(map[string]any{
"Knowledge": topic,
})
fmt.Println("Me: ", prompt)
if err != nil {
fmt.Printf("Prompt err: %v", err)
}
responseString, err := llms.GenerateFromSinglePrompt(
ctx,
llm,
"用户正在学习Go语言,你是用户的助手。\n"+prompt,
)
if err != nil {
log.Fatal(err)
}
response, err = parse.Parse(responseString)
if err != nil {
fmt.Printf("Parse err: %v", err)
return
}
return response
}
-66
View File
@@ -1,66 +0,0 @@
package agent
// Card 是顶层结构体,包含所有关于 Go map 的笔记内容
type Card struct {
Summary string `json:"summary"`
KeyPoints KeyPoints `json:"key_points"`
CommonMisconceptions CommonMisconceptions `json:"common_misconceptions"`
BestPractices BestPractices `json:"best_practices"`
Mnemonic Mnemonic `json:"mnemonic"`
}
// KeyPoints 包含关键要点部分
type KeyPoints struct {
Header string `json:"header"`
Items []KeyPointItem `json:"items"`
}
// KeyPointItem 是关键要点的单个条目
type KeyPointItem struct {
Point string `json:"point"`
Description string `json:"description"`
}
// CommonMisconceptions 包含常见误区部分
type CommonMisconceptions struct {
Header string `json:"header"`
Items []MisconceptionItem `json:"items"`
}
// MisconceptionItem 是常见误区的单个条目
type MisconceptionItem struct {
ID string `json:"id"`
Title string `json:"title"`
Detail string `json:"detail"`
Fix string `json:"fix"`
}
// BestPractices 包含最佳实践部分
type BestPractices struct {
Header string `json:"header"`
Items []PracticeItem `json:"items"`
}
// PracticeItem 是最佳实践的单个条目
type PracticeItem struct {
ID string `json:"id"`
Title string `json:"title"`
Detail string `json:"detail"`
CorrectExample string `json:"correct_example,omitempty"` // 可能不存在于某些条目
IncorrectExample string `json:"incorrect_example,omitempty"` // 可能不存在于某些条目
}
// Mnemonic 包含记忆口诀部分
type Mnemonic struct {
Header string `json:"header"`
Content string `json:"content"`
}
// 使用示例:
// var card Card
// err := json.Unmarshal(data, &card)
// 或
// wrapper := struct {
// Card Card `json:"card"`
// }{}
// err := json.Unmarshal(data, &wrapper)
@@ -1,18 +0,0 @@
package agent
import (
"testing"
"github.com/tmc/langchaingo/outputparser"
)
func TestCard(t *testing.T) {
definedCard, err := outputparser.NewDefined(Card{})
if err != nil {
t.Fatalf("%v", err)
}
output := definedCard.GetFormatInstructions()
t.Log(output)
}