Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prem AI langchain integration (migrating PR from org account to personal account) #19113

Merged
merged 39 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
58970e2
Merge pull request #1 from langchain-ai/master
Anindyadeep Feb 28, 2024
410d7bf
Merge pull request #2 from langchain-ai/master
Anindyadeep Mar 15, 2024
761aea9
migrating docs from premai org to personal
Mar 15, 2024
0b94317
Add 'poetry add premai' changes
Mar 15, 2024
5f6fe4e
Migrate providers prem.md from prem org to personal
Mar 15, 2024
a48325a
Migrate docs for text embeddings for premai from prem org to personal
Mar 15, 2024
2c267b5
migrate ChatPremAI changes from premai org to personal
Mar 15, 2024
434cdce
migrate PremAIEmbeddings changes from premai org to personal
Mar 15, 2024
7e5b4d7
migrate premai integration tests from premai org to personal
Mar 15, 2024
8a5ea43
migrate premai unit tests from premai org to personal
Mar 15, 2024
77a1705
migrate premai embeddings unit tests from premai org to personal
Mar 15, 2024
cac6f2d
Merge branch 'master' into prem-sdk
baskaryan Mar 15, 2024
91bb80e
undo
baskaryan Mar 15, 2024
376eee5
fmt
baskaryan Mar 15, 2024
e9625e7
Merge branch 'master' into prem-sdk
efriis Mar 15, 2024
63c962f
Added lint checks for ChatPremAI
Mar 16, 2024
e93a715
Added lint checks for PremAIEmbeddings
Mar 16, 2024
fd9fbaa
run make format
Mar 16, 2024
a82f15f
fix mypy type checks for ChatPremAI _delta
Mar 16, 2024
d513fec
add type ignore in _delta
Mar 16, 2024
d268b76
run make format
Mar 16, 2024
44a0d6f
changed file name from prem to premai and fix code spell checks
Mar 17, 2024
6bd818a
Added premai in pyproject toml file
Mar 17, 2024
34f9899
name changed from ChatPrem to ChatPremAI
Mar 18, 2024
2c094fe
fix: lint issues
Mar 18, 2024
74f026e
Changed name from ChatPrem to ChatPremAI
Mar 18, 2024
6d30402
changed name from ChatPrem to ChatPremAI
Mar 18, 2024
f96cf14
fix: spell checks and grammar
Mar 18, 2024
477b337
changed name from ChatPrem to ChatPremAI and more spell fixes
Mar 18, 2024
8679bcc
Merge branch 'master' into prem-sdk
baskaryan Mar 19, 2024
c293d04
run poetry run --no-update to update packages
Mar 19, 2024
d564a54
Merge branch 'prem-sdk' of https://github.com/Anindyadeep/langchain i…
Mar 19, 2024
30c8267
Merge branch 'master' into prem-sdk
Anindyadeep Mar 19, 2024
69f3569
run format for docs
Mar 19, 2024
12819f7
Merge branch 'master' into prem-sdk
Anindyadeep Mar 21, 2024
3bc4037
removed premai from test_integration grp
Mar 25, 2024
9f1ba02
run poetry --no-update
Mar 25, 2024
2676160
fmt
baskaryan Mar 26, 2024
e1182a1
fmt
baskaryan Mar 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
271 changes: 271 additions & 0 deletions docs/docs/integrations/chat/premai.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
{
"cells": [
{
"cell_type": "raw",
"metadata": {},
"source": [
"---\n",
"sidebar_label: Prem\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ChatPrem\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. \n",
"\n",
"\n",
"This example goes over how to use LangChain to interact with `ChatPremAI`. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Installation and setup\n",
"\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",
"\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 paramters (like: max-tokens, temperature etc) and also pre-set your system prompt. \n",
"\n",
"Congratulations on creating your first deployed application on Prem 🎉 Now we can use langchain to interact with our application. "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from langchain_core.messages import HumanMessage, SystemMessage\n",
"from langchain_community.chat_models import ChatPremAI"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Setup ChatPrem instance in LangChain \n",
"\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",
"In order 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 paramters of the LaunchPad model. \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. "
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import getpass\n",
"\n",
"if os.environ.get(\"PREMAI_API_KEY\") is None:\n",
" os.environ[\"PREMAI_API_KEY\"] = getpass.getpass(\"PremAI API Key:\")\n",
"\n",
"chat = ChatPremAI(project_id=8)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Calling the Model\n",
"\n",
"Now you are all set. We can now start with interacting with our application. `ChatPrem` supports two methods `invoke` (which is same as `generate`) and `stream`. \n",
"\n",
"The first one will give us a static result. Where as the second one will stream tokens one by one. Here's how you can generate chat like completions. \n",
"\n",
"### Generation"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"AIMessage(content=\"Ahoy there, matey! I be a virtual assistant here to assist ye with all yer needs. What can I be helpin' ye with today?\")"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"human_message = HumanMessage(content=\"Who are you?\")\n",
"\n",
"chat.invoke([human_message])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Above looks interesting right? I set my default lanchpad 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. "
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"AIMessage(content='I am a friendly assistant here to help you with any questions or tasks you may have. How can I assist you today?')"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"system_message = SystemMessage(content=\"You are a friendly assistant.\")\n",
"human_message = HumanMessage(content=\"Who are you?\")\n",
"\n",
"chat.invoke([system_message, human_message])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also change generation parameters while calling the model. Here's how you can do that"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"AIMessage(content='I am a friendly assistant here to help you with any questions or tasks you may have. Feel free')"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chat.invoke(\n",
" [system_message, human_message],\n",
" temperature = 0.7, max_tokens = 20, top_p = 0.95\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Important notes:\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",
"\n",
"### Streaming\n",
"\n",
"And finally, here's how you do token streaming for dynamic chat like applications. "
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Ahoy there! I be doin' well, me hearty! And yerself? Arrr!"
]
}
],
"source": [
"import sys\n",
"\n",
"for chunk in chat.stream(\"hello how are you\"):\n",
" sys.stdout.write(chunk.content)\n",
" sys.stdout.flush()"
]
},
{
"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. "
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello! I'm here to help you. How can I assist you today?"
]
}
],
"source": [
"import sys\n",
"\n",
"for chunk in chat.stream(\n",
" \"hello how are you\",\n",
" system_prompt = \"You are an helpful assistant\", temperature = 0.7, max_tokens = 20\n",
"):\n",
" sys.stdout.write(chunk.content)\n",
" sys.stdout.flush()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading
Loading