forked from databricks/databricks-ml-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04_langchain.py
134 lines (93 loc) · 4.13 KB
/
04_langchain.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# Databricks notebook source
# MAGIC %md
# MAGIC # Load Falcon-7B-Instruct from LangChain on Databricks
# MAGIC
# MAGIC This example notebook is adapts the [LangChain integration documentation](https://python.langchain.com/docs/modules/model_io/models/llms/integrations/databricks), and shows how to wrap Databricks endpoints as LLMs in LangChain. It supports two endpoint types:
# MAGIC
# MAGIC - Serving endpoint, recommended for production and development. See `02_mlflow_logging_inference` for how to create one.
# MAGIC - Cluster driver proxy app, recommended for iteractive development. See `03_serve_driver_proxy` for how to create one.
# MAGIC
# MAGIC Environment tested:
# MAGIC - MLR: 13.1 ML
# MAGIC - Instance:
# MAGIC - Wrapping a serving endpoint: `i3.xlarge` on AWS, `Standard_DS3_v2` on Azure
# MAGIC - Wrapping a cluster driver proxy app: `g5.4xlarge` on AWS, `Standard_NV36ads_A10_v5` on Azure for wrapping a cluster driver proxy app (same instance as the driver proxy app)
# COMMAND ----------
# MAGIC %md
# MAGIC ## Wrapping a serving endpoint
# MAGIC The LangChain Databricks integration could wrap a serving endpoint.
# MAGIC
# MAGIC It requires a model serving endpoint (see `02_mlflow_logging_inference` for how to create one) to be in the "Ready" state.
# COMMAND ----------
# MAGIC %pip install -q -U langchain
# MAGIC dbutils.library.restartPython()
# COMMAND ----------
from langchain.llms import Databricks
# This model serving endpoint is created in `02_mlflow_logging_inference`
llm = Databricks(endpoint_name='falcon-7b-instruct-example')
# COMMAND ----------
result = llm("How to master Python in 3 days?", temperature=0.1, max_new_tokens=200)
displayHTML(result)
# COMMAND ----------
# MAGIC %md
# MAGIC ## Wrapping a cluster driver proxy app
# MAGIC The LangChain Databricks integration also works when given the port that runs a proxy.
# MAGIC
# MAGIC It requires the driver proxy app of the `03_serve_driver_proxy` example notebook running.
# COMMAND ----------
# MAGIC %md
# MAGIC ### Same cluster
# MAGIC If using the same cluster that runs the `03_serve_driver_proxy` notebook, specifying `cluster_driver_port` is required.
# COMMAND ----------
# MAGIC %pip install -q -U langchain
# MAGIC dbutils.library.restartPython()
# COMMAND ----------
from langchain.llms import Databricks
# COMMAND ----------
llm = Databricks(cluster_driver_port="7777")
print(llm("How to master Python in 3 days?"))
# COMMAND ----------
# If the app accepts extra parameters like `temperature`,
# you can set them in `model_kwargs`.
llm = Databricks(cluster_driver_port="7777", model_kwargs={"temperature": 0.1})
print(llm("How to master Python in 3 days?"))
# COMMAND ----------
# Use `transform_input_fn` and `transform_output_fn` if the app
# expects a different input schema and does not return a JSON string,
# respectively, or you want to apply a prompt template on top.
def transform_input(**request):
"""
Add more instructions into the prompt.
"""
full_prompt = f"""User: Let's think step by step.
{request["prompt"]}
Assistant:
"""
request["prompt"] = full_prompt
return request
def transform_output(response):
"""
Add timestamps for the anwsers.
"""
from datetime import datetime
now = datetime.now()
current_time = now.strftime("%d/%m/%Y %H:%M:%S")
return f"[{current_time}] falcon: {response}"
llm = Databricks(
cluster_driver_port="7777",
transform_input_fn=transform_input,
transform_output_fn=transform_output,
)
print(llm("How to master Python in 3 days?"))
# COMMAND ----------
# MAGIC %md
# MAGIC ### Different cluster
# MAGIC If using a different cluster, it's required to also specify `cluster_id`, which you can find in the cluster configuration page.
# COMMAND ----------
# MAGIC %pip install -q -U langchain
# MAGIC dbutils.library.restartPython()
# COMMAND ----------
from langchain.llms import Databricks
# TODO: this cluster ID is a place holder, please replace `cluster_id` with the actual cluster ID of the server proxy app's cluster
llm = Databricks(cluster_id="0621-205218-lzbwa3m8", cluster_driver_port="7777")
print(llm("How to master Python in 3 days?"))