重构了一下 rag的方法, 使用 goframe的框架, 还有redis连接部分

This commit is contained in:
Cold
2025-12-06 18:04:29 +08:00
committed by 张斌
parent f7cb007491
commit 4b2b5e6177
16 changed files with 398 additions and 260 deletions

View File

@@ -2,8 +2,9 @@ package ragflow
import (
"context"
"encoding/json"
"fmt"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/errors/gerror"
)
// OpenAICompatibleAPI 与 OpenAI 兼容的 API
@@ -64,11 +65,11 @@ type ChatCompletionChunk struct {
// CreateChatCompletion 创建聊天补全(与聊天助手)
// POST /api/v1/chats_openai/{chat_id}/chat/completions
func (c *Client) CreateChatCompletion(ctx context.Context, chatID string, req *ChatCompletionRequest) (*ChatCompletionResponse, error) {
path := fmt.Sprintf("/api/v1/chats_openai/%s/chat/completions", chatID)
path := "/api/v1/chats_openai/" + chatID + "/chat/completions"
var resp ChatCompletionResponse
if err := c.request(ctx, "POST", path, req, &resp); err != nil {
return nil, fmt.Errorf("create chat completion failed: %w", err)
return nil, gerror.Newf("create chat completion failed: %v", err)
}
return &resp, nil
@@ -77,11 +78,11 @@ func (c *Client) CreateChatCompletion(ctx context.Context, chatID string, req *C
// CreateAgentCompletion 创建 Agent 补全
// POST /api/v1/agents_openai/{agent_id}/chat/completions
func (c *Client) CreateAgentCompletion(ctx context.Context, agentID string, req *ChatCompletionRequest) (*ChatCompletionResponse, error) {
path := fmt.Sprintf("/api/v1/agents_openai/%s/chat/completions", agentID)
path := "/api/v1/agents_openai/" + agentID + "/chat/completions"
var resp ChatCompletionResponse
if err := c.request(ctx, "POST", path, req, &resp); err != nil {
return nil, fmt.Errorf("create agent completion failed: %w", err)
return nil, gerror.Newf("create agent completion failed: %v", err)
}
return &resp, nil
@@ -91,31 +92,26 @@ func (c *Client) CreateAgentCompletion(ctx context.Context, agentID string, req
// 注意:流式响应需要特殊处理,这里返回一个可用于读取流的接口
func (c *Client) CreateChatCompletionStream(ctx context.Context, chatID string, req *ChatCompletionRequest) (*StreamReader, error) {
req.Stream = true
_ = fmt.Sprintf("/api/v1/chats_openai/%s/chat/completions", chatID)
// TODO: 实现流式读取逻辑
return nil, fmt.Errorf("stream mode not implemented yet")
return nil, gerror.New("stream mode not implemented yet")
}
// StreamReader 流式响应读取器
type StreamReader struct {
decoder *json.Decoder
close func() error
_ *gjson.Json // TODO: 实现流式读取时使用
close func() error
}
// ReadChunk 读取下一个响应块
// TODO: 实现流式读取逻辑
func (sr *StreamReader) ReadChunk() (*ChatCompletionChunk, error) {
var chunk ChatCompletionChunk
if err := sr.decoder.Decode(&chunk); err != nil {
return nil, err
}
return &chunk, nil
return nil, gerror.New("stream mode not implemented yet")
}
// Close 关闭流
func (sr *StreamReader) Close() error {
func (sr *StreamReader) Close() (err error) {
if sr.close != nil {
return sr.close()
}
return nil
return
}