Skip to content

Commit da7c02d

Browse files
authored
Merge pull request #364 from lu-ohai/ODSC-50481/deploy_langchain_notebook
Added example to deploy langchain application
2 parents ab7a5f2 + 2d91cfb commit da7c02d

File tree

1 file changed

+386
-0
lines changed

1 file changed

+386
-0
lines changed
Lines changed: 386 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,386 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "raw",
5+
"metadata": {},
6+
"source": [
7+
"@notebook{deploy-langchain-as-oci-data-science-model-deployment.ipynb,\n",
8+
" title: Deploy LangChain Application as OCI Data Science Model Deployment,\n",
9+
" summary: Deploy LangChain applications as OCI data science model deployment,\n",
10+
" developed_on: pytorch21_p39_gpu_v1,\n",
11+
" keywords: langchain, deploy model, register model, LLM,\n",
12+
" license: Universal Permissive License v 1.0\n",
13+
"}"
14+
]
15+
},
16+
{
17+
"cell_type": "markdown",
18+
"metadata": {},
19+
"source": [
20+
"<font color=gray>Oracle Data Science service sample notebook.\n",
21+
"\n",
22+
"Copyright (c) 2023 Oracle, Inc. All rights reserved.\n",
23+
"Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.\n",
24+
"</font>\n",
25+
"\n",
26+
"***\n",
27+
"# <font color=red>Deploy LangChain Application as OCI Data Science Model Deployment</font>\n",
28+
"<p style=\"margin-left:10%; margin-right:10%;\">by the <font color=teal> Oracle Cloud Infrastructure Data Science Service Team </font></p>\n",
29+
"\n",
30+
"***\n",
31+
"\n",
32+
"## Overview\n",
33+
"\n",
34+
"The notebook demonstrates how to deploy LangChain application as OCI Data Science Model Deployment using Oracle Accelerated Data Science (ADS) SDK.\n",
35+
"\n",
36+
"The `ChainDeployment` class in ADS allows you to rapidly get a LangChain application into production. The `.prepare()` method serializes the LangChain application as `chain.yaml` file and generates `score.py` file which can further be uploaded to OCI model catalog. The uploaded model can be subsequently deployed into production.\n",
37+
"\n",
38+
"Compatible conda pack: [pytorch21_p39_gpu_v1](https://docs.oracle.com/en-us/iaas/data-science/using/conda-gml-fam.htm) for CPU on Python 3.9 (version 1.0)\n",
39+
"\n",
40+
"### Prequisites\n",
41+
"\n",
42+
"This notebook requires authorization to work with the OCI Data Science Service. Details can be found [here](https://accelerated-data-science.readthedocs.io/en/latest/user_guide/cli/authentication.html#). For the purposes of this notebook what is important to to know is that resource principals will be used absent api_key authentication.\n",
43+
"\n",
44+
"---\n",
45+
"\n",
46+
"## Contents\n",
47+
"\n",
48+
"* <a href='#intro'>Introduction</a>\n",
49+
"* <a href='#create'>Create a LangChain Application</a>\n",
50+
"* <a href='#deploy'>Deploy LangChain Application as OCI Model Deployment</a>\n",
51+
" * <a href='#deploy_chaindeployment'>Create a ChainDeployment</a>\n",
52+
" * <a href='#deploy_prepare'>Prepare</a>\n",
53+
" * <a href='#deploy_verify'>Verify</a>\n",
54+
" * <a href='#deploy_save'>Save</a>\n",
55+
" * <a href='#deploy_deploy'>Deploy</a>\n",
56+
" * <a href='#deploy_predict'>Predict</a>\n",
57+
"* <a href='#clean_up'>Clean Up</a>\n",
58+
"* <a href='#ref'>References</a> \n",
59+
"\n",
60+
"---"
61+
]
62+
},
63+
{
64+
"cell_type": "code",
65+
"execution_count": null,
66+
"metadata": {},
67+
"outputs": [],
68+
"source": [
69+
"import ads\n",
70+
"import os\n",
71+
"import tempfile\n",
72+
"\n",
73+
"from ads.llm.deploy import ChainDeployment\n",
74+
"from langchain.llms import Cohere\n",
75+
"from langchain.chains import LLMChain\n",
76+
"from langchain.prompts import PromptTemplate\n",
77+
"from shutil import rmtree"
78+
]
79+
},
80+
{
81+
"cell_type": "markdown",
82+
"metadata": {},
83+
"source": [
84+
"<a id='intro'></a>\n",
85+
"# Introduction\n",
86+
"\n",
87+
"In this notebook, you will create a custom LangChain application that links prompt and Cohere model and deploy it on OCI model deployment. It is designed to demonstrate how to use the `ChainDeployment` class in Oracle ADS SDK.\n",
88+
"\n",
89+
"The `.prepare()` method will serialize the LangChain application as `chain.yaml` file. It will also generate a `score.py` file that will load the LangChain yaml and call the `predict()` method. The `.save()` and `.deploy()` methods will upload the artifacts to OCI model catalog and deploy the uploaded model, respectively."
90+
]
91+
},
92+
{
93+
"cell_type": "markdown",
94+
"metadata": {},
95+
"source": [
96+
"### Authenticate\n",
97+
"\n",
98+
"Authentication to the OCI Data Science service is required. Here we default to resource principals."
99+
]
100+
},
101+
{
102+
"cell_type": "code",
103+
"execution_count": null,
104+
"metadata": {},
105+
"outputs": [],
106+
"source": [
107+
"ads.set_auth(auth=\"resource_principal\")"
108+
]
109+
},
110+
{
111+
"cell_type": "markdown",
112+
"metadata": {},
113+
"source": [
114+
"<a id='create'></a>\n",
115+
"# Create a LangChain Application\n",
116+
"\n",
117+
"The next cell creates a LangChain application that links prompt and Cohere model. The LangChain application will utilize Cohere model to generate a joke based on the subject that user provides. Remember to replace the `<cohere_api_key>` with the actual api key as Cohere model needs it. You can acquire this key by registering on [Cohere](https://dashboard.cohere.com/welcome/register)."
118+
]
119+
},
120+
{
121+
"cell_type": "code",
122+
"execution_count": null,
123+
"metadata": {},
124+
"outputs": [],
125+
"source": [
126+
"os.environ[\"COHERE_API_KEY\"] = \"<cohere_api_key>\"\n",
127+
"\n",
128+
"cohere = Cohere() \n",
129+
"prompt = PromptTemplate.from_template(\"Tell me a joke about {subject}\")\n",
130+
"llm_chain = LLMChain(prompt=prompt, llm=cohere, verbose=True)"
131+
]
132+
},
133+
{
134+
"cell_type": "markdown",
135+
"metadata": {},
136+
"source": [
137+
"Now you have a LangChain object `llm_chain`. Try running it with the prompt `{\"subject\": \"animals\"}` and it should give you the corresponding answer."
138+
]
139+
},
140+
{
141+
"cell_type": "code",
142+
"execution_count": null,
143+
"metadata": {},
144+
"outputs": [],
145+
"source": [
146+
"llm_chain.run({\"subject\": \"animals\"})"
147+
]
148+
},
149+
{
150+
"cell_type": "markdown",
151+
"metadata": {},
152+
"source": [
153+
"<a id='deploy'></a>\n",
154+
"# Deploy LangChain Application as OCI Model Deployment\n",
155+
"\n",
156+
"<a id='deploy_chaindeployment'></a>\n",
157+
"## Create a ChainDeployment\n",
158+
"\n",
159+
"The next cell creates a model artifact directory. This directory is used to store the artifacts that are needed to deploy the model. It also creates the `ChainDeployment` object. The `ChainDeployment` requires the LangChain object `llm_chain` as `chain` parameter."
160+
]
161+
},
162+
{
163+
"cell_type": "code",
164+
"execution_count": null,
165+
"metadata": {},
166+
"outputs": [],
167+
"source": [
168+
"artifact_dir = tempfile.mkdtemp()\n",
169+
"\n",
170+
"chain_deployment = ChainDeployment(\n",
171+
" chain=llm_chain,\n",
172+
" artifact_dir=artifact_dir\n",
173+
")"
174+
]
175+
},
176+
{
177+
"cell_type": "markdown",
178+
"metadata": {},
179+
"source": [
180+
"<a id='deploy_prepare'></a>\n",
181+
"## Prepare\n",
182+
"\n",
183+
"The prepare step is performed by the `.prepare()` method of the `ChainDeployment` class. It creates a number of customized files that are used to run the model once it is deployed. These include:\n",
184+
"\n",
185+
"* `chain.yaml`: A YAML file that is serialized from the LangChain application and can be deserialized in `load_model` in `score.py`.\n",
186+
"* `runtime.yaml`: This file contains information that is needed to set up the runtime environment on the deployment server.\n",
187+
"* `score.py`: This script contains the `load_model()` and `predict()` functions. The `load_model()` function understands the format the model file was saved in, and loads it into memory. The `.predict()` method is used to make inferences in a deployed model.\n",
188+
"\n",
189+
"To create the model artifacts, you use the `.prepare()` method\n",
190+
"\n",
191+
"* `inference_conda_env` variable defines the slug of the conda environment that is used to train the model\n",
192+
"\n",
193+
"Note that you can only pass in slug for service conda environment. For custom conda environment, you have to pass in the full path along with the `inference_python_version`. \n",
194+
"\n",
195+
"Here, replace `<custom_conda_environment_uri>` with your conda environment uri that has the latest ADS 2.9.1 and replace `<python_version>` with your conda environment python version. For how to customize and publish conda environment, take reference to [Publishing a Conda Environment to an Object Storage Bucket](https://docs.oracle.com/en-us/iaas/data-science/using/conda_publishs_object.htm)."
196+
]
197+
},
198+
{
199+
"cell_type": "code",
200+
"execution_count": null,
201+
"metadata": {},
202+
"outputs": [],
203+
"source": [
204+
"chain_deployment.prepare(\n",
205+
" inference_conda_env=\"<custom_conda_environment_uri>\",\n",
206+
" inference_python_version=\"<python_version>\",\n",
207+
")"
208+
]
209+
},
210+
{
211+
"cell_type": "markdown",
212+
"metadata": {},
213+
"source": [
214+
"<a id='deploy_verify'></a>\n",
215+
"## Verify\n",
216+
"\n",
217+
"The `.verify()` method takes a set of test parameters and performs the prediction by calling the `predict` function in `score.py`. It also runs the `load_model` function."
218+
]
219+
},
220+
{
221+
"cell_type": "code",
222+
"execution_count": null,
223+
"metadata": {},
224+
"outputs": [],
225+
"source": [
226+
"chain_deployment.verify({\"subject\": \"animals\"})"
227+
]
228+
},
229+
{
230+
"cell_type": "markdown",
231+
"metadata": {},
232+
"source": [
233+
"<a id='deploy_save'></a>\n",
234+
"## Save\n",
235+
"\n",
236+
"Call `.save()` to pack and upload the artifacts under `artifact_dir` to OCI data science model catalog. Once the artifacts are successfully uploaded, you should be able to see the id of the model."
237+
]
238+
},
239+
{
240+
"cell_type": "code",
241+
"execution_count": null,
242+
"metadata": {},
243+
"outputs": [],
244+
"source": [
245+
"chain_deployment.save(display_name=\"LangChain Model\")"
246+
]
247+
},
248+
{
249+
"cell_type": "markdown",
250+
"metadata": {},
251+
"source": [
252+
"<a id='deploy_deploy'></a>\n",
253+
"## Deploy\n",
254+
"\n",
255+
"Deploy the LangChain model from previous step by calling `.deploy()`. For more information regarding the allowed parameters, see [here](https://accelerated-data-science.readthedocs.io/en/latest/user_guide/model_serialization/genericmodel.html#deploy). Remember to replace the `<cohere_api_key>` with the actual cohere api key in the `environment_variables`. It usually takes a couple of minutes to deploy the model and you should see the model deployment in the output once the process completes."
256+
]
257+
},
258+
{
259+
"cell_type": "code",
260+
"execution_count": null,
261+
"metadata": {},
262+
"outputs": [],
263+
"source": [
264+
"chain_deployment.deploy(\n",
265+
" display_name=\"LangChain Model Deployment\",\n",
266+
" environment_variables={\"COHERE_API_KEY\":\"<cohere_api_key>\"}, \n",
267+
")"
268+
]
269+
},
270+
{
271+
"cell_type": "markdown",
272+
"metadata": {},
273+
"source": [
274+
"<a id='deploy_predict'></a>\n",
275+
"## Predict\n",
276+
"\n",
277+
"After the deployment is active, you can call the `predict()` on the `ChainDeployment` object to send request to the deployed endpoint. "
278+
]
279+
},
280+
{
281+
"cell_type": "code",
282+
"execution_count": null,
283+
"metadata": {},
284+
"outputs": [],
285+
"source": [
286+
"chain_deployment.predict(data={\"subject\": \"animals\"})"
287+
]
288+
},
289+
{
290+
"cell_type": "markdown",
291+
"metadata": {},
292+
"source": [
293+
"<a id='clean_up'></a>\n",
294+
"# Clean Up\n",
295+
"\n",
296+
"This notebook created a model deployment and a model. This section deletes those resources. \n",
297+
"\n",
298+
"The model deployment must be deleted before the model can be deleted. You can use the `.delete_deployment()` method on the `ChainDeployment` object to do this."
299+
]
300+
},
301+
{
302+
"cell_type": "code",
303+
"execution_count": null,
304+
"metadata": {},
305+
"outputs": [],
306+
"source": [
307+
"delete = chain_deployment.delete_deployment(wait_for_completion=True)"
308+
]
309+
},
310+
{
311+
"cell_type": "markdown",
312+
"metadata": {},
313+
"source": [
314+
"Use the `.delete()` method to delete the model:"
315+
]
316+
},
317+
{
318+
"cell_type": "code",
319+
"execution_count": null,
320+
"metadata": {},
321+
"outputs": [],
322+
"source": [
323+
"chain_deployment.delete()"
324+
]
325+
},
326+
{
327+
"cell_type": "markdown",
328+
"metadata": {},
329+
"source": [
330+
"The next cell removes the model artifacts that were stored on your local drive:"
331+
]
332+
},
333+
{
334+
"cell_type": "code",
335+
"execution_count": null,
336+
"metadata": {},
337+
"outputs": [],
338+
"source": [
339+
"rmtree(artifact_dir)"
340+
]
341+
},
342+
{
343+
"cell_type": "markdown",
344+
"metadata": {},
345+
"source": [
346+
"<a id='ref'></a>\n",
347+
"# References\n",
348+
"- [ADS Library Documentation](https://accelerated-data-science.readthedocs.io/en/latest/index.html)\n",
349+
"- [Data Science YouTube Videos](https://www.youtube.com/playlist?list=PLKCk3OyNwIzv6CWMhvqSB_8MLJIZdO80L)\n",
350+
"- [OCI Data Science Documentation](https://docs.cloud.oracle.com/en-us/iaas/data-science/using/data-science.htm)\n",
351+
"- [Oracle Data & AI Blog](https://blogs.oracle.com/datascience/)\n",
352+
"- [Understanding Conda Environments](https://docs.cloud.oracle.com/en-us/iaas/data-science/using/use-notebook-sessions.htm#conda_understand_environments)\n",
353+
"- [Use Resource Manager to Configure Your Tenancy for Data Science](https://docs.cloud.oracle.com/en-us/iaas/data-science/using/orm-configure-tenancy.htm)\n",
354+
"- [runtime.yaml](https://docs.content.oci.oracleiaas.com/en-us/iaas/data-science/using/model_runtime_yaml.htm#model_runtime_yaml)\n",
355+
"- [score.py](https://docs.content.oci.oracleiaas.com/en-us/iaas/data-science/using/model_score_py.htm#model_score_py)\n",
356+
"- [Model artifact](https://docs.content.oci.oracleiaas.com/en-us/iaas/data-science/using/models_saving_catalog.htm#create-models)"
357+
]
358+
}
359+
],
360+
"metadata": {
361+
"kernelspec": {
362+
"display_name": "Python [conda env:pytorch110_p38_cpu_v1]",
363+
"language": "python",
364+
"name": "conda-env-pytorch110_p38_cpu_v1-py"
365+
},
366+
"language_info": {
367+
"codemirror_mode": {
368+
"name": "ipython",
369+
"version": 3
370+
},
371+
"file_extension": ".py",
372+
"mimetype": "text/x-python",
373+
"name": "python",
374+
"nbconvert_exporter": "python",
375+
"pygments_lexer": "ipython3",
376+
"version": "3.8.13"
377+
},
378+
"vscode": {
379+
"interpreter": {
380+
"hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
381+
}
382+
}
383+
},
384+
"nbformat": 4,
385+
"nbformat_minor": 4
386+
}

0 commit comments

Comments
 (0)