Skip to content

Commit b6bcb1a

Browse files
committed
Added doc for deploying langchain application.
1 parent eebdd41 commit b6bcb1a

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed

docs/source/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ Oracle Accelerated Data Science (ADS)
6868
user_guide/logs/logs
6969
user_guide/pipeline/index
7070
user_guide/secrets/index
71+
user_guide/langchain/deploy
7172

7273
.. toctree::
7374
:hidden:
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
################################
2+
LangChain Application Deployment
3+
################################
4+
5+
Oracle ADS SDK now supports the deployment of LangChain application to OCI data science model deployment and you can easily do so just by writing a couple lines of code.
6+
7+
Installation
8+
************
9+
10+
Before you start, install the ``oracle-ads >= 2.9.1`` package following the command below.
11+
The Oracle ADS SDK has all of the required tools needed for the deployment.
12+
13+
.. code-block:: shell
14+
15+
pip install oracle-ads —-upgrade
16+
17+
Configuration
18+
*************
19+
20+
Ensure that you have created the necessary `policies, authentication, and authorization for model deployments <https://docs.oracle.com/en-us/iaas/data-science/using/model-dep-policies-auth.htm#model_dep_policies_auth>`_.
21+
Here we're using the ``resource_principal`` as auth type and you can configure the policy as below.
22+
23+
.. code-block:: shell
24+
25+
allow dynamic-group <dynamic-group-name> to manage data-science-model-deployments in compartment <compartment-name>
26+
27+
Set auth for ADS SDK to access the OCI data science model deployment resources.
28+
29+
.. code-block:: python3
30+
31+
import ads
32+
ads.set_auth(auth="resource_principal")
33+
34+
Create LangChain Application
35+
****************************
36+
37+
Create a simple LangChain application that links prompt and cohere model as below. Remember to replace the ``<cohere_api_key>`` with the actual cohere api key.
38+
39+
.. code-block:: python3
40+
41+
import os
42+
from langchain.llms import Cohere
43+
from langchain.chains import LLMChain
44+
from langchain.prompts import PromptTemplate
45+
os.environ["COHERE_API_KEY"] = "<cohere_api_key>"
46+
47+
cohere = Cohere()
48+
prompt = PromptTemplate.from_template("Tell me a joke about {subject}")
49+
llm_chain = LLMChain(prompt=prompt, llm=cohere, verbose=True)
50+
51+
Now you have a LangChain object ``llm_chain``. Try running it with the prompt ``{"subject": "animals"}`` and it should give you the corresponding result.
52+
53+
.. code-block:: python3
54+
55+
llm_chain.run({"subject": "animals"})
56+
57+
Initialize the ChainDeployment
58+
******************************
59+
60+
Initialize class ``ChainDeployment`` from ADS SDK and pass the LangChain object ``llm_chain`` from previous step as parameter.
61+
The ``artifact_dir`` is an optional parameter which points to the folder where the model artifacts will be put locally.
62+
In this example, we're using a temporary folder generated by ``tempfile``.
63+
64+
.. code-block:: python3
65+
66+
import tempfile
67+
from ads.llm.chain import ChainDeployment
68+
69+
artifact_dir = tempfile.mkdtemp()
70+
71+
chain_deployment = ChainDeployment(
72+
chain=llm_chain,
73+
artifact_dir=artifact_dir
74+
)
75+
76+
Prepare the Model Artifacts
77+
***************************
78+
79+
Call ``prepare`` from ``ChainDeployment`` to generate the ``score.py`` and serialize the LangChain application to ``chain.yaml`` file under ``artifact_dir`` folder.
80+
Parameters ``inference_conda_env`` and ``inference_python_version`` are passed to define the conda environment where your LangChain application will be running on OCI cloud.
81+
Here we're using ``pytorch21_p39_gpu_v1`` with python 3.9.
82+
83+
.. code-block:: python3
84+
85+
chain_deployment.prepare(
86+
inference_conda_env="pytorch21_p39_gpu_v1",
87+
inference_python_version="3.9",
88+
)
89+
90+
Below is the ``chain.yaml`` file that was saved from ``llm_chain`` object. For more information regarding LLMs model serialization, see `here <https://python.langchain.com/docs/modules/model_io/llms/llm_serialization>`_.
91+
92+
.. code-block:: YAML
93+
94+
_type: llm_chain
95+
llm:
96+
_type: cohere
97+
frequency_penalty: 0.0
98+
k: 0
99+
max_tokens: 256
100+
model: null
101+
p: 1
102+
presence_penalty: 0.0
103+
temperature: 0.75
104+
truncate: null
105+
llm_kwargs: {}
106+
memory: null
107+
metadata: null
108+
output_key: text
109+
output_parser:
110+
_type: default
111+
prompt:
112+
_type: prompt
113+
input_types: {}
114+
input_variables:
115+
- subject
116+
output_parser: null
117+
partial_variables: {}
118+
template: Tell me a joke about {subject}
119+
template_format: f-string
120+
validate_template: false
121+
return_final_only: true
122+
tags: null
123+
verbose: true
124+
125+
Save Artifacts to OCI Model Catalog
126+
***********************************
127+
128+
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.
129+
130+
.. code-block:: python3
131+
132+
chain_deployment.save(display_name="LangChain Model")
133+
134+
Deploy the Model
135+
****************
136+
137+
Deploy the LangChain model from previous step by calling ``deploy``. Remember to replace the ``<cohere_api_key>`` with the actual cohere api key in the ``environment_variables``.
138+
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.
139+
140+
.. code-block:: python3
141+
142+
chain_deployment.deploy(
143+
display_name="LangChain Model Deployment",
144+
environment_variables={"COHERE_API_KEY":"<cohere_api_key>"},
145+
)
146+
147+
Invoke the Deployed Model
148+
*************************
149+
150+
Now the OCI data science model deployment endpoint is ready and you can invoke it to ``tell a joke about animals``.
151+
152+
.. code-block:: python3
153+
154+
chain_deployment.predict(data={"subject": "animals"})["prediction"]
155+
156+
Alternatively, you can use OCI CLI to invoke the model deployment. Remember to replace the ``langchain_application_model_deployment_url`` with the actual model deployment url which you can find in the output from deploy step.
157+
158+
.. code-block:: shell
159+
160+
oci raw-request --http-method POST --target-uri <langchain_application_model_deployment_url>/predict --request-body '{"subject": "animals"}' --auth resource_principal

0 commit comments

Comments
 (0)