How to build my first module With SDK and Webhooks

In this article, we will describe how to build your first module for FinanceGPT so you can run your own code against the chat.

As an example, we are going to deploy the most simple app from this repository the demo app: GitHub - Unique-AG/sdk-deploy-template: Template repository for repos that are used to deploy Python modules developed with the Unique SDK to Azure Container Apps.


Table of Contents:

Ensure a reachable Webhook endpoint (not needed in local dev setup)

To do that we need to create a connection between Unique FinanceGPT and your development environment. For this we use ngrok but you can also use any other way to get a webhook redirected to your development machine. E.g. Azure functions or other mechanisms. It is important that the data can connect from your FinanceGPT environment to your local machine somehow.

Here is an example with ngrok: this forwards the https://cbac-178-197-218-164.ngrok-free.app to your local 5001 port, where the apps will run.

ngrok http 5001 --- ngrok (Ctrl+C to quit) K8s Gateway API support available now: https://ngrok.com/r/k8sgb Session Status online Account Andreas Hauri (Plan: Free) Update update available (version 3.9.0, Ctrl-U to update) Version 3.8.0 Region Europe (eu) Latency 13ms Web Interface http://127.0.0.1:4040 Forwarding https://cbac-178-197-218-164.ngrok-free.app -> http://localhost:5001 Connections ttl opn rt1 rt5 p50 p90 543 0 0.00 0.00 2.40 45.26 HTTP Requests ------------- POST /webhook 200 OK POST /webhook 200 OK

 

Setting up the app

Now login to Unique and go to the app registration. This requires admin rights to do so.

Screenshot 2024-05-15 at 22.36.40.png

Create a new app

Screenshot 2024-05-15 at 22.37.33.png

 

Activate the app

Add an endpoint

Make sure you know the URL that your machine is exposing, here it is the ngrok URL but it is webhook because the demo application exposes that.
In development was chosen so the webhook does not expire on too many errors

The unique.chat.external-module.chosen is the event we are expecting.

 

 

Now you can see that it created a key named using_*** which you can expose. This is used for validating on the demo application side that the request is coming from this unique instance and from nowhere else.

Create an API key


Now you create an API KEY

Dont worry my app will be deleted after writing this so you can ignore it.

 

You will only see this once! Copy the key and store it SECURELY. This has the potential to leak a lot of data to the world.

Remember all the variables for the Python App

Now we need all this info (yes this is the .env variable for your app!):

API_KEY=ukey_qXqoh5lGhTa399CPNHHLUqnkIaWXlwRwAWtPJSrubyM APP_ID=app_ueltalg142m341pskcankxk0 API_BASE=https://gateway.oleole.unique.app/public/chat ENDPOINT_SECRET=usig_ad4wJ17fETob6uh0CYasNVn_aYtU1MRH4YnMrMiXjNE

API Base must be set correctly


API_BASE is very important to do it right:

Local dev setup:

  • Base url: http://localhost:8092/public

Remote setup:

  1. no trailing / be careful

  2. public/chat (not the other way around)

  3. make sure you use the correct host! you can look it up in any graphql request on the front end like in the picture below (this is the one from the next multi-tenant):

     

Defining the module

Create a Module Template in the UI

Use the “AI Module Templates” section in the Unique solution’s frontend to create a custom module template for your SDK module. Documentation on this can be found here: AI Module Templates | Creating an AI Module Template.

Create a Module Template via API (Deprecated)

Now we need to make sure that you can create the module that will react to the event in a space.
For this, we need a bit of API magic for the moment.

Here is the simplest way. Copy your bearer Token from the browser illustrated in the picture below:

Run the GraphQL via Postman

Now you will need to execute a graphql request like this one for example in Postman:

  1. Make sure you have set the correct URL:

    • Local dev setup, use http://localhost:8092/graphql

    • For remote setup, see the graphql request from the browser like above

  2. The name here is very important. Your app will receive a payload that contains this name should it be called upon.

  3. the descriptions are also very important. Here you see that this function will only be called if asked for a demo. This is up to you to provide and also test how well it reacts to its intent.

And make sure you have set the correct Bearer token like in the example below, which is the token copied from above.

There is no Bearer in front and no carriage return at the end!

Here is the mutation as text:

mutation CreateModuleTemplate { createModuleTemplate( input: { configuration: { languageModel: "AZURE_GPT_35_TURBO_16K" } isExternal: true name: "DemoApp" templateName: "Demo App Template Name" toolDefinition: { type: "function" function: { name: "DemoApp" parameters: { type: "object" properties: { instruction: { type: "string" description: "The instruction given by the employee e.g., 'Demo my App please' or 'Tell me about the demo'." } } } description: "This function is specifically designed for demos" } } weight: 5500 } ) { templateName companyId } }

Use the Module in a space

Now if you go into the spaces you can choose this as a module so:

Select the Email Writer module first:

Go ahead and publish it. Dont worry, only you can see this space for now.

Select the created Module Template

See the last entry and select this (Demo App Template Name) and also for example the email writer module.

Publish again.

Running your Python app

 

Now you are all set to run your app locally so it can react to chat messages:

In the location where you cloned the template repo GitHub - Unique-AG/sdk-deploy-template: Template repository for repos that are used to deploy Python modules developed with the Unique SDK to Azure Container Apps.

The .env file

Go into the directory of the assistant_demo app and add the .env file with your settings:

  • All of these are different for you! Make sure they are correct biggest source of mistakes usually!

  • Look at the prefixes of the key app using

  • Remember how a correct API_BASE is defined

 

Now go to the terminal and execute the following in that folder:

Modify the App to act as a Module

We can now edit the app and make some modifications:

This is the app.py of the example we need to modify it in a few places:


Check now to make sure that the app does:

  1. Not react to unique.chat.user-message.created but to a unique.chat.external-module.chosen event.

  2. React only if it is the defined DemoApp

  3. Ensure that the assistant_id is taken from the event and not from the env variables

  4. The text is now on the user message object event["payload"]["userMessage"]["text"]

  5. We also echo the message back to the user

Here the last part changes from lines 72 to 89:

Save and the flask app should automatically restart.

Now chat within the created Demo space

What you see in the console of flask:

 

The frontend of Unique:

 


Author

@Andreas Hauri



© 2024 Unique AG. All rights reserved. Privacy PolicyTerms of Service