From aed1338071c43cd1235ab880f5e2e3fed62b45d4 Mon Sep 17 00:00:00 2001 From: Fernando Rocha Date: Tue, 28 Jan 2025 11:29:02 -0800 Subject: [PATCH 1/3] Python Conversation API - HTTP quickstart Signed-off-by: Fernando Rocha --- conversation/python/http/README.md | 97 +++++++++++++++++++ conversation/python/http/conversation/app.py | 31 ++++++ .../python/http/conversation/requirements.txt | 1 + conversation/python/http/dapr.yaml | 7 ++ conversation/python/http/makefile | 2 + 5 files changed, 138 insertions(+) create mode 100644 conversation/python/http/README.md create mode 100644 conversation/python/http/conversation/app.py create mode 100644 conversation/python/http/conversation/requirements.txt create mode 100644 conversation/python/http/dapr.yaml create mode 100644 conversation/python/http/makefile diff --git a/conversation/python/http/README.md b/conversation/python/http/README.md new file mode 100644 index 000000000..2c0139036 --- /dev/null +++ b/conversation/python/http/README.md @@ -0,0 +1,97 @@ +# Dapr Conversation API (Python 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: + +- `app.py`, 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 +pip3 install -r requirements.txt +cd .. +``` + +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? +``` + + + +2. Stop and clean up application processes. + + + +```bash +dapr stop -f . +``` + + + +## Run the app with the Dapr CLI + +1. Install dependencies: + +Open a terminal and run: + +```bash +cd ./conversation +pip3 install -r requirements.txt +``` + +2. Run the application: + +```bash +dapr run --app-id conversation --resources-path ../../../components -- python3 app.py +``` + +You should see the output: + +```bash +== APP == INFO:root:Input sent: What is dapr? +== APP == INFO:root:Output response: What is dapr? +``` diff --git a/conversation/python/http/conversation/app.py b/conversation/python/http/conversation/app.py new file mode 100644 index 000000000..ae4fa2388 --- /dev/null +++ b/conversation/python/http/conversation/app.py @@ -0,0 +1,31 @@ +import logging +import requests +import os + +logging.basicConfig(level=logging.INFO) + +base_url = os.getenv('BASE_URL', 'http://localhost') + ':' + os.getenv( + 'DAPR_HTTP_PORT', '3500') + +CONVERSATION_COMPONENT_NAME = 'echo' + +input = { + 'name': 'echo', + 'inputs': [{'message':'What is dapr?'}], + 'parameters': {}, + 'metadata': {} + } + +# Send input to conversation endpoint +result = requests.post( + url='%s/v1.0-alpha1/conversation/%s/converse' % (base_url, CONVERSATION_COMPONENT_NAME), + json=input +) + +logging.info('Input sent: What is dapr?') + +# Parse conversation output +data = result.json() +output = data["outputs"][0]["result"] + +logging.info('Output response: ' + output) \ No newline at end of file diff --git a/conversation/python/http/conversation/requirements.txt b/conversation/python/http/conversation/requirements.txt new file mode 100644 index 000000000..f2293605c --- /dev/null +++ b/conversation/python/http/conversation/requirements.txt @@ -0,0 +1 @@ +requests diff --git a/conversation/python/http/dapr.yaml b/conversation/python/http/dapr.yaml new file mode 100644 index 000000000..8e047f35a --- /dev/null +++ b/conversation/python/http/dapr.yaml @@ -0,0 +1,7 @@ +version: 1 +common: + resourcesPath: ../../components/ +apps: + - appDirPath: ./conversation/ + appID: conversation + command: ["python3", "app.py"] diff --git a/conversation/python/http/makefile b/conversation/python/http/makefile new file mode 100644 index 000000000..e7a8826bf --- /dev/null +++ b/conversation/python/http/makefile @@ -0,0 +1,2 @@ +include ../../../docker.mk +include ../../../validate.mk \ No newline at end of file From 8237b54cb08251b5ee9dec3f0be3f272756ee3c2 Mon Sep 17 00:00:00 2001 From: Fernando Rocha Date: Wed, 29 Jan 2025 08:54:42 -0800 Subject: [PATCH 2/3] Update conversation/python/http/dapr.yaml Co-authored-by: Marc Duiker Signed-off-by: Fernando Rocha --- conversation/python/http/dapr.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conversation/python/http/dapr.yaml b/conversation/python/http/dapr.yaml index 8e047f35a..27eb7c65e 100644 --- a/conversation/python/http/dapr.yaml +++ b/conversation/python/http/dapr.yaml @@ -3,5 +3,5 @@ common: resourcesPath: ../../components/ apps: - appDirPath: ./conversation/ - appID: conversation + appDirPath: ./conversation/ command: ["python3", "app.py"] From 3ce7782a62eae2968ea2ed440bd5b2c771d0888b Mon Sep 17 00:00:00 2001 From: Fernando Rocha Date: Wed, 29 Jan 2025 08:54:51 -0800 Subject: [PATCH 3/3] Update conversation/python/http/dapr.yaml Co-authored-by: Marc Duiker Signed-off-by: Fernando Rocha --- conversation/python/http/dapr.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conversation/python/http/dapr.yaml b/conversation/python/http/dapr.yaml index 27eb7c65e..d2002c929 100644 --- a/conversation/python/http/dapr.yaml +++ b/conversation/python/http/dapr.yaml @@ -2,6 +2,6 @@ version: 1 common: resourcesPath: ../../components/ apps: - - appDirPath: ./conversation/ + - appID: conversation appDirPath: ./conversation/ command: ["python3", "app.py"]