Salesforce Langchain Python OpenAI AWS Lambda1

Converting Salesforce Data into Embeddings with OpenAI and AWS Lambda

Last week on Dec 7, I had an opportunity to speak in the Salesforce Alpharetta Developer Group meeting. This is a 3 part blog series, explaining samples & demos I covered in that group meet. To get information about future sessions, you can join this group and also the chatter group to get content.

Let’s start with the basics.

What is Embedding?

Imagine you have a big box of crayons with many different colors. Each crayon is like a word or a picture. Now, suppose you want to organize these crayons so that similar colors are close to each other. For example, different shades of blue are grouped together, and so are the shades of red. This makes it easier for you to find and compare colors.

In the world of AI, embeddings do something similar but with words, pictures, or other kinds of information. Instead of crayons, we have lots of data, like words in a book or images. An embedding is a special method that sorts this data into groups of similar things.

For example, in a book, the words “happy” and “joyful” might be close together in this sorted space because they mean similar things. This helps AI models quickly understand and work with lots of information, like helping you find a book you might like or understanding what you mean when you talk to a computer.

So, embeddings are like a smart way of organizing a huge box of crayons (or data) so that similar things are close together, making it much easier and faster to use them for different tasks!

If we want to work with AI on Salesforce, very first step that needs to happen is to convert your data to embedding. In this case we would convert Salesforce data to embedding from lambda function using OpenAI API. Eventually, we would use some kind of library like langChain that can retrieve similar data to enrich prompts and get more contextual responses which otherwise would not be possible, this process is known as RAG – Retrieval-Augmented Generation.

Prerequisite

  • Salesforce account with API access
  • AWS account with Lambda and IAM access
  • OpenAI API key
  • Basic understanding of Python programming

AWS Lambda requires external libraries to be included as layers. You’ll need to create layers for OpenAI, numpy, and pandas

  • Package your Python libraries in a ZIP file.
  • Upload the ZIP file to Lambda Layers.
  • Attach these layers to your Lambda function.

Complete Source Code & Video

Below source code, connects with Salesforce, fetches lead record and uses Open AI to convert data into embedding. It also shows how searched parameter can be compared with matched word using weightage

Few Observations

  • I did not use SImple_Salesforce python library as it would need to be imported as a layer in lambda
  • Environment variables can be created by navigating to the Configuration tab in the lambda

Below is the screenshot of the outcome

Posted

in

by


Related Posts

Comments

5 responses to “Converting Salesforce Data into Embeddings with OpenAI and AWS Lambda”

  1. Yamini Machha Avatar
    Yamini Machha

    Hi Sir, Thanks for the blogpost,I tried but getting this error —
    cosine_similarity
    return np.dot(A, B) / (norm(A) * norm(B))
    ~~~~~~~~^~~~~~~~~
    TypeError: unsupported operand type(s) for *: ‘rv_continuous_frozen’ and ‘rv_continuous_frozen’

  2. Jitendra Avatar

    Please make sure these libraries are installed using PIP command – openai , numpy , pandas , requests

  3. Esteve Graells Avatar

    Hello Jitendra,

    I would like to begin by commending your excellent example of a RAG implementation in the context of Salesforce. Thanks for sharing it with the community, very appreciate it.

    However, I’d like to offer some feedback regarding certain aspects of your explanation:

    Touching base concepts: the concepts that you mention about training the model with RAG or emproving the model do not apply here. The MML keeps immutable and you can only enrich via fine-tunning, what is hugely expensive and need very dedicated time and effort to create datasets that are good but not contaminated. RAG is a technique to enrich a prompt not improving/incrementing a model via some pre-work before sending the request to the completion API to the model.

    Input to LLM: It’s important to clarify that any Large Language Model (LLM), whether it’s an openAI-compatible API or not, doesn’t receive a pre-existing vector or embedding as input. Instead, it processes a combination of elements, such as a custom context, system prompt, user query and optionally, an assistant prompt in the form of text, never a vector or any other form.

    Purpose of Embeddings and Vector Database: The creation of embeddings and the establishment of a vector database serve the purpose of enriching the knowledge base, never to train the LLM as it can’t be trained but only amplified with a huge cost (not applicable here). This knowledge base is constructed by capturing information before sending the context to the LLM by using what is called a RAG pipeline: a vector database that receives a loader (splitting and vectorizing splits) and retrievers which try to find the best semantic matches with the user query.

    Semantic Search: The semantic search operation is executed on the user’s query against the vector database. The results are generated based on the applied search algorithm. These results consist of the context that is subsequently sent to the LLM along with the user’s request and other parts of the prompts. As you sometimes experienced the retriever can find the right semantic asnwers, because your retriever algorithm is not working well, I think you could try Contextual Compression or my favourite the Multi-query retriever.

    Role of Embeddings: While creating embeddings does involve populating the vector database, it is crucial to create the right embeddings, but you didn’t mention how to splitt the knowledge that you want to augment. Splitting in the wrong way without overlapping techniques and not using MMR like prompting agents will probably provide poor results.

    Deployment Considerations: You mentioned encountering challenges with the use of Lambdas for deployment. It’s worth noting that there are alternative deployment options much better than Lambdas. Solutions such as Bedrock, which can facilitate the use of other LLMs that may be more tailored to your specific problem is one of my favorites. Additionally, you may consider employing larger models like Falcon to address your requirements if desired or constraints or chat ones reducing the sizing requirements a lot.

    I trust that these observations will be beneficial not only to you but also to your readers, including individuals like myself who deeply appreciate the work you have undertaken. Your dedication to the Salesforce arena, and I look forward to seeing more of your contributions in the future.
    Please don’t hesitate to reach me if you want to clarify some of these points as I would be honored.

    1. Jitendra Avatar

      Thats great explanation Esteve, Thank You. In this blog post , there is no RAG. I am just show casing how to do embedding. RAG is happening in part 2 blog post where I have used Langchain to enrich prompt.

      1. Esteve Graells Avatar

        Hi Jitendra, I watched the 3 posts, but I thought posting in the first one just to help on the following 2, if you prefer I can post my comment on the third one.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading