128 lines
2.6 KiB
Go
128 lines
2.6 KiB
Go
package agent
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/tmc/langchaingo/llms"
|
|
"github.com/tmc/langchaingo/outputparser"
|
|
"github.com/tmc/langchaingo/prompts"
|
|
)
|
|
|
|
func Call(llm llms.Model, ctx context.Context) (response string) {
|
|
|
|
response, err := llms.GenerateFromSinglePrompt(
|
|
ctx,
|
|
llm,
|
|
"Hello! My name is wonder!",
|
|
)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
fmt.Println("AI:", response)
|
|
|
|
return response
|
|
|
|
}
|
|
func CallWithMessage(llm llms.Model, ctx context.Context, prompt string) (response string) {
|
|
|
|
response, err := llms.GenerateFromSinglePrompt(
|
|
ctx,
|
|
llm,
|
|
"用户正在学习Go语言,你是用户的助手。\n"+prompt,
|
|
)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
fmt.Println("AI:", response)
|
|
|
|
return response
|
|
|
|
}
|
|
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)
|
|
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 {
|
|
fmt.Printf("Prompt err: %v", err)
|
|
}
|
|
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语言和React框架,你是用户的助手。\n"+prompt,
|
|
)
|
|
|
|
fmt.Printf("Raw output: %v\n\n", responseString)
|
|
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
responseString = strings.TrimPrefix(responseString, "\n")
|
|
responseString = strings.TrimSuffix(responseString, "\n")
|
|
|
|
if !strings.Contains(responseString, "```json") {
|
|
responseString = "```json\n" + responseString + "\n```"
|
|
}
|
|
|
|
// fmt.Printf("AI: %s\n\n", responseString)
|
|
|
|
response, err = parse.Parse(responseString)
|
|
if err != nil {
|
|
fmt.Printf("Parse err: %v", err)
|
|
return
|
|
}
|
|
|
|
fmt.Printf("API:\n%v", response)
|
|
|
|
return response
|
|
}
|