-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for the conversation API (#646)
* feat: conversation api implementation Signed-off-by: mikeee <[email protected]> * chore: deps for conversation api Signed-off-by: mikeee <[email protected]> * fix: cleanup convo example Signed-off-by: mikeee <[email protected]> * refactor: add a conversationrequest builder and docs Signed-off-by: mikeee <[email protected]> * fix: lint and refactor, adding preallocations for ins/outs Signed-off-by: Mike Nguyen <[email protected]> * fix: lint and imports Signed-off-by: mikeee <[email protected]> * fix: bump to dapr master refs Signed-off-by: mikeee <[email protected]> * fix: enable scheduler with cli fix + tidy Signed-off-by: mikeee <[email protected]> --------- Signed-off-by: mikeee <[email protected]> Signed-off-by: Mike Nguyen <[email protected]>
- Loading branch information
Showing
10 changed files
with
301 additions
and
60 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/* | ||
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" | ||
|
||
"google.golang.org/protobuf/types/known/anypb" | ||
|
||
runtimev1pb "github.com/dapr/dapr/pkg/proto/runtime/v1" | ||
) | ||
|
||
// conversationRequest object - currently unexported as used in a functions option pattern | ||
type conversationRequest struct { | ||
name string | ||
inputs []ConversationInput | ||
Parameters map[string]*anypb.Any | ||
Metadata map[string]string | ||
ContextID *string | ||
ScrubPII *bool // Scrub PII from the output | ||
Temperature *float64 | ||
} | ||
|
||
// NewConversationRequest defines a request with a component name and one or more inputs as a slice | ||
func NewConversationRequest(llmName string, inputs []ConversationInput) conversationRequest { | ||
return conversationRequest{ | ||
name: llmName, | ||
inputs: inputs, | ||
} | ||
} | ||
|
||
type conversationRequestOption func(request *conversationRequest) | ||
|
||
// ConversationInput defines a single input. | ||
type ConversationInput struct { | ||
// The string to send to the llm. | ||
Message string | ||
// The role of the message. | ||
Role *string | ||
// Whether to Scrub PII from the input | ||
ScrubPII *bool | ||
} | ||
|
||
// ConversationResponse is the basic response from a conversationRequest. | ||
type ConversationResponse struct { | ||
ContextID string | ||
Outputs []ConversationResult | ||
} | ||
|
||
// ConversationResult is the individual | ||
type ConversationResult struct { | ||
Result string | ||
Parameters map[string]*anypb.Any | ||
} | ||
|
||
// WithParameters should be used to provide parameters for custom fields. | ||
func WithParameters(parameters map[string]*anypb.Any) conversationRequestOption { | ||
return func(o *conversationRequest) { | ||
o.Parameters = parameters | ||
} | ||
} | ||
|
||
// WithMetadata used to define metadata to be passed to components. | ||
func WithMetadata(metadata map[string]string) conversationRequestOption { | ||
return func(o *conversationRequest) { | ||
o.Metadata = metadata | ||
} | ||
} | ||
|
||
// WithContextID to provide a new context or continue an existing one. | ||
func WithContextID(id string) conversationRequestOption { | ||
return func(o *conversationRequest) { | ||
o.ContextID = &id | ||
} | ||
} | ||
|
||
// WithScrubPII to define whether the outputs should have PII removed. | ||
func WithScrubPII(scrub bool) conversationRequestOption { | ||
return func(o *conversationRequest) { | ||
o.ScrubPII = &scrub | ||
} | ||
} | ||
|
||
// WithTemperature to specify which way the LLM leans. | ||
func WithTemperature(temp float64) conversationRequestOption { | ||
return func(o *conversationRequest) { | ||
o.Temperature = &temp | ||
} | ||
} | ||
|
||
// ConverseAlpha1 can invoke an LLM given a request created by the NewConversationRequest function. | ||
func (c *GRPCClient) ConverseAlpha1(ctx context.Context, req conversationRequest, options ...conversationRequestOption) (*ConversationResponse, error) { | ||
cinputs := make([]*runtimev1pb.ConversationInput, len(req.inputs)) | ||
for i, in := range req.inputs { | ||
cinputs[i] = &runtimev1pb.ConversationInput{ | ||
Message: in.Message, | ||
Role: in.Role, | ||
ScrubPII: in.ScrubPII, | ||
} | ||
} | ||
|
||
for _, opt := range options { | ||
if opt != nil { | ||
opt(&req) | ||
} | ||
} | ||
|
||
request := runtimev1pb.ConversationRequest{ | ||
Name: req.name, | ||
ContextID: req.ContextID, | ||
Inputs: cinputs, | ||
Parameters: req.Parameters, | ||
Metadata: req.Metadata, | ||
ScrubPII: req.ScrubPII, | ||
Temperature: req.Temperature, | ||
} | ||
|
||
resp, err := c.protoClient.ConverseAlpha1(ctx, &request) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
outputs := make([]ConversationResult, len(resp.GetOutputs())) | ||
for i, o := range resp.GetOutputs() { | ||
outputs[i] = ConversationResult{ | ||
Result: o.GetResult(), | ||
Parameters: o.GetParameters(), | ||
} | ||
} | ||
|
||
return &ConversationResponse{ | ||
ContextID: resp.GetContextID(), | ||
Outputs: outputs, | ||
}, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Dapr Conversation Example with go-sdk | ||
|
||
## Step | ||
|
||
### Prepare | ||
|
||
- Dapr installed | ||
|
||
### Run Conversation Example | ||
|
||
<!-- STEP | ||
name: Run Conversation | ||
output_match_mode: substring | ||
expected_stdout_lines: | ||
- '== APP == conversation output: hello world' | ||
background: true | ||
sleep: 60 | ||
timeout_seconds: 60 | ||
--> | ||
|
||
```bash | ||
dapr run --app-id conversation \ | ||
--dapr-grpc-port 50001 \ | ||
--log-level debug \ | ||
--resources-path ./config \ | ||
-- go run ./main.go | ||
``` | ||
|
||
<!-- END_STEP --> | ||
|
||
## Result | ||
|
||
``` | ||
- '== APP == conversation output: hello world' | ||
``` |
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,7 @@ | ||
apiVersion: dapr.io/v1alpha1 | ||
kind: Component | ||
metadata: | ||
name: echo | ||
spec: | ||
type: conversation.echo | ||
version: v1 |
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,48 @@ | ||
/* | ||
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.NewClient() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
input := dapr.ConversationInput{ | ||
Message: "hello world", | ||
// Role: nil, // Optional | ||
// ScrubPII: nil, // Optional | ||
} | ||
|
||
fmt.Printf("conversation input: %s\n", input.Message) | ||
|
||
var conversationComponent = "echo" | ||
|
||
request := dapr.NewConversationRequest(conversationComponent, []dapr.ConversationInput{input}) | ||
|
||
resp, err := client.ConverseAlpha1(context.Background(), request) | ||
if err != nil { | ||
log.Fatalf("err: %v", err) | ||
} | ||
|
||
fmt.Printf("conversation output: %s\n", resp.Outputs[0].Result) | ||
} |
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
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
Oops, something went wrong.