Files
chatgpt-dingtalk/pkg/llm/api.go
二丫讲梵 f7326b6797 增加卡片交互流式输出的能力 (#315)
* 将ai交互切换为go-openai

* add stream

*  feat(stream): 优化流式响应机制,实现实时卡片更新

- 将固定1.5秒更新改为基于300ms最小间隔的实时更新策略
- 新增内容缓冲区机制,避免过于频繁的API调用
- 改进流式中断处理,保护已接收的内容不丢失

🔧 chore(llm): 优化HTTP客户端配置

- 增加连接池设置(MaxIdleConns: 100, MaxIdleConnsPerHost: 10)
- 设置空闲连接超时时间为90秒
- 添加HTTP/2禁用选项注释,用于解决流式错误问题

📝 docs(stream): 更新流式更新策略文档

- 详细说明实时流式更新机制和缓冲策略
- 新增HTTP/2流式错误的故障排除指南
- 更新配置参数说明和建议范围

🐛 fix(stream): 修复流式中断时的内容丢失问题

- 在流式接收中断时,确保已接收的内容不会丢失
- 改进错误处理逻辑,区分有内容和无内容的情况

* modify ai
2025-12-11 18:22:35 +08:00

35 lines
764 B
Go

package llm
import (
"context"
"github.com/eryajf/chatgpt-dingtalk/public"
)
// SingleQa 单聊
func SingleQa(question, userId string) (string, error) {
client := NewClient(userId)
defer client.Close()
return client.ChatWithContext(question)
}
// ContextQa 串聊
func ContextQa(question, userId string) (*Client, string, error) {
client := NewClient(userId)
if public.UserService.GetUserSessionContext(userId) != "" {
_ = client.ChatContext.LoadConversation(userId)
}
answer, err := client.ChatWithContext(question)
return client, answer, err
}
// ImageQa 生成图片
func ImageQa(ctx context.Context, question, userId string) (string, error) {
client := NewClient(userId)
defer client.Close()
return client.GenerateImage(ctx, question)
}