Skip to content

Commit

Permalink
Fixing Conversation HTTP example for RC5
Browse files Browse the repository at this point in the history
Signed-off-by: Fernando Rocha <[email protected]>
  • Loading branch information
rochabr committed Jan 21, 2025
1 parent e87f407 commit 6395fad
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 52 deletions.
7 changes: 7 additions & 0 deletions conversation/components/conversation.yaml
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
46 changes: 46 additions & 0 deletions conversation/go/http/README.md
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.
71 changes: 71 additions & 0 deletions conversation/go/http/conversation/conversation.go
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)

}
48 changes: 0 additions & 48 deletions conversation/go/http/conversation/main.go

This file was deleted.

6 changes: 4 additions & 2 deletions conversation/go/http/dapr.yaml
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.
4 changes: 2 additions & 2 deletions conversation/go/sdk/dapr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ version: 1
apps:
- appDirPath: ./conversation/
appID: conversation
appPort: 6300
daprHTTPPort: 6380
appPort: 3500
daprHTTPPort: 3501
command: ["go", "run", "."]

0 comments on commit 6395fad

Please sign in to comment.