Skip to content

Commit

Permalink
init conversation API with openai component (#3518)
Browse files Browse the repository at this point in the history
Signed-off-by: Loong <[email protected]>
  • Loading branch information
daixiang0 authored Sep 11, 2024
1 parent 3830b41 commit 8225a11
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 0 deletions.
3 changes: 3 additions & 0 deletions conversation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Conversation

Conversations provide a common way to converse with different LLM providers.
53 changes: 53 additions & 0 deletions conversation/converse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
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 conversation

import (
"context"

"google.golang.org/protobuf/types/known/anypb"

"github.com/dapr/components-contrib/metadata"
)

type Conversation interface {
metadata.ComponentWithMetadata

Init(ctx context.Context, meta Metadata) error

Converse(ctx context.Context, req *ConversationRequest) (*ConversationResponse, error)
}

type ConversationRequest struct {
Inputs []string `json:"inputs"`
Parameters map[string]*anypb.Any `json:"parameters"`
ConversationContext string `json:"conversationContext"`

// from metadata
Key string `json:"key"`
Model string `json:"model"`
Endpoints []string `json:"endpoints"`
Policy string `json:"loadBalancingPolicy"`
}

type ConversationResult struct {
Result string `json:"result"`
Parameters map[string]*anypb.Any `json:"parameters"`
}

type ConversationResponse struct {
ConversationContext string `json:"conversationContext"`
Outputs []ConversationResult `json:"outputs"`
}
22 changes: 22 additions & 0 deletions conversation/metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
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 conversation

import "github.com/dapr/components-contrib/metadata"

// Metadata represents a set of conversation specific properties.
type Metadata struct {
metadata.Base `json:",inline"`
}
105 changes: 105 additions & 0 deletions conversation/openai/openai.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
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 openai

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"

openai "github.com/sashabaranov/go-openai"
)

type OpenAI struct {
cilent *openai.Client
model string

logger logger.Logger
}

func NewOpenAI(logger logger.Logger) conversation.Conversation {
o := &OpenAI{
logger: logger,
}

return o
}

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

o.cilent = openai.NewClient(r.Key)
o.model = r.Model

return nil
}

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

func (o *OpenAI) Converse(ctx context.Context, r *conversation.ConversationRequest) (res *conversation.ConversationResponse, err error) {
// Note: OPENAI does not support load balance

messages := make([]openai.ChatCompletionMessage, 0, len(r.Inputs))

for _, input := range r.Inputs {
messages = append(messages, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser,
Content: input,
})
}

req := openai.ChatCompletionRequest{
Model: o.model,
Messages: messages,
}

// TODO: support ConversationContext

resp, err := o.cilent.CreateChatCompletion(ctx, req)
if err != nil {
o.logger.Error(err)
return nil, err
}

o.logger.Debug(resp)

outputs := make([]conversation.ConversationResult, 0, len(resp.Choices))

for i := range resp.Choices {
outputs = append(outputs, conversation.ConversationResult{
Result: resp.Choices[i].Message.Content,
Parameters: r.Parameters,
})
}

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

return res, nil
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ require (
github.com/rabbitmq/amqp091-go v1.8.1
github.com/redis/go-redis/v9 v9.2.1
github.com/riferrei/srclient v0.6.0
github.com/sashabaranov/go-openai v1.27.1
github.com/sendgrid/sendgrid-go v3.13.0+incompatible
github.com/sijms/go-ora/v2 v2.7.18
github.com/spf13/cast v1.5.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1424,6 +1424,8 @@ github.com/ryanuber/go-glob v1.0.0/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIH
github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E=
github.com/santhosh-tekuri/jsonschema/v5 v5.0.0 h1:TToq11gyfNlrMFZiYujSekIsPd9AmsA2Bj/iv+s4JHE=
github.com/santhosh-tekuri/jsonschema/v5 v5.0.0/go.mod h1:FKdcjfQW6rpZSnxxUvEA5H/cDPdvJ/SZJQLWWXWGrZ0=
github.com/sashabaranov/go-openai v1.27.1 h1:7Nx6db5NXbcoutNmAUQulEQZEpHG/SkzfexP2X5RWMk=
github.com/sashabaranov/go-openai v1.27.1/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529 h1:nn5Wsu0esKSJiIVhscUtVbo7ada43DJhG55ua/hjS5I=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
Expand Down

0 comments on commit 8225a11

Please sign in to comment.