How to use GPT API as middleware

How to use GPT API as middleware

The problem

The sample that I am going to showcase is not a complex one, but it is really useful and can be used for more powerful automation.

If you have followed my previous blog post you will know that I am currently creating my own inventory management system and automating WooCommerce for my website.

Initially, before finding out and learning about the WooCommerce API I used GPT API to extract the data from the email and transform that data into the schema that the post office service was expecting

Let's talk about and try to understand middleware first

"Middleware" refers to software that acts as an intermediary layer between different software applications or components. It provides a set of services that allow those components to communicate with each other or perform certain tasks without needing to understand the intricate details of each component's operation. Middleware can be likened to "glue" that connects different pieces of software together.

Middleware can provide different functions, some of which are:

  • Communication: Enabling data exchange between distributed components, often using methods like message passing.
  • Authentication and Authorization: Ensuring that only permitted users can access certain services.
  • Transaction Management: Ensuring that a series of operations either complete successfully or are entirely rolled back, maintaining data integrity.
  • Load Balancing: Distributing tasks across multiple servers to optimize resource utilization and maximize throughput.
  • Session Management: Maintaining state information across multiple requests from the same user.

In essence, middleware streamlines the development process, provides essential services that applications often need, and enhances the scalability, reliability, and maintainability of software systems.

We are using GPT to convert unstructured data from one system into a structured format suitable for another system, in our case Post Office API schema.

The idea is to use GPT to always generate the same JSON response, which we could easily use in Power Automate and Power Automate Desktop.

We can do this by telling GPT the JSON Schema that we want our response to be in.

Later we can use that same Schema to validate the response and easily parse and use its values and map them to other services.

Query

We need to create a proper System role query that the model is going to follow. With a little bit of trial and error, I have found the best approach to force GPT to give us a response in JSON.

We need to separate what we want as a result into multiple steps so that the model is going to understand it better.

Since I have 2 entities that I want to extract from the email (Customer Information and Order Information) I describe fields for each object in my query. (those will be defined in Step 1 and Step 2)

Step 3 will be the JSON Schema that we want our response to be generated in.

Be very careful with this step since we are going to use the same Schema in Power Automate to validate the response that has been provided.

The query looks something like this:

Implementation:

The implementation in Power Automate is pretty straightforward. We are going to pull unread emails from the mailbox and we are going to extract the details about the customer and the order that he/she has placed.

We could have done this using string manipulation but it would take a lot more time and I am not even sure that we can configure all the business rules.

Part 1: We are transforming the HTML Body to readable text and we setting that text in the User role

(Tip - Create a Config file where you can define the query, it is much easier to maintain)


Part 2: We need to extract the response from the HTTP call and parse it using Parse JSON activity. We can get the response from the activity with the following expression:

outputs('Call_GPT')['body']?['choices'][0]['message']?['content']        

Besides that, we are going to put the Schema that we placed in the GPT call. This way if it happens to generate a wrong response (in my experience it happens in 1 out of 20 responses) we can configure a retry mechanism or error handling.


After that, we will have all the information that we need to call other systems and schedule our order.

Let's compare the values that are in our mailbox and the values that we have received from the service:

Image from Email


Response from GPT service

We can see that the information is matching the values that we have received.

Consequently, I have put the Schema of another service, so I could just call that service and schedule the order.

Conclusion:

In our modern technological landscape, the versatility and power of middleware cannot be overstated. My journey in automating WooCommerce for my website offered me hands-on experience in harnessing the capabilities of the GPT API, effectively turning it into a middleware to extract and structure email data. Through this process, not only did I find a more efficient method than traditional string manipulation, but I also ensured a consistent JSON response, seamlessly integrating it with Power Automate. The key lies in the careful crafting of a JSON Schema and system role queries. By bridging the gap between unstructured email content and structured system requirements using GPT, the potential of AI-driven middleware in simplifying complex tasks and enhancing automation efforts becomes abundantly clear. As we move forward in the era of digital transformation, tools like the GPT API remain pivotal in reshaping the way we approach and solve challenges in our software ecosystems.

Mateusz Macheta

?? RPA Developer / Craftware

1 年

That's great that you are using your daily challenges, figuring out how to overcome them and sharing with community ??

要查看或添加评论,请登录

Sr?an Su?的更多文章

社区洞察

其他会员也浏览了