DSPy
This example explains how to use DsPY by Stanford NLP to interact with different chat models.
DSPy by Stanford NLP is a framework designed to optimize language model (LM) prompts and weights, simplifying the process of building complex systems with LMs by automating and unifying techniques for prompting, fine-tuning, and reasoning. It provides composable and declarative modules in Pythonic syntax and includes an automatic compiler to instruct LMs on executing declarative steps.
Installation and Setup
We start by installing dspy
and premai-sdk
. Use the following commands to install them:
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 instance with DsPY
Once we have imported our required modules, let’s set up our dspy-premai client. For now, let’s assume that our project_id
is 123. However, be sure to use your actual project ID; otherwise, it will throw an error.
To use dspy 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
Here is a quick example of how to get a response from the model:
You can also change the 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
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 dspy 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
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.
Here is how an example generation would look like with Prem Repositories.
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.