Ok. So you want to supplement your LLM chat application with your own knowledge base but you could not be bothered about the cumbersome code development that might be involved. Or you are more visual-oriented and would like to make sense of your application workflow.
LangFlow is a drag-and-drop framework that helps you build fully customizable GenAI applications. You can assemble several components with a few clicks to create the exact Retrieval Augmented Generation application you envision, powered by your data source. This means you can access a more reliable, easy-to-build, GenAI model built to address your unique business needs.
In this tutorial, we will utilize some of the available building components in LangFlow to make an application that provides food recommendations based on the US dietary guidelines. This project builds upon our previous work where we integrated Azure Openai and Document Intelligence to scan food products and get more insights to guide our nutrition.
This project requires that you have:
- Access to Azure Openai
- Python 3.10
Creating a LangFlow account
To start LangFlow, ensure you are working in an environment with python 3.10. Then go to your terminal and run the following (I promise, you will not have to write any code beyond this):
python -m pip install langflow -Upython -m langflow run
If everything runs successfully, you should get something similar to the image below. Click on the endpoint. This should direct you to your LangFlow account. Proceed to create a new project.
Creating the workflow
Deploying an Azure Openai model
Now, open another page and log onto your Azure portal,and create an Openai resource (fill out this registration form if you don't already have access):
- Subscription: Select your active subscription
- Resource group: Select an existing resource group or create a new one
- Name: Name your resource.
- Region: Randomly select any region from the available lists of regions.
- Pricing: Select Standard S0
After deploying the resource, click on “Go to Azure Openai studio” on the top pane
- Scroll down on the left pane and click on the “Deployments” page
- Click on “create new deployment” next
- Select GPT4o in the list of models
- Assign a name to the deployment (note the deployment name)
- Reduce the token rate limit to 7K and then “Create”
Once that has been successfully deployed, go back to the Azure portal. Click on the Openai resource you just deployed and copy the endpoint and keys.
Building your application pipeline
Go back to your LangFlow page. Note the panes on the right; this is where you will find your necessary building components. Click on the inputs dropdown menu and drag the chat input pane onto the canvas. Do the same with the outputs menu and select 'chat output'. Go to the models pane, find and select 'Azure Openai'.
Once that is done, connect the components as seen in the image above:
- Paste the following to the template field in the 'Prompt' pane:
You are an AI assistant that helps users resolve their questionQuestion:{question}
- Connect the input node in 'Chat Input' to the 'Prompt' question node
- Connect the prompt message in 'Chat Input' to the text node in 'Azure OpenAI'.
- Fill in the copied credentials.
Now let’s test it. Click on the play button in the top-right corner on the Chat Output pane. Click on ‘playground’ in the bottom right of the screen and ask any question. It should return a response successfully:
Creating a vector database and populating it with vector embeddings
Keeping to the objective of our application, we need a database to store, retrieve, and query our data on dietary guidelines. We need to be able to search this database and retrieve information closely related to our query. To do this we would be using Azure OpenAI embeddings to create the vectors that represent this relationship.
First sign up onAstra db. Once that is done, you should get an interface like the image below. If not, toggle the drop-down in the red box and select Astra db. Then select ‘create database’ on the right in the yellow box. Name the database and select Azure as the provider (please note, this costs a minimal fee to use), select us-east-2 as the region and proceed to create the database. Once created, note the database details on the right of the create database page.
Now go back to your LangFlow page. Save the current flow and then create a new project. On this page populate the screen with the following components:
‘File’
‘Recursive Character Text splitting’
‘Azure OpenAI Embeddings’
‘Astra DB’
You should have something similar to the image below:
Return to OpenAI studio and follow the steps in creating and deploying a vector embedding model (select text-embedding-002 as the model type) as stated earlier. Now go back to the LangFlow pane and do the following:
- File component:
- Attach the US dietary guideline PDF.
- Connect the output to the input node in the recursive character text split pane
- Recursive split:
- Leave as default
- Connect to Astra DB ‘Ingest Data’ node
- Azure Openai Embeddings:
- Fill the endpoint and API keys as you did with the GPT4o model
- Fill in the name of the deployed model
- Connect the embeddings model to Astra DB
- Astra DB:
- Return to the Astra DB page and copy the API and token from the database detail
- Name the collection
- Fill the parameters
- Press the play button on the top right and then give it a few minutes to complete
The process above will create a collection of the vector embeddings in the database and might take a few minutes. Return to the Astra DB database page and you should see the collection has been created and proceed to inspect it.
Creating the final workflow
Next, open the previously saved flow and connect the Astra DB pane to the existing flow:
- Fill the details of the Astra db as same as before
- Create a new thread that connects chat input data to Astra DB
- Drag ‘Parse Data’ onto the pane and connect Astra DB ‘search result’ node to ‘data’
Copy the template below and paste into the template field of the ‘prompt’ pane:
You are an AI assistant that helps that recommends healthy meals based on the {guidelines} providedquestion: {question}context: {guidelines}
Connect the text node in 'Parse Data' to the 'guidelines' node in templates. If all instructions are followed correctly, it should look something like this:
Now we have completed the pipeline and are ready to test.
Click on ‘playground’ and test out your app!
Bonus:
You can export this flow and integrate it into your app. LangFlow provides several API codes. Below, we generate a chat widget by simply copying the API code:
- Click on API in the bottom left of the screen next to 'Playground' in LangFlow. Click on the Chat widget HTML and copy the code.
- Create an HTML file in VS code. Paste the code. Right-click and ‘ open with live server’
- This will direct you to a page with the chat widget.
References and further reading:
1. AstraDB On Azure
2. Vector database