-
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.
Fixing Conversation HTTP example for RC5
Signed-off-by: Fernando Rocha <[email protected]>
- Loading branch information
Showing
8 changed files
with
130 additions
and
52 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,7 @@ | ||
apiVersion: dapr.io/v1alpha1 | ||
kind: Component | ||
metadata: | ||
name: echo | ||
spec: | ||
type: conversation.echo | ||
version: v1 |
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 |
---|---|---|
@@ -1,2 +1,48 @@ | ||
# Dapr Conversation | ||
|
||
In this quickstart, you'll send an input to a 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. If you are looking for the example using the Dapr Client SDK (recommended) [click here](../sdk/). | ||
This quickstart includes one app: | ||
|
||
- `conversation.go`, responsible for sending and 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 for testing purposes. Here are other [supported Conversation components](https://v1-15.docs.dapr.io/reference/components-reference/supported-conversation/). | ||
|
||
Open a new terminal window and run the multi app run template: | ||
|
||
<!-- STEP | ||
name: Run multi app run template | ||
expected_stdout_lines: | ||
- '== APP - conversation == Input sent: What is dapr?' | ||
- '== APP - conversation == Output response: What is dapr?' | ||
expected_stderr_lines: | ||
output_match_mode: substring | ||
match_order: none | ||
background: false | ||
sleep: 5 | ||
timeout_seconds: 120 | ||
--> | ||
|
||
```bash | ||
dapr run -f . | ||
``` | ||
|
||
The terminal console output should look similar to this, where: | ||
|
||
- The app send an input `What is dapr?` to the `echo` LLM. | ||
- The LLM echoes `What is dapr?`. | ||
|
||
```text | ||
== APP - conversation == Input sent: What is dapr? | ||
== APP - conversation == Output response: What is dapr? | ||
``` | ||
|
||
<!-- END_STEP --> |
Empty file.
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,71 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"log" | ||
"net/http" | ||
"os" | ||
"strings" | ||
"time" | ||
) | ||
|
||
const conversationComponentName = "echo" | ||
|
||
func main() { | ||
daprHost := os.Getenv("DAPR_HOST") | ||
if daprHost == "" { | ||
daprHost = "http://localhost" | ||
} | ||
daprHttpPort := os.Getenv("DAPR_HTTP_PORT") | ||
if daprHttpPort == "" { | ||
daprHttpPort = "3500" | ||
} | ||
|
||
client := http.Client{ | ||
Timeout: 15 * time.Second, | ||
} | ||
|
||
var inputBody = `{ | ||
"name": "echo", | ||
"inputs": [{"message":"What is dapr?"}], | ||
"parameters": {}, | ||
"metadata": {} | ||
}` | ||
|
||
reqURL := daprHost + ":" + daprHttpPort + "/v1.0-alpha1/conversation/" + conversationComponentName + "/converse" | ||
|
||
req, err := http.NewRequest("POST", reqURL, strings.NewReader(inputBody)) | ||
if err != nil { | ||
log.Fatal(err.Error()) | ||
} | ||
|
||
req.Header.Set("Content-Type", "application/json") | ||
|
||
// Send a request to the echo LLM component | ||
res, err := client.Do(req) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
defer res.Body.Close() | ||
|
||
fmt.Println("Input sent: What is dapr?") | ||
|
||
bodyBytes, err := io.ReadAll(res.Body) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// Unmarshal the response | ||
var data map[string][]map[string]string | ||
err = json.Unmarshal(bodyBytes, &data) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
result := data["outputs"][0]["result"] | ||
fmt.Println("Output response:", result) | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
version: 1 | ||
common: | ||
resourcesPath: ../../components/ | ||
apps: | ||
- appDirPath: ./conversation/ | ||
appID: conversation | ||
appPort: 6300 | ||
daprHTTPPort: 6380 | ||
appPort: 3500 | ||
daprHTTPPort: 3501 | ||
command: ["go", "run", "."] |
File renamed without changes.
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