2026-03-22 21:25:36 +08:00
|
|
|
package agent
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
2026-03-23 13:03:18 +08:00
|
|
|
"os"
|
2026-03-22 21:25:36 +08:00
|
|
|
|
|
|
|
|
"github.com/tmc/langchaingo/llms"
|
2026-03-23 13:03:18 +08:00
|
|
|
"github.com/tmc/langchaingo/prompts"
|
2026-03-27 19:30:04 +08:00
|
|
|
"github.com/tmc/langchaingo/outputparser"
|
2026-03-22 21:25:36 +08:00
|
|
|
)
|
|
|
|
|
|
2026-03-22 21:45:27 +08:00
|
|
|
func Call(llm llms.Model, ctx context.Context) (response string) {
|
2026-03-22 21:25:36 +08:00
|
|
|
|
|
|
|
|
response, err := llms.GenerateFromSinglePrompt(
|
|
|
|
|
ctx,
|
|
|
|
|
llm,
|
|
|
|
|
"Hello! My name is wonder!",
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Println("AI:", response)
|
|
|
|
|
|
2026-03-22 21:45:27 +08:00
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
func CallWithMessage(llm llms.Model, ctx context.Context, prompt string) (response string) {
|
|
|
|
|
|
|
|
|
|
response, err := llms.GenerateFromSinglePrompt(
|
|
|
|
|
ctx,
|
|
|
|
|
llm,
|
2026-03-23 13:03:18 +08:00
|
|
|
"用户正在学习Go语言,你是用户的助手。\n"+prompt,
|
2026-03-22 21:45:27 +08:00
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Println("AI:", response)
|
|
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
|
2026-03-22 21:25:36 +08:00
|
|
|
}
|
2026-03-27 19:30:04 +08:00
|
|
|
func CallWithAgent(llm llms.Model, ctx context.Context, topic string, promptPath string,) (response string) {
|
2026-03-23 13:03:18 +08:00
|
|
|
content, err := os.ReadFile(promptPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Printf("File err: %v", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
text := string(content)
|
|
|
|
|
|
|
|
|
|
template := prompts.NewPromptTemplate(text, []string{".Knowledge"})
|
|
|
|
|
prompt, err := template.Format(map[string]any{
|
|
|
|
|
"Knowledge": topic,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
fmt.Println("Me: ", prompt)
|
|
|
|
|
if err != nil {
|
2026-03-23 13:13:46 +08:00
|
|
|
fmt.Printf("Prompt err: %v", err)
|
2026-03-23 13:03:18 +08:00
|
|
|
}
|
|
|
|
|
response = CallWithMessage(llm, ctx, prompt)
|
|
|
|
|
return response
|
|
|
|
|
}
|
2026-03-27 19:30:04 +08:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|