Skip to content

Commit d301573

Browse files
Added Conversation API to JS SDK
Signed-off-by: Constantin Chirila <[email protected]>
1 parent 26b3527 commit d301573

File tree

14 files changed

+843
-0
lines changed

14 files changed

+843
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
apiVersion: dapr.io/v1alpha1
2+
kind: Component
3+
metadata:
4+
name: echo
5+
spec:
6+
type: conversation.echo
7+
version: v1

examples/conversation/package-lock.json

Lines changed: 250 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/conversation/package.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "conversation",
3+
"version": "1.0.0",
4+
"description": "An example utilizing the Dapr JS SDK to call a conversation component",
5+
"main": "dist/index.js",
6+
"private": true,
7+
"type": "module",
8+
"scripts": {
9+
"build": "rimraf ./dist && tsc",
10+
"start": "npm run build && node dist/index.js",
11+
"start:dapr": "dapr run --app-id conversation --app-protocol grpc --dapr-grpc-port 3500 --resources-path ./components/ -- npm run start"
12+
},
13+
"author": "",
14+
"keywords": [],
15+
"license": "ISC",
16+
"devDependencies": {
17+
"rimraf": "^3.0.2",
18+
"ts-node": "^9.1.1",
19+
"typescript": "^4.2.4"
20+
},
21+
"dependencies": {
22+
"@dapr/dapr": "file:../../build",
23+
"@types/node": "^15.3.0"
24+
}
25+
}

examples/conversation/src/index.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
Copyright 2022 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+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
// @ts-nocheck
15+
import { DaprClient } from "@dapr/dapr";
16+
17+
const LLM_NAME = "echo";
18+
19+
async function main() {
20+
const daprHost = process.env.DAPR_HOST || "http://localhost";
21+
const daprHttpPort = process.env.DAPR_HTTP_PORT || "3500";
22+
const client = new DaprClient({ daprHost, daprPort: daprHttpPort });
23+
24+
try {
25+
const response = await client.conversation.converseAsync(
26+
LLM_NAME,
27+
[
28+
{
29+
messages: [
30+
{
31+
of_user: {
32+
content: [
33+
{
34+
text: "What is the weather like in San Francisco in celsius?",
35+
},
36+
],
37+
},
38+
},
39+
],
40+
scrub_pii: false,
41+
},
42+
],
43+
{
44+
metadata: {
45+
api_key: "test-key",
46+
version: "1.0",
47+
},
48+
scrub_pii: false,
49+
temperature: 0.7,
50+
tools: [
51+
{
52+
function: {
53+
name: "get_weather",
54+
description: "Get the current weather for a location",
55+
parameters: {
56+
type: "object",
57+
properties: {
58+
location: {
59+
type: "string",
60+
description: "The city and state, e.g. San Francisco, CA",
61+
},
62+
unit: {
63+
type: "string",
64+
enum: ["celsius", "fahrenheit"],
65+
description: "The temperature unit to use",
66+
},
67+
},
68+
required: ["location"],
69+
},
70+
},
71+
},
72+
],
73+
tool_choice: "auto",
74+
},
75+
);
76+
console.log("Response:", response);
77+
} catch (error) {
78+
console.error("Error:", (error as Error).message);
79+
process.exit(1);
80+
}
81+
}
82+
83+
main().catch((error) => {
84+
console.error("Unhandled error:", error);
85+
process.exit(1);
86+
});

0 commit comments

Comments
 (0)