|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "attachments": {}, |
| 5 | + "cell_type": "markdown", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Context\n", |
| 9 | + "\n", |
| 10 | + "\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 | +} |
0 commit comments