-
Notifications
You must be signed in to change notification settings - Fork 479
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
init conversation API with openai component (#3518)
Signed-off-by: Loong <[email protected]>
- Loading branch information
Showing
6 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters