Skip to content

Commit 6dda3c5

Browse files
authored
Add --http-log CLI flag to replace DEBUG environment variable for HTTP request logging (#77)
2 parents 06a9caa + 7903afa commit 6dda3c5

File tree

5 files changed

+58
-16
lines changed

5 files changed

+58
-16
lines changed

cmd/eval/eval.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ func NewEvalCommand(cfg *command.Config) *cobra.Command {
111111
// Get the org flag
112112
org, _ := cmd.Flags().GetString("org")
113113

114+
// Get the http-log flag
115+
httpLog, _ := cmd.Flags().GetString("http-log")
116+
114117
// Load the evaluation prompt file
115118
evalFile, err := loadEvaluationPromptFile(promptFilePath)
116119
if err != nil {
@@ -126,7 +129,13 @@ func NewEvalCommand(cfg *command.Config) *cobra.Command {
126129
org: org,
127130
}
128131

129-
err = handler.runEvaluation(cmd.Context())
132+
ctx := cmd.Context()
133+
// Add HTTP log filename to context if provided
134+
if httpLog != "" {
135+
ctx = azuremodels.WithHTTPLogFile(ctx, httpLog)
136+
}
137+
138+
err = handler.runEvaluation(ctx)
130139
if err == FailedTests {
131140
// Cobra by default will show the help message when an error occurs,
132141
// which is not what we want for failed evaluations.
@@ -139,6 +148,7 @@ func NewEvalCommand(cfg *command.Config) *cobra.Command {
139148

140149
cmd.Flags().Bool("json", false, "Output results in JSON format")
141150
cmd.Flags().String("org", "", "Organization to attribute usage to (omitting will attribute usage to the current actor")
151+
cmd.Flags().String("http-log", "", "Path to log HTTP requests to (optional)")
142152
return cmd
143153
}
144154

cmd/generate/generate.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,18 @@ func NewGenerateCommand(cfg *command.Config) *cobra.Command {
5050
// Get organization
5151
org, _ := cmd.Flags().GetString("org")
5252

53+
// Get http-log flag
54+
httpLog, _ := cmd.Flags().GetString("http-log")
55+
56+
ctx := cmd.Context()
57+
// Add HTTP log filename to context if provided
58+
if httpLog != "" {
59+
ctx = azuremodels.WithHTTPLogFile(ctx, httpLog)
60+
}
61+
5362
// Create the command handler
5463
handler := &generateCommandHandler{
55-
ctx: cmd.Context(),
64+
ctx: ctx,
5665
cfg: cfg,
5766
client: cfg.Client,
5867
options: options,
@@ -97,6 +106,7 @@ func AddCommandLineFlags(cmd *cobra.Command) {
97106
flags.String("custom-metric", "", "Custom evaluation metric")
98107
flags.Float64("temperature", 0.0, "Temperature for model inference")
99108
flags.Bool("verbose", false, "Enable verbose output including LLM payloads")
109+
flags.String("http-log", "", "Path to log HTTP requests to (optional)")
100110
}
101111

102112
// parseFlags parses command-line flags and applies them to the options

cmd/run/run.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ func NewRunCommand(cfg *command.Config) *cobra.Command {
423423
cmd.Flags().String("top-p", "", "Controls text diversity by selecting the most probable words until a set probability is reached.")
424424
cmd.Flags().String("system-prompt", "", "Prompt the system.")
425425
cmd.Flags().String("org", "", "Organization to attribute usage to (omitting will attribute usage to the current actor")
426+
cmd.Flags().String("http-log", "", "Path to log HTTP requests to (optional)")
426427

427428
return cmd
428429
}
@@ -472,7 +473,15 @@ type runCommandHandler struct {
472473
}
473474

474475
func newRunCommandHandler(cmd *cobra.Command, cfg *command.Config, args []string) *runCommandHandler {
475-
return &runCommandHandler{ctx: cmd.Context(), cfg: cfg, client: cfg.Client, args: args}
476+
ctx := cmd.Context()
477+
httpLog, _ := cmd.Flags().GetString("http-log")
478+
479+
// Add HTTP log filename to context if provided
480+
if httpLog != "" {
481+
ctx = azuremodels.WithHTTPLogFile(ctx, httpLog)
482+
}
483+
484+
return &runCommandHandler{ctx: ctx, cfg: cfg, client: cfg.Client, args: args}
476485
}
477486

478487
func (h *runCommandHandler) loadModels() ([]*azuremodels.ModelSummary, error) {

internal/azuremodels/azure_client.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,15 @@ func (c *AzureClient) GetChatCompletionStream(ctx context.Context, req ChatCompl
6767
inferenceURL = c.cfg.InferenceRoot + "/" + c.cfg.InferencePath
6868
}
6969

70-
// TODO: remove logging
71-
// Write request details to llm.http file for debugging
72-
if os.Getenv("DEBUG") != "" {
73-
httpFile, err := os.OpenFile("llm.http", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
70+
// Write request details to specified log file for debugging
71+
httpLogFile := HTTPLogFileFromContext(ctx)
72+
if httpLogFile != "" {
73+
logFile, err := os.OpenFile(httpLogFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
7474
if err == nil {
75-
defer httpFile.Close()
76-
fmt.Fprintf(httpFile, "### %s\n", time.Now().Format(time.RFC3339))
77-
fmt.Fprintf(httpFile, "POST %s\n", inferenceURL)
78-
fmt.Fprintf(httpFile, "Authorization: Bearer {{$processEnv GITHUB_TOKEN}}\n")
79-
fmt.Fprintf(httpFile, "Content-Type: application/json\n")
80-
fmt.Fprintf(httpFile, "x-ms-useragent: github-cli-models\n")
81-
fmt.Fprintf(httpFile, "x-ms-user-agent: github-cli-models\n")
82-
fmt.Fprintf(httpFile, "\n%s\n\n", string(bodyBytes))
75+
defer logFile.Close()
76+
fmt.Fprintf(logFile, "### %s\nPOST %s\nAuthorization: Bearer {{$processEnv GITHUB_TOKEN}}\nContent-Type: application/json\nx-ms-useragent: github-cli-models\nx-ms-user-agent: github-cli-models\n\n%s\n\n",
77+
const logFormat = "### %s\nPOST %s\nAuthorization: Bearer {{$processEnv GITHUB_TOKEN}}\nContent-Type: application/json\nx-ms-useragent: github-cli-models\nx-ms-user-agent: github-cli-models\n\n%s\n\n"
78+
fmt.Fprintf(logFile, logFormat, time.Now().Format(time.RFC3339), inferenceURL, string(bodyBytes))
8379
}
8480
}
8581

internal/azuremodels/client.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,27 @@ package azuremodels
22

33
import "context"
44

5+
// httpLogFileKey is the context key for the HTTP log filename
6+
type httpLogFileKey struct{}
7+
8+
// WithHTTPLogFile returns a new context with the HTTP log filename attached
9+
func WithHTTPLogFile(ctx context.Context, httpLogFile string) context.Context {
10+
return context.WithValue(ctx, httpLogFileKey{}, httpLogFile)
11+
}
12+
13+
// HTTPLogFileFromContext returns the HTTP log filename from the context, if any
14+
func HTTPLogFileFromContext(ctx context.Context) string {
15+
if httpLogFile, ok := ctx.Value(httpLogFileKey{}).(string); ok {
16+
return httpLogFile
17+
}
18+
return ""
19+
}
20+
521
// Client represents a client for interacting with an API about models.
622
type Client interface {
723
// GetChatCompletionStream returns a stream of chat completions using the given options.
8-
GetChatCompletionStream(context.Context, ChatCompletionOptions, string) (*ChatCompletionResponse, error)
24+
// HTTP logging configuration is extracted from the context if present.
25+
GetChatCompletionStream(ctx context.Context, req ChatCompletionOptions, org string) (*ChatCompletionResponse, error)
926
// GetModelDetails returns the details of the specified model in a particular registry.
1027
GetModelDetails(ctx context.Context, registry, modelName, version string) (*ModelDetails, error)
1128
// ListModels returns a list of available models.

0 commit comments

Comments
 (0)