Feat: 实现简单对话功能
This commit is contained in:
+30
-13
@@ -1,9 +1,11 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/tmc/langchaingo/llms"
|
"github.com/tmc/langchaingo/llms"
|
||||||
"github.com/tmc/langchaingo/llms/openai"
|
"github.com/tmc/langchaingo/llms/openai"
|
||||||
@@ -30,21 +32,36 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ctx := context.Background()
|
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)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user