Преглед изворни кода

Add dry run option for chat request

ParthSareen пре 4 месеци
родитељ
комит
38cd80d52c
2 измењених фајлова са 21 додато и 4 уклоњено
  1. 9 4
      api/types.go
  2. 12 0
      server/routes.go

+ 9 - 4
api/types.go

@@ -103,6 +103,10 @@ type ChatRequest struct {
 	// Tools is an optional list of tools the model has access to.
 	// Tools is an optional list of tools the model has access to.
 	Tools `json:"tools,omitempty"`
 	Tools `json:"tools,omitempty"`
 
 
+	// DryRun when true will prepare and validate the request but stop before sending it to the model.
+	// This allows inspecting how the prompt would be constructed and what parameters would be used.
+	DryRun bool `json:"dry_run,omitempty"`
+
 	// Options lists model-specific options.
 	// Options lists model-specific options.
 	Options map[string]interface{} `json:"options"`
 	Options map[string]interface{} `json:"options"`
 }
 }
@@ -185,10 +189,11 @@ func (t *ToolFunction) String() string {
 // ChatResponse is the response returned by [Client.Chat]. Its fields are
 // ChatResponse is the response returned by [Client.Chat]. Its fields are
 // similar to [GenerateResponse].
 // similar to [GenerateResponse].
 type ChatResponse struct {
 type ChatResponse struct {
-	Model      string    `json:"model"`
-	CreatedAt  time.Time `json:"created_at"`
-	Message    Message   `json:"message"`
-	DoneReason string    `json:"done_reason,omitempty"`
+	Model        string    `json:"model"`
+	CreatedAt    time.Time `json:"created_at"`
+	Message      Message   `json:"message"`
+	DoneReason   string    `json:"done_reason,omitempty"`
+	DryRunOutput string    `json:"dry_run_output,omitempty"`
 
 
 	Done bool `json:"done"`
 	Done bool `json:"done"`
 
 

+ 12 - 0
server/routes.go

@@ -1572,6 +1572,18 @@ func (s *Server) ChatHandler(c *gin.Context) {
 		return
 		return
 	}
 	}
 
 
+	if req.DryRun {
+		c.JSON(http.StatusOK, api.ChatResponse{
+			Model:        req.Model,
+			CreatedAt:    time.Now().UTC(),
+			Message:      api.Message{Role: "assistant", Content: ""},
+			Done:         true,
+			DoneReason:   "dry_run",
+			DryRunOutput: prompt,
+		})
+		return
+	}
+
 	slog.Debug("chat request", "images", len(images), "prompt", prompt)
 	slog.Debug("chat request", "images", len(images), "prompt", prompt)
 
 
 	ch := make(chan any)
 	ch := make(chan any)