docs/daprdocs/content/en/getting-started/quickstarts/conversation-quickstart.md

6.6 KiB

type title linkTitle weight description
docs Quickstart: Conversation Conversation 90 Get started with the Dapr conversation building block

{{% alert title="Alpha" color="warning" %}} The conversation building block is currently in alpha. {{% /alert %}}

Let's take a look at how the [Dapr conversation building block]({{< ref conversation-overview.md >}}) makes interacting with the Anthropic LLM easier. In this quickstart, you use the Anthropic component to communicate with the LLM and ask it for a poem about Dapr.

{{< tabs ".NET" >}}

{{% codetab %}}

Step 1: Pre-requisites

For this example, you will need:

Step 2: Set up the environment

Clone the sample provided in the Quickstarts repo.

git clone https://github.com/dapr/quickstarts.git

From the root of the Quickstarts directory, navigate into the conversation directory:

cd conversation/csharp/sdk/conversation

Install the dependencies:

dotnet restore
dotnet build

Step 3: Launch the conversation service

Start the conversation service with the following command:

dapr run --app-id conversation --resources-path "../../../components/" -- dotnet run

Expected output

== APP == info: System.Net.Http.HttpClient.Default.LogicalHandler[100]
== APP ==       Start processing HTTP request POST http://localhost:50115/dapr.proto.runtime.v1.Dapr/ConverseAlpha1
== APP == info: System.Net.Http.HttpClient.Default.ClientHandler[100]
== APP ==       Sending HTTP request POST http://localhost:50115/dapr.proto.runtime.v1.Dapr/ConverseAlpha1
== APP == info: System.Net.Http.HttpClient.Default.ClientHandler[101]
== APP ==       Received HTTP response headers after 3241.804ms - 200
== APP == info: System.Net.Http.HttpClient.Default.LogicalHandler[101]
== APP ==       End processing HTTP request after 3260.3206ms - 200
== APP == info: Program[1330097018]
== APP ==       Sent prompt to conversation API: 'Please write a witty sonnet about the Dapr distributed programming framework at dapr.io'
== APP == info: Program[1283986522]
== APP ==       Received message from the conversation API: 'Here's a witty sonnet about the Dapr distributed programming framework:
== APP == 
== APP == O Dapr, thou art a framework most divine,
== APP == Distributed apps, thy specialty so fine.
== APP == With microservices, thou dost unite,
== APP == And make cloud-native dreams take flight.
== APP == 
== APP == Thy building blocks, like stars in cosmic dance,
== APP == State management, pub/sub, and more enhance.
== APP == Polyglot and platform-agnostic too,
== APP == Developers swoon, their hearts you woo.
== APP == 
== APP == From Kubernetes to edge, you scale with ease,
== APP == Your sidecar pattern doth the experts please.
== APP == Open-source and loved by geeks galore,
== APP == At dapr.io, they come to explore.
== APP == 
== APP == Though some may mock my verse as quite absurd,
== APP == I say Dapr's praises must be heard!
== APP ==       '
Exited App successfully

What happened?

conversation.yaml LLM component

In conversation/components directly of the quickstart, the conversation.yaml file configures the Anthropic LLM component.

apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
  name: conversation
spec:
  type: conversation.anthropic
  version: v1
  metadata:
    - name: key
      secretKeyRef:
        name: anthropic
        key: anthropic
auth:
  secretStore: envvar-secrets

For authentication, the component also uses a secret store called envvar-secrets.

Program.cs conversation app

using System.Text;
using Dapr.AI.Conversation;
using Dapr.AI.Conversation.Extensions;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDaprConversationClient();
var app = builder.Build();

var conversationClient = app.Services.GetRequiredService<DaprConversationClient>();
var logger = app.Services.GetRequiredService<ILogger<Program>>();

// Send prompt to send to the Anthropic LLM
const string prompt = "Please write a witty sonnet about the Dapr distributed programming framework at dapr.io";
var response = await conversationClient.ConverseAsync("conversation",
        [new(prompt, DaprConversationRole.Generic)]);
Log.LogRequest(logger, prompt);

var stringBuilder = new StringBuilder();
foreach (var resp in response.Outputs)
{
        stringBuilder.AppendLine(resp.Result);
}
Log.LogResponse(logger, stringBuilder.ToString());

// Output log with response from LLM
static partial class Log
{
        [LoggerMessage(LogLevel.Information, "Sent prompt to conversation API: '{message}'")]
        internal static partial void LogRequest(ILogger logger, string message);

        [LoggerMessage(LogLevel.Information, "Received message from the conversation API: '{message}'")]
        internal static partial void LogResponse(ILogger logger, string message);
}

{{% /codetab %}}

{{< /tabs >}}

Demo

Watch the demo presented during Diagrid's Dapr v1.15 celebration to see how the conversation API works using the .NET SDK.

Tell us what you think!

We're continuously working to improve our Quickstart examples and value your feedback. Did you find this Quickstart helpful? Do you have suggestions for improvement?

Join the discussion in our discord channel.

Next steps

  • SDK samples of this quickstart:
  • Learn more about [the conversation building block]({{< ref conversation-overview.md >}})

{{< button text="Explore Dapr tutorials >>" page="getting-started/tutorials/_index.md" >}}