avatarLaxfed Paulacy

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1694

Abstract

n class="hljs-built_in">context</span>-python --upgrade</pre></div><p id="769c">Next, you’ll need to obtain your Context API token by following these steps:</p><ol><li>Go to the settings page within your Context account.</li><li>Generate a new API Token.</li><li>Store this token securely.</li></ol><h2 id="dbb4">Setting Up Context</h2><p id="b047">After obtaining your API token, you can set up the ContextCallbackHandler by importing it from Langchain and instantiating it with your Context API token:</p><div id="5a32"><pre><span class="hljs-keyword">import</span> <span class="hljs-built_in">os</span> <span class="hljs-keyword">from</span> langchain.callbacks <span class="hljs-keyword">import</span> ContextCallbackHandler

token = <span class="hljs-built_in">os</span>.environ[<span class="hljs-string">"CONTEXT_API_TOKEN"</span>]

context_callback = ContextCallbackHandler(token)</pre></div><h2 id="e41f">Usage</h2><p id="672a">You can use the Context callback handler to directly record transcripts between users and AI assistants within a chat model. Here’s an example of how to incorporate the Context callback within a Chat Model:</p><div id="8676"><pre>import os <span class="hljs-keyword">from</span> langchain.chat_models import ChatOpenAI <span class="hljs-keyword">from</span> langchain.schema import SystemMessage, HumanMessage <span class="hljs-keyword">from</span> langchain.callbacks import ContextCallbackHandler

token = os.environ[<span class="hljs-string">"CONTEXT_API_TOKEN"</span>]

chat = ChatOpenAI( headers={<span class="hljs-string">"user_id"</span>: <span class="hljs-string">"123"</span>}, <span class="hljs-attribute">temperature</span>=0, callbacks

Options

=[ContextCallbackHandler(token)] )

messages = [ SystemMessage(<span class="hljs-attribute">content</span>=<span class="hljs-string">"You are a helpful assistant that translates English to French."</span>), HumanMessage(<span class="hljs-attribute">content</span>=<span class="hljs-string">"I love programming."</span>), ]

<span class="hljs-built_in">print</span>(chat(messages))</pre></div><p id="b722">By integrating the ContextCallbackHandler into your chat models, you can gain valuable insights into user behavior and conversation themes, allowing you to iteratively improve your chat products based on user analytics.</p><div id="a7cc" class="link-block"> <a href="https://readmedium.com/langchain-what-is-langchain-streamlit-8a606e7c297e"> <div> <div> <h2>LANGCHAIN — What is Langchain Streamlit?</h2> <div><h3>Digital design is like painting, except the paint never dries. — Neville Brody.</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*nu7ZXSdSXeo6aCLEJYoZpg.jpeg)"></div> </div> </div> </a> </div><p id="27e2">In conclusion, leveraging user analytics from Context within Langchain chat products provides developers with the visibility and understanding needed to enhance user experience and product performance. By following the steps outlined in this tutorial, you can seamlessly integrate user analytics into your chat products and drive iterative improvements based on real user interactions.</p></article></body>

LANGCHAIN — Building Better Chat Products with User Analytics?

In the software world, the moment you start using someone else’s software, you are living in their world, under their philosophy. — Richard Stallman

Building better chat products with user analytics is crucial for understanding user behavior and improving product performance. Langchain’s integration with Context, a product analytics platform for LLM-powered chat products, provides valuable insights into user interactions. In this tutorial, we’ll explore how to integrate Context with Langchain chat products and leverage user analytics.

Installation and Setup

To begin, install the Context Python package using pip:

pip install context-python --upgrade

Next, you’ll need to obtain your Context API token by following these steps:

  1. Go to the settings page within your Context account.
  2. Generate a new API Token.
  3. Store this token securely.

Setting Up Context

After obtaining your API token, you can set up the ContextCallbackHandler by importing it from Langchain and instantiating it with your Context API token:

import os
from langchain.callbacks import ContextCallbackHandler

token = os.environ["CONTEXT_API_TOKEN"]

context_callback = ContextCallbackHandler(token)

Usage

You can use the Context callback handler to directly record transcripts between users and AI assistants within a chat model. Here’s an example of how to incorporate the Context callback within a Chat Model:

import os
from langchain.chat_models import ChatOpenAI
from langchain.schema import SystemMessage, HumanMessage
from langchain.callbacks import ContextCallbackHandler

token = os.environ["CONTEXT_API_TOKEN"]

chat = ChatOpenAI(
    headers={"user_id": "123"},
    temperature=0,
    callbacks=[ContextCallbackHandler(token)]
)

messages = [
    SystemMessage(content="You are a helpful assistant that translates English to French."),
    HumanMessage(content="I love programming."),
]

print(chat(messages))

By integrating the ContextCallbackHandler into your chat models, you can gain valuable insights into user behavior and conversation themes, allowing you to iteratively improve your chat products based on user analytics.

In conclusion, leveraging user analytics from Context within Langchain chat products provides developers with the visibility and understanding needed to enhance user experience and product performance. By following the steps outlined in this tutorial, you can seamlessly integrate user analytics into your chat products and drive iterative improvements based on real user interactions.

Chat
Langchain
ChatGPT
User
Products
Recommended from ReadMedium