✨Feat: 增添结构体解析函数
This commit is contained in:
@@ -25,3 +25,5 @@ go.work.sum
|
|||||||
# env file
|
# env file
|
||||||
.env
|
.env
|
||||||
.vscode/
|
.vscode/
|
||||||
|
|
||||||
|
.exe
|
||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
|
|
||||||
"github.com/tmc/langchaingo/llms"
|
"github.com/tmc/langchaingo/llms"
|
||||||
"github.com/tmc/langchaingo/prompts"
|
"github.com/tmc/langchaingo/prompts"
|
||||||
|
"github.com/tmc/langchaingo/outputparser"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Call(llm llms.Model, ctx context.Context) (response string) {
|
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
|
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)
|
content, err := os.ReadFile(promptPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("File err: %v", err)
|
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)
|
response = CallWithMessage(llm, ctx, prompt)
|
||||||
return response
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -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)
|
|
||||||
}
|
|
||||||
+19
-17
@@ -2,6 +2,7 @@ package handle
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"agent/agent"
|
"agent/agent"
|
||||||
|
"agent/response_struct"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
@@ -18,38 +19,38 @@ var files = []string{
|
|||||||
".\\prompt\\scenario.txt",
|
".\\prompt\\scenario.txt",
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetCard(llm llms.Model, ctx context.Context, topic string) (response string) {
|
func GetCard(llm llms.Model, ctx context.Context, topic string) (response response_struct.Card) {
|
||||||
file := files[0]
|
file := files[0]
|
||||||
response = agent.CallWithAgent(llm, ctx, topic, file)
|
response = agent.CallWithAgentDefined(llm, ctx, topic, file, response_struct.Card{})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetGuodegang(llm llms.Model, ctx context.Context, topic string) (response string) {
|
func GetGuodegang(llm llms.Model, ctx context.Context, topic string) (response response_struct.Guodegang) {
|
||||||
file := files[1]
|
file := files[1]
|
||||||
response = agent.CallWithAgent(llm, ctx, topic, file)
|
response = agent.CallWithAgentDefined(llm, ctx, topic, file, response_struct.Guodegang{})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetMermaid(llm llms.Model, ctx context.Context, topic string) (response string) {
|
func GetMermaid(llm llms.Model, ctx context.Context, topic string) (response response_struct.Mermaid) {
|
||||||
file := files[2]
|
file := files[2]
|
||||||
response = agent.CallWithAgent(llm, ctx, topic, file)
|
response = agent.CallWithAgentDefined(llm, ctx, topic, file, response_struct.Mermaid{})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetQuiz(llm llms.Model, ctx context.Context, topic string) (response string) {
|
func GetQuiz(llm llms.Model, ctx context.Context, topic string) (response response_struct.Quiz) {
|
||||||
file := files[3]
|
file := files[3]
|
||||||
response = agent.CallWithAgent(llm, ctx, topic, file)
|
response = agent.CallWithAgentDefined(llm, ctx, topic, file, response_struct.Quiz{})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetScenario(llm llms.Model, ctx context.Context, topic string) (response string) {
|
func GetScenario(llm llms.Model, ctx context.Context, topic string) (response response_struct.Scenario) {
|
||||||
file := files[4]
|
file := files[4]
|
||||||
response = agent.CallWithAgent(llm, ctx, topic, file)
|
response = agent.CallWithAgentDefined(llm, ctx, topic, file, response_struct.Scenario{})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAll(llm llms.Model, ctx context.Context, topic string) (response map[string]string) {
|
|
||||||
response = make(map[string]string)
|
func GetAll(llm llms.Model, ctx context.Context, topic string) (response *response_struct.AllStruct) {
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
@@ -57,31 +58,32 @@ func GetAll(llm llms.Model, ctx context.Context, topic string) (response map[str
|
|||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
response["card"] = GetCard(llm, ctx, topic)
|
response.CardStruct = GetCard(llm, ctx, topic)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
response["guodegang"] = GetGuodegang(llm, ctx, topic)
|
response.GuodegangStruct = GetGuodegang(llm, ctx, topic)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
response["mermaid"] = GetMermaid(llm, ctx, topic)
|
response.MermaidStruct = GetMermaid(llm, ctx, topic)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
response["quiz"] = GetQuiz(llm, ctx, topic)
|
response.QuizStruct = GetQuiz(llm, ctx, topic)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
response["scenario"] = GetScenario(llm, ctx, topic)
|
response.ScenarioStruct = GetScenario(llm, ctx, topic)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
|
|
||||||
fmt.Println("======== All responses received ========")
|
fmt.Println("======== All responses received ========")
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package response_struct
|
||||||
|
|
||||||
|
type AllStruct struct {
|
||||||
|
CardStruct Card `json:"card"`
|
||||||
|
GuodegangStruct Guodegang `json:"guodegang"`
|
||||||
|
MermaidStruct Mermaid `json:"mermaid"`
|
||||||
|
QuizStruct Quiz `json:"quiz"`
|
||||||
|
ScenarioStruct Scenario `json:"scenario"`
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package agent
|
package response_struct
|
||||||
|
|
||||||
// Card 是顶层结构体,包含所有关于 Go map 的笔记内容
|
// Card 是顶层结构体,包含所有关于 Go map 的笔记内容
|
||||||
type Card struct {
|
type Card struct {
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package response_struct
|
||||||
|
|
||||||
|
type Guodegang struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Content string `json:"content"`
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package response_struct
|
||||||
|
|
||||||
|
type Mermaid struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
Content string `json:"content"`
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package response_struct
|
||||||
|
|
||||||
|
type Quiz struct {
|
||||||
|
Questions []Question `json:"questions"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Question struct {
|
||||||
|
Question string `json:"question"`
|
||||||
|
Options []string `json:"options"`
|
||||||
|
Answer string `json:"answer"`
|
||||||
|
Explanation string `json:"explanation"`
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
package response_struct
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGuodegang(t *testing.T) {
|
||||||
|
definedGuodegang, err := outputparser.NewDefined(Guodegang{})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
output := definedGuodegang.GetFormatInstructions()
|
||||||
|
t.Log(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMermaid(t *testing.T) {
|
||||||
|
definedMermaid, err := outputparser.NewDefined(Mermaid{})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
output := definedMermaid.GetFormatInstructions()
|
||||||
|
t.Log(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestQuiz(t *testing.T) {
|
||||||
|
definedQuiz, err := outputparser.NewDefined(Quiz{})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
output := definedQuiz.GetFormatInstructions()
|
||||||
|
t.Log(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestScenario(t *testing.T) {
|
||||||
|
definedScenario, err := outputparser.NewDefined(Scenario{})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
output := definedScenario.GetFormatInstructions()
|
||||||
|
t.Log(output)
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package response_struct
|
||||||
|
|
||||||
|
type ScenarioResponse struct {
|
||||||
|
Scenarios []Scenario `json:"scenarios"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Scenario struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Background string `json:"background"`
|
||||||
|
Solution string `json:"solution"`
|
||||||
|
CodeExample string `json:"code_example"`
|
||||||
|
CoreProblemSolved string `json:"core_problem_solved"`
|
||||||
|
KeyConsiderations []string `json:"key_considerations"`
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user