-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from bricks-cloud/v1.4.2
[V1.5.0] Support monthly spend limit on API keys
- Loading branch information
Showing
9 changed files
with
798 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package proxy | ||
|
||
import ( | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
type SpeechRequest struct { | ||
Model string `json:"model"` | ||
Input string `json:"input"` | ||
Voice string `json:"voice"` | ||
ResponseFormat string `json:"response_format"` | ||
Speed float64 `json:"speed"` | ||
} | ||
|
||
func logCreateSpeechRequest(log *zap.Logger, sr *SpeechRequest, prod, private bool, cid string) { | ||
if prod { | ||
fields := []zapcore.Field{ | ||
zap.String(correlationId, cid), | ||
zap.String("model", sr.Model), | ||
zap.String("voice", sr.Voice), | ||
} | ||
|
||
if !private { | ||
fields = append(fields, zap.String("input", sr.Input)) | ||
} | ||
|
||
if len(sr.ResponseFormat) != 0 { | ||
fields = append(fields, zap.String("response_format", sr.ResponseFormat)) | ||
} | ||
|
||
if sr.Speed != 0 { | ||
fields = append(fields, zap.Float64("speed", sr.Speed)) | ||
} | ||
|
||
log.Info("openai create speech request", fields...) | ||
} | ||
} | ||
|
||
func logCreateTranscriptionRequest(log *zap.Logger, model, language, prompt, responseFormat string, temperature float64, prod, private bool, cid string) { | ||
if prod { | ||
fields := []zapcore.Field{ | ||
zap.String(correlationId, cid), | ||
zap.String("model", model), | ||
} | ||
|
||
if !private && len(prompt) != 0 { | ||
fields = append(fields, zap.String("prompt", prompt)) | ||
} | ||
|
||
if len(language) != 0 { | ||
fields = append(fields, zap.String("language", language)) | ||
} | ||
|
||
if len(responseFormat) != 0 { | ||
fields = append(fields, zap.String("response_format", responseFormat)) | ||
} | ||
|
||
if temperature != 0 { | ||
fields = append(fields, zap.Float64("temperature", temperature)) | ||
} | ||
|
||
log.Info("openai create transcription request", fields...) | ||
} | ||
} | ||
|
||
func logCreateTranslationRequest(log *zap.Logger, model, prompt, responseFormat string, temperature float64, prod, private bool, cid string) { | ||
if prod { | ||
fields := []zapcore.Field{ | ||
zap.String(correlationId, cid), | ||
zap.String("model", model), | ||
} | ||
|
||
if !private && len(prompt) == 0 { | ||
fields = append(fields, zap.String("prompt", prompt)) | ||
} | ||
|
||
if len(responseFormat) != 0 { | ||
fields = append(fields, zap.String("response_format", responseFormat)) | ||
} | ||
|
||
log.Info("openai create translation request", fields...) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package proxy | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
goopenai "github.com/sashabaranov/go-openai" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
func logCreateImageRequest(log *zap.Logger, ir *goopenai.ImageRequest, prod, private bool, cid string) { | ||
if prod { | ||
fields := []zapcore.Field{ | ||
zap.String(correlationId, cid), | ||
zap.String("model", ir.Model), | ||
zap.Int("n", ir.N), | ||
zap.String("quality", ir.Quality), | ||
zap.String("size", ir.Size), | ||
zap.String("style", ir.Style), | ||
zap.String("response_format", ir.ResponseFormat), | ||
zap.String("user", ir.User), | ||
} | ||
|
||
if !private && len(ir.Prompt) != 0 { | ||
fields = append(fields, zap.String("prompt", ir.Prompt)) | ||
} | ||
|
||
log.Info("openai create image request", fields...) | ||
} | ||
} | ||
|
||
func logEditImageRequest(log *zap.Logger, prompt, model string, n int, size, responseFormat, user string, prod, private bool, cid string) { | ||
if prod { | ||
fields := []zapcore.Field{ | ||
zap.String(correlationId, cid), | ||
} | ||
|
||
if !private && len(prompt) != 0 { | ||
fields = append(fields, zap.String("prompt", prompt)) | ||
} | ||
|
||
if len(model) != 0 { | ||
fields = append(fields, zap.String("prompt", model)) | ||
} | ||
|
||
if n != 0 { | ||
fields = append(fields, zap.Int("n", n)) | ||
} | ||
|
||
if len(size) != 0 { | ||
fields = append(fields, zap.String("size", size)) | ||
} | ||
|
||
if len(responseFormat) != 0 { | ||
fields = append(fields, zap.String("response_format", responseFormat)) | ||
} | ||
|
||
if len(user) != 0 { | ||
fields = append(fields, zap.String("user", user)) | ||
} | ||
|
||
log.Info("openai edit image request", fields...) | ||
} | ||
} | ||
|
||
func logImageVariationsRequest(log *zap.Logger, model string, n int, size, responseFormat, user string, prod, private bool, cid string) { | ||
if prod { | ||
fields := []zapcore.Field{ | ||
zap.String(correlationId, cid), | ||
} | ||
|
||
if len(model) != 0 { | ||
fields = append(fields, zap.String("model", model)) | ||
} | ||
|
||
if n != 0 { | ||
fields = append(fields, zap.Int("n", n)) | ||
} | ||
|
||
if len(size) != 0 { | ||
fields = append(fields, zap.String("size", size)) | ||
} | ||
|
||
if len(responseFormat) != 0 { | ||
fields = append(fields, zap.String("response_format", user)) | ||
} | ||
|
||
if len(user) != 0 { | ||
fields = append(fields, zap.String("user", user)) | ||
} | ||
|
||
log.Info("openai image variations request", fields...) | ||
} | ||
} | ||
|
||
func logImageResponse(log *zap.Logger, data []byte, prod, private bool, cid string) { | ||
ir := &goopenai.ImageResponse{} | ||
err := json.Unmarshal(data, ir) | ||
if err != nil { | ||
logError(log, "error when unmarshalling image response", prod, cid, err) | ||
return | ||
} | ||
|
||
if prod { | ||
fields := []zapcore.Field{ | ||
zap.String(correlationId, cid), | ||
zap.Int64("created", ir.Created), | ||
} | ||
|
||
if !private && len(ir.Data) != 0 { | ||
fields = append(fields, zap.Any("data", ir.Data)) | ||
} | ||
|
||
log.Info("openai image response", fields...) | ||
} | ||
} |
Oops, something went wrong.