2

Integrating GPT Embedding API with SAP CAP for Message Classification

 1 year ago
source link: https://blogs.sap.com/2023/06/20/integrating-gpt-embedding-api-with-sap-cap-for-message-classification/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
June 20, 2023 3 minute read

Integrating GPT Embedding API with SAP CAP for Message Classification

This blog post is part of a series of technical enablement sessions on SAP BTP for Industries. Check the full calendar here to watch the recordings of past sessions and register for the upcoming ones! The replay of this session available here.

Authors: Yatsea Li, Alice Magnani, Edward Neveux, Jacob Tan

Introduction

Large Language Models (LLMs) like ChatGPT and GPT-4 have gained significant popularity with emergent capabilities. In this fourth session of the series, we’ll explore the potentials of GPT in SAP ecosystem. In this series, we will discuss how SAP partners can leverage this powerful AI technology in combination with SAP technologies to accelerate their solution development processes, and create more intelligent solutions on SAP Business Technology Platform (SAP BTP).

Here list the full blog post series of Exploring the potential of GPT in SAP ecosystem:

In this blog post (the last of the series), we will cover is how to implement message intent classification in our intelligent ticketing solution. We will achieve it by using the GPT embedding API.

Customer message classification with GPT embeddings

As we have discussed in previous blog post about GPT in SAP ecosystem, text embeddings allow to represent words or sentences in vectorial form, and to introduce a concept of distance between the meaning of sentences. We will use distance to classify incoming customer messages into a few predefined categories, as in the example below.

50-message-classification-embedding.png

The first step to achieve this is understanding how to use the GPT embedding API. The video below shows how to get the embedding for one of the message categories we have chosen. It’s necessary to submit a title and brief description in the payload that we post to the dedicated API, and we will get the embedding array as a response.

247041904-7aeb93d6-be83-4d34-b656-a585b221da54.gif

Below, you can see how to do the same with NodeJS code. As we have seen earlier with the chat completion API, there are different ways we can implement the API call. Here we show three options, with direct HTTP call, Azure OpenAI Node module, or LangChain.The NodeJS sample code of GPT Embedding API available here.

Option 1: direct HTTP call with axios node module

52-embedding-axios.png

Option 2: azure-openai node module

53-embedding-azure-openai.png

Option 3: LangChain.js

54-embedding-langchain.png

To finalize our message classification, we need to get the embedding for every message category and store them in the HANA DB underlying the CAP application.

55-message-intent-embedding.png
Then, we need to add a piece of code in the application to extract the embedding for every incoming customer message and compare it with the categories’ embedding to find the closest one. Here below is our implementation. Notice that to evaluate the distance between embeddings we have used a metric called cosine similarity, with is widely used for similar tasks in the natural language processing domain.
56-cosine-distance.png

Further resources

Wrap up

We have seen that embedding can be helpful with customer message classification in our sample among many use cases, such as text searching, answering question with custom knowledge base, clustering, anomaly detection, recommendation etc. Here conclude our blog post series of exploring the potential of GPT in SAP ecosystem, which is just the fourth of our SAP BTP technical enablement sessions for industry. We hope you have enjoyed the reading.

Check the full calendar, register to the upcoming sessions and review the already recorded sessions here!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK