Skip to content

Commit da4cce1

Browse files
authored
add content to batch example (jaybaird#29)
1 parent f29e959 commit da4cce1

File tree

1 file changed

+103
-11
lines changed

1 file changed

+103
-11
lines changed

langkit/examples/Batch_to_Whylabs.ipynb

Lines changed: 103 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,52 @@
99
">*Did you know you can store, visualize, and monitor language model profiles with the [WhyLabs Observability Platform](https://whylabs.ai/whylogs-free-signup?utm_source=github&utm_medium=referral&utm_campaign=langkit)? Sign up for a [free WhyLabs account](https://whylabs.ai/whylogs-free-signup?utm_source=github&utm_medium=referral&utm_campaign=langkit) to leverage the power of LangKit and WhyLabs together!*"
1010
]
1111
},
12+
{
13+
"attachments": {},
14+
"cell_type": "markdown",
15+
"metadata": {},
16+
"source": [
17+
"# Logging and Monitoring Text Metrics for LLMs with LangKit and WhyLabs"
18+
]
19+
},
20+
{
21+
"attachments": {},
22+
"cell_type": "markdown",
23+
"metadata": {},
24+
"source": [
25+
"[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/whylabs/LanguageToolkit/blob/main/langkit/examples/Batch_to_Whylabs.ipynb)"
26+
]
27+
},
28+
{
29+
"attachments": {},
30+
"cell_type": "markdown",
31+
"metadata": {},
32+
"source": [
33+
"In this example, we'll show how you can generate out-of-the-box text metrics using LangKit and whylogs, and then log and monitor them in the WhyLabs Observability Platform.\n",
34+
"\n",
35+
"With LangKit, you'll be able to extract relevant signals from unstructured text data, such as:\n",
36+
"\n"
37+
]
38+
},
39+
{
40+
"attachments": {},
41+
"cell_type": "markdown",
42+
"metadata": {},
43+
"source": [
44+
"## Loading the Dataset - Chatbot prompts\n",
45+
"\n",
46+
"Let's first download a huggingface dataset containint prompts and responses from a chatbot. We'll generate text metrics for the prompts and responses, and then log them to WhyLabs."
47+
]
48+
},
1249
{
1350
"cell_type": "code",
1451
"execution_count": null,
1552
"metadata": {},
1653
"outputs": [],
1754
"source": [
1855
"from datasets import load_dataset\n",
19-
"from whylogs.experimental.core.metrics.udf_metric import generate_udf_schema\n",
20-
"from whylogs.core.schema import DeclarativeSchema\n",
21-
"\n",
22-
"from langkit.sentiment import *\n",
23-
"from langkit.textstat import *\n",
24-
"from langkit.regexes import *\n",
25-
"from langkit.themes import *\n",
26-
"\n",
27-
"print(\"downloading models and initialized metrics...\")\n",
28-
"text_schema = DeclarativeSchema(generate_udf_schema())\n",
2956
"print(\"initialize hugging face archived chat prompt/response dataset...\")\n",
30-
"archived_chats = load_dataset('alespalla/chatbot_instruction_prompts', split=\"test\", streaming=True)\n"
57+
"archived_chats = load_dataset('alespalla/chatbot_instruction_prompts', split=\"test\", streaming=True)"
3158
]
3259
},
3360
{
@@ -72,6 +99,51 @@
7299
"print(\"Using API Key ID: \", os.environ[\"WHYLABS_API_KEY\"][0:10])"
73100
]
74101
},
102+
{
103+
"attachments": {},
104+
"cell_type": "markdown",
105+
"metadata": {},
106+
"source": [
107+
"## Initializing Metrics from LangKit\n",
108+
"\n",
109+
"In order to calculate the text metrics, we simply need to import the relevant modules from `LangKit`. In this case, we will calculate metrics using the following modules:\n",
110+
"\n",
111+
"- textstat: text statistics such as scores for readability, complexity, and grade\n",
112+
"- sentiment: sentiment scores\n",
113+
"- regexes: label text according to user-defined regex pattern groups\n",
114+
"- themes: compute sentence similarity scores with respect to groups of: a) known jailbreak and b) LLM refusal of service responses\n",
115+
"\n",
116+
"After importing the modules, we can generate a schema that will inform whylogs of the metrics we want to calculate. We can then use this schema to log our data."
117+
]
118+
},
119+
{
120+
"cell_type": "code",
121+
"execution_count": null,
122+
"metadata": {},
123+
"outputs": [],
124+
"source": [
125+
"from whylogs.experimental.core.metrics.udf_metric import generate_udf_schema\n",
126+
"from whylogs.core.schema import DeclarativeSchema\n",
127+
"\n",
128+
"from langkit.sentiment import *\n",
129+
"from langkit.textstat import *\n",
130+
"from langkit.regexes import *\n",
131+
"from langkit.themes import *\n",
132+
"\n",
133+
"print(\"downloading models and initialized metrics...\")\n",
134+
"text_schema = DeclarativeSchema(generate_udf_schema())\n"
135+
]
136+
},
137+
{
138+
"attachments": {},
139+
"cell_type": "markdown",
140+
"metadata": {},
141+
"source": [
142+
"## Profiling and Writing to WhyLabs - Single Example\n",
143+
"\n",
144+
"The following code block will log a single prompt/response pair. The resulting profile will then be sent over to your dashboard at WhyLabs."
145+
]
146+
},
75147
{
76148
"cell_type": "code",
77149
"execution_count": null,
@@ -100,6 +172,16 @@
100172
"print()\n"
101173
]
102174
},
175+
{
176+
"attachments": {},
177+
"cell_type": "markdown",
178+
"metadata": {},
179+
"source": [
180+
"## Profiling and Writing to WhyLabs - Multiple Batches\n",
181+
"\n",
182+
"Let's get us closer to a real scenario. If you have an LLM-powered system, you'll be interested in monitoring your text inputs/outputs in a streaming fashion. In this case, we'll simulate a streaming scenario by iterating through the examples and logging them into daily batches. Let's say we have 7 days worth of data, with 10 examples per day."
183+
]
184+
},
103185
{
104186
"cell_type": "code",
105187
"execution_count": null,
@@ -133,6 +215,16 @@
133215
" print()\n",
134216
"print(\"Done. Go see your metrics on the WhyLabs dashboard!\")"
135217
]
218+
},
219+
{
220+
"attachments": {},
221+
"cell_type": "markdown",
222+
"metadata": {},
223+
"source": [
224+
"And that's it! You can now go to your WhyLabs dashboard and explore the profiles for the past 7 days.\n",
225+
"\n",
226+
"Feel free to play around with the code and the metrics. You can inject anomalies manually to see how the metrics change, or you can set monitors and alert over at the WhyLabs dashboard."
227+
]
136228
}
137229
],
138230
"metadata": {

0 commit comments

Comments
 (0)