From ee689412ab3c586373cbc84d2da8c0fcb93f7380 Mon Sep 17 00:00:00 2001 From: Anindyadeep Date: Fri, 24 May 2024 21:25:32 +0530 Subject: [PATCH] docs: Update PremAI Docs (#22114) Thank you for contributing to LangChain! - [X] **PR title**: community: Updated langchain-community PremAI documentation - [X] **Lint and test**: Run `make format`, `make lint` and `make test` from the root of the package(s) you've modified. See contribution guidelines for more: https://python.langchain.com/docs/contributing/ --- docs/docs/integrations/chat/premai.ipynb | 47 +++----- docs/docs/integrations/providers/premai.md | 106 ++++++++---------- .../integrations/text_embedding/premai.ipynb | 42 +++---- 3 files changed, 79 insertions(+), 116 deletions(-) diff --git a/docs/docs/integrations/chat/premai.ipynb b/docs/docs/integrations/chat/premai.ipynb index 13a2ece273..2e26a39bef 100644 --- a/docs/docs/integrations/chat/premai.ipynb +++ b/docs/docs/integrations/chat/premai.ipynb @@ -15,10 +15,9 @@ "source": [ "# ChatPremAI\n", "\n", - ">[PremAI](https://app.premai.io) is a unified platform that lets you build powerful production-ready GenAI-powered applications with the least effort so that you can focus more on user experience and overall growth. \n", + "[PremAI](https://premai.io/) is an all-in-one platform that simplifies the creation of robust, production-ready applications powered by Generative AI. By streamlining the development process, PremAI allows you to concentrate on enhancing user experience and driving overall growth for your application. You can quickly start using our platform [here](https://docs.premai.io/quick-start).\n", "\n", - "\n", - "This example goes over how to use LangChain to interact with `ChatPremAI`. " + "This example goes over how to use LangChain to interact with different chat models with `ChatPremAI`" ] }, { @@ -27,23 +26,13 @@ "source": [ "### Installation and setup\n", "\n", - "We start by installing langchain and premai-sdk. You can type the following command to install:\n", + "We start by installing `langchain` and `premai-sdk`. You can type the following command to install:\n", "\n", "```bash\n", "pip install premai langchain\n", "```\n", "\n", - "Before proceeding further, please make sure that you have made an account on PremAI and already started a project. If not, then here's how you can start for free:\n", - "\n", - "1. Sign in to [PremAI](https://app.premai.io/accounts/login/), if you are coming for the first time and create your API key [here](https://app.premai.io/api_keys/).\n", - "\n", - "2. Go to [app.premai.io](https://app.premai.io) and this will take you to the project's dashboard. \n", - "\n", - "3. Create a project and this will generate a project-id (written as ID). This ID will help you to interact with your deployed application. \n", - "\n", - "4. Head over to LaunchPad (the one with 🚀 icon). And there deploy your model of choice. Your default model will be `gpt-4`. You can also set and fix different generation parameters (like max-tokens, temperature, etc) and also pre-set your system prompt. \n", - "\n", - "Congratulations on creating your first deployed application on PremAI 🎉 Now we can use langchain to interact with our application. " + "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](https://docs.premai.io/introduction) guide to get started with the PremAI platform. Create your first project and grab your API key." ] }, { @@ -60,13 +49,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Setup ChatPremAI instance in LangChain \n", + "### Setup PremAI client in LangChain\n", "\n", - "Once we import our required modules, let's set up 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 an error.\n", + "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.\n", "\n", - "To use langchain with prem, you do not need to pass any model name or set any parameters with our chat client. All of those will use the default model name and parameters of the LaunchPad model. \n", + "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](https://docs.premai.io/get-started/launchpad). \n", "\n", - "`NOTE:` If you change the `model_name` or any other parameter like `temperature` while setting the client, it will override existing default configurations. " + "> Note: 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. " ] }, { @@ -102,13 +91,11 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Calling the Model\n", + "### Chat Completions\n", "\n", - "Now you are all set. We can now start by interacting with our application. `ChatPremAI` supports two methods `invoke` (which is the same as `generate`) and `stream`. \n", + "`ChatPremAI` supports two methods: `invoke` (which is the same as `generate`) and `stream`. \n", "\n", - "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. \n", - "\n", - "### Generation" + "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. " ] }, { @@ -165,7 +152,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "You can also change generation parameters while calling the model. Here's how you can do that" + "You can provide system prompt here like this:" ] }, { @@ -192,15 +179,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Important notes:\n", + "> 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. \n", "\n", - "Before proceeding further, please note that the current version of ChatPrem does not support parameters: [n](https://platform.openai.com/docs/api-reference/chat/create#chat-create-n) and [stop](https://platform.openai.com/docs/api-reference/chat/create#chat-create-stop) are not supported. \n", - "\n", - "We will provide support for those two above parameters in sooner versions. \n", + "> Please note that the current version of ChatPremAI does not support parameters: [n](https://platform.openai.com/docs/api-reference/chat/create#chat-create-n) and [stop](https://platform.openai.com/docs/api-reference/chat/create#chat-create-stop). \n", "\n", "### Streaming\n", "\n", - "And finally, here's how you do token streaming for dynamic chat like applications. " + "In this section, let's see how we can stream tokens using langchain and PremAI. Here's how you do it. " ] }, { @@ -228,7 +213,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Similar to above, if you want to override the system-prompt and the generation parameters, here's how you can do it. " + "Similar to above, if you want to override the system-prompt and the generation parameters, you need to add the following:" ] }, { diff --git a/docs/docs/integrations/providers/premai.md b/docs/docs/integrations/providers/premai.md index 7ec2ca5fa8..a15a9c0d40 100644 --- a/docs/docs/integrations/providers/premai.md +++ b/docs/docs/integrations/providers/premai.md @@ -1,6 +1,6 @@ # PremAI ->[PremAI](https://app.premai.io) is a unified platform that lets you build powerful production-ready GenAI-powered applications with the least effort so that you can focus more on user experience and overall growth. +[PremAI](https://premai.io/) is an all-in-one platform that simplifies the creation of robust, production-ready applications powered by Generative AI. By streamlining the development process, PremAI allows you to concentrate on enhancing user experience and driving overall growth for your application. You can quickly start using our platform [here](https://docs.premai.io/quick-start). ## ChatPremAI @@ -9,36 +9,27 @@ This example goes over how to use LangChain to interact with different chat mode ### Installation and setup -We start by installing langchain and premai-sdk. You can type the following command to install: +We start by installing `langchain` and `premai-sdk`. You can type the following command to install: ```bash pip install premai langchain ``` -Before proceeding further, please make sure that you have made an account on PremAI and already started a project. If not, then here's how you can start for free: - -1. Sign in to [PremAI](https://app.premai.io/accounts/login/), if you are coming for the first time and create your API key [here](https://app.premai.io/api_keys/). - -2. Go to [app.premai.io](https://app.premai.io) and this will take you to the project's dashboard. - -3. Create a project and this will generate a project-id (written as ID). This ID will help you to interact with your deployed application. - -4. Head over to LaunchPad (the one with 🚀 icon). And there deploy your model of choice. Your default model will be `gpt-4`. You can also set and fix different generation parameters (like max-tokens, temperature, etc) and also pre-set your system prompt. - -Congratulations on creating your first deployed application on PremAI 🎉 Now we can use langchain to interact with our application. +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](https://docs.premai.io/introduction) guide to get started with the PremAI platform. Create your first project and grab your API key. ```python from langchain_core.messages import HumanMessage, SystemMessage from langchain_community.chat_models import ChatPremAI ``` -### Setup ChatPrem instance in LangChain +### Setup PremAI client in LangChain -Once we import our required modules, let's set up 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 an error. +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. All of those will use the default model name and parameters of the LaunchPad model. +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](https://docs.premai.io/get-started/launchpad). + +> Note: 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. -`NOTE:` If you change the `model_name` or any other parameter like `temperature` while setting the client, it will override existing default configurations. ```python import os @@ -50,21 +41,19 @@ if "PREMAI_API_KEY" not in os.environ: chat = ChatPremAI(project_id=8) ``` -### Calling the Model +### Chat Completions -Now you are all set. We can now start by interacting with our application. `ChatPremAI` supports two methods `invoke` (which is the same as `generate`) and `stream`. +`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. -### Generation - ```python human_message = HumanMessage(content="Who are you?") chat.invoke([human_message]) ``` -The above looks interesting, right? I set my default launchpad system-prompt as: `Always sound like a pirate` You can also, override the default system prompt if you need to. Here's how you can do it. +You can provide system prompt here like this: ```python system_message = SystemMessage(content="You are a friendly assistant.") @@ -82,16 +71,13 @@ chat.invoke( ) ``` +> 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. -### Important notes: - -Before proceeding further, please note that the current version of ChatPrem does not support parameters: [n](https://platform.openai.com/docs/api-reference/chat/create#chat-create-n) and [stop](https://platform.openai.com/docs/api-reference/chat/create#chat-create-stop) are not supported. - -We will provide support for those two above parameters in later versions. +> Please note that the current version of ChatPremAI does not support parameters: [n](https://platform.openai.com/docs/api-reference/chat/create#chat-create-n) and [stop](https://platform.openai.com/docs/api-reference/chat/create#chat-create-stop). ### Streaming -And finally, here's how you do token streaming for dynamic chat-like applications. +In this section, let's see how we can stream tokens using langchain and PremAI. Here's how you do it. ```python import sys @@ -101,7 +87,7 @@ for chunk in chat.stream("hello how are you"): sys.stdout.flush() ``` -Similar to above, if you want to override the system-prompt and the generation parameters, here's how you can do it. +Similar to above, if you want to override the system-prompt and the generation parameters, you need to add the following: ```python import sys @@ -114,47 +100,30 @@ for chunk in chat.stream( sys.stdout.flush() ``` -## Embedding +This will stream tokens one after the other. -In this section, we are going to discuss how we can get access to different embedding models using `PremEmbeddings`. Let's start by doing some imports and defining our embedding object +## PremEmbeddings + +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. ```python -from langchain_community.embeddings import PremEmbeddings -``` - -Once we import our required modules, let's set up 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 an error. - - -```python - import os import getpass +from langchain_community.embeddings import PremEmbeddings + if os.environ.get("PREMAI_API_KEY") is None: os.environ["PREMAI_API_KEY"] = getpass.getpass("PremAI API Key:") -# Define a model as a required parameter here since there is no default embedding model +``` + +We support lots of state of the art embedding models. You can view our list of supported LLMs and embedding models [here](https://docs.premai.io/get-started/supported-models). For now let's go for `text-embedding-3-large` model for this example. . + +```python model = "text-embedding-3-large" embedder = PremEmbeddings(project_id=8, model=model) -``` -We have defined our embedding model. We support a lot of embedding models. Here is a table that shows the number of embedding models we support. - - -| Provider | Slug | Context Tokens | -|-------------|------------------------------------------|----------------| -| cohere | embed-english-v3.0 | N/A | -| openai | text-embedding-3-small | 8191 | -| openai | text-embedding-3-large | 8191 | -| openai | text-embedding-ada-002 | 8191 | -| replicate | replicate/all-mpnet-base-v2 | N/A | -| together | togethercomputer/Llama-2-7B-32K-Instruct | N/A | -| mistralai | mistral-embed | 4096 | - -To change the model, you simply need to copy the `slug` and access your embedding model. Now let's start using our embedding model with a single query followed by multiple queries (which is also called as a document) - -```python query = "Hello, this is a test query" query_result = embedder.embed_query(query) @@ -162,8 +131,11 @@ query_result = embedder.embed_query(query) print(query_result[:5]) ``` + +Setting `model_name` argument in mandatory for PremAIEmbeddings unlike chat. + -Finally, let's embed a document +Finally, let's embed some sample document ```python documents = [ @@ -178,4 +150,20 @@ doc_result = embedder.embed_documents(documents) # of the first document vector print(doc_result[0][:5]) -``` \ No newline at end of file +``` + +```python +print(f"Dimension of embeddings: {len(query_result)}") +``` +Dimension of embeddings: 3072 + +```python +doc_result[:5] +``` +>Result: +> +>[-0.02129288576543331, + 0.0008162345038726926, + -0.004556538071483374, + 0.02918623760342598, + -0.02547479420900345] \ No newline at end of file diff --git a/docs/docs/integrations/text_embedding/premai.ipynb b/docs/docs/integrations/text_embedding/premai.ipynb index d8bf54fd43..6c509e8fd1 100644 --- a/docs/docs/integrations/text_embedding/premai.ipynb +++ b/docs/docs/integrations/text_embedding/premai.ipynb @@ -6,25 +6,26 @@ "source": [ "# PremAI\n", "\n", - ">[PremAI](https://app.premai.io) is an unified platform that let's you build powerful production-ready GenAI powered applications with least effort, so that you can focus more on user experience and overall growth. In this section we are going to dicuss how we can get access to different embedding model using `PremAIEmbeddings`\n", + "[PremAI](https://premai.io/) is an all-in-one platform that simplifies the creation of robust, production-ready applications powered by Generative AI. By streamlining the development process, PremAI allows you to concentrate on enhancing user experience and driving overall growth for your application. You can quickly start using our platform [here](https://docs.premai.io/quick-start).\n", "\n", - "## Installation and Setup\n", + "### Installation and setup\n", "\n", - "We start by installing langchain and premai-sdk. You can type the following command to install:\n", + "We start by installing `langchain` and `premai-sdk`. You can type the following command to install:\n", "\n", "```bash\n", "pip install premai langchain\n", "```\n", "\n", - "Before proceeding further, please make sure that you have made an account on Prem and already started a project. If not, then here's how you can start for free:\n", + "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](https://docs.premai.io/introduction) guide to get started with the PremAI platform. Create your first project and grab your API key." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## PremEmbeddings\n", "\n", - "1. Sign in to [PremAI](https://app.premai.io/accounts/login/), if you are coming for the first time and create your API key [here](https://app.premai.io/api_keys/).\n", - "\n", - "2. Go to [app.premai.io](https://app.premai.io) and this will take you to the project's dashboard. \n", - "\n", - "3. Create a project and this will generate a project-id (written as ID). This ID will help you to interact with your deployed application. \n", - "\n", - "Congratulations on creating your first deployed application on Prem 🎉 Now we can use langchain to interact with our application. " + "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. " ] }, { @@ -42,7 +43,9 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "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.\n" + "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.\n", + "\n", + "> Note: Setting `model_name` argument in mandatory for PremAIEmbeddings unlike [ChatPremAI.](https://python.langchain.com/v0.1/docs/integrations/chat/premai/)" ] }, { @@ -72,20 +75,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "We have defined our embedding model. We support a lot of embedding models. Here is a table that shows the number of embedding models we support. \n", - "\n", - "\n", - "| Provider | Slug | Context Tokens |\n", - "|-------------|------------------------------------------|----------------|\n", - "| cohere | embed-english-v3.0 | N/A |\n", - "| openai | text-embedding-3-small | 8191 |\n", - "| openai | text-embedding-3-large | 8191 |\n", - "| openai | text-embedding-ada-002 | 8191 |\n", - "| replicate | replicate/all-mpnet-base-v2 | N/A |\n", - "| together | togethercomputer/Llama-2-7B-32K-Instruct | N/A |\n", - "| mistralai | mistral-embed | 4096 |\n", - "\n", - "To change the model, you simply need to copy the `slug` and access your embedding model. Now let's start using our embedding model with a single query followed by multiple queries (which is also called as a document)" + "We support lots of state of the art embedding models. You can view our list of supported LLMs and embedding models [here](https://docs.premai.io/get-started/supported-models). For now let's go for `text-embedding-3-large` model for this example." ] }, {