Skip to content

Commit 110a88e

Browse files
authored
Merge pull request #4880 from bibryam/issue-4879
Update "Conversation API how-to guide" too show the code for every language
2 parents 4867d9d + 856a1ea commit 110a88e

File tree

1 file changed

+124
-16
lines changed

1 file changed

+124
-16
lines changed

daprdocs/content/en/developing-applications/building-blocks/conversation/howto-conversation-layer.md

Lines changed: 124 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ spec:
5656
5757
## Connect the conversation client
5858
59-
The following examples use an HTTP client to send a POST request to Dapr's sidecar HTTP endpoint. You can also use [the Dapr SDK client instead]({{% ref "#related-links" %}}).
59+
The following examples use the Dapr SDK client to interact with LLMs.
6060
6161
{{< tabpane text=true >}}
6262
@@ -83,7 +83,7 @@ var response = await conversationClient.ConverseAsync("conversation",
8383
DaprConversationRole.Generic)
8484
});
8585
86-
Console.WriteLine("Received the following from the LLM:");
86+
Console.WriteLine("conversation output: ");
8787
foreach (var resp in response.Outputs)
8888
{
8989
Console.WriteLine($"\t{resp.Result}");
@@ -92,6 +92,77 @@ foreach (var resp in response.Outputs)
9292

9393
{{% /tab %}}
9494

95+
<!-- Java -->
96+
{{% tab "Java" %}}
97+
98+
```java
99+
//dependencies
100+
import io.dapr.client.DaprClientBuilder;
101+
import io.dapr.client.DaprPreviewClient;
102+
import io.dapr.client.domain.ConversationInput;
103+
import io.dapr.client.domain.ConversationRequest;
104+
import io.dapr.client.domain.ConversationResponse;
105+
import reactor.core.publisher.Mono;
106+
107+
import java.util.List;
108+
109+
public class Conversation {
110+
111+
public static void main(String[] args) {
112+
String prompt = "Please write a witty haiku about the Dapr distributed programming framework at dapr.io";
113+
114+
try (DaprPreviewClient client = new DaprClientBuilder().buildPreviewClient()) {
115+
System.out.println("Input: " + prompt);
116+
117+
ConversationInput daprConversationInput = new ConversationInput(prompt);
118+
119+
// Component name is the name provided in the metadata block of the conversation.yaml file.
120+
Mono<ConversationResponse> responseMono = client.converse(new ConversationRequest("echo",
121+
List.of(daprConversationInput))
122+
.setContextId("contextId")
123+
.setScrubPii(true).setTemperature(1.1d));
124+
ConversationResponse response = responseMono.block();
125+
System.out.printf("conversation output: %s", response.getConversationOutputs().get(0).getResult());
126+
} catch (Exception e) {
127+
throw new RuntimeException(e);
128+
}
129+
}
130+
}
131+
```
132+
133+
{{% /tab %}}
134+
135+
<!-- Python -->
136+
{{% tab "Python" %}}
137+
138+
```python
139+
#dependencies
140+
from dapr.clients import DaprClient
141+
from dapr.clients.grpc._request import ConversationInput
142+
143+
#code
144+
with DaprClient() as d:
145+
inputs = [
146+
ConversationInput(content="Please write a witty haiku about the Dapr distributed programming framework at dapr.io", role='user', scrub_pii=True),
147+
]
148+
149+
metadata = {
150+
'model': 'modelname',
151+
'key': 'authKey',
152+
'cacheTTL': '10m',
153+
}
154+
155+
response = d.converse_alpha1(
156+
name='echo', inputs=inputs, temperature=0.7, context_id='chat-123', metadata=metadata
157+
)
158+
159+
for output in response.outputs:
160+
print(f'conversation output: {output.result}')
161+
```
162+
163+
{{% /tab %}}
164+
165+
95166
<!-- Go -->
96167
{{% tab "Go" %}}
97168

@@ -189,39 +260,58 @@ dapr run --app-id conversation --dapr-grpc-port 50001 --log-level debug --resour
189260

190261
{{% /tab %}}
191262

192-
<!-- Go -->
193-
{{% tab "Go" %}}
263+
264+
{{% tab "Java" %}}
194265

195266
```bash
196-
dapr run --app-id conversation --dapr-grpc-port 50001 --log-level debug --resources-path ./config -- go run ./main.go
267+
268+
dapr run --app-id conversation --dapr-grpc-port 50001 --log-level debug --resources-path ./config -- mvn spring-boot:run
197269
```
198270

199-
**Expected output**
271+
{{% /tab %}}
272+
273+
200274

275+
{{% tab "Python" %}}
276+
277+
```bash
278+
279+
dapr run --app-id conversation --dapr-grpc-port 50001 --log-level debug --resources-path ./config -- python3 app.py
201280
```
202-
- '== APP == conversation output: Please write a witty haiku about the Dapr distributed programming framework at dapr.io'
281+
282+
{{% /tab %}}
283+
284+
285+
<!-- Go -->
286+
{{% tab "Go" %}}
287+
288+
```bash
289+
dapr run --app-id conversation --dapr-grpc-port 50001 --log-level debug --resources-path ./config -- go run ./main.go
203290
```
204291

292+
205293
{{% /tab %}}
206294

295+
296+
207297
<!-- Rust -->
208298
{{% tab "Rust" %}}
209299

210300
```bash
211301
dapr run --app-id=conversation --resources-path ./config --dapr-grpc-port 3500 -- cargo run --example conversation
212302
```
213303

304+
{{% /tab %}}
305+
306+
{{< /tabpane >}}
307+
308+
214309
**Expected output**
215310

216311
```
217-
- 'conversation input: hello world'
218-
- 'conversation output: hello world'
312+
- '== APP == conversation output: Please write a witty haiku about the Dapr distributed programming framework at dapr.io'
219313
```
220314

221-
{{% /tab %}}
222-
223-
{{< /tabpane >}}
224-
225315
## Advanced features
226316

227317
The conversation API supports the following features:
@@ -230,9 +320,11 @@ The conversation API supports the following features:
230320

231321
1. **PII scrubbing:** Allows for the obfuscation of data going in and out of the LLM.
232322

323+
1. **Tool calling:** Allows LLMs to interact with external functions and APIs.
324+
233325
To learn how to enable these features, see the [conversation API reference guide]({{% ref conversation_api %}}).
234326

235-
## Related links
327+
## Conversation API examples in Dapr SDK repositories
236328

237329
Try out the conversation API using the full examples provided in the supported SDK repos.
238330

@@ -246,7 +338,23 @@ Try out the conversation API using the full examples provided in the supported S
246338

247339
{{% /tab %}}
248340

249-
<!-- Go -->
341+
342+
<!-- Java -->
343+
{{% tab "Java" %}}
344+
345+
[Dapr conversation example with the Java SDK](https://github.com/dapr/java-sdk/tree/master/examples/src/main/java/io/dapr/examples/conversation)
346+
347+
{{% /tab %}}
348+
349+
350+
<!-- Python -->
351+
{{% tab "Python" %}}
352+
353+
[Dapr conversation example with the Python SDK](https://github.com/dapr/python-sdk/tree/main/examples/conversation)
354+
355+
{{% /tab %}}
356+
357+
<!-- Go -->
250358
{{% tab "Go" %}}
251359

252360
[Dapr conversation example with the Go SDK](https://github.com/dapr/go-sdk/tree/main/examples/conversation)
@@ -264,6 +372,6 @@ Try out the conversation API using the full examples provided in the supported S
264372

265373

266374
## Next steps
267-
375+
- [Conversation quickstart]({{% ref conversation-quickstart %}})
268376
- [Conversation API reference guide]({{% ref conversation_api %}})
269377
- [Available conversation components]({{% ref supported-conversation %}})

0 commit comments

Comments
 (0)