Skip to content

Commit

Permalink
conversation: add echo implement (#3549)
Browse files Browse the repository at this point in the history
Signed-off-by: Loong <[email protected]>
  • Loading branch information
daixiang0 authored Sep 19, 2024
1 parent ff8d562 commit 414e997
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions conversation/echo/echo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Copyright 2024 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package echo

import (
"context"
"reflect"

"github.com/dapr/components-contrib/conversation"
"github.com/dapr/components-contrib/metadata"
"github.com/dapr/kit/logger"
kmeta "github.com/dapr/kit/metadata"
)

// Echo implement is only for test.
type Echo struct {
model string
logger logger.Logger
}

func NewEcho(logger logger.Logger) conversation.Conversation {
e := &Echo{
logger: logger,
}

return e
}

func (e *Echo) Init(ctx context.Context, meta conversation.Metadata) error {
r := &conversation.ConversationRequest{}
err := kmeta.DecodeMetadata(meta.Properties, &r)
if err != nil {
return err
}

e.model = r.Model

return nil
}

func (e *Echo) GetComponentMetadata() (metadataInfo metadata.MetadataMap) {
metadataStruct := conversation.ConversationRequest{}
metadata.GetMetadataInfoFromStructType(reflect.TypeOf(metadataStruct), &metadataInfo, metadata.StateStoreType)
return
}

// Converse returns inputs directly.
func (e *Echo) Converse(ctx context.Context, r *conversation.ConversationRequest) (res *conversation.ConversationResponse, err error) {
outputs := make([]conversation.ConversationResult, 0, len(r.Inputs))

for _, input := range r.Inputs {
outputs = append(outputs, conversation.ConversationResult{
Result: input,
Parameters: r.Parameters,
})
}

res = &conversation.ConversationResponse{
Outputs: outputs,
}

return res, nil
}

0 comments on commit 414e997

Please sign in to comment.