Skip to content

Commit 194d2b9

Browse files
committed
Csharp conversation quickstarts
Signed-off-by: Alice Gibbons <alice@diagrid.io>
1 parent 7000a78 commit 194d2b9

File tree

12 files changed

+345
-6
lines changed

12 files changed

+345
-6
lines changed

conversation/csharp/http/README.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Dapr Conversation API (C# HTTP)
2+
3+
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.
4+
5+
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.
6+
7+
> **Note:** This example leverages HTTP `requests` only. If you are looking for the example using the Dapr Client SDK (recommended) [click here](../sdk/).
8+
9+
This quickstart includes one app:
10+
11+
- Conversation, responsible for sending an input to the underlying LLM and retrieving an output.
12+
13+
## Run the app with the template file
14+
15+
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 .`.
16+
17+
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/).
18+
19+
Open a new terminal window and run the multi app run template:
20+
21+
<!-- STEP
22+
name: Run multi app run template
23+
expected_stdout_lines:
24+
- '== APP - conversation == Input sent: What is dapr?'
25+
- '== APP - conversation == Output response: What is dapr?'
26+
expected_stderr_lines:
27+
output_match_mode: substring
28+
match_order: none
29+
background: true
30+
sleep: 15
31+
timeout_seconds: 15
32+
-->
33+
34+
```bash
35+
dapr run -f .
36+
```
37+
38+
The terminal console output should look similar to this, where:
39+
40+
- The app sends an input `What is dapr?` to the `echo` Component mock LLM.
41+
- The mock LLM echoes `What is dapr?`.
42+
43+
```text
44+
== APP - conversation == Input sent: What is dapr?
45+
== APP - conversation == Output response: What is dapr?
46+
```
47+
48+
<!-- END_STEP -->
49+
50+
2. Stop and clean up application processes.
51+
52+
<!-- STEP
53+
name: Stop multi-app run
54+
-->
55+
56+
```bash
57+
dapr stop -f .
58+
```
59+
60+
<!-- END_STEP -->
61+
62+
## Run the app individually
63+
64+
1. Open a terminal and run the `conversation` app. Build the dependencies if you haven't already.
65+
66+
```bash
67+
cd ./conversation
68+
dotnet build
69+
```
70+
71+
2. Run the Dapr process alongside the application.
72+
73+
```bash
74+
dapr run --app-id conversation --resources-path ../../../components/ -- dotnet run
75+
```
76+
77+
The terminal console output should look similar to this, where:
78+
79+
- The app sends an input `What is dapr?` to the `echo` Component mock LLM.
80+
- The mock LLM echoes `What is dapr?`.
81+
82+
```text
83+
== APP - conversation == Input sent: What is dapr?
84+
== APP - conversation == Output response: What is dapr?
85+
```
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
Copyright 2024 The Dapr Authors
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
using System.Net.Http;
17+
using System.Text.Json;
18+
using System.Text;
19+
20+
class Program
21+
{
22+
private const string ConversationComponentName = "echo";
23+
24+
static async Task Main(string[] args)
25+
{
26+
var daprHost = Environment.GetEnvironmentVariable("DAPR_HOST") ?? "http://localhost";
27+
var daprHttpPort = Environment.GetEnvironmentVariable("DAPR_HTTP_PORT") ?? "3500";
28+
29+
var client = new HttpClient
30+
{
31+
Timeout = TimeSpan.FromSeconds(15)
32+
};
33+
34+
var inputBody = new
35+
{
36+
name = "echo",
37+
inputs = new[] { new { message = "What is dapr?" } },
38+
parameters = new { },
39+
metadata = new { }
40+
};
41+
42+
var daprUrl = $"{daprHost}:{daprHttpPort}/v1.0-alpha1/conversation/{ConversationComponentName}/converse";
43+
44+
try
45+
{
46+
var content = new StringContent(JsonSerializer.Serialize(inputBody), Encoding.UTF8, "application/json");
47+
48+
// Send a request to the echo mock LLM component
49+
var response = await client.PostAsync(daprUrl, content);
50+
response.EnsureSuccessStatusCode();
51+
52+
Console.WriteLine("Input sent: " + inputBody.inputs[0].message);
53+
54+
var responseBody = await response.Content.ReadAsStringAsync();
55+
56+
// Parse the response
57+
var data = JsonSerializer.Deserialize<Dictionary<string, List<Dictionary<string, string>>>>(responseBody);
58+
var result = data?["outputs"]?[0]?["result"];
59+
60+
Console.WriteLine("Output response: " + result);
61+
}
62+
catch (Exception ex)
63+
{
64+
Console.WriteLine("Error: " + ex.Message);
65+
}
66+
}
67+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="System.Text.Json" Version="9.0.1" />
12+
</ItemGroup>
13+
14+
</Project>

conversation/csharp/http/dapr.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
version: 1
2+
common:
3+
resourcesPath: ../../components/
4+
apps:
5+
- appDirPath: ./conversation/
6+
appID: conversation
7+
daprHTTPPort: 3500
8+
command: ["dotnet", "run"]

conversation/csharp/http/makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
include ../../../docker.mk
2+
include ../../../validate.mk

conversation/csharp/sdk/README.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Dapr Conversation API (C# SDK)
2+
3+
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.
4+
5+
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.
6+
7+
> **Note:** This example leverages the Dapr SDK. If you are looking for the example using the HTTP API [click here](../http/).
8+
9+
This quickstart includes one app:
10+
11+
- Conversation, responsible for sending an input to the underlying LLM and retrieving an output.
12+
13+
## Run the app with the template file
14+
15+
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 .`.
16+
17+
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/).
18+
19+
Open a new terminal window and run the multi app run template:
20+
21+
<!-- STEP
22+
name: Run multi app run template
23+
expected_stdout_lines:
24+
- '== APP - conversation == Input sent: What is dapr?'
25+
- '== APP - conversation == Output response: What is dapr?'
26+
expected_stderr_lines:
27+
output_match_mode: substring
28+
match_order: none
29+
background: true
30+
sleep: 15
31+
timeout_seconds: 15
32+
-->
33+
34+
```bash
35+
dapr run -f .
36+
```
37+
38+
The terminal console output should look similar to this, where:
39+
40+
- The app sends an input `What is dapr?` to the `echo` Component mock LLM.
41+
- The mock LLM echoes `What is dapr?`.
42+
43+
```text
44+
== APP - conversation == Input sent: What is dapr?
45+
== APP - conversation == Output response: What is dapr?
46+
```
47+
48+
<!-- END_STEP -->
49+
50+
2. Stop and clean up application processes.
51+
52+
<!-- STEP
53+
name: Stop multi-app run
54+
-->
55+
56+
```bash
57+
dapr stop -f .
58+
```
59+
60+
<!-- END_STEP -->
61+
62+
## Run the app individually
63+
64+
1. Open a terminal and run the `conversation` app. Build the dependencies if you haven't already.
65+
66+
```bash
67+
cd ./conversation
68+
dotnet build
69+
```
70+
71+
2. Run the Dapr process alongside the application.
72+
73+
```bash
74+
dapr run --app-id conversation --resources-path ../../../components/ -- dotnet run
75+
```
76+
77+
The terminal console output should look similar to this, where:
78+
79+
- The app sends an input `What is dapr?` to the `echo` Component mock LLM.
80+
- The mock LLM echoes `What is dapr?`.
81+
82+
```text
83+
== APP - conversation == Input sent: What is dapr?
84+
== APP - conversation == Output response: What is dapr?
85+
```
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Copyright 2024 The Dapr Authors
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
using Dapr.AI.Conversation;
17+
using Dapr.AI.Conversation.Extensions;
18+
19+
class Program
20+
{
21+
private const string ConversationComponentName = "echo";
22+
23+
static async Task Main(string[] args)
24+
{
25+
const string prompt = "What is dapr?";
26+
27+
var builder = WebApplication.CreateBuilder(args);
28+
builder.Services.AddDaprConversationClient();
29+
var app = builder.Build();
30+
31+
//Instantiate Dapr Conversation Client
32+
var conversationClient = app.Services.GetRequiredService<DaprConversationClient>();
33+
34+
try
35+
{
36+
// Send a request to the echo mock LLM component
37+
var response = await conversationClient.ConverseAsync(ConversationComponentName, [new(prompt, DaprConversationRole.Generic)]);
38+
Console.WriteLine("Input sent: " + prompt);
39+
40+
if (response != null)
41+
{
42+
Console.Write("Output response:");
43+
foreach (var resp in response.Outputs)
44+
{
45+
Console.WriteLine($" {resp.Result}");
46+
}
47+
}
48+
}
49+
catch (Exception ex)
50+
{
51+
Console.WriteLine("Error: " + ex.Message);
52+
}
53+
}
54+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Dapr.AI" Version="1.15.0-rc02" />
12+
</ItemGroup>
13+
14+
</Project>

conversation/csharp/sdk/dapr.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
version: 1
2+
common:
3+
resourcesPath: ../../components/
4+
apps:
5+
- appDirPath: ./conversation/
6+
appID: conversation
7+
daprHTTPPort: 3500
8+
command: ["dotnet", "run"]

conversation/csharp/sdk/makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
include ../../../docker.mk
2+
include ../../../validate.mk

conversation/go/http/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ In this quickstart, you'll send an input to a mock Large Language Model (LLM) us
44

55
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.
66

7-
> **Note:** This example leverages HTTP `requests` only. If you are looking for the example using the Dapr Client SDK (recommended) [click here](../sdk/).
7+
> **Note:** This example leverages HTTP `requests` only. If you are looking for the example using the Dapr Client SDK (recommended) [click here](../sdk/).
88
99
This quickstart includes one app:
1010

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

1313
## Run the app with the template file
1414

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

4848
<!-- END_STEP -->
4949

50-
2. Stop and clean up application processes
50+
2. Stop and clean up application processes.
5151

5252
<!-- STEP
5353
name: Stop multi-app run

conversation/go/sdk/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ In this quickstart, you'll send an input to a mock Large Language Model (LLM) us
44

55
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.
66

7-
> **Note:** This example leverages the Dapr SDK. If you are looking for the example using the HTTP API [click here](../http/).
7+
> **Note:** This example leverages the Dapr SDK. If you are looking for the example using the HTTP API [click here](../http/).
88
99
This quickstart includes one app:
1010

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

1313
## Run the app with the template file
1414

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

4848
<!-- END_STEP -->
4949

50-
2. Stop and clean up application processes
50+
2. Stop and clean up application processes.
5151

5252
<!-- STEP
5353
name: Stop multi-app run

0 commit comments

Comments
 (0)