Langchain with vector database connected to Bubble

I’m at the same sort of point - i’m just going to get stuck in even if it means using Python for embedding etc. I just want a basic proof of concept that I can iterate on right now.

How are you thinking to approach the parsing and chunking? I haven’t found a way to do it other than using Python.

I ended up using Supabase for a vectorstore (they have a SQL snippet to install pgvector, set up tables, and create the function for searching based on similarity with an input vector embedding). I had just signed up for Pinecone today and there is a waitlist due to high demand. :sweat_smile:

SQL Snippet: OpenAI Vector Search

I then used Xano to create an API endpoint that takes in a question input, calls the OpenAI embedding API to create the vector embedding, searches with that embedding in the function created in Supabase (it is a RPC function that you can call via REST API), adds each match into a prompt (appended text through a For Each Loop function), sends to GPT 3.5 Turbo to answer based on the context found in the vector store, and then returns the answer to the original question.

I am using up-to-date news as a test case. So importing news articles into the vector store and asking questions about the day’s news.

I think I should be able to do the text parsing and chunking through Xano as well by using loops and text functions. I’ll let you know how that goes.

Once I clean this up, I’ll create some Snippets in Xano that I think I can share and maybe do a quick video tutorial.

2 Likes

You can do all with bubble and some API’s to OpenAI (embedd creation/LLM) and Pinecone (vectorstore, semantic search).

I have started doing it, the only thing that is tricky is the chunking side of it that is probably part of the reason why you want langchain.

I’m not a coder but from my research into this, chunking has been a part of a developers toolset for sometime, which means we can build a plugin to chunk for us in JS.

But im not a coder, so I asked ChatGPT4 for some helping building a plugin to chunk and it did it perfectly, with overlapping and everything. I’m not confident enough to publish this plugin as I just copy/pasted some code and tested it a little, I’m happy to send the code though.

Once chunking is done, you’ll have a list of the chunked parts of a document, then you need to:

  1. embed each chunk using OpenAI’s embedding API
  2. POST the resulting embedding into a Pincone API Upsert call. Add the chunked data into the metadata called something like “Content”.
  3. Now vector store is ready to Query and add to your LLM conversation system.

This is super high level but you don’t need langchain or another DB to get this process up and running.

The way I learnt was I just watched heaps of videos from Pinecone Youtube of developer’s showcasing and then doing a Q&A, this helped me understand all the concepts fast.

BTW, there are many different methodologies on how to use a vectorstore, I showed you one where you put the content in the metadata, BUT that has its own pro’s and and con’s, as that added content will make the process ‘simple’ but will also slow down the query as it’s more content to fetch. Other people only add the UID of the object (thing), and keep the content in their own DB (for us that could be bubble).

Like you said the tech is new right now and next to nobody KNOWS what the ‘right’ way to go about this. There are so many factors where people can test their creativity, but just know that you most likely wont find the ONE way to do this because there are many roads to create a similar process.

3 Likes

hey you can check out this exact case with q&a chat here educonvo.com
it uses finetuner plugin at the moment, but I agree that xano-openai-pinecone-bubble is the better way when you need to scale

Just skimmed through this video and the core of what I said is in there. What he is missing is the conversational LLM aspect. All its doing is bringing back the EXACT text as a response

My first proof of concept I’ll build soon wont include a ‘conversation’ but just to ask one question where I inject the pinecone query result into it and then prompt the LLM probs GPT-4 with both the original question and pinecone results.

Will look like this assuming all vectors have been upserted.

OG question > Interpret questions (GPT3.5) > Send Interpreted question to get embed (OpenAI embedings api) > Query Pinecone with Interpreted question Embedding > Prompt GPT4 with both the OG questions and the contents of the results from Pinecone.

Make sense?

Honestly, the hardest part of all this is ‘Input’. If you want to take a random PDF and input it then you need a bunch of plugins to help you parse that text from the pdf. This challenge is for even type of input you want to embed. For anyone that already has the data in their database, then they have it super easy.

I signed up to Pinecone last week, I think this thread must have crashed it :joy:

I’m curious - what is Xano doing here that you can’t do inside Bubble? It’s another subscription on top of Bubble after all, when you come to launch/scale.

I followed the video posted by @zapclassbr above, yesterday, and built the same thing… https://youtu.be/DkOyTUGxOSo

I manually chunked a document using GPT3.5 and fed a few into Bubble one by one: OpenAI Embeddings API - Results sent to Pinecone Upsert API.

Now, as @bkerryk mentioned, when I query Pinecone via the API from Bubble I get the exact same chunk of text returned that was fed in.

I should then be able to send the Pinecone query result along with the OG question to the GPT3.5 API ( I don’t have GPT4 API access yet) to get some sort of useful result. Is that correct? Have I missed something?

Yes I tried this yesterday following the video. I’m going to try something similar to what you suggest today. Can you explain what is the purpose of step 2 “Interpret questions (GPT3.5)”?

Why is this necessary rather than just sending the OG question to the Open AI embeddings API?

Yeh that’s exactly the concept, but as I said, everyone is experimenting at this stage and there are still not many principled approaches

From what I understand, the OG message might not be very descriptive and wont query the semantic search as well as if you use lets say gpt 3.5 to expand the OG question in some way. I haven’t tested this to see if there are serious differences.

Got it, thanks. Interesting to find out what effect this has on the output.

It would be great if you could post the code you generated. I tried similar with GPT4 but it doesn’t know the Bubble UI very well and wasn’t that helpful.

Here’s the code :smiley: and a screen shot to show how to set it up.

Explanation for each property:

  1. text_input: The text you want to break into smaller pieces.
  2. chunk_size: The size of each piece (number of characters). The function will try not to break sentences in the middle.
  3. chunk_overlap: The number of characters that two consecutive pieces should have in common. This helps when you want some context from the previous piece.

function(properties, context) {
const text = properties.text_input;
const chunkSize = properties.chunk_size;
const chunkOverlap = properties.chunk_overlap;
const splitTexts = ;

  let startPosition = 0;
  while (startPosition < text.length) {
    let endPosition = startPosition + chunkSize;

    // Check if the chunk overlaps a line break
    if (endPosition < text.length) {
      while (text[endPosition] !== '\n' && endPosition > startPosition) {
        endPosition--;
      }
      // If the chunk doesn't overlap a line break, move the endPosition to the next line break
      if (text[endPosition] !== '\n') {
        endPosition = text.indexOf('\n', startPosition + chunkSize);
        if (endPosition === -1) {
          endPosition = text.length;
        }
      }
    }

    splitTexts.push(text.slice(startPosition, endPosition).trim());
    startPosition = endPosition + 1 - chunkOverlap;
  }

  return { split_text: splitTexts };
}

4 Likes

Yeah, just redid the flow in Bubble and everything seems to be possible.

In addition to the document input → text chunks, the thing I wasn’t sure about was adding the list of all context items returned from the vector store into the prompt. But using arbitrary text and formatting the list as text when I insert it into the prompt.

I’d say if you can get the text chunking done with that plugin code @bkerryk included, should be able to get most done directly in Bubble.

I personally prefer Xano for these types of actions as it keeps a little cleaner behind the scenes on the backend and more control to manipulate data coming in and out of APIs. Also looking at creating modular functions so can mix and match things for some other use cases.

Who needs LangChain, Bubble got it all. :raised_hands:

3 Likes

Nice! Really glad you got it working!

Look I am only committed to figuring out everything in bubble because I don’t know anything else. I’d love to know how Xano works, especially since people are talking about it a lot recently. My assumption is that it would make it less ‘clean’ but hearing what you said I am interested.

I agree, I seem to be able to do everything in Bubble so far with various API calls to Pinecone, OpenAI Embeddings and GPT. It’s working pretty well so far and will give pretty good answers, from GPT3.5, fed with the retrievals from Pinecone and original query.

The next step for me is to experiment with the text chunking code from @bkerryk and also trying out your solution to the problem of adding multiple vector database results into the prompt for GPT.

It certainly seems as though you can achieve a lot in Bubble without going anywhere near Langchain. One issue here is speed. It takes a while to return results as there are so many API calls going on. Not sure if Langchain or any other solution can provide a faster user experience.

I’ve just tried making a plugin using this. I copied everything from your post and all seems to be OK… but when I run it in Bubble if the Chunk Overlap value is anything other than 0 or blank it returns the following error:

If I leave Chunk Overlap blank or 0 it returns the full text un-chunked. I’ve played with different values for Chunk Size too and can’t get it to behave any differently.

ChatGPT doesn’t see any problems in the code.

Did you have any of these issues?

Yeh I have hit that before. Its because its working but its taking too long to chunk. The more chunks it needs to create the longer it takes. Even its a small amount of text but you break it up into too small chunks it will take longer than 30 seconds therefor will time out.

Just for testing, try using less text with more characters.

If I had to work with just this plugin, I might try letting gpt4 know about the error and see if it can think of a code solution. Or a bubble solution might be (haven’t tested) do large chunks, then chunk thoe large chunks into smallers chunks and so on until you get the size you want.

BTW, the plugin will chunk on linebreaks to make sure that it doesn’t cut a word or sentence in half.

This is a fascinating development. How’s it going at this point? Is someone working on a plugin to streamline the process?

There are plugins available such as Finetuner, but it’s a bit limited with how much control you have, and needs a separate subscription. I’m not aware of anyone else developing a plugin for this, at the moment people are using APIs and workflows in Bubble to achieve the kind of results they want.