LangChain
This example goes over how to use LangChain to interact with different chat models using langchain ChatPremAI
LangChain is a framework for developing applications powered by large language models (LLMs). It provides open-source building blocks for development, LangSmith for monitoring and optimizing production chains, and LangServe for turning chains into deployable APIs.
Installation and setup
We start by installing langchain
and premai-sdk
. You can type the following command to install:
Before proceeding further, please make sure that you have made an account on PremAI and already created a project. If not, please refer to the quick start guide to get started with the PremAI platform. Create your first project and grab your API key.
Setup PremAI client in LangChain
Once we imported our required modules, let’s setup our client. For now let’s assume that our project_id
is 8
. But make sure you use your project-id, otherwise it will throw error.
To use langchain with prem, you do not need to pass any model name or set any parameters with our chat-client. By default it will use the model name and parameters used in the LaunchPad.
If you change the model
or any other parameters like temperature
or max_tokens
while setting the client, it will override existing default configurations, that was used in LaunchPad.
Chat Completions
ChatPremAI
supports two methods: invoke
(which is the same as generate
) and stream
.
The first one will give us a static result. Whereas the second one will stream tokens one by one. Here’s how you can generate chat-like completions.
You can provide system prompt here like this:
You can also change generation parameters while calling the model. Here’s how you can do that:
If you are going to place system prompt here, then it will override your system prompt that was fixed while deploying the application from the platform.
You can find all the optional parameters here. Any parameters other than these supported parameters will be automatically removed before calling the model.
Native RAG Support with Prem Repositories
Prem Repositories which allows users to upload documents (.txt, .pdf etc) and connect those repositories to the LLMs. You can think Prem repositories as native RAG, where each repository can be considered as a vector database. You can connect multiple repositories. You can learn more about repositories here.
Repositories are also supported in langchain premai. Here is how you can do it.
First we start by defining our repository with some repository ids. Make sure that the ids are valid repository ids. You can learn more about how to get the repository id here.
Please note: Similar like
model_name
when you invoke the argumentrepositories
, then you are potentially overriding the repositories connected in the launchpad.
Now, we connect the repository with our chat object to invoke RAG based generations.
This is how an output looks like.
So, this also means that you do not need to make your own RAG pipeline when using the Prem Platform. Prem uses it’s own RAG technology to deliver best in class performance for Retrieval Augmented Generations.
Ideally, you do not need to connect Repository IDs here to get Retrieval Augmented Generations. You can still get the same result if you have connected the repositories in prem platform.
Streaming
In this section, let’s see how we can stream tokens using langchain and PremAI. Here’s how you do it.
Similar to above, if you want to override the system-prompt and the generation parameters, you need to add the following:
This will stream tokens one after the other.
Please note: As of now, RAG with streaming is not supported. However we still support it with our API. You can learn more about that here.
Embedding
In this section we are going to dicuss how we can get access to different embedding model using PremEmbeddings
with LangChain. Lets start by importing our modules and setting our API Key.
We support lots of state of the art embedding models. You can view our list of supported LLMs and embedding models here. For now let’s go for text-embedding-3-large
model for this example. .
Setting model_name
argument in mandatory for PremAIEmbeddings unlike chat.
Finally, let’s embed some sample document
Dimension of embeddings: 3072
Result:
[-0.02129288576543331, 0.0008162345038726926, -0.004556538071483374, 0.02918623760342598, -0.02547479420900345]
Was this page helpful?