Skip to content

Commit

Permalink
feat: conversation api implementation
Browse files Browse the repository at this point in the history
Signed-off-by: mikeee <[email protected]>
  • Loading branch information
mikeee committed Nov 5, 2024
1 parent dd9a2d5 commit c738dbe
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 0 deletions.
3 changes: 3 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ type Client interface {
// DeleteJobAlpha1 deletes a scheduled job.
DeleteJobAlpha1(ctx context.Context, name string) error

// ConverseAlpha1 interacts with a conversational AI model.
ConverseAlpha1(ctx context.Context, componentName string, inputs []ConversationInput, options ...conversationRequestOption) (*ConversationResponse, error)

// GrpcClient returns the base grpc client if grpc is used and nil otherwise
GrpcClient() pb.DaprClient

Expand Down
132 changes: 132 additions & 0 deletions client/conversation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
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 client

import (
"context"
"fmt"
runtimev1pb "github.com/dapr/dapr/pkg/proto/runtime/v1"
"google.golang.org/protobuf/types/known/anypb"
)

type conversationRequestOptions struct {
Parameters map[string]*anypb.Any
Metadata map[string]string
ContextID *string
ScrubPII *bool // Scrub PII from the output
Temperature *float64
}

type conversationRequestOption func(request *conversationRequestOptions)

type ConversationInput struct {
Message string
Role *string
ScrubPII *bool // Scrub PII from the input
}

type ConversationInputOption func(*ConversationInput)

func NewConversationInput(message string, opts ...ConversationInputOption) ConversationInput {
return ConversationInput{}
}

type ConversationResponse struct {
ContextID string
Outputs []ConversationResult
}

type ConversationResult struct {
Result string
Parameters map[string]*anypb.Any
}

func WithParameters(parameters map[string]*anypb.Any) conversationRequestOption {
return func(o *conversationRequestOptions) {
o.Parameters = parameters
}
}

func WithMetadata(metadata map[string]string) conversationRequestOption {
return func(o *conversationRequestOptions) {
o.Metadata = metadata
}
}

func WithContextID(id string) conversationRequestOption {
return func(o *conversationRequestOptions) {
o.ContextID = &id
}
}

func WithScrubPII(scrub bool) conversationRequestOption {
return func(o *conversationRequestOptions) {
o.ScrubPII = &scrub
}
}

func WithTemperature(temp float64) conversationRequestOption {
return func(o *conversationRequestOptions) {
o.Temperature = &temp
}
}

func (c *GRPCClient) ConverseAlpha1(ctx context.Context, componentName string, inputs []ConversationInput, options ...conversationRequestOption) (*ConversationResponse, error) {

var cinputs []*runtimev1pb.ConversationInput
for _, i := range inputs {
cinputs = append(cinputs, &runtimev1pb.ConversationInput{
Message: i.Message,
Role: i.Role,
ScrubPII: i.ScrubPII,
})
}

var o conversationRequestOptions
for _, opt := range options {
if opt != nil {
opt(&o)
}
}

request := runtimev1pb.ConversationRequest{
Name: componentName,
ContextID: o.ContextID,
Inputs: cinputs,
Parameters: o.Parameters,
Metadata: o.Metadata,
ScrubPII: o.ScrubPII,
Temperature: o.Temperature,
}

fmt.Println("invoking")

resp, err := c.protoClient.ConverseAlpha1(ctx, &request)
if err != nil {
return nil, err
}

var outputs []ConversationResult
for _, i := range resp.GetOutputs() {
outputs = append(outputs, ConversationResult{
Result: i.GetResult(),
Parameters: i.GetParameters(),
})
}

return &ConversationResponse{
ContextID: resp.GetContextID(),
Outputs: outputs,
}, nil
}
7 changes: 7 additions & 0 deletions examples/conversation/config/conversation-echo.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: echo
spec:
type: conversation.echo
version: v1
36 changes: 36 additions & 0 deletions examples/conversation/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
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 main

import (
"context"
"fmt"
dapr "github.com/dapr/go-sdk/client"
"log"
)

func main() {
client, err := dapr.NewClientWithPort("47649")
if err != nil {
panic(err)
}

resp, err := client.ConverseAlpha1(context.Background(), "echo", []dapr.ConversationInput{{Message: "hello"}})
if err != nil {
log.Fatalf("err: %v", err)
}

fmt.Println(resp.Outputs)
}

0 comments on commit c738dbe

Please sign in to comment.