Objective
This recipe aims to show developers and users how to get started with Premโs Generative AI Platform. The overall tutorial is going to be done in four simple steps:- Setting up all the required packages.
- Introducing Prem repositories and how to upload documents (like PDF) into repositories.
- Writing a simple Streamlit chat function that will take user prompts, get the context from Prem repositories and then pass it to the LLM to provide us accurate results.
Setting up the project
Letโs start by creating a virtual environment and installing Prem SDK and Streamlit.Before getting started, make sure you have an account at Prem AI Platform and a valid project ID and an API Key. As a last requirement, you also need to have at least one repository-id.
.streamlit
. Inside that folder, create a file called secrets.toml
. Inside secrets.toml
add your PREMAI_API_KEY
as shown here:
app.py
, we import some basic libraries along with premai
and initialise some constants:
Uploading documents to Prem repositories
RAG is a powerful technique that combines the strengths of retrieval-based systems and generative models to enable assistants to provide accurate and contextually relevant responses based on the content of your documents. With Prem Repositories, you can upload any kind of document (like .txt, .pdf, etc.), and we take care of the rest. Our recipeโs workflow is simple: We start by uploading some documents to a repository on-prem. Once uploaded, the repository will automatically index them using our internal state-of-the-art RAG technology. You can learn more about Prem repositories here. Letโs write some code that will do the following:- First, upload some PDF documents using streamlit
file_uploader
. - Save each file in a temporary directory.
- Use prem-SDK to upload those temporarily saved files to Prem repositories.
Writing the chat function
Streamlit has a concept of session_state which shares variables within a session. To maintain the history of the chat, we can either create a new session or display the history of chats and retrieved documents in the current session.- We take the userโs prompt, display it using Streamlit, and add it to the Streamlit session_state object.
- Next, start with our assistant block, where we get our response from Prem SDK and then stream the response in Streamlit until the streaming is finished.
- Finally, we also show the chunks of documents retrieved to generate the answer, which helps with some interpretability.