|
| 1 | +--- |
| 2 | +type: docs |
| 3 | +title: "How-To: Converse with an LLM using the conversation API" |
| 4 | +linkTitle: "How-To: Converse" |
| 5 | +weight: 2000 |
| 6 | +description: "Learn how to abstract the complexities of interacting with large language models" |
| 7 | +--- |
| 8 | + |
| 9 | +{{% alert title="Alpha" color="primary" %}} |
| 10 | +The conversation API is currently in [alpha]({{< ref "certification-lifecycle.md#certification-levels" >}}). |
| 11 | +{{% /alert %}} |
| 12 | + |
| 13 | +Let's get started using the [conversation API]({{< ref conversation-overview.md >}}). In this guide, you'll learn how to: |
| 14 | + |
| 15 | +- Set up one of the available Dapr components (echo) that work with the conversation API. |
| 16 | +- Add the conversation client to your application. |
| 17 | + |
| 18 | +## Set up the conversation component |
| 19 | + |
| 20 | +Create a new configuration file called `conversation.yaml` and save to a components or config sub-folder in your application directory. |
| 21 | + |
| 22 | +Select your [preferred conversation component spec]({{< ref supported-conversation >}}) for your `conversation.yaml` file. |
| 23 | + |
| 24 | +For this scenario, we use a simple echo component. |
| 25 | + |
| 26 | +```yml |
| 27 | +apiVersion: dapr.io/v1alpha1 |
| 28 | +kind: Component |
| 29 | +metadata: |
| 30 | + name: echo |
| 31 | +spec: |
| 32 | + type: conversation.echo |
| 33 | + version: v1 |
| 34 | +``` |
| 35 | +
|
| 36 | +## Connect the conversation client |
| 37 | +
|
| 38 | +
|
| 39 | +{{< tabs ".NET" "Go" "Rust" >}} |
| 40 | +
|
| 41 | +
|
| 42 | + <!-- .NET --> |
| 43 | +{{% codetab %}} |
| 44 | +
|
| 45 | +```dotnet |
| 46 | +todo |
| 47 | +``` |
| 48 | + |
| 49 | +{{% /codetab %}} |
| 50 | + |
| 51 | + <!-- Go --> |
| 52 | +{{% codetab %}} |
| 53 | + |
| 54 | +```go |
| 55 | +package main |
| 56 | + |
| 57 | +import ( |
| 58 | + "context" |
| 59 | + "fmt" |
| 60 | + dapr "github.com/dapr/go-sdk/client" |
| 61 | + "log" |
| 62 | +) |
| 63 | + |
| 64 | +func main() { |
| 65 | + client, err := dapr.NewClient() |
| 66 | + if err != nil { |
| 67 | + panic(err) |
| 68 | + } |
| 69 | + |
| 70 | + input := dapr.ConversationInput{ |
| 71 | + Message: "hello world", |
| 72 | + // Role: nil, // Optional |
| 73 | + // ScrubPII: nil, // Optional |
| 74 | + } |
| 75 | + |
| 76 | + fmt.Printf("conversation input: %s\n", input.Message) |
| 77 | + |
| 78 | + var conversationComponent = "echo" |
| 79 | + |
| 80 | + request := dapr.NewConversationRequest(conversationComponent, []dapr.ConversationInput{input}) |
| 81 | + |
| 82 | + resp, err := client.ConverseAlpha1(context.Background(), request) |
| 83 | + if err != nil { |
| 84 | + log.Fatalf("err: %v", err) |
| 85 | + } |
| 86 | + |
| 87 | + fmt.Printf("conversation output: %s\n", resp.Outputs[0].Result) |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +{{% /codetab %}} |
| 92 | + |
| 93 | + <!-- Rust --> |
| 94 | +{{% codetab %}} |
| 95 | + |
| 96 | +```rust |
| 97 | +use dapr::client::{ConversationInputBuilder, ConversationRequestBuilder}; |
| 98 | +use std::thread; |
| 99 | +use std::time::Duration; |
| 100 | + |
| 101 | +type DaprClient = dapr::Client<dapr::client::TonicClient>; |
| 102 | + |
| 103 | +#[tokio::main] |
| 104 | +async fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 105 | + // Sleep to allow for the server to become available |
| 106 | + thread::sleep(Duration::from_secs(5)); |
| 107 | + |
| 108 | + // Set the Dapr address |
| 109 | + let address = "https://127.0.0.1".to_string(); |
| 110 | + |
| 111 | + let mut client = DaprClient::connect(address).await?; |
| 112 | + |
| 113 | + let input = ConversationInputBuilder::new("hello world").build(); |
| 114 | + |
| 115 | + let conversation_component = "echo"; |
| 116 | + |
| 117 | + let request = |
| 118 | + ConversationRequestBuilder::new(conversation_component, vec![input.clone()]).build(); |
| 119 | + |
| 120 | + println!("conversation input: {:?}", input.message); |
| 121 | + |
| 122 | + let response = client.converse_alpha1(request).await?; |
| 123 | + |
| 124 | + println!("conversation output: {:?}", response.outputs[0].result); |
| 125 | + Ok(()) |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +{{% /codetab %}} |
| 130 | + |
| 131 | +{{< /tabs >}} |
| 132 | + |
| 133 | + |
| 134 | +## Next steps |
| 135 | + |
| 136 | +- [Conversation API reference guide]({{< ref conversation_api.md >}}) |
| 137 | +- [Available conversation components]({{< ref supported-conversation >}}) |
0 commit comments