ChatPremAI
This example goes over how to use LangChain to interact with different chat models withChatPremAI
Installation and setup
We start by installinglangchain
and premai-sdk
. You can type the following command to install:
Setup PremAI client in LangChain
Once we imported our required modules, let’s setup our client. For now let’s assume that ourproject_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.
Note: If you change themodel
or any other parameters liketemperature
ormax_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.
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.Please note: Similar likeNow, we connect the repository with our chat object to invoke RAG based generations.model_name
when you invoke the argumentrepositories
, then you are potentially overriding the repositories connected in the launchpad.
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.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.
Prem Templates
Writing Prompt Templates can be super messy. Prompt templates are long, hard to manage, and must be continuously tweaked to improve and keep the same throughout the application. With Prem, writing and managing prompts can be super easy. The Templates tab inside the launchpad helps you write as many prompts you need and use it inside the SDK to make your application running using those prompts. You can read more about Prompt Templates here. To use Prem Templates natively with LangChain, you need to pass an id theHumanMessage
. This id should be the name the variable of your prompt template. the content
in HumanMessage
should be the value of that variable.
let’s say for example, if your prompt template was this:
human_messages
to ChatPremAI Client. Please note: Do not forget to
pass the additional template_id
to invoke generation with Prem Templates. If you are not aware of template_id
you can learn more about that in our docs. Here is an example:
Prem Embeddings
In this section we cover how we can get access to different embedding models usingPremEmbeddings
with LangChain. Let’s start by importing our modules and setting our API Key.
text-embedding-3-large
model for this example. .
Setting
model_name
argument in mandatory for PremAIEmbeddings unlike chat.Result: [-0.02129288576543331, 0.0008162345038726926, -0.004556538071483374, 0.02918623760342598, -0.02547479420900345]
Tool/Function Calling
LangChain PremAI supports tool/function calling. Tool/function calling allows a model to respond to a given prompt by generating output that matches a user-defined schema.- You can learn all about tool calling in details in our documentation here.
- You can learn more about langchain tool calling in this part of the docs.
The current version of LangChain ChatPremAI do not support function/tool calling with streaming support. Streaming support along with function calling will come soon.
Passing tools to model
In order to pass tools and let the LLM choose the tool it needs to call, we need to pass a tool schema. A tool schema is the function definition along with proper docstring on what does the function do, what each argument of the function is etc. Below are some simple arithmetic functions with their schema. NOTE:When defining function/tool schema, do not forget to add information around the function arguments, otherwise it would throw error.
Binding tool schemas with our LLM
We will now use thebind_tools
method to convert our above functions to a “tool” and binding it with the model. This means we are going to pass these tool information every time we invoke the model.
- in our first call, we gathered all the tools that the LLM decided to tool, so that it can get the result as an added context to give more accurate and hallucination free result.
- in our second call, we will parse those set of tools decided by LLM and run them (in our case it will be the functions we defined, with the LLM’s extracted arguments) and pass this result to the LLM
Defining tool schemas: Pydantic class Optional
Above we have shown how to define schema using tool
decorator, however we can equivalently define the schema using Pydantic. Pydantic is useful when your tool inputs are more complex: