This commit is contained in:
Cold
2025-11-27 18:03:01 +08:00
committed by 张斌
parent ad1ccc2bc1
commit d8410fab37
2 changed files with 27 additions and 29 deletions

View File

@@ -11,15 +11,15 @@ import (
// ChatCompletionMessage OpenAI 格式的消息
type ChatCompletionMessage struct {
Role string `json:"role"` // "user", "assistant", "system"
Role string `json:"role"` // "user", "assistant", "system"
Content string `json:"content"`
}
// ChatCompletionRequest OpenAI 格式的聊天补全请求
type ChatCompletionRequest struct {
Model string `json:"model"` // 模型名称(服务器会自动解析,可设置为任意值)
Messages []ChatCompletionMessage `json:"messages"` // 消息列表,必须至少包含一条 user 消息
Stream bool `json:"stream,omitempty"` // 是否流式返回,默认 false
Model string `json:"model"` // 模型名称(服务器会自动解析,可设置为任意值)
Messages []ChatCompletionMessage `json:"messages"` // 消息列表,必须至少包含一条 user 消息
Stream bool `json:"stream,omitempty"` // 是否流式返回,默认 false
}
// ChatCompletionResponse OpenAI 格式的聊天补全响应(非流式)
@@ -29,9 +29,9 @@ type ChatCompletionResponse struct {
Created int64 `json:"created"`
Model string `json:"model"`
Choices []struct {
Index int `json:"index"`
Index int `json:"index"`
Message ChatCompletionMessage `json:"message"`
FinishReason string `json:"finish_reason"`
FinishReason string `json:"finish_reason"`
} `json:"choices"`
Usage struct {
PromptTokens int `json:"prompt_tokens"`
@@ -47,8 +47,8 @@ type ChatCompletionChunk struct {
Created int64 `json:"created"`
Model string `json:"model"`
Choices []struct {
Index int `json:"index"`
Delta struct {
Index int `json:"index"`
Delta struct {
Content string `json:"content"`
Role string `json:"role"`
} `json:"delta"`
@@ -65,12 +65,12 @@ type ChatCompletionChunk struct {
// 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)
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 &resp, nil
}
@@ -78,12 +78,12 @@ func (c *Client) CreateChatCompletion(ctx context.Context, chatID string, req *C
// 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)
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 &resp, nil
}
@@ -91,8 +91,8 @@ 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
apiPath := fmt.Sprintf("/api/v1/chats_openai/%s/chat/completions", chatID)
_ = fmt.Sprintf("/api/v1/chats_openai/%s/chat/completions", chatID)
// TODO: 实现流式读取逻辑
return nil, fmt.Errorf("stream mode not implemented yet")
}
@@ -119,4 +119,3 @@ func (sr *StreamReader) Close() error {
}
return nil
}