feat: 添加配置文件支持并重构聊天模块

- 新增config.yaml和config.go用于管理配置
- 重构chatmodel.go使用配置初始化模型
- 修改GenerateChatMessage和ChatStream函数签名添加context参数
- 更新main.go加载配置并初始化聊天模型
- 优化错误处理和日志输出
This commit is contained in:
lirui
2026-01-11 20:32:41 +08:00
parent 23ad2909e5
commit 5632bf88f8
8 changed files with 137 additions and 124 deletions

28
main.go
View File

@@ -1,9 +1,31 @@
package main
import "enio_meowrain/chat"
import (
"context"
"enio_meowrain/chat"
"enio_meowrain/config"
"log"
)
func main() {
model, ctx := chat.CreateChatModel()
// 加载配置
cfg, err := config.LoadConfig("config.yaml")
if err != nil {
log.Fatalf("无法加载配置文件: %v", err)
}
ctx := context.Background()
model, err := chat.CreateChatModel(ctx, cfg)
if err != nil {
log.Fatalf("创建聊天模型失败: %v", err)
}
question := "我写的代码老是出bug搞得我很焦虑"
chat.ChatStream(model, ctx, question)
role := "心理咨询师"
style := "温柔可爱"
// 使用 ChatStream
if err := chat.ChatStream(model, ctx, role, style, question); err != nil {
log.Printf("聊天过程出错: %v", err)
}
}