diff --git a/cmd/single/main.go b/cmd/single/main.go index c3ad5d6..a7fce44 100644 --- a/cmd/single/main.go +++ b/cmd/single/main.go @@ -1,9 +1,11 @@ package main import ( + "bufio" "context" "fmt" "log" + "os" "github.com/tmc/langchaingo/llms" "github.com/tmc/langchaingo/llms/openai" @@ -30,21 +32,36 @@ func main() { } ctx := context.Background() + reader := bufio.NewReader(os.Stdin) - prompt := "哟什么风把您给吹来了" + fmt.Println("========= 请输入您的问题 (输入 'exit' 退出) =========") + for { + fmt.Print("😇 You: ") + prompt, err := reader.ReadString('\n') + if err != nil { + log.Fatalf("读取输入失败: %v", err) + } + + if prompt == "exit\n" { + fmt.Println("========= 谢谢使用,再见! =========") + break + } + + // Chat + message := []llms.MessageContent{ + llms.TextParts(llms.ChatMessageTypeHuman, prompt), + } + + response, err := llm.GenerateContent(ctx, message, + llms.WithModel(model), + ) + + if err != nil { + log.Fatalf("调用失败: %v\n模型: %s", err, model) + } + + fmt.Println("🤖 AI: ", response.Choices[0].Content) - message := []llms.MessageContent{ - llms.TextParts(llms.ChatMessageTypeHuman, prompt), } - response, err := llm.GenerateContent(ctx, message, - llms.WithModel(model), - ) - - if err != nil { - log.Fatalf("调用失败: %v\n模型: %s", err, model) - } - - fmt.Println("模型响应:", response.Choices[0].Content) - } diff --git a/cmd/test/main.go b/cmd/test/main.go new file mode 100644 index 0000000..c3ad5d6 --- /dev/null +++ b/cmd/test/main.go @@ -0,0 +1,50 @@ +package main + +import ( + "context" + "fmt" + "log" + + "github.com/tmc/langchaingo/llms" + "github.com/tmc/langchaingo/llms/openai" +) + +func main() { + apiKey := "sk-angtzujyplaqsrxvlxkmjahpyxwnhhaonnfamokggxclmdlv" + baseUrl := "https://api.siliconflow.cn/v1" + + //baseUrl := "https://api.siliconflow.cn/v1/chat/completions" + model := "Pro/deepseek-ai/DeepSeek-V3.2" + + if apiKey == "" { + log.Fatal("apiKey is empty") + } + + llm, err := openai.New( + openai.WithToken(apiKey), + openai.WithBaseURL(baseUrl), + ) + + if err != nil { + log.Fatalf("初始化失败: %v\nAPI Key: %s\nBase URL: %s", err, apiKey, baseUrl) + } + + ctx := context.Background() + + prompt := "哟什么风把您给吹来了" + + message := []llms.MessageContent{ + llms.TextParts(llms.ChatMessageTypeHuman, prompt), + } + + response, err := llm.GenerateContent(ctx, message, + llms.WithModel(model), + ) + + if err != nil { + log.Fatalf("调用失败: %v\n模型: %s", err, model) + } + + fmt.Println("模型响应:", response.Choices[0].Content) + +}