-
Notifications
You must be signed in to change notification settings - Fork 530
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Python Conversation http Quickstart (#1150)
* Python Conversation API - HTTP quickstart Signed-off-by: Fernando Rocha <fernando@diagrid.io> * Update conversation/python/http/dapr.yaml Co-authored-by: Marc Duiker <marcduiker@users.noreply.github.com> Signed-off-by: Fernando Rocha <fernandorsl@gmail.com> * Update conversation/python/http/dapr.yaml Co-authored-by: Marc Duiker <marcduiker@users.noreply.github.com> Signed-off-by: Fernando Rocha <fernandorsl@gmail.com> --------- Signed-off-by: Fernando Rocha <fernando@diagrid.io> Signed-off-by: Fernando Rocha <fernandorsl@gmail.com> Co-authored-by: Marc Duiker <marcduiker@users.noreply.github.com>
- Loading branch information
1 parent
6993063
commit 7e0433e
Showing
5 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: | ||
|
||
<!-- STEP | ||
name: Install Python dependencies | ||
--> | ||
|
||
```bash | ||
cd ./conversation | ||
pip3 install -r requirements.txt | ||
cd .. | ||
``` | ||
|
||
2. Open a new terminal window and run the multi app run template: | ||
|
||
<!-- STEP | ||
name: Run multi app run template | ||
expected_stdout_lines: | ||
- '== APP == INFO:root:Input sent: What is dapr?' | ||
- '== APP == INFO:root:Output response: What is dapr?' | ||
expected_stderr_lines: | ||
output_match_mode: substring | ||
match_order: none | ||
background: true | ||
sleep: 15 | ||
timeout_seconds: 30 | ||
--> | ||
|
||
```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? | ||
``` | ||
|
||
<!-- END_STEP --> | ||
|
||
2. Stop and clean up application processes. | ||
|
||
<!-- STEP | ||
name: Stop multi-app run | ||
sleep: 5 | ||
--> | ||
|
||
```bash | ||
dapr stop -f . | ||
``` | ||
|
||
<!-- END_STEP --> | ||
|
||
## 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? | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
requests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
version: 1 | ||
common: | ||
resourcesPath: ../../components/ | ||
apps: | ||
- appID: conversation | ||
appDirPath: ./conversation/ | ||
command: ["python3", "app.py"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
include ../../../docker.mk | ||
include ../../../validate.mk |