From e01cc78df5046e6d6623f4d533f3592bd3612c65 Mon Sep 17 00:00:00 2001 From: Constantin Chirila Date: Tue, 4 Feb 2025 21:15:18 +0000 Subject: [PATCH] Conversation JS HTTP Quickstart (#1154) * Added Quickstart for Javascript Conversation HTTP api Signed-off-by: Constantin Chirila * Minor change Signed-off-by: Constantin Chirila * Updated Readme Signed-off-by: Constantin Chirila * Removed redundant check on response Signed-off-by: Constantin Chirila * Updated Readme Signed-off-by: Constantin Chirila --------- Signed-off-by: Constantin Chirila Co-authored-by: Constantin Chirila --- conversation/javascript/http/README.md | 99 +++++++++++++++++++ .../javascript/http/conversation/index.js | 39 ++++++++ .../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, 175 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..fef3c5548 --- /dev/null +++ b/conversation/javascript/http/README.md @@ -0,0 +1,99 @@ +# 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. + +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: + +- `index.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 navigate to the `conversation` app. Install 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..51a6c7ab3 --- /dev/null +++ b/conversation/javascript/http/conversation/index.js @@ -0,0 +1,39 @@ +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), + }); + + 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..6ff5c7ddd --- /dev/null +++ b/conversation/javascript/http/dapr.yaml @@ -0,0 +1,8 @@ +version: 1 +common: + resourcesPath: ../../components/ +apps: + - appID: conversation + appDirPath: ./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