✨Feat: 增添结构体解析函数
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user