Skip to content

Commit

Permalink
feat: try debug openai mode
Browse files Browse the repository at this point in the history
  • Loading branch information
JingYiJun committed Feb 9, 2024
1 parent b49bde9 commit 82ee95b
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions apis/record/infer.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ func InferOpenAI(
record.Response = response.Choices[0].Message.Content
} else {
// streaming
if config.Config.Debug {
Logger.Info("openai streaming")
}

stream, err := client.CreateChatCompletionStream(
context.Background(),
Expand All @@ -96,6 +99,9 @@ func InferOpenAI(
startTime := time.Now()

var resultBuilder strings.Builder
var nowOutput string
var detectedOutput string

for {
if ctx.connectionClosed.Load() {
return interruptError
Expand All @@ -113,13 +119,31 @@ func InferOpenAI(
}

resultBuilder.WriteString(response.Choices[0].Delta.Content)
err = sensitiveCheck(ctx.c, record, resultBuilder.String(), startTime, user)
nowOutput = resultBuilder.String()
before, _, found := CutLastAny(nowOutput, ",.?!\n,。?!")
if !found || before == detectedOutput {
continue
}
detectedOutput = before
err = sensitiveCheck(ctx.c, record, detectedOutput, startTime, user)
if err != nil {
return err
}

_ = ctx.c.WriteJSON(InferResponseModel{
Status: 1,
Output: detectedOutput,
Stage: "MOSS",
})
}
if nowOutput != detectedOutput {
err = sensitiveCheck(ctx.c, record, nowOutput, startTime, user)
if err != nil {
return err
}
_ = ctx.c.WriteJSON(InferResponseModel{
Status: 1,
Output: resultBuilder.String(),
Output: nowOutput,
Stage: "MOSS",
})
}
Expand Down Expand Up @@ -583,6 +607,10 @@ func sensitiveCheck(
startTime time.Time,
user *User,
) error {
if config.Config.Debug {
Logger.Info("sensitive checking", zap.String("output", output))
}

if sensitive.IsSensitive(output, user) {
record.ResponseSensitive = true
// log new record
Expand Down

0 comments on commit 82ee95b

Please sign in to comment.