Skip to content

Commit

Permalink
Merge branch 'master' into http-wasm-middleware-config
Browse files Browse the repository at this point in the history
Signed-off-by: zhangchao <[email protected]>

# Conflicts:
#	middleware/http/wasm/benchmark_test.go
#	middleware/http/wasm/httpwasm.go
#	middleware/http/wasm/internal/e2e_test.go
  • Loading branch information
Taction committed Jul 30, 2023
2 parents f8fbbc3 + 387c238 commit f8a9415
Show file tree
Hide file tree
Showing 37 changed files with 1,140 additions and 731 deletions.
4 changes: 2 additions & 2 deletions .build-tools/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ require (
github.com/invopop/jsonschema v0.6.0
github.com/spf13/cobra v1.6.1
github.com/xeipuuv/gojsonschema v1.2.1-0.20201027075954-b076d39a02e5
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1
golang.org/x/exp v0.0.0-20230711153332-06a737ee72cb
gopkg.in/yaml.v3 v3.0.1
sigs.k8s.io/yaml v1.3.0
)

require (
github.com/dapr/kit v0.11.3-0.20230615225244-804821bb8f2d // indirect
github.com/dapr/kit v0.11.3 // indirect
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.5.1-0.20220423185008-bf980b35cac4 // indirect
Expand Down
12 changes: 6 additions & 6 deletions .build-tools/go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/dapr/kit v0.11.3-0.20230615225244-804821bb8f2d h1:bICHJ3JgkFVxN1W90k2KFfOO32dun13PQsvMqVol8Uw=
github.com/dapr/kit v0.11.3-0.20230615225244-804821bb8f2d/go.mod h1:hQA6xOhcLAiccXTj7e3/bzpHwvAJCSCp70p2xg3jB40=
github.com/dapr/kit v0.11.3 h1:u1X92tE8xsrwXIej7nkcI5Z1t1CFznPwlL18tizNEw4=
github.com/dapr/kit v0.11.3/go.mod h1:hQA6xOhcLAiccXTj7e3/bzpHwvAJCSCp70p2xg3jB40=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0 h1:i462o439ZjprVSFSZLZxcsoAe592sZB1rci2Z8j4wdk=
Expand All @@ -16,8 +16,8 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/mitchellh/mapstructure v1.5.1-0.20220423185008-bf980b35cac4 h1:BpfhmLKZf+SjVanKKhCgf3bg+511DmU9eDQTen7LLbY=
github.com/mitchellh/mapstructure v1.5.1-0.20220423185008-bf980b35cac4/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cast v1.5.1 h1:R+kOtfhWQE6TVQzY+4D7wJLBgkdVasCEFxSUBYBYIlA=
Expand All @@ -37,8 +37,8 @@ github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHo
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.2.1-0.20201027075954-b076d39a02e5 h1:ImnGIsrcG8vwbovhYvvSY8fagVV6QhCWSWXfzwGDLVs=
github.com/xeipuuv/gojsonschema v1.2.1-0.20201027075954-b076d39a02e5/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 h1:k/i9J1pBpvlfR+9QsetwPyERsqu1GIbi967PQMq3Ivc=
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
golang.org/x/exp v0.0.0-20230711153332-06a737ee72cb h1:xIApU0ow1zwMa2uL1VDNeQlNVFTWMQxZUZCMDy0Q4Us=
golang.org/x/exp v0.0.0-20230711153332-06a737ee72cb/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
Expand Down
42 changes: 41 additions & 1 deletion .build-tools/pkg/metadataschema/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,46 @@ func (c *ComponentMetadata) AppendBuiltin() error {
URL: "https://docs.dapr.io/developing-applications/building-blocks/pubsub/howto-namespace/",
},
},
Metadata{
Name: "allowedTopics",
Type: "string",
Description: "A comma-separated list of allowed topics for all applications. If empty (default) apps can publish and subscribe to all topics, notwithstanding `publishingScopes` and `subscriptionScopes`.",
Example: `"app1=topic1;app2=topic2,topic3"`,
URL: &URL{
Title: "Documentation",
URL: "https://docs.dapr.io/developing-applications/building-blocks/pubsub/pubsub-scopes/",
},
},
Metadata{
Name: "publishingScopes",
Type: "string",
Description: "A semicolon-separated list of applications and comma-separated topic lists, allowing that app to publish to that list of topics. If empty (default), apps can publish to all topics.",
Example: `"app1=topic1;app2=topic2,topic3;app3="`,
URL: &URL{
Title: "Documentation",
URL: "https://docs.dapr.io/developing-applications/building-blocks/pubsub/pubsub-scopes/",
},
},
Metadata{
Name: "subscriptionScopes",
Type: "string",
Description: "A semicolon-separated list of applications and comma-separated topic lists, allowing that app to subscribe to that list of topics. If empty (default), apps can subscribe to all topics.",
Example: `"app1=topic1;app2=topic2,topic3"`,
URL: &URL{
Title: "Documentation",
URL: "https://docs.dapr.io/developing-applications/building-blocks/pubsub/pubsub-scopes/",
},
},
Metadata{
Name: "protectedTopics",
Type: "string",
Description: `A comma-separated list of topics marked as "protected" for all applications. If a topic is marked as protected then an application must be explicitly granted publish or subscribe permissions through 'publishingScopes' or 'subscriptionScopes' to publish or subscribe to it.`,
Example: `"topic1,topic2"`,
URL: &URL{
Title: "Documentation",
URL: "https://docs.dapr.io/developing-applications/building-blocks/pubsub/pubsub-scopes/",
},
},
)
case mdutils.BindingType:
if c.Binding != nil {
Expand All @@ -176,7 +216,7 @@ func (c *ComponentMetadata) AppendBuiltin() error {
Name: bindingDirectionMetadataKey,
Type: "string",
Description: "Indicates the direction of the binding component.",
Example: `"`+direction+`"`,
Example: `"` + direction + `"`,
URL: &URL{
Title: "Documentation",
URL: "https://docs.dapr.io/reference/api/bindings_api/#binding-direction-optional",
Expand Down
80 changes: 41 additions & 39 deletions bindings/azure/openai/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/dapr/components-contrib/bindings"
azauth "github.com/dapr/components-contrib/internal/authentication/azure"
"github.com/dapr/components-contrib/metadata"
"github.com/dapr/kit/config"
"github.com/dapr/kit/logger"
)

Expand All @@ -51,8 +50,9 @@ const (

// AzOpenAI represents OpenAI output binding.
type AzOpenAI struct {
logger logger.Logger
client *azopenai.Client
logger logger.Logger
client *azopenai.Client
deploymentID string
}

type openAIMetadata struct {
Expand All @@ -64,15 +64,6 @@ type openAIMetadata struct {
Endpoint string `mapstructure:"endpoint"`
}

type ChatSettings struct {
Temperature float32 `mapstructure:"temperature"`
MaxTokens int32 `mapstructure:"maxTokens"`
TopP float32 `mapstructure:"topP"`
N int32 `mapstructure:"n"`
PresencePenalty float32 `mapstructure:"presencePenalty"`
FrequencyPenalty float32 `mapstructure:"frequencyPenalty"`
}

// ChatMessages type for chat completion API.
type ChatMessages struct {
Messages []Message `json:"messages"`
Expand All @@ -82,6 +73,7 @@ type ChatMessages struct {
N int32 `json:"n"`
PresencePenalty float32 `json:"presencePenalty"`
FrequencyPenalty float32 `json:"frequencyPenalty"`
Stop []string `json:"stop"`
}

// Message type stores the messages for bot conversation.
Expand All @@ -92,13 +84,14 @@ type Message struct {

// Prompt type for completion API.
type Prompt struct {
Prompt string `json:"prompt"`
Temperature float32 `json:"temperature"`
MaxTokens int32 `json:"maxTokens"`
TopP float32 `json:"topP"`
N int32 `json:"n"`
PresencePenalty float32 `json:"presencePenalty"`
FrequencyPenalty float32 `json:"frequencyPenalty"`
Prompt string `json:"prompt"`
Temperature float32 `json:"temperature"`
MaxTokens int32 `json:"maxTokens"`
TopP float32 `json:"topP"`
N int32 `json:"n"`
PresencePenalty float32 `json:"presencePenalty"`
FrequencyPenalty float32 `json:"frequencyPenalty"`
Stop []string `json:"stop"`
}

// NewOpenAI returns a new OpenAI output binding.
Expand Down Expand Up @@ -130,7 +123,7 @@ func (p *AzOpenAI) Init(ctx context.Context, meta bindings.Metadata) error {
return fmt.Errorf("error getting credentials object: %w", err)
}

p.client, err = azopenai.NewClientWithKeyCredential(m.Endpoint, keyCredential, m.DeploymentID, nil)
p.client, err = azopenai.NewClientWithKeyCredential(m.Endpoint, keyCredential, nil)
if err != nil {
return fmt.Errorf("error creating Azure OpenAI client: %w", err)
}
Expand All @@ -146,11 +139,12 @@ func (p *AzOpenAI) Init(ctx context.Context, meta bindings.Metadata) error {
return fmt.Errorf("error getting token credential: %w", innerErr)
}

p.client, err = azopenai.NewClient(m.Endpoint, token, m.DeploymentID, nil)
p.client, err = azopenai.NewClient(m.Endpoint, token, nil)
if err != nil {
return fmt.Errorf("error creating Azure OpenAI client: %w", err)
}
}
p.deploymentID = m.DeploymentID

return nil
}
Expand Down Expand Up @@ -208,10 +202,6 @@ func (p *AzOpenAI) Invoke(ctx context.Context, req *bindings.InvokeRequest) (res
return resp, nil
}

func (s *ChatSettings) Decode(in any) error {
return config.Decode(in, s)
}

func (p *AzOpenAI) completion(ctx context.Context, message []byte, metadata map[string]string) (response []azopenai.Choice, err error) {
prompt := Prompt{
Temperature: 1.0,
Expand All @@ -230,12 +220,18 @@ func (p *AzOpenAI) completion(ctx context.Context, message []byte, metadata map[
return nil, fmt.Errorf("prompt is required for completion operation")
}

if len(prompt.Stop) == 0 {
prompt.Stop = nil
}

resp, err := p.client.GetCompletions(ctx, azopenai.CompletionsOptions{
Prompt: []*string{&prompt.Prompt},
MaxTokens: &prompt.MaxTokens,
Temperature: &prompt.Temperature,
TopP: &prompt.TopP,
N: &prompt.N,
DeploymentID: p.deploymentID,
Prompt: []string{prompt.Prompt},
MaxTokens: &prompt.MaxTokens,
Temperature: &prompt.Temperature,
TopP: &prompt.TopP,
N: &prompt.N,
Stop: prompt.Stop,
}, nil)
if err != nil {
return nil, fmt.Errorf("error getting completion api: %w", err)
Expand All @@ -249,7 +245,7 @@ func (p *AzOpenAI) completion(ctx context.Context, message []byte, metadata map[
choices := resp.Completions.Choices
response = make([]azopenai.Choice, len(choices))
for i, c := range choices {
response[i] = *c
response[i] = c
}

return response, nil
Expand All @@ -272,9 +268,13 @@ func (p *AzOpenAI) chatCompletion(ctx context.Context, messageRequest []byte, me
return nil, fmt.Errorf("messages are required for chat-completion operation")
}

messageReq := make([]*azopenai.ChatMessage, len(messages.Messages))
if len(messages.Stop) == 0 {
messages.Stop = nil
}

messageReq := make([]azopenai.ChatMessage, len(messages.Messages))
for i, m := range messages.Messages {
messageReq[i] = &azopenai.ChatMessage{
messageReq[i] = azopenai.ChatMessage{
Role: to.Ptr(azopenai.ChatRole(m.Role)),
Content: to.Ptr(m.Message),
}
Expand All @@ -286,11 +286,13 @@ func (p *AzOpenAI) chatCompletion(ctx context.Context, messageRequest []byte, me
}

res, err := p.client.GetChatCompletions(ctx, azopenai.ChatCompletionsOptions{
MaxTokens: maxTokens,
Temperature: &messages.Temperature,
TopP: &messages.TopP,
N: &messages.N,
Messages: messageReq,
DeploymentID: p.deploymentID,
MaxTokens: maxTokens,
Temperature: &messages.Temperature,
TopP: &messages.TopP,
N: &messages.N,
Messages: messageReq,
Stop: messages.Stop,
}, nil)
if err != nil {
return nil, fmt.Errorf("error getting chat completion api: %w", err)
Expand All @@ -304,7 +306,7 @@ func (p *AzOpenAI) chatCompletion(ctx context.Context, messageRequest []byte, me
choices := res.ChatCompletions.Choices
response = make([]azopenai.ChatChoice, len(choices))
for i, c := range choices {
response[i] = *c
response[i] = c
}

return response, nil
Expand Down
35 changes: 26 additions & 9 deletions bindings/wasm/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"strings"
"sync/atomic"

"github.com/stealthrocket/wasi-go/imports/wasi_http"
"github.com/stealthrocket/wasi-go/imports/wasi_http/default_http"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
Expand Down Expand Up @@ -63,7 +65,7 @@ func NewWasmOutput(logger logger.Logger) bindings.OutputBinding {
}

func (out *outputBinding) Init(ctx context.Context, metadata bindings.Metadata) (err error) {
if out.meta, err = wasm.GetInitMetadata(metadata.Base); err != nil {
if out.meta, err = wasm.GetInitMetadata(ctx, metadata.Base); err != nil {
return fmt.Errorf("wasm: failed to parse metadata: %w", err)
}

Expand All @@ -77,16 +79,27 @@ func (out *outputBinding) Init(ctx context.Context, metadata bindings.Metadata)
return fmt.Errorf("wasm: error compiling binary: %w", err)
}

switch detectImports(out.module.ImportedFunctions()) {
case modeWasiP1:
imports := detectImports(out.module.ImportedFunctions())

if _, found := imports[modeWasiP1]; found {
_, err = wasi_snapshot_preview1.Instantiate(ctx, out.runtime)
}

if err != nil {
_ = out.runtime.Close(context.Background())
return fmt.Errorf("wasm: error instantiating host functions: %w", err)
return fmt.Errorf("wasm: error instantiating host wasi functions: %w", err)
}
return
if _, found := imports[modeWasiHTTP]; found {
if out.meta.StrictSandbox {
_ = out.runtime.Close(context.Background())
return fmt.Errorf("can not instantiate wasi-http with strict sandbox")
}
err = wasi_http.Instantiate(ctx, out.runtime)
}
if err != nil {
_ = out.runtime.Close(context.Background())
return fmt.Errorf("wasm: error instantiating host wasi-http functions: %w", err)
}
return nil
}

func (out *outputBinding) Invoke(ctx context.Context, req *bindings.InvokeRequest) (*bindings.InvokeResponse, error) {
Expand Down Expand Up @@ -152,19 +165,23 @@ func (out *outputBinding) Close() error {
const (
modeDefault importMode = iota
modeWasiP1
modeWasiHTTP
)

type importMode uint

func detectImports(imports []api.FunctionDefinition) importMode {
func detectImports(imports []api.FunctionDefinition) map[importMode]bool {
result := make(map[importMode]bool)
for _, f := range imports {
moduleName, _, _ := f.Import()
switch moduleName {
case wasi_snapshot_preview1.ModuleName:
return modeWasiP1
result[modeWasiP1] = true
case default_http.ModuleName:
result[modeWasiHTTP] = true
}
}
return modeDefault
return result
}

// GetComponentMetadata returns the metadata of the component.
Expand Down
Loading

0 comments on commit f8a9415

Please sign in to comment.