diff --git a/conversation/components/conversation.yaml b/conversation/components/conversation.yaml new file mode 100644 index 000000000..efb651fef --- /dev/null +++ b/conversation/components/conversation.yaml @@ -0,0 +1,7 @@ +apiVersion: dapr.io/v1alpha1 +kind: Component +metadata: + name: echo +spec: + type: conversation.echo + version: v1 diff --git a/conversation/go/http/README.md b/conversation/go/http/README.md index eb07be211..462de6f93 100644 --- a/conversation/go/http/README.md +++ b/conversation/go/http/README.md @@ -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: + + + +```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? +``` + + diff --git a/conversation/go/http/components/conversation.yaml b/conversation/go/http/components/conversation.yaml deleted file mode 100644 index e69de29bb..000000000 diff --git a/conversation/go/http/conversation/conversation.go b/conversation/go/http/conversation/conversation.go new file mode 100644 index 000000000..fbf8860b2 --- /dev/null +++ b/conversation/go/http/conversation/conversation.go @@ -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) + +} diff --git a/conversation/go/http/conversation/main.go b/conversation/go/http/conversation/main.go deleted file mode 100644 index 4e24e5022..000000000 --- a/conversation/go/http/conversation/main.go +++ /dev/null @@ -1,48 +0,0 @@ -package main - -import ( - "bytes" - "encoding/json" - "fmt" - "io" - "net/http" - "os" - "time" -) - -const stateStoreComponentName = "statestore" - -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, - } - - input, _ := json.Marshal([]map[string]string{ - { - "inputs": "what is Dapr", - }, - }) - - res, err := client.Post(daprHost+":"+daprHttpPort+"/v1.0-alpha1/conversation/"+stateStoreComponentName+"/converse", "application/json", bytes.NewReader(input)) - if err != nil { - panic(err) - } - - output, err := io.ReadAll(res.Request.Response.Body) - if err != nil { - panic(err) - } - fmt.Println("Retrieved response:", string(output)) - - res.Body.Close() - fmt.Println("Input sent sent:", input) -} diff --git a/conversation/go/http/dapr.yaml b/conversation/go/http/dapr.yaml index e355de4d5..211d9cfd2 100644 --- a/conversation/go/http/dapr.yaml +++ b/conversation/go/http/dapr.yaml @@ -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", "."] diff --git a/conversation/go/sdk/conversation/main.go b/conversation/go/sdk/conversation/conversation.go similarity index 100% rename from conversation/go/sdk/conversation/main.go rename to conversation/go/sdk/conversation/conversation.go diff --git a/conversation/go/sdk/dapr.yaml b/conversation/go/sdk/dapr.yaml index e355de4d5..046b7bde3 100644 --- a/conversation/go/sdk/dapr.yaml +++ b/conversation/go/sdk/dapr.yaml @@ -2,6 +2,6 @@ version: 1 apps: - appDirPath: ./conversation/ appID: conversation - appPort: 6300 - daprHTTPPort: 6380 + appPort: 3500 + daprHTTPPort: 3501 command: ["go", "run", "."]