From 568458c140ca24f5b814dd9c3870a45e9d537fa6 Mon Sep 17 00:00:00 2001 From: Constantin Chirila Date: Mon, 3 Feb 2025 11:53:24 +0000 Subject: [PATCH 1/5] Added Quickstart for Javascript Conversation HTTP api Signed-off-by: Constantin Chirila --- conversation/javascript/http/README.md | 99 +++++++++++++++++++ .../javascript/http/conversation/index.js | 43 ++++++++ .../http/conversation/package-lock.json | 13 +++ .../javascript/http/conversation/package.json | 14 +++ conversation/javascript/http/dapr.yaml | 8 ++ conversation/javascript/http/makefile | 2 + 6 files changed, 179 insertions(+) create mode 100644 conversation/javascript/http/README.md create mode 100644 conversation/javascript/http/conversation/index.js create mode 100644 conversation/javascript/http/conversation/package-lock.json create mode 100644 conversation/javascript/http/conversation/package.json create mode 100644 conversation/javascript/http/dapr.yaml create mode 100644 conversation/javascript/http/makefile diff --git a/conversation/javascript/http/README.md b/conversation/javascript/http/README.md new file mode 100644 index 000000000..6bdc8eb5e --- /dev/null +++ b/conversation/javascript/http/README.md @@ -0,0 +1,99 @@ +# Dapr Conversation API (Go 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. + +This quickstart includes one app: + +- `conversation.js`, 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 files](https://docs.dapr.io/developing-applications/local-development/multi-app-dapr-run/multi-app-overview/) with `dapr run -f .`. + +This example uses the default LLM Component provided by Dapr which simply echoes the input provided, for testing purposes. Here are other [supported Conversation components](https://v1-15.docs.dapr.io/reference/components-reference/supported-conversation/). + +1. Install dependencies: + + + +```bash +cd ./conversation +npm install +``` + + + +2. Open a new terminal window and run the multi app run template: + + + +```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? +``` + + + +3. Stop and clean up application processes. + + + +```bash +dapr stop -f . +``` + + + +## Run the app individually + +1. Open a terminal and run the `conversation` app. Build the dependencies if you haven't already. + +```bash +cd ./conversation +npm install +``` + +2. Run the Dapr process alongside the application. + +```bash +dapr run --app-id conversation --resources-path ../../../components/ -- npm run start +``` + +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? +``` diff --git a/conversation/javascript/http/conversation/index.js b/conversation/javascript/http/conversation/index.js new file mode 100644 index 000000000..8cdf0867d --- /dev/null +++ b/conversation/javascript/http/conversation/index.js @@ -0,0 +1,43 @@ +const conversationComponentName = "echo"; + +async function main() { + const daprHost = process.env.DAPR_HOST || "http://localhost"; + const daprHttpPort = process.env.DAPR_HTTP_PORT || "3500"; + + const inputBody = { + name: "echo", + inputs: [{ message: "What is dapr?" }], + parameters: {}, + metadata: {}, + }; + + const reqURL = `${daprHost}:${daprHttpPort}/v1.0-alpha1/conversation/${conversationComponentName}/converse`; + + try { + const response = await fetch(reqURL, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(inputBody), + }); + + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + + console.log("Input sent: What is dapr?"); + + const data = await response.json(); + const result = data.outputs[0].result; + console.log("Output response:", result); + } catch (error) { + console.error("Error:", error.message); + process.exit(1); + } +} + +main().catch((error) => { + console.error("Unhandled error:", error); + process.exit(1); +}); diff --git a/conversation/javascript/http/conversation/package-lock.json b/conversation/javascript/http/conversation/package-lock.json new file mode 100644 index 000000000..90b1295f1 --- /dev/null +++ b/conversation/javascript/http/conversation/package-lock.json @@ -0,0 +1,13 @@ +{ + "name": "conversation", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "conversation", + "version": "1.0.0", + "license": "ISC" + } + } +} diff --git a/conversation/javascript/http/conversation/package.json b/conversation/javascript/http/conversation/package.json new file mode 100644 index 000000000..86ceb8b14 --- /dev/null +++ b/conversation/javascript/http/conversation/package.json @@ -0,0 +1,14 @@ +{ + "name": "conversation", + "version": "1.0.0", + "description": "", + "main": "index.js", + "type": "module", + "scripts": { + "start": "node index.js", + "start:dapr": "dapr run --app-id conversation --resources-path ../../../components/ -- npm start" + }, + "keywords": [], + "author": "", + "license": "ISC" +} diff --git a/conversation/javascript/http/dapr.yaml b/conversation/javascript/http/dapr.yaml new file mode 100644 index 000000000..4c88690d0 --- /dev/null +++ b/conversation/javascript/http/dapr.yaml @@ -0,0 +1,8 @@ +version: 1 +common: + resourcesPath: ../../components/ +apps: + - appDirPath: ./conversation/ + appID: conversation + daprHTTPPort: 3502 + command: ["npm", "run", "start"] diff --git a/conversation/javascript/http/makefile b/conversation/javascript/http/makefile new file mode 100644 index 000000000..e7a8826bf --- /dev/null +++ b/conversation/javascript/http/makefile @@ -0,0 +1,2 @@ +include ../../../docker.mk +include ../../../validate.mk \ No newline at end of file From f5a6d24bbe15e760876f56238c0fe4883f6b8c53 Mon Sep 17 00:00:00 2001 From: Constantin Chirila Date: Tue, 4 Feb 2025 09:34:45 +0000 Subject: [PATCH 2/5] Minor change Signed-off-by: Constantin Chirila --- conversation/javascript/http/dapr.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/conversation/javascript/http/dapr.yaml b/conversation/javascript/http/dapr.yaml index 4c88690d0..6ff5c7ddd 100644 --- a/conversation/javascript/http/dapr.yaml +++ b/conversation/javascript/http/dapr.yaml @@ -2,7 +2,7 @@ version: 1 common: resourcesPath: ../../components/ apps: - - appDirPath: ./conversation/ - appID: conversation + - appID: conversation + appDirPath: ./conversation/ daprHTTPPort: 3502 command: ["npm", "run", "start"] From 76e2723374b615f9f0c0c5ee5505f83b80adb875 Mon Sep 17 00:00:00 2001 From: Constantin Chirila Date: Tue, 4 Feb 2025 16:20:25 +0000 Subject: [PATCH 3/5] Updated Readme Signed-off-by: Constantin Chirila --- conversation/javascript/http/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conversation/javascript/http/README.md b/conversation/javascript/http/README.md index 6bdc8eb5e..207602f5d 100644 --- a/conversation/javascript/http/README.md +++ b/conversation/javascript/http/README.md @@ -1,4 +1,4 @@ -# Dapr Conversation API (Go HTTP) +# Dapr Conversation API (JS 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. From 3ae8e4dc3add11bb87324cf9c6b25a2a3ede371f Mon Sep 17 00:00:00 2001 From: Constantin Chirila Date: Tue, 4 Feb 2025 16:20:49 +0000 Subject: [PATCH 4/5] Removed redundant check on response Signed-off-by: Constantin Chirila --- conversation/javascript/http/conversation/index.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/conversation/javascript/http/conversation/index.js b/conversation/javascript/http/conversation/index.js index 8cdf0867d..51a6c7ab3 100644 --- a/conversation/javascript/http/conversation/index.js +++ b/conversation/javascript/http/conversation/index.js @@ -22,10 +22,6 @@ async function main() { body: JSON.stringify(inputBody), }); - if (!response.ok) { - throw new Error(`HTTP error! status: ${response.status}`); - } - console.log("Input sent: What is dapr?"); const data = await response.json(); From c0cd69e95903b84b529a51d57419bad573ed034b Mon Sep 17 00:00:00 2001 From: Constantin Chirila Date: Tue, 4 Feb 2025 16:21:59 +0000 Subject: [PATCH 5/5] Updated Readme Signed-off-by: Constantin Chirila --- conversation/javascript/http/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/conversation/javascript/http/README.md b/conversation/javascript/http/README.md index 207602f5d..fef3c5548 100644 --- a/conversation/javascript/http/README.md +++ b/conversation/javascript/http/README.md @@ -8,7 +8,7 @@ Visit [this](https://v1-15.docs.dapr.io/developing-applications/building-blocks/ This quickstart includes one app: -- `conversation.js`, responsible for sending an input to the underlying LLM and retrieving an output. +- `index.js`, responsible for sending an input to the underlying LLM and retrieving an output. ## Run the app with the template file @@ -75,7 +75,7 @@ dapr stop -f . ## Run the app individually -1. Open a terminal and run the `conversation` app. Build the dependencies if you haven't already. +1. Open a terminal and navigate to the `conversation` app. Install the dependencies if you haven't already. ```bash cd ./conversation