avatarLaxfed Paulacy

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

3197

Abstract

=[<span class="hljs-string">"tutorial"</span>], metadata={<span class="hljs-string">"githash"</span>: <span class="hljs-string">"e38f04c83"</span>}, </span>)</span> <span class="hljs-keyword">def</span> <span class="hljs-title function_">call_openai</span>(<span class="hljs-params"> messages: <span class="hljs-type">List</span>[<span class="hljs-built_in">dict</span>], model: <span class="hljs-built_in">str</span> = <span class="hljs-string">"gpt-3.5-turbo"</span>, temperature: <span class="hljs-built_in">float</span> = <span class="hljs-number">0.0</span> </span>) -> <span class="hljs-built_in">str</span>: <span class="hljs-keyword">return</span> openai.ChatCompletion.create( model=model, messages=messages, temperature=temperature, )</pre></div><div id="7984"><pre><span class="hljs-comment">// Using the TypeScript SDK</span> <span class="hljs-title class_">import</span> <span class="hljs-punctuation">{</span> RunTree, RunTreeConfig <span class="hljs-punctuation">}</span> from <span class="hljs-string">"langsmith"</span><span class="hljs-punctuation">;</span>

const parentRunConfig: RunTreeC<span class="hljs-attr">onfig</span> <span class="hljs-operator">=</span> <span class="hljs-punctuation">{</span> <span class="hljs-symbol"> name:</span> <span class="hljs-string">"My Chat Bot"</span>, <span class="hljs-symbol"> run_type:</span> <span class="hljs-string">"chain"</span>, <span class="hljs-symbol"> inputs:</span> <span class="hljs-punctuation">{</span> <span class="hljs-symbol"> text:</span> <span class="hljs-string">"Summarize this morning's meetings."</span>, <span class="hljs-punctuation">}</span>, <span class="hljs-symbol"> extra:</span> <span class="hljs-punctuation">{</span> <span class="hljs-symbol"> metadata:</span> <span class="hljs-punctuation">{</span><span class="hljs-string">"githash"</span>: <span class="hljs-string">"e38f04c83"</span><span class="hljs-punctuation">}</span> <span class="hljs-punctuation">}</span>, <span class="hljs-attr">tags</span><span class="hljs-operator">=</span>[<span class="hljs-string">"tutorial"</span>] <span class="hljs-punctuation">};</span>

const parentR<span class="hljs-attr">un</span> <span class="hljs-operator">=</span> new RunTree(parentRunConfig)<span class="hljs-punctuation">;</span> await parentRun.postRun()<span class="hljs-punctuation">;</span></pre></div><div id="1442"><pre><span class="hljs-comment"># Using the REST API (in Python)</span> requests.post( <span class="hljs-string">"https://api.smith.langchain.com/runs"</span>, <span class="hljs-keyword">json={ </span> <span class="hljs-string">"id"</span>: run_id, <span class="hljs-string">"name"</span>: <span class="hljs-string">"My Run"</span>, <span class="hljs-string">"run_type"</span>: <span class="hljs-string">"chain"</span>, <span class="hljs-string">"inputs"</span>: {<span class="hljs-string">"text"</span>: <span class="hljs-string">"Foo"</span>}, <span class="hljs-string">"start_time"</span>: datetime.datetime.utcnow().isoformat(), <span class="hljs-string">"session_name"</span>: project_name,

Options

   <span class="hljs-string">"tags"</span>: [<span class="hljs-string">"langsmith"</span>, <span class="hljs-string">"rest"</span>, <span class="hljs-string">"my-example"</span>],
    <span class="hljs-string">"extra"</span>: {
        <span class="hljs-string">"metadata"</span>: {<span class="hljs-string">"my_key"</span>: <span class="hljs-string">"My value"</span>},
    },
},
headers={<span class="hljs-string">"x-api-key"</span>: _LANGSMITH_API_KEY},

)</pre></div><h2 id="c2b1">Case Study: Testing Different LLM Providers in Chat LangChain</h2><p id="05c4">We can use metadata and tagging in LangSmith to group data into different categories and analyze performance metrics for each category alongside each other.</p><h2 id="a9a8">LLM Latency</h2><figure id="a08a"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*Ck9gth3dgfGnl6ZR.png"><figcaption></figcaption></figure><p id="bff2">Chart in LangSmith showing LLM latency over time</p><h2 id="7d5c">Time to First Token</h2><figure id="8611"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*irCHxPfk5NjQeoH6.png"><figcaption></figcaption></figure><p id="fcc4">Chart in LangSmith showing time-to-first-token over time</p><h2 id="7a8c">Feedback</h2><figure id="da86"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*YoBZn4mK4XMh_OZ0.png"><figcaption></figcaption></figure><p id="e2ca">Chart in LangSmith showing User Score (binary) over time</p><p id="2d78">These charts demonstrate the capability to identify variations or discrepancies between different LLM providers and make data-driven decisions about the application.</p><h2 id="0bc8">Other Use-Cases</h2><p id="8ed2">The grouping feature in LangSmith can be applied to other use-cases:</p><h2 id="c8a6">A/B Testing with Revisions</h2><p id="97bd">By sending up a <code>revision</code> identifier in the metadata and grouping by this revision in your charts, you can clearly see how each version performs with respect to each other.</p><h2 id="bcd0">Enhancing User Experience</h2><p id="2037">By grouping data using <code>user_id</code> or <code>conversation_id</code> in metadata, you can gain an in-depth understanding of how different users are experiencing the application and identify any user-specific issues or trends.</p><div id="d100" class="link-block"> <a href="https://readmedium.com/langchain-is-mental-health-therapy-a-state-machine-76fa0dccf7d5"> <div> <div> <h2>LANGCHAIN — Is Mental Health Therapy a State Machine?</h2> <div><h3>Software is like entropy: It is difficult to grasp, weighs nothing, and obeys the Second Law of Thermodynamics; i.e…</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="01fa">These examples just scratch the surface of what’s possible with LangSmith’s new grouping feature. For more information and to sign up for LangSmith,</p></article></body>

LANGCHAIN — Grouped Monitoring Charts

Technology alone is not enough. It’s technology married with the liberal arts, married with the humanities, that yields the results that make our hearts sing. — Steve Jobs.

LangSmith has introduced a new feature, Grouped Monitoring Charts, that allows users to compare metrics of logged traces containing different tags or metadata. This provides the ability to analyze and compare the performance of different versions of applications. In this tutorial, we will explore how to send traces with tags and metadata using LangChain and LangSmith SDK or API, and also look at a case study of testing different LLM providers in Chat LangChain.

Sending Traces With Tags and Metadata

LangChain

If using LangChain, you can send a dictionary with tags and/or metadata in invoke to any Runnable. The same concept works in TypeScript as well.

chain.invoke({"input": "What is the meaning of life?"}, {"metadata": {"my_key": "My Value"}})  # sending custom metadata

chain.invoke({"input": "Hello, World!"}, {"tags": ["shared-tags"]})  # sending custom tags

LangSmith SDK / API

If you’re not using LangChain, you can either use the SDK or API to log traces with custom tags and/or metadata.

# Using the Python SDK
import openai
from langsmith.run_helpers import traceable

@traceable(
    run_type="llm",
    name="My LLM Call",
    tags=["tutorial"],
    metadata={"githash": "e38f04c83"},
)
def call_openai(
    messages: List[dict], model: str = "gpt-3.5-turbo", temperature: float = 0.0
) -> str:
    return openai.ChatCompletion.create(
        model=model,
        messages=messages,
        temperature=temperature,
    )
// Using the TypeScript SDK
import { RunTree, RunTreeConfig } from "langsmith";

const parentRunConfig: RunTreeConfig = {
    name: "My Chat Bot",
    run_type: "chain",
    inputs: {
        text: "Summarize this morning's meetings.",
    },
    extra: {
        metadata: {"githash": "e38f04c83"}
    },
    tags=["tutorial"]
};

const parentRun = new RunTree(parentRunConfig);
await parentRun.postRun();
# Using the REST API (in Python)
requests.post(
    "https://api.smith.langchain.com/runs",
    json={
        "id": run_id,
        "name": "My Run",
        "run_type": "chain",
        "inputs": {"text": "Foo"},
        "start_time": datetime.datetime.utcnow().isoformat(),
        "session_name": project_name,
        "tags": ["langsmith", "rest", "my-example"],
        "extra": {
            "metadata": {"my_key": "My value"},
        },
    },
    headers={"x-api-key": _LANGSMITH_API_KEY},
)

Case Study: Testing Different LLM Providers in Chat LangChain

We can use metadata and tagging in LangSmith to group data into different categories and analyze performance metrics for each category alongside each other.

LLM Latency

Chart in LangSmith showing LLM latency over time

Time to First Token

Chart in LangSmith showing time-to-first-token over time

Feedback

Chart in LangSmith showing User Score (binary) over time

These charts demonstrate the capability to identify variations or discrepancies between different LLM providers and make data-driven decisions about the application.

Other Use-Cases

The grouping feature in LangSmith can be applied to other use-cases:

A/B Testing with Revisions

By sending up a revision identifier in the metadata and grouping by this revision in your charts, you can clearly see how each version performs with respect to each other.

Enhancing User Experience

By grouping data using user_id or conversation_id in metadata, you can gain an in-depth understanding of how different users are experiencing the application and identify any user-specific issues or trends.

These examples just scratch the surface of what’s possible with LangSmith’s new grouping feature. For more information and to sign up for LangSmith,

Langchain
Grouped
ChatGPT
Monitoring
Recommended from ReadMedium