Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Csharp conversation API quickstarts #1144

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions conversation/csharp/http/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Dapr Conversation API (C# HTTP)

In this quickstart, you'll send an input to a mock Large Language Model (LLM) using Dapr's Conversation API. This API is responsible for providing one consistent API entry point to talk to underlying LLM providers.

Visit [this](https://v1-15.docs.dapr.io/developing-applications/building-blocks/conversation/conversation-overview/) link for more information about Dapr and the Conversation API.

> **Note:** This example leverages HTTP `requests` only. If you are looking for the example using the Dapr Client SDK (recommended) [click here](../sdk/).

This quickstart includes one app:

- Conversation, responsible for sending an input to the underlying LLM and retrieving an output.

## Run the app with the template file

This section shows how to run the application using the [multi-app run template file](https://docs.dapr.io/developing-applications/local-development/multi-app-dapr-run/multi-app-overview/) and Dapr CLI with `dapr run -f .`.

This example uses the default LLM Component provided by Dapr which simply echoes the input provided, for testing purposes. Integrate with popular LLM models by using one of the other [supported conversation components](https://v1-15.docs.dapr.io/reference/components-reference/supported-conversation/).

Open a new terminal window and run the multi app run template:

<!-- STEP
name: Run multi app run template
expected_stdout_lines:
- '== APP - conversation == Input sent: What is dapr?'
- '== APP - conversation == Output response: What is dapr?'
expected_stderr_lines:
output_match_mode: substring
match_order: none
background: true
sleep: 15
timeout_seconds: 15
-->

```bash
dapr run -f .
```

The terminal console output should look similar to this, where:

- The app sends an input `What is dapr?` to the `echo` Component mock LLM.
- The mock LLM echoes `What is dapr?`.

```text
== APP - conversation == Input sent: What is dapr?
== APP - conversation == Output response: What is dapr?
```

<!-- END_STEP -->

2. Stop and clean up application processes.

<!-- STEP
name: Stop multi-app run
-->

```bash
dapr stop -f .
```

<!-- END_STEP -->

## Run the app individually

1. Open a terminal and run the `conversation` app. Build the dependencies if you haven't already.

```bash
cd ./conversation
dotnet build
```

2. Run the Dapr process alongside the application.

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

The terminal console output should look similar to this, where:

- The app sends an input `What is dapr?` to the `echo` Component mock LLM.
- The mock LLM echoes `What is dapr?`.

```text
== APP - conversation == Input sent: What is dapr?
== APP - conversation == Output response: What is dapr?
```
67 changes: 67 additions & 0 deletions conversation/csharp/http/conversation/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
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.
*/

using System.Net.Http;
using System.Text.Json;
using System.Text;

class Program
{
private const string ConversationComponentName = "echo";

static async Task Main(string[] args)
{
var daprHost = Environment.GetEnvironmentVariable("DAPR_HOST") ?? "http://localhost";
var daprHttpPort = Environment.GetEnvironmentVariable("DAPR_HTTP_PORT") ?? "3500";

var client = new HttpClient
{
Timeout = TimeSpan.FromSeconds(15)
};

var inputBody = new
{
name = "echo",
inputs = new[] { new { message = "What is dapr?" } },
parameters = new { },
metadata = new { }
};

var daprUrl = $"{daprHost}:{daprHttpPort}/v1.0-alpha1/conversation/{ConversationComponentName}/converse";

try
{
var content = new StringContent(JsonSerializer.Serialize(inputBody), Encoding.UTF8, "application/json");

// Send a request to the echo mock LLM component
var response = await client.PostAsync(daprUrl, content);
response.EnsureSuccessStatusCode();

Console.WriteLine("Input sent: " + inputBody.inputs[0].message);

var responseBody = await response.Content.ReadAsStringAsync();

// Parse the response
var data = JsonSerializer.Deserialize<Dictionary<string, List<Dictionary<string, string>>>>(responseBody);
var result = data?["outputs"]?[0]?["result"];

Console.WriteLine("Output response: " + result);
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
}
14 changes: 14 additions & 0 deletions conversation/csharp/http/conversation/Program.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Text.Json" Version="9.0.1" />
</ItemGroup>

</Project>
8 changes: 8 additions & 0 deletions conversation/csharp/http/dapr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: 1
common:
resourcesPath: ../../components/
apps:
- appDirPath: ./conversation/
appID: conversation
daprHTTPPort: 3500
command: ["dotnet", "run"]
2 changes: 2 additions & 0 deletions conversation/csharp/http/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include ../../../docker.mk
include ../../../validate.mk
85 changes: 85 additions & 0 deletions conversation/csharp/sdk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Dapr Conversation API (C# SDK)

In this quickstart, you'll send an input to a mock Large Language Model (LLM) using Dapr's Conversation API. This API is responsible for providing one consistent API entry point to talk to underlying LLM providers.

Visit [this](https://v1-15.docs.dapr.io/developing-applications/building-blocks/conversation/conversation-overview/) link for more information about Dapr and the Conversation API.

> **Note:** This example leverages the Dapr SDK. If you are looking for the example using the HTTP API [click here](../http/).

This quickstart includes one app:

- Conversation, responsible for sending an input to the underlying LLM and retrieving an output.

## Run the app with the template file

This section shows how to run the application using the [multi-app run template file](https://docs.dapr.io/developing-applications/local-development/multi-app-dapr-run/multi-app-overview/) and Dapr CLI with `dapr run -f .`.

This example uses the default LLM Component provided by Dapr which simply echoes the input provided, for testing purposes. Integrate with popular LLM models by using one of the other [supported conversation components](https://v1-15.docs.dapr.io/reference/components-reference/supported-conversation/).

Open a new terminal window and run the multi app run template:

<!-- STEP
name: Run multi app run template
expected_stdout_lines:
- '== APP - conversation == Input sent: What is dapr?'
- '== APP - conversation == Output response: What is dapr?'
expected_stderr_lines:
output_match_mode: substring
match_order: none
background: true
sleep: 15
timeout_seconds: 15
-->

```bash
dapr run -f .
```

The terminal console output should look similar to this, where:

- The app sends an input `What is dapr?` to the `echo` Component mock LLM.
- The mock LLM echoes `What is dapr?`.

```text
== APP - conversation == Input sent: What is dapr?
== APP - conversation == Output response: What is dapr?
```

<!-- END_STEP -->

2. Stop and clean up application processes.

<!-- STEP
name: Stop multi-app run
-->

```bash
dapr stop -f .
```

<!-- END_STEP -->

## Run the app individually

1. Open a terminal and run the `conversation` app. Build the dependencies if you haven't already.

```bash
cd ./conversation
dotnet build
```

2. Run the Dapr process alongside the application.

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

The terminal console output should look similar to this, where:

- The app sends an input `What is dapr?` to the `echo` Component mock LLM.
- The mock LLM echoes `What is dapr?`.

```text
== APP - conversation == Input sent: What is dapr?
== APP - conversation == Output response: What is dapr?
```
54 changes: 54 additions & 0 deletions conversation/csharp/sdk/conversation/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
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.
*/

using Dapr.AI.Conversation;
using Dapr.AI.Conversation.Extensions;

class Program
{
private const string ConversationComponentName = "echo";

static async Task Main(string[] args)
{
const string prompt = "What is dapr?";

var builder = WebApplication.CreateBuilder(args);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@WhitWaldo can you review the Conversation Client instantiation here? Should I do it a different way?

Also, is it weird putting the builder/app in a Class? Can remove that...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is precisely right. As this is configured to use a "Microsoft.NET.Sdk.Web" project, this is the appropriate way to set up the "app builder" itself. Dapr then gets registered on line 28, DI finalized on line 29 and the Dapr client instantiated from that DI pipeline on 32.

Technically this could be shortened a little bit as single-file C# apps don't as much of the window dressing any longer, but this is fine - if you want to shorten it, remove lines 19, 20 and 54 (class Program { })

builder.Services.AddDaprConversationClient();
var app = builder.Build();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing a line here: await app.RunAsync(); - while not strictly necessary since we don't need the whole web server in this example, it'd be a good practice to add it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for checking this out! This got merged but will add this feedback into my next PR

//Instantiate Dapr Conversation Client
var conversationClient = app.Services.GetRequiredService<DaprConversationClient>();

try
{
// Send a request to the echo mock LLM component
var response = await conversationClient.ConverseAsync(ConversationComponentName, [new(prompt, DaprConversationRole.Generic)]);
Console.WriteLine("Input sent: " + prompt);

if (response != null)
{
Console.Write("Output response:");
foreach (var resp in response.Outputs)
{
Console.WriteLine($" {resp.Result}");
}
}
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
}
14 changes: 14 additions & 0 deletions conversation/csharp/sdk/conversation/Program.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Dapr.AI" Version="1.15.0-rc02" />
</ItemGroup>

</Project>
8 changes: 8 additions & 0 deletions conversation/csharp/sdk/dapr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: 1
common:
resourcesPath: ../../components/
apps:
- appDirPath: ./conversation/
appID: conversation
daprHTTPPort: 3500
command: ["dotnet", "run"]
2 changes: 2 additions & 0 deletions conversation/csharp/sdk/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include ../../../docker.mk
include ../../../validate.mk
6 changes: 3 additions & 3 deletions conversation/go/http/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ In this quickstart, you'll send an input to a mock Large Language Model (LLM) us

Visit [this](https://v1-15.docs.dapr.io/developing-applications/building-blocks/conversation/conversation-overview/) link for more information about Dapr and the Conversation API.

> **Note:** This example leverages HTTP `requests` only. If you are looking for the example using the Dapr Client SDK (recommended) [click here](../sdk/).
> **Note:** This example leverages HTTP `requests` only. If you are looking for the example using the Dapr Client SDK (recommended) [click here](../sdk/).

This quickstart includes one app:

- `conversation.go`, responsible for sending and input to the underlying LLM and retrieving an output.
- `conversation.go`, responsible for sending an input to the underlying LLM and retrieving an output.

## Run the app with the template file

Expand Down Expand Up @@ -47,7 +47,7 @@ The terminal console output should look similar to this, where:

<!-- END_STEP -->

2. Stop and clean up application processes
2. Stop and clean up application processes.

<!-- STEP
name: Stop multi-app run
Expand Down
6 changes: 3 additions & 3 deletions conversation/go/sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ In this quickstart, you'll send an input to a mock Large Language Model (LLM) us

Visit [this](https://v1-15.docs.dapr.io/developing-applications/building-blocks/conversation/conversation-overview/) link for more information about Dapr and the Conversation API.

> **Note:** This example leverages the Dapr SDK. If you are looking for the example using the HTTP API [click here](../http/).
> **Note:** This example leverages the Dapr SDK. If you are looking for the example using the HTTP API [click here](../http/).

This quickstart includes one app:

- `conversation.go`, responsible for sending and input to the underlying LLM and retrieving an output.
- `conversation.go`, responsible for sending an input to the underlying LLM and retrieving an output.

## Run the app with the template file

Expand Down Expand Up @@ -47,7 +47,7 @@ The terminal console output should look similar to this, where:

<!-- END_STEP -->

2. Stop and clean up application processes
2. Stop and clean up application processes.

<!-- STEP
name: Stop multi-app run
Expand Down
Loading