-
Notifications
You must be signed in to change notification settings - Fork 0
images/ #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
images/ #16
Conversation
|
@coderabbitai full review |
✅ Actions performedFull review triggered. |
WalkthroughAdded support for capturing and leveraging OpenAI image response metadata to enable more accurate cost estimation. The changes introduce new types to represent image usage data, extend cost estimation interfaces to accept metadata, and implement logic to extract and propagate metadata through HTTP proxying and event logging. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant Proxy
participant OpenAI as OpenAI API
participant Handler
participant CostEstimator
participant EventLog
Client->>Proxy: POST /images/generations
Proxy->>OpenAI: Forward request
OpenAI->>Proxy: JSON response with metadata
Proxy->>Proxy: Extract metadata (imageResponseMetadataFromBytes)
Proxy->>Proxy: Store in context (setCtxImageResponseMetadata)
Proxy->>Client: Return response
Proxy->>Handler: Process event with metadata
Handler->>CostEstimator: EstimateImagesCost(model, quality, resolution, metadata)
alt Metadata available
CostEstimator->>CostEstimator: estimateImageByMetadata (text + image tokens)
else Fallback
CostEstimator->>CostEstimator: Resolution-based pricing
end
CostEstimator->>Handler: Cost calculated
Handler->>EventLog: Store event with ImageResponseMetadata
EventLog->>EventLog: Log complete
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes 🚥 Pre-merge checks | ✅ 1 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@internal/server/web/proxy/image.go`:
- Around line 126-143: The setter and getter use mismatched types:
setCtxImageResponseMetadata stores imageResponse.Usage (ImageResponseUsage) but
getCtxImageResponseMetadata type-asserts to openai.ImageResponseMetadata,
causing the assertion to fail; fix by storing the full metadata object in
setCtxImageResponseMetadata (store imageResponse itself) and update the getter's
type assertion to expect a *openai.ImageResponseMetadata (or the concrete value
you store) so getCtxImageResponseMetadata returns the stored metadata pointer
correctly.
🧹 Nitpick comments (1)
internal/provider/openai/cost.go (1)
472-506: Metadata-based cost estimation looks good, but silently ignores missing cost entries.The function correctly calculates costs from text tokens, image tokens, and output tokens. However, the
okreturn values from cost map lookups (lines 486, 494, 502) are ignored. If a model is inimageModelsWithTokensCostbut missing from one of the token cost maps, the cost for that component will silently be 0.Consider adding validation or logging when a model passes the initial check but is missing from a specific cost map.
♻️ Optional: Add logging for missing cost entries
textInputCost, _ := textInputCostMap[model] + // Note: If model is missing, textInputCost defaults to 0 totalCost += (float64(textInputTokens) / 1000) * textInputCostOr return an error if the lookup fails:
- textInputCost, _ := textInputCostMap[model] + textInputCost, ok := textInputCostMap[model] + if !ok { + return 0, fmt.Errorf("model %s not found in prompt cost map", model) + }
internal/server/web/proxy/image.go
Outdated
| func setCtxImageResponseMetadata(ctx *gin.Context, imageResponse *openai.ImageResponseMetadata) { | ||
| if imageResponse == nil { | ||
| return | ||
| } | ||
| ctx.Set(imageResponseMetadataKey, imageResponse.Usage) | ||
| } | ||
|
|
||
| func getCtxImageResponseMetadata(ctx *gin.Context) *openai.ImageResponseMetadata { | ||
| usage, exists := ctx.Get(imageResponseMetadataKey) | ||
| if !exists { | ||
| return nil | ||
| } | ||
| iru, ok := usage.(openai.ImageResponseMetadata) | ||
| if !ok { | ||
| return nil | ||
| } | ||
| return &iru | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Type mismatch between set and get functions will cause metadata to always be nil.
setCtxImageResponseMetadata stores imageResponse.Usage (type ImageResponseUsage) at line 130, but getCtxImageResponseMetadata attempts to type-assert to openai.ImageResponseMetadata at line 138. These are different types, so the type assertion will always fail, returning nil.
Either store the full ImageResponseMetadata or retrieve ImageResponseUsage and reconstruct the metadata.
🐛 Proposed fix: Store the full metadata object
func setCtxImageResponseMetadata(ctx *gin.Context, imageResponse *openai.ImageResponseMetadata) {
if imageResponse == nil {
return
}
- ctx.Set(imageResponseMetadataKey, imageResponse.Usage)
+ ctx.Set(imageResponseMetadataKey, imageResponse)
}
func getCtxImageResponseMetadata(ctx *gin.Context) *openai.ImageResponseMetadata {
usage, exists := ctx.Get(imageResponseMetadataKey)
if !exists {
return nil
}
- iru, ok := usage.(openai.ImageResponseMetadata)
+ iru, ok := usage.(*openai.ImageResponseMetadata)
if !ok {
return nil
}
- return &iru
+ return iru
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| func setCtxImageResponseMetadata(ctx *gin.Context, imageResponse *openai.ImageResponseMetadata) { | |
| if imageResponse == nil { | |
| return | |
| } | |
| ctx.Set(imageResponseMetadataKey, imageResponse.Usage) | |
| } | |
| func getCtxImageResponseMetadata(ctx *gin.Context) *openai.ImageResponseMetadata { | |
| usage, exists := ctx.Get(imageResponseMetadataKey) | |
| if !exists { | |
| return nil | |
| } | |
| iru, ok := usage.(openai.ImageResponseMetadata) | |
| if !ok { | |
| return nil | |
| } | |
| return &iru | |
| } | |
| func setCtxImageResponseMetadata(ctx *gin.Context, imageResponse *openai.ImageResponseMetadata) { | |
| if imageResponse == nil { | |
| return | |
| } | |
| ctx.Set(imageResponseMetadataKey, imageResponse) | |
| } | |
| func getCtxImageResponseMetadata(ctx *gin.Context) *openai.ImageResponseMetadata { | |
| usage, exists := ctx.Get(imageResponseMetadataKey) | |
| if !exists { | |
| return nil | |
| } | |
| iru, ok := usage.(*openai.ImageResponseMetadata) | |
| if !ok { | |
| return nil | |
| } | |
| return iru | |
| } |
🤖 Prompt for AI Agents
In `@internal/server/web/proxy/image.go` around lines 126 - 143, The setter and
getter use mismatched types: setCtxImageResponseMetadata stores
imageResponse.Usage (ImageResponseUsage) but getCtxImageResponseMetadata
type-asserts to openai.ImageResponseMetadata, causing the assertion to fail; fix
by storing the full metadata object in setCtxImageResponseMetadata (store
imageResponse itself) and update the getter's type assertion to expect a
*openai.ImageResponseMetadata (or the concrete value you store) so
getCtxImageResponseMetadata returns the stored metadata pointer correctly.
https://bugtracker.codiodev.com/issue/codio-17369/Upgrade-BricksLLM-to-Support-OpenAI-Image-Generation
Summary by CodeRabbit