Files
chatgpt-dingtalk/pkg/llm/chat.go
T
二丫讲梵 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

49 lines
1.0 KiB
Go

package llm
import (
"github.com/pandodao/tokenizer-go"
openai "github.com/sashabaranov/go-openai"
"github.com/eryajf/chatgpt-dingtalk/public"
)
// ChatWithContext 对话接口
func (c *Client) ChatWithContext(question string) (string, error) {
if tokenizer.MustCalToken(question) > c.maxQuestionLen {
return "", ErrOverMaxQuestionLength
}
// 构建消息列表
messages := c.buildMessages(question)
model := public.Config.Model
userId := c.userId
if public.Config.AzureOn {
userId = ""
}
req := openai.ChatCompletionRequest{
Model: model,
Messages: messages,
MaxTokens: c.maxAnswerLen,
Temperature: 0.6,
User: userId,
}
resp, err := c.client.CreateChatCompletion(c.ctx, req)
if err != nil {
return "", err
}
answer := resp.Choices[0].Message.Content
// 保存对话上下文
c.ChatContext.old = append(c.ChatContext.old,
conversation{Role: c.ChatContext.humanRole, Prompt: question},
conversation{Role: c.ChatContext.aiRole, Prompt: answer},
)
c.ChatContext.seqTimes++
return answer, nil
}