第一个聊天ai
This commit is contained in:
54
chat/chat.go
Normal file
54
chat/chat.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package chat
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/cloudwego/eino-ext/components/model/openai"
|
||||
"github.com/cloudwego/eino/schema"
|
||||
)
|
||||
|
||||
func Chat(chatModel *openai.ChatModel, ctx context.Context, question string) {
|
||||
role := "心理咨询师"
|
||||
style := "温柔可爱"
|
||||
history := []*schema.Message{}
|
||||
messages, err := GenerateChatMessage(role, style, question, history)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
response, err := chatModel.Generate(ctx, messages)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
// 获取 Token 使用情况
|
||||
if usage := response.ResponseMeta.Usage; usage != nil {
|
||||
println("提示 Tokens:", usage.PromptTokens)
|
||||
println("生成 Tokens:", usage.CompletionTokens)
|
||||
println("总 Tokens:", usage.TotalTokens)
|
||||
}
|
||||
fmt.Println(response)
|
||||
}
|
||||
func ChatStream(chatModel *openai.ChatModel, ctx context.Context, question string) {
|
||||
role := "心理咨询师"
|
||||
style := "温柔可爱"
|
||||
history := []*schema.Message{}
|
||||
messages, err := GenerateChatMessage(role, style, question, history)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
// 获取流式回复
|
||||
reader, err := chatModel.Stream(ctx, messages)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer reader.Close() // 注意要关闭
|
||||
|
||||
// 处理流式内容
|
||||
for {
|
||||
chunk, err := reader.Recv()
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
print(chunk.Content)
|
||||
}
|
||||
}
|
||||
24
chat/chatmodel.go
Normal file
24
chat/chatmodel.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package chat
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/cloudwego/eino-ext/components/model/openai"
|
||||
)
|
||||
|
||||
func CreateChatModel() (*openai.ChatModel, context.Context) {
|
||||
ctx := context.Background()
|
||||
timeout := 30 * time.Second
|
||||
chatModel, err := openai.NewChatModel(ctx, &openai.ChatModelConfig{
|
||||
BaseURL: "https://api-proxy.meowrain.cn/ai/v1",
|
||||
Model: "deepseek-v3.2",
|
||||
APIKey: "meowrain",
|
||||
Timeout: timeout,
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return chatModel, ctx
|
||||
|
||||
}
|
||||
31
chat/templates.go
Normal file
31
chat/templates.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package chat
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/cloudwego/eino/components/prompt"
|
||||
"github.com/cloudwego/eino/schema"
|
||||
)
|
||||
|
||||
var template = prompt.FromMessages(schema.FString,
|
||||
schema.SystemMessage("你是一个{role}。你需要用{style}的语气回答问题。你的目标是帮助程"+
|
||||
"序员保持积极乐观的心态,提供技术建议的同时也要关注他们的心理健康。"),
|
||||
schema.MessagesPlaceholder("chat_history", true),
|
||||
schema.UserMessage("问题: {question}"))
|
||||
|
||||
func GenerateChatMessage(role, style, question string, history []*schema.Message) ([]*schema.Message, error) {
|
||||
// 在这里调用模板
|
||||
messages, err := template.Format(context.Background(), map[string]any{
|
||||
"role": role,
|
||||
"style": style,
|
||||
"question": question,
|
||||
"chat_history": history,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("生成消息失败: %w", err)
|
||||
}
|
||||
|
||||
return messages, nil
|
||||
}
|
||||
Reference in New Issue
Block a user