Skip to content

Commit

Permalink
openai: use interpolated string instead of bloblang for prompt
Browse files Browse the repository at this point in the history
This is for consistency between all the other AI processors.
  • Loading branch information
rockwotj committed Sep 3, 2024
1 parent 1c664da commit 307ed4a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ model: gpt4-turbo
=== `prompt`
The user prompt you want to generate a response for. By default, the processor submits the entire payload as a string.
This field supports xref:configuration:interpolation.adoc#bloblang-queries[interpolation functions].
*Type*: `string`
Expand Down
15 changes: 7 additions & 8 deletions internal/impl/openai/chat_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"slices"
"time"

"github.com/redpanda-data/benthos/v4/public/bloblang"
"github.com/redpanda-data/benthos/v4/public/service"
"github.com/redpanda-data/connect/v4/internal/impl/confluent/sr"
oai "github.com/sashabaranov/go-openai"
Expand Down Expand Up @@ -76,7 +75,7 @@ To learn more about chat completion, see the https://platform.openai.com/docs/gu
)...,
).
Fields(
service.NewBloblangField(ocpFieldUserPrompt).
service.NewInterpolatedStringField(ocpFieldUserPrompt).
Description("The user prompt you want to generate a response for. By default, the processor submits the entire payload as a string.").
Optional(),
service.NewInterpolatedStringField(ocpFieldSystemPrompt).
Expand Down Expand Up @@ -163,9 +162,9 @@ func makeChatProcessor(conf *service.ParsedConfig, mgr *service.Resources) (serv
if err != nil {
return nil, err
}
var up *bloblang.Executor
var up *service.InterpolatedString
if conf.Contains(ocpFieldUserPrompt) {
up, err = conf.FieldBloblang(ocpFieldUserPrompt)
up, err = conf.FieldInterpolatedString(ocpFieldUserPrompt)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -334,7 +333,7 @@ func newDynamicSchemaProvider(conf *service.ParsedConfig, mgr *service.Resources
type chatProcessor struct {
*baseProcessor

userPrompt *bloblang.Executor
userPrompt *service.InterpolatedString
systemPrompt *service.InterpolatedString
maxTokens *int
temperature *float32
Expand Down Expand Up @@ -396,13 +395,13 @@ func (p *chatProcessor) Process(ctx context.Context, msg *service.Message) (serv
})
}
if p.userPrompt != nil {
s, err := msg.BloblangQueryValue(p.userPrompt)
s, err := p.userPrompt.TryString(msg)
if err != nil {
return nil, fmt.Errorf("%s execution error: %w", ocpFieldUserPrompt, err)
return nil, fmt.Errorf("%s interpolation error: %w", ocpFieldUserPrompt, err)
}
body.Messages = append(body.Messages, oai.ChatCompletionMessage{
Role: "user",
Content: bloblang.ValueToString(s),
Content: s,
})
} else {
b, err := msg.AsBytes()
Expand Down
3 changes: 1 addition & 2 deletions internal/impl/openai/chat_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"testing"

"github.com/go-faker/faker/v4"
"github.com/redpanda-data/benthos/v4/public/bloblang"
"github.com/redpanda-data/benthos/v4/public/service"
oai "github.com/sashabaranov/go-openai"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -54,7 +53,7 @@ func TestChat(t *testing.T) {
}

func TestChatInterpolationError(t *testing.T) {
text, err := bloblang.GlobalEnvironment().Parse(`throw("kaboom!")`)
text, err := service.NewInterpolatedString(`${!throw("kaboom!")}`)
assert.NoError(t, err)
p := chatProcessor{
baseProcessor: &baseProcessor{
Expand Down

0 comments on commit 307ed4a

Please sign in to comment.