Skip to content

Commit df746ad

Browse files
authored
Add a callback handler for Context (https://getcontext.ai) (#7151)
### Description Adding a callback handler for Context. Context is a product analytics platform for AI chat experiences to help you understand how users are interacting with your product. I've added the callback library + an example notebook showing its use. ### Dependencies Requires the user to install the `context-python` library. The library is lazily-loaded when the callback is instantiated. ### Announcing the feature We spoke with Harrison a few weeks ago about also doing a blog post announcing our integration, so will coordinate this with him. Our Twitter handle for the company is @getcontextai, and the founders are @_agamble and @HenrySG. Thanks in advance!
1 parent c9a0f24 commit df746ad

File tree

3 files changed

+415
-0
lines changed

3 files changed

+415
-0
lines changed
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
{
2+
"cells": [
3+
{
4+
"attachments": {},
5+
"cell_type": "markdown",
6+
"metadata": {},
7+
"source": [
8+
"# Context\n",
9+
"\n",
10+
"![Context - Product Analytics for AI Chatbots](https://go.getcontext.ai/langchain.png)\n",
11+
"\n",
12+
"[Context](https://getcontext.ai/) provides product analytics for AI chatbots.\n",
13+
"\n",
14+
"Context helps you understand how users are interacting with your AI chat products.\n",
15+
"Gain critical insights, optimise poor experiences, and minimise brand risks.\n"
16+
]
17+
},
18+
{
19+
"attachments": {},
20+
"cell_type": "markdown",
21+
"metadata": {},
22+
"source": [
23+
"In this guide we will show you how to integrate with Context."
24+
]
25+
},
26+
{
27+
"attachments": {},
28+
"cell_type": "markdown",
29+
"metadata": {
30+
"tags": []
31+
},
32+
"source": [
33+
"## Installation and Setup"
34+
]
35+
},
36+
{
37+
"cell_type": "code",
38+
"execution_count": null,
39+
"metadata": {
40+
"vscode": {
41+
"languageId": "shellscript"
42+
}
43+
},
44+
"outputs": [],
45+
"source": [
46+
"$ pip install context-python --upgrade"
47+
]
48+
},
49+
{
50+
"attachments": {},
51+
"cell_type": "markdown",
52+
"metadata": {},
53+
"source": [
54+
"### Getting API Credentials\n",
55+
"\n",
56+
"To get your Context API token:\n",
57+
"\n",
58+
"1. Go to the settings page within your Context account (https://go.getcontext.ai/settings).\n",
59+
"2. Generate a new API Token.\n",
60+
"3. Store this token somewhere secure."
61+
]
62+
},
63+
{
64+
"attachments": {},
65+
"cell_type": "markdown",
66+
"metadata": {},
67+
"source": [
68+
"### Setup Context\n",
69+
"\n",
70+
"To use the `ContextCallbackHandler`, import the handler from Langchain and instantiate it with your Context API token.\n",
71+
"\n",
72+
"Ensure you have installed the `context-python` package before using the handler."
73+
]
74+
},
75+
{
76+
"cell_type": "code",
77+
"execution_count": 3,
78+
"metadata": {},
79+
"outputs": [],
80+
"source": [
81+
"import os\n",
82+
"\n",
83+
"from langchain.callbacks import ContextCallbackHandler\n",
84+
"\n",
85+
"token = os.environ[\"CONTEXT_API_TOKEN\"]\n",
86+
"\n",
87+
"context_callback = ContextCallbackHandler(token)"
88+
]
89+
},
90+
{
91+
"attachments": {},
92+
"cell_type": "markdown",
93+
"metadata": {},
94+
"source": [
95+
"## Usage\n",
96+
"### Using the Context callback within a Chat Model\n",
97+
"\n",
98+
"The Context callback handler can be used to directly record transcripts between users and AI assistants.\n",
99+
"\n",
100+
"#### Example"
101+
]
102+
},
103+
{
104+
"cell_type": "code",
105+
"execution_count": 4,
106+
"metadata": {},
107+
"outputs": [],
108+
"source": [
109+
"import os\n",
110+
"\n",
111+
"from langchain.chat_models import ChatOpenAI\n",
112+
"from langchain.schema import (\n",
113+
" SystemMessage,\n",
114+
" HumanMessage,\n",
115+
")\n",
116+
"from langchain.callbacks import ContextCallbackHandler\n",
117+
"\n",
118+
"token = os.environ[\"CONTEXT_API_TOKEN\"]\n",
119+
"\n",
120+
"chat = ChatOpenAI(\n",
121+
" headers={\"user_id\": \"123\"}, temperature=0, callbacks=[ContextCallbackHandler(token)]\n",
122+
")\n",
123+
"\n",
124+
"messages = [\n",
125+
" SystemMessage(\n",
126+
" content=\"You are a helpful assistant that translates English to French.\"\n",
127+
" ),\n",
128+
" HumanMessage(content=\"I love programming.\"),\n",
129+
"]\n",
130+
"\n",
131+
"print(chat(messages))"
132+
]
133+
},
134+
{
135+
"attachments": {},
136+
"cell_type": "markdown",
137+
"metadata": {},
138+
"source": [
139+
"### Using the Context callback within Chains\n",
140+
"\n",
141+
"The Context callback handler can also be used to record the inputs and outputs of chains. Note that intermediate steps of the chain are not recorded - only the starting inputs and final outputs.\n",
142+
"\n",
143+
"__Note:__ Ensure that you pass the same context object to the chat model and the chain.\n",
144+
"\n",
145+
"Wrong:\n",
146+
"> ```python\n",
147+
"> chat = ChatOpenAI(temperature=0.9, callbacks=[ContextCallbackHandler(token)])\n",
148+
"> chain = LLMChain(llm=chat, prompt=chat_prompt_template, callbacks=[ContextCallbackHandler(token)])\n",
149+
"> ```\n",
150+
"\n",
151+
"Correct:\n",
152+
">```python\n",
153+
">handler = ContextCallbackHandler(token)\n",
154+
">chat = ChatOpenAI(temperature=0.9, callbacks=[callback])\n",
155+
">chain = LLMChain(llm=chat, prompt=chat_prompt_template, callbacks=[callback])\n",
156+
">```\n",
157+
"\n",
158+
"#### Example"
159+
]
160+
},
161+
{
162+
"cell_type": "code",
163+
"execution_count": null,
164+
"metadata": {},
165+
"outputs": [],
166+
"source": [
167+
"import os\n",
168+
"\n",
169+
"from langchain.chat_models import ChatOpenAI\n",
170+
"from langchain import LLMChain\n",
171+
"from langchain.prompts import PromptTemplate\n",
172+
"from langchain.prompts.chat import (\n",
173+
" ChatPromptTemplate,\n",
174+
" HumanMessagePromptTemplate,\n",
175+
")\n",
176+
"from langchain.callbacks import ContextCallbackHandler\n",
177+
"\n",
178+
"token = os.environ[\"CONTEXT_API_TOKEN\"]\n",
179+
"\n",
180+
"human_message_prompt = HumanMessagePromptTemplate(\n",
181+
" prompt=PromptTemplate(\n",
182+
" template=\"What is a good name for a company that makes {product}?\",\n",
183+
" input_variables=[\"product\"],\n",
184+
" )\n",
185+
")\n",
186+
"chat_prompt_template = ChatPromptTemplate.from_messages([human_message_prompt])\n",
187+
"callback = ContextCallbackHandler(token)\n",
188+
"chat = ChatOpenAI(temperature=0.9, callbacks=[callback])\n",
189+
"chain = LLMChain(llm=chat, prompt=chat_prompt_template, callbacks=[callback])\n",
190+
"print(chain.run(\"colorful socks\"))"
191+
]
192+
}
193+
],
194+
"metadata": {
195+
"kernelspec": {
196+
"display_name": "Python 3 (ipykernel)",
197+
"language": "python",
198+
"name": "python3"
199+
},
200+
"language_info": {
201+
"codemirror_mode": {
202+
"name": "ipython",
203+
"version": 3
204+
},
205+
"file_extension": ".py",
206+
"mimetype": "text/x-python",
207+
"name": "python",
208+
"nbconvert_exporter": "python",
209+
"pygments_lexer": "ipython3",
210+
"version": "3.11.3"
211+
},
212+
"vscode": {
213+
"interpreter": {
214+
"hash": "a53ebf4a859167383b364e7e7521d0add3c2dbbdecce4edf676e8c4634ff3fbb"
215+
}
216+
}
217+
},
218+
"nbformat": 4,
219+
"nbformat_minor": 4
220+
}

langchain/callbacks/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from langchain.callbacks.arthur_callback import ArthurCallbackHandler
77
from langchain.callbacks.clearml_callback import ClearMLCallbackHandler
88
from langchain.callbacks.comet_ml_callback import CometCallbackHandler
9+
from langchain.callbacks.context_callback import ContextCallbackHandler
910
from langchain.callbacks.file import FileCallbackHandler
1011
from langchain.callbacks.flyte_callback import FlyteCallbackHandler
1112
from langchain.callbacks.human import HumanApprovalCallbackHandler
@@ -36,6 +37,7 @@
3637
"ArthurCallbackHandler",
3738
"ClearMLCallbackHandler",
3839
"CometCallbackHandler",
40+
"ContextCallbackHandler",
3941
"FileCallbackHandler",
4042
"HumanApprovalCallbackHandler",
4143
"InfinoCallbackHandler",

0 commit comments

Comments
 (0)