Implement chat completions in go (#16491)

### Summary

POST   /api/v1/chat/completions
This commit is contained in:
qinling0210
2026-07-01 15:52:52 +08:00
committed by GitHub
parent b8e960e6c8
commit 7862f69f39
19 changed files with 1917 additions and 2362 deletions

View File

@@ -227,192 +227,6 @@ func (h *ChatHandler) MindMap(c *gin.Context) {
jsonResponse(c, common.CodeSuccess, mindMap, "success")
}
// ListChatsNext list chats with advanced filtering and pagination
// @Summary List Chats Next
// @Description Get list of chats with filtering, pagination and sorting (equivalent to list_dialogs_next)
// @Tags chat
// @Accept json
// @Produce json
// @Param keywords query string false "search keywords"
// @Param page query int false "page number"
// @Param page_size query int false "items per page"
// @Param orderby query string false "order by field (default: create_time)"
// @Param desc query bool false "descending order (default: true)"
// @Param request body service.ListChatsNextRequest true "filter options including owner_ids"
// @Success 200 {object} service.ListChatsNextResponse
// @Router /v1/dialog/next [post]
func (h *ChatHandler) ListChatsNext(c *gin.Context) {
user, errorCode, errorMessage := GetUser(c)
if errorCode != common.CodeSuccess {
jsonError(c, errorCode, errorMessage)
return
}
userID := user.ID
// Parse query parameters
keywords := c.Query("keywords")
page := 0
if pageStr := c.Query("page"); pageStr != "" {
if p, err := strconv.Atoi(pageStr); err == nil && p > 0 {
page = p
}
}
pageSize := 0
if pageSizeStr := c.Query("page_size"); pageSizeStr != "" {
if ps, err := strconv.Atoi(pageSizeStr); err == nil && ps > 0 {
pageSize = ps
}
}
orderby := c.DefaultQuery("orderby", "create_time")
desc := true
if descStr := c.Query("desc"); descStr != "" {
desc = descStr != "false"
}
// Parse request body for owner_ids
var req service.ListChatsNextRequest
if c.Request.ContentLength > 0 {
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"code": 400,
"message": err.Error(),
})
return
}
}
// List chats with advanced filtering
result, err := h.chatService.ListChatsNext(userID, keywords, page, pageSize, orderby, desc, req.OwnerIDs)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"code": 500,
"message": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"data": result,
"message": "success",
})
}
// SetDialog create or update a dialog
// @Summary Set Dialog
// @Description Create or update a dialog (chat). If dialog_id is provided, updates existing dialog; otherwise creates new one.
// @Tags chat
// @Accept json
// @Produce json
// @Param request body service.SetDialogRequest true "dialog configuration"
// @Success 200 {object} service.SetDialogResponse
// @Router /v1/dialog/set [post]
func (h *ChatHandler) SetDialog(c *gin.Context) {
user, errorCode, errorMessage := GetUser(c)
if errorCode != common.CodeSuccess {
jsonError(c, errorCode, errorMessage)
return
}
userID := user.ID
// Parse request body
var req service.SetDialogRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"code": 400,
"message": err.Error(),
})
return
}
// Validate required field: prompt_config
if req.PromptConfig == nil {
c.JSON(http.StatusBadRequest, gin.H{
"code": 400,
"message": "prompt_config is required",
})
return
}
// Call service to set dialog
result, err := h.chatService.SetDialog(userID, &req)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"code": 500,
"message": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"data": result,
"message": "success",
})
}
// RemoveDialogsRequest remove dialogs request
type RemoveDialogsRequest struct {
DialogIDs []string `json:"dialog_ids" binding:"required"`
}
// RemoveChats remove/delete dialogs (soft delete by setting status to invalid)
// @Summary Remove Dialogs
// @Description Remove dialogs by setting their status to invalid. Only the owner of the dialog can perform this operation.
// @Tags chat
// @Accept json
// @Produce json
// @Param request body RemoveDialogsRequest true "dialog IDs to remove"
// @Success 200 {object} map[string]interface{}
// @Router /v1/dialog/rm [post]
func (h *ChatHandler) RemoveChats(c *gin.Context) {
user, errorCode, errorMessage := GetUser(c)
if errorCode != common.CodeSuccess {
jsonError(c, errorCode, errorMessage)
return
}
userID := user.ID
// Parse request body
var req RemoveDialogsRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"code": 400,
"message": err.Error(),
})
return
}
// Call service to remove dialogs
if err := h.chatService.RemoveChats(userID, req.DialogIDs); err != nil {
// Check if it's an authorization error
if err.Error() == "only owner of chat authorized for this operation" {
c.JSON(http.StatusForbidden, gin.H{
"code": 403,
"data": false,
"message": err.Error(),
})
return
}
c.JSON(http.StatusInternalServerError, gin.H{
"code": 500,
"message": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"data": true,
"message": "success",
})
}
// DeleteChat soft deletes a chat by ID.
func (h *ChatHandler) DeleteChat(c *gin.Context) {
user, errorCode, errorMessage := GetUser(c)
if errorCode != common.CodeSuccess {

View File

@@ -19,7 +19,6 @@ package handler
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"ragflow/internal/common"
@@ -44,107 +43,6 @@ func NewChatSessionHandler(chatSessionService *service.ChatSessionService, userS
}
}
// SetChatSession create or update a chat session
// @Summary Set chat session
// @Description Create or update a chat session. If is_new is true, creates new chat session; otherwise updates existing one.
// @Tags chat_session
// @Accept json
// @Produce json
// @Param request body service.SetChatSessionRequest true "chat session configuration"
// @Success 200 {object} service.SetChatSessionResponse
// @Router /v1/conversation/set [post]
func (h *ChatSessionHandler) SetChatSession(c *gin.Context) {
user, errorCode, errorMessage := GetUser(c)
if errorCode != common.CodeSuccess {
jsonError(c, errorCode, errorMessage)
return
}
userID := user.ID
// Parse request body
var req service.SetChatSessionRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"code": 400,
"message": err.Error(),
})
return
}
// Call service to set chat session
result, err := h.chatSessionService.SetChatSession(userID, &req)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"code": 500,
"message": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"data": result,
"message": "success",
})
}
// RemoveChatSessionsRequest remove chat sessions request
type RemoveChatSessionsRequest struct {
ConversationIDs []string `json:"conversation_ids" binding:"required"`
}
// RemoveChatSessions remove/delete chat sessions
// @Summary Remove Chat Sessions
// @Description Remove chat sessions by their IDs. Only the owner of the chat session can perform this operation.
// @Tags chat_session
// @Accept json
// @Produce json
// @Param request body RemoveChatSessionsRequest true "chat session IDs to remove"
// @Success 200 {object} map[string]interface{}
// @Router /v1/conversation/rm [post]
func (h *ChatSessionHandler) RemoveChatSessions(c *gin.Context) {
user, errorCode, errorMessage := GetUser(c)
if errorCode != common.CodeSuccess {
jsonError(c, errorCode, errorMessage)
return
}
userID := user.ID
// Parse request body
var req RemoveChatSessionsRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"code": 400,
"message": err.Error(),
})
return
}
// Call service to remove chat sessions
if err := h.chatSessionService.RemoveChatSessions(userID, req.ConversationIDs); err != nil {
// Check if it's an authorization error
if err.Error() == "Only owner of chat session authorized for this operation" {
c.JSON(http.StatusForbidden, gin.H{
"code": 403,
"data": false,
"message": err.Error(),
})
return
}
c.JSON(http.StatusInternalServerError, gin.H{
"code": 500,
"message": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"data": true,
"message": "success",
})
}
// ListChatSessions list chat sessions for a dialog
// @Summary List Chat Sessions
// @Description Get list of chat sessions for a specific dialog
@@ -198,30 +96,37 @@ func (h *ChatSessionHandler) ListChatSessions(c *gin.Context) {
})
}
// CompletionRequest completion request
type CompletionRequest struct {
ConversationID string `json:"conversation_id" binding:"required"`
Messages []map[string]interface{} `json:"messages" binding:"required"`
LLMID string `json:"llm_id,omitempty"`
Stream *bool `json:"stream,omitempty"`
Thinking *bool `json:"thinking,omitempty"`
Temperature float64 `json:"temperature,omitempty"`
TopP float64 `json:"top_p,omitempty"`
FrequencyPenalty float64 `json:"frequency_penalty,omitempty"`
PresencePenalty float64 `json:"presence_penalty,omitempty"`
MaxTokens int `json:"max_tokens,omitempty"`
type ChatCompletionsRequest struct {
ChatID string `json:"chat_id,omitempty"`
SessionID string `json:"session_id,omitempty"`
ConversationID string `json:"conversation_id,omitempty"`
Messages []map[string]interface{} `json:"messages,omitempty"`
Question string `json:"question,omitempty"`
Files []interface{} `json:"files,omitempty"`
LLMID string `json:"llm_id,omitempty"`
PassAllHistoryMessages *bool `json:"pass_all_history_messages,omitempty"`
PassAllHistory *bool `json:"pass_all_history,omitempty"`
Legacy bool `json:"legacy,omitempty"`
Stream *bool `json:"stream"`
Temperature *float64 `json:"temperature,omitempty"`
TopP *float64 `json:"top_p,omitempty"`
FrequencyPenalty *float64 `json:"frequency_penalty,omitempty"`
PresencePenalty *float64 `json:"presence_penalty,omitempty"`
MaxTokens *int `json:"max_tokens,omitempty"`
}
// Completion chat completion
// ChatCompletions chat completion
// @Summary Chat Completion
// @Description Send messages to the chat model and get a response. Supports streaming and non-streaming modes.
// @Description Send messages to the chat model and get a response.
// @Description Default is streaming (text/event-stream); set stream:false for JSON.
// @Tags chat_session
// @Accept json
// @Produce json
// @Param request body CompletionRequest true "completion request"
// @Success 200 {object} map[string]interface{}
// @Router /v1/conversation/completion [post]
func (h *ChatSessionHandler) Completion(c *gin.Context) {
// @Produce json, text/event-stream
// @Param request body ChatCompletionsRequest true "chat completion request"
// @Success 200 {object} map[string]interface{} "Non-streaming JSON response"
// @Success 200 {string} text/event-stream "Streaming SSE response"
// @Router /api/v1/chat/completions [post]
func (h *ChatSessionHandler) ChatCompletions(c *gin.Context) {
user, errorCode, errorMessage := GetUser(c)
if errorCode != common.CodeSuccess {
jsonError(c, errorCode, errorMessage)
@@ -229,83 +134,97 @@ func (h *ChatSessionHandler) Completion(c *gin.Context) {
}
userID := user.ID
// Parse request body
var req CompletionRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"code": 400,
"message": err.Error(),
})
var rawBody map[string]interface{}
if err := c.ShouldBindJSON(&rawBody); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
return
}
// Build chat model config
chatModelConfig := make(map[string]interface{})
if req.Temperature != 0 {
chatModelConfig["temperature"] = req.Temperature
var req ChatCompletionsRequest
b, err := json.Marshal(rawBody)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
return
}
if req.TopP != 0 {
chatModelConfig["top_p"] = req.TopP
if err := json.Unmarshal(b, &req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
return
}
if req.FrequencyPenalty != 0 {
chatModelConfig["frequency_penalty"] = req.FrequencyPenalty
// Normalize session_id / conversation_id
sessionID := req.SessionID
if sessionID == "" {
sessionID = req.ConversationID
}
if req.PresencePenalty != 0 {
chatModelConfig["presence_penalty"] = req.PresencePenalty
// Build generation config
genConfig := make(map[string]interface{})
if req.Temperature != nil {
genConfig["temperature"] = *req.Temperature
}
if req.MaxTokens != 0 {
chatModelConfig["max_tokens"] = req.MaxTokens
if req.TopP != nil {
genConfig["top_p"] = *req.TopP
}
if req.FrequencyPenalty != nil {
genConfig["frequency_penalty"] = *req.FrequencyPenalty
}
if req.PresencePenalty != nil {
genConfig["presence_penalty"] = *req.PresencePenalty
}
if req.MaxTokens != nil {
genConfig["max_tokens"] = *req.MaxTokens
}
// Resolve pass_all_history from either alias
passAllHistory := false
if req.PassAllHistory != nil {
passAllHistory = *req.PassAllHistory
}
if req.PassAllHistoryMessages != nil {
passAllHistory = *req.PassAllHistoryMessages
}
// Remove known keys from rawBody; what remains is passthrough kwargs
knownKeys := []string{
"chat_id", "session_id", "conversation_id",
"messages", "question", "files",
"llm_id",
"pass_all_history_messages", "pass_all_history",
"legacy", "stream",
"temperature", "top_p", "frequency_penalty", "presence_penalty", "max_tokens",
}
for _, key := range knownKeys {
delete(rawBody, key)
}
kwargs := rawBody
// Determine stream mode
streamMode := true
if req.Stream != nil {
chatModelConfig["stream"] = *req.Stream
}
if req.Thinking != nil {
chatModelConfig["thinking"] = *req.Thinking
streamMode = *req.Stream
}
// Process messages - filter out system messages and initial assistant messages
var processedMessages []map[string]interface{}
for i, m := range req.Messages {
role, _ := m["role"].(string)
if role == "system" {
continue
}
if role == "assistant" && len(processedMessages) == 0 {
continue
}
processedMessages = append(processedMessages, m)
_ = i
}
// Get last message ID if present
var messageID string
if len(processedMessages) > 0 {
if id, ok := processedMessages[len(processedMessages)-1]["id"].(string); ok {
messageID = id
}
}
// Call service
if req.Stream != nil && *req.Stream {
// Streaming response
if streamMode {
disableWriteDeadlineForSSE(c)
c.Header("Content-Type", "text/event-stream")
c.Header("Cache-Control", "no-cache")
c.Header("Connection", "keep-alive")
c.Header("X-Accel-Buffering", "no")
// Create a channel for streaming data
streamChan := make(chan string)
streamChan := make(chan string, 32)
reqCtx := c.Request.Context()
go func() {
defer close(streamChan)
err := h.chatSessionService.CompletionStream(reqCtx, userID, req.ConversationID, processedMessages, req.LLMID, chatModelConfig, messageID, streamChan)
if err != nil {
streamChan <- fmt.Sprintf("data: %s\n\n", err.Error())
}
_, _ = h.chatSessionService.ChatCompletions(
reqCtx, userID,
req.ChatID, sessionID,
req.Messages, req.Question, req.Files,
req.LLMID, genConfig, kwargs,
passAllHistory, req.Legacy,
true, streamChan,
)
}()
// Stream data to client
c.Stream(func(w io.Writer) bool {
data, ok := <-streamChan
if !ok {
@@ -315,8 +234,14 @@ func (h *ChatSessionHandler) Completion(c *gin.Context) {
return true
})
} else {
// Non-streaming response
result, err := h.chatSessionService.Completion(userID, req.ConversationID, processedMessages, req.LLMID, chatModelConfig, messageID)
result, err := h.chatSessionService.ChatCompletions(
c.Request.Context(), userID,
req.ChatID, sessionID,
req.Messages, req.Question, req.Files,
req.LLMID, genConfig, kwargs,
passAllHistory, req.Legacy,
false, nil,
)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"code": 500,
@@ -324,7 +249,6 @@ func (h *ChatSessionHandler) Completion(c *gin.Context) {
})
return
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"data": result,