Files
langchain-go/backend/agent/basicAgent.go
T

128 lines
2.6 KiB
Go
Raw Normal View History

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-27 20:30:49 +08:00
"strings"
2026-03-22 21:25:36 +08:00
"github.com/tmc/langchaingo/llms"
2026-03-27 19:30:04 +08:00
"github.com/tmc/langchaingo/outputparser"
2026-03-27 20:01:56 +08:00
"github.com/tmc/langchaingo/prompts"
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-28 20:00:08 +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) {
2026-03-28 20:00:08 +08:00
2026-03-27 19:30:04 +08:00
parse, err := outputparser.NewDefined(responseType)
if err != nil {
fmt.Printf("CreateParse err: %v", err)
return
}
2026-03-28 20:00:08 +08:00
2026-03-27 19:30:04 +08:00
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)
}
2026-03-28 20:00:08 +08:00
2026-03-27 19:30:04 +08:00
responseString, err := llms.GenerateFromSinglePrompt(
ctx,
llm,
2026-03-27 20:01:56 +08:00
"用户正在学习Go语言和React框架,你是用户的助手。\n"+prompt,
2026-03-27 19:30:04 +08:00
)
2026-03-27 20:30:49 +08:00
2026-03-29 10:10:32 +08:00
fmt.Printf("Raw output: %v\n\n", responseString)
2026-03-27 19:30:04 +08:00
if err != nil {
log.Fatal(err)
}
2026-03-28 20:00:08 +08:00
responseString = strings.TrimPrefix(responseString, "\n")
responseString = strings.TrimSuffix(responseString, "\n")
2026-03-27 20:30:49 +08:00
if !strings.Contains(responseString, "```json") {
2026-03-28 20:00:08 +08:00
responseString = "```json\n" + responseString + "\n```"
2026-03-27 20:30:49 +08:00
}
2026-03-28 20:00:08 +08:00
// fmt.Printf("AI: %s\n\n", responseString)
2026-03-27 20:30:49 +08:00
2026-03-27 19:30:04 +08:00
response, err = parse.Parse(responseString)
if err != nil {
fmt.Printf("Parse err: %v", err)
return
}
2026-03-28 20:00:08 +08:00
fmt.Printf("API:\n%v", response)
2026-03-27 19:30:04 +08:00
return response
}