From e76d71a5ef4e246750ad4b9ffc7252efd531cfa6 Mon Sep 17 00:00:00 2001 From: Loong Date: Thu, 19 Sep 2024 14:10:21 +0800 Subject: [PATCH] conversation: add echo implement Signed-off-by: Loong --- conversation/echo/echo.go | 75 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 conversation/echo/echo.go diff --git a/conversation/echo/echo.go b/conversation/echo/echo.go new file mode 100644 index 0000000000..5aca0df3b5 --- /dev/null +++ b/conversation/echo/echo.go @@ -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 +}