mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-07-14 00:48:26 +08:00
Go: add stream / think chat (#14242)
### What problem does this PR solve? 1. Supports stream and non-stream chat 2. Supports think and non-think chat 3. List supported models from DeepSeek service. (This command can be used to verify the API validity) ### Type of change - [x] New Feature (non-breaking change which adds functionality) --------- Signed-off-by: Jin Hai <haijin.chn@gmail.com>
This commit is contained in:
@@ -458,6 +458,41 @@ func (h *ProviderHandler) ListInstanceModels(c *gin.Context) {
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
keywords := ""
|
||||
if queryKeywords := c.Query("supported"); queryKeywords != "" {
|
||||
keywords = queryKeywords
|
||||
}
|
||||
|
||||
// convert keywords to small case
|
||||
keywords = strings.ToLower(keywords)
|
||||
if keywords == "true" {
|
||||
// list supported models
|
||||
|
||||
modelList, err := h.modelProviderService.ListSupportedModels(providerName, instanceName, c.GetString("user_id"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": common.CodeServerError,
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
var modelResponse []map[string]string
|
||||
for _, modelName := range modelList {
|
||||
modelResponse = append(modelResponse, map[string]string{
|
||||
"model_name": modelName,
|
||||
})
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": 0,
|
||||
"message": "success",
|
||||
"data": modelResponse,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
modelInstances, err := h.modelProviderService.ListInstanceModels(providerName, instanceName, c.GetString("user_id"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
@@ -533,9 +568,9 @@ func (h *ProviderHandler) EnableOrDisableModel(c *gin.Context) {
|
||||
}
|
||||
|
||||
type ChatToModelRequest struct {
|
||||
Message string `json:"message" binding:"required"`
|
||||
Stream bool `json:"stream"`
|
||||
Reasoning bool `json:"reasoning"`
|
||||
Message string `json:"message" binding:"required"`
|
||||
Stream bool `json:"stream"`
|
||||
Thinking bool `json:"thinking"`
|
||||
}
|
||||
|
||||
func (h *ProviderHandler) ChatToModel(c *gin.Context) {
|
||||
@@ -610,19 +645,23 @@ func (h *ProviderHandler) ChatToModel(c *gin.Context) {
|
||||
return nil
|
||||
}
|
||||
|
||||
apiConfig := models.APIConfig{
|
||||
ApiKey: nil,
|
||||
Region: nil,
|
||||
}
|
||||
|
||||
chatConfig := models.ChatConfig{
|
||||
Reasoning: &req.Reasoning,
|
||||
Thinking: &req.Thinking,
|
||||
Stream: &req.Stream,
|
||||
Stop: &[]string{},
|
||||
DoSample: nil,
|
||||
MaxTokens: nil,
|
||||
Temperature: nil,
|
||||
TopP: nil,
|
||||
Region: nil,
|
||||
}
|
||||
|
||||
// Stream response using sender function (best performance, no channel)
|
||||
errorCode := h.modelProviderService.ChatToModelStreamWithSender(providerName, instanceName, modelName, userID, req.Message, &chatConfig, sender)
|
||||
errorCode := h.modelProviderService.ChatToModelStreamWithSender(providerName, instanceName, modelName, userID, req.Message, &apiConfig, &chatConfig, sender)
|
||||
|
||||
if errorCode != common.CodeSuccess {
|
||||
c.SSEvent("error", "stream failed")
|
||||
@@ -630,19 +669,23 @@ func (h *ProviderHandler) ChatToModel(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
apiConfig := models.APIConfig{
|
||||
ApiKey: nil,
|
||||
Region: nil,
|
||||
}
|
||||
|
||||
chatConfig := models.ChatConfig{
|
||||
Reasoning: &req.Reasoning,
|
||||
Thinking: &req.Thinking,
|
||||
Stream: &req.Stream,
|
||||
Stop: &[]string{},
|
||||
DoSample: nil,
|
||||
MaxTokens: nil,
|
||||
Temperature: nil,
|
||||
TopP: nil,
|
||||
Region: nil,
|
||||
}
|
||||
|
||||
// Non-stream response
|
||||
response, errorCode, err := h.modelProviderService.ChatToModel(providerName, instanceName, modelName, userID, req.Message, &chatConfig)
|
||||
response, errorCode, err := h.modelProviderService.ChatToModel(providerName, instanceName, modelName, userID, req.Message, &apiConfig, &chatConfig)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": errorCode,
|
||||
@@ -652,7 +695,8 @@ func (h *ProviderHandler) ChatToModel(c *gin.Context) {
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": 0,
|
||||
"message": response,
|
||||
"code": 0,
|
||||
"reasoning_content": response.ReasonContent,
|
||||
"answer": response.Answer,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user