!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){var t=null;try{t=new URLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}return t}()||function(){var t=null;try{t=localStorage.getItem("theme")}catch(t){}return t}();t(null!==e?e:"light")}(),document.documentElement.setAttribute("data-announcement-bar-initially-dismissed",function(){try{return"true"===localStorage.getItem("docusaurus.announcement.dismiss")}catch(t){}return!1}())

[Skip to main content](#__docusaurus_skipToContent_fallback)

LangChain v0.2 is coming soon! Preview the new docs [here](/v0.2/docs/introduction/).

[

![🦜️🔗 Langchain](/v0.1/img/brand/wordmark.png)![🦜️🔗 Langchain](/v0.1/img/brand/wordmark-dark.png)

](/v0.1/)[Docs](/v0.1/docs/get_started/introduction/)[Use cases](/v0.1/docs/use_cases/)[Integrations](/v0.1/docs/integrations/platforms/)[API Reference](https://api.js.langchain.com)

[More](#)

*   [People](/v0.1/docs/people/)
*   [Community](/v0.1/docs/community/)
*   [Tutorials](/v0.1/docs/additional_resources/tutorials/)
*   [Contributing](/v0.1/docs/contributing/)

[v0.1](#)

*   [v0.2](https://js.langchain.com/v0.2/docs/introduction)
*   [v0.1](/v0.1/docs/get_started/introduction/)

[🦜🔗](#)

*   [LangSmith](https://smith.langchain.com)
*   [LangSmith Docs](https://docs.smith.langchain.com)
*   [LangChain Hub](https://smith.langchain.com/hub)
*   [LangServe](https://github.com/langchain-ai/langserve)
*   [Python Docs](https://python.langchain.com/)

[Chat](https://chatjs.langchain.com)[](https://github.com/langchain-ai/langchainjs)

Search

*   [Get started](/v0.1/docs/get_started/)
    
    *   [Introduction](/v0.1/docs/get_started/introduction/)
    *   [Installation](/v0.1/docs/get_started/installation/)
    *   [Quickstart](/v0.1/docs/get_started/quickstart/)
*   [LangChain Expression Language](/v0.1/docs/expression_language/)
    
    *   [Get started](/v0.1/docs/expression_language/get_started/)
    *   [Why use LCEL?](/v0.1/docs/expression_language/why/)
    *   [Interface](/v0.1/docs/expression_language/interface/)
    *   [Streaming](/v0.1/docs/expression_language/streaming/)
    *   [How to](/v0.1/docs/expression_language/how_to/routing/)
        
    *   [Cookbook](/v0.1/docs/expression_language/cookbook/)
        
    *   [LangChain Expression Language (LCEL)](/v0.1/docs/expression_language/)
*   [Modules](/v0.1/docs/modules/)
    
    *   [Model I/O](/v0.1/docs/modules/model_io/)
        
    *   [Retrieval](/v0.1/docs/modules/data_connection/)
        
    *   [Chains](/v0.1/docs/modules/chains/)
    *   [Agents](/v0.1/docs/modules/agents/)
        
    *   [More](/v0.1/docs/modules/memory/)
        
*   [Security](/v0.1/docs/security/)
*   [Guides](/v0.1/docs/guides/)
    
    *   [Debugging](/v0.1/docs/guides/debugging/)
    *   [Deployment](/v0.1/docs/guides/deployment/)
        
    *   [Evaluation](/v0.1/docs/guides/evaluation/)
        
        *   [String Evaluators](/v0.1/docs/guides/evaluation/string/)
            
        *   [Comparison Evaluators](/v0.1/docs/guides/evaluation/comparison/)
            
        *   [Trajectory Evaluators](/v0.1/docs/guides/evaluation/trajectory/)
            
        *   [Examples](/v0.1/docs/guides/evaluation/examples/)
            
            *   [Comparing Chain Outputs](/v0.1/docs/guides/evaluation/examples/comparisons/)
    *   [Extending LangChain.js](/v0.1/docs/guides/extending_langchain/)
    *   [Fallbacks](/v0.1/docs/guides/fallbacks/)
    *   [LangSmith Walkthrough](/v0.1/docs/guides/langsmith_evaluation/)
    *   [Migrating to 0.1](/v0.1/docs/guides/migrating/)
*   [Ecosystem](/v0.1/docs/ecosystem/)
    
*   [LangGraph](/v0.1/docs/langgraph/)
*   * * *
    

*   [](/v0.1/)
*   [Guides](/v0.1/docs/guides/)
*   [Evaluation](/v0.1/docs/guides/evaluation/)
*   [Examples](/v0.1/docs/guides/evaluation/examples/)
*   Comparing Chain Outputs

Comparing Chain Outputs
=======================

Suppose you have two different prompts (or LLMs). How do you know which will generate "better" results?

One automated way to predict the preferred configuration is to use a `PairwiseStringEvaluator` like the `PairwiseStringEvalChain`[\[1\]](#cite_note-1). This chain prompts an LLM to select which output is preferred, given a specific input.

For this evaluation, we will need 3 things:

1.  An evaluator
2.  A dataset of inputs
3.  2 (or more) LLMs, Chains, or Agents to compare

Then we will aggregate the restults to determine the preferred model.

tip

See [this section for general instructions on installing integration packages](/v0.1/docs/get_started/installation/#installing-integration-packages).

*   npm
*   Yarn
*   pnpm

    npm install @langchain/openai

    yarn add @langchain/openai

    pnpm add @langchain/openai

    import { loadEvaluator } from "langchain/evaluation";import { initializeAgentExecutorWithOptions } from "langchain/agents";import { ChatOpenAI } from "@langchain/openai";import { ChainValues } from "@langchain/core/utils/types";import { SerpAPI } from "@langchain/community/tools/serpapi";//  Step 1. Create the Evaluator// In this example, you will use gpt-4 to select which output is preferred.const evalChain = await loadEvaluator("pairwise_string");//  Step 2. Select Dataset// If you already have real usage data for your LLM, you can use a representative sample. More examples// provide more reliable results. We will use some example queries someone might have about how to use langchain here.const dataset = [  "Can I use LangChain to automatically rate limit or retry failed API calls?",  "How can I ensure the accuracy and reliability of the travel data with LangChain?",  "How can I track student progress with LangChain?",  "langchain how to handle different document formats?",  // "Can I chain API calls to different services in LangChain?",  // "How do I handle API errors in my langchain app?",  // "How do I handle different currency and tax calculations with LangChain?",  // "How do I extract specific data from the document using langchain tools?",  // "Can I use LangChain to handle real-time data from these APIs?",  // "Can I use LangChain to track and manage travel alerts and updates?",  // "Can I use LangChain to create and grade quizzes from these APIs?",  // "Can I use LangChain to automate data cleaning and preprocessing for the AI plugins?",  // "How can I ensure the accuracy and reliability of the financial data with LangChain?",  // "Can I integrate medical imaging tools with LangChain?",  // "How do I ensure the privacy and security of the patient data with LangChain?",  // "How do I handle authentication for APIs in LangChain?",  // "Can I use LangChain to recommend personalized study materials?",  // "How do I connect to the arXiv API using LangChain?",  // "How can I use LangChain to interact with educational APIs?",  // "langchain how to sort retriever results - relevance or date?",  // "Can I integrate a recommendation engine with LangChain to suggest products?"];// Step 3. Define Models to Compare// We will be comparing two agents in this case.const model = new ChatOpenAI({  temperature: 0,  model: "gpt-3.5-turbo-16k-0613",});const serpAPI = new SerpAPI(process.env.SERPAPI_API_KEY, {  location: "Austin,Texas,United States",  hl: "en",  gl: "us",});serpAPI.description =  "Useful when you need to answer questions about current events. You should ask targeted questions.";const tools = [serpAPI];const conversationAgent = await initializeAgentExecutorWithOptions(  tools,  model,  {    agentType: "chat-zero-shot-react-description",  });const functionsAgent = await initializeAgentExecutorWithOptions(tools, model, {  agentType: "openai-functions",});// Step 4. Generate Responses// We will generate outputs for each of the models before evaluating them.const results = [];const agents = [functionsAgent, conversationAgent];const concurrencyLevel = 4; // How many concurrent agents to run. May need to decrease if OpenAI is rate limiting.// We will only run the first 20 examples of this dataset to speed things up// This will lead to larger confidence intervals downstream.const batch = [];for (const example of dataset) {  batch.push(    Promise.all(agents.map((agent) => agent.invoke({ input: example })))  );  if (batch.length >= concurrencyLevel) {    const batchResults = await Promise.all(batch);    results.push(...batchResults);    batch.length = 0;  }}if (batch.length) {  const batchResults = await Promise.all(batch);  results.push(...batchResults);}console.log(JSON.stringify(results));// Step 5. Evaluate Pairs// Now it's time to evaluate the results. For each agent response, run the evaluation chain to select which output is preferred (or return a tie).// Randomly select the input order to reduce the likelihood that one model will be preferred just because it is presented first.const preferences = await predictPreferences(dataset, results);// Print out the ratio of preferences.const nameMap: { [key: string]: string } = {  a: "OpenAI Functions Agent",  b: "Structured Chat Agent",};const counts = counter(preferences);const prefRatios: { [key: string]: number } = {};for (const k of Object.keys(counts)) {  prefRatios[k] = counts[k] / preferences.length;}for (const k of Object.keys(prefRatios)) {  console.log(`${nameMap[k]}: ${(prefRatios[k] * 100).toFixed(2)}%`);}/*OpenAI Functions Agent: 100.00% */// Estimate Confidence Intervals// The results seem pretty clear, but if you want to have a better sense of how confident we are, that model "A" (the OpenAI Functions Agent) is the preferred model, we can calculate confidence intervals.// Below, use the Wilson score to estimate the confidence interval.for (const [which_, name] of Object.entries(nameMap)) {  const [low, high] = wilsonScoreInterval(preferences, which_);  console.log(    `The "${name}" would be preferred between ${(low * 100).toFixed(2)}% and ${(      high * 100    ).toFixed(2)}% percent of the time (with 95% confidence).`  );}/*The "OpenAI Functions Agent" would be preferred between 51.01% and 100.00% percent of the time (with 95% confidence).The "Structured Chat Agent" would be preferred between 0.00% and 48.99% percent of the time (with 95% confidence). */function counter(arr: string[]): { [key: string]: number } {  return arr.reduce(    (countMap: { [key: string]: number }, word: string) => ({      ...countMap,      [word]: (countMap[word] || 0) + 1,    }),    {}  );}async function predictPreferences(dataset: string[], results: ChainValues[][]) {  const preferences: string[] = [];  for (let i = 0; i < dataset.length; i += 1) {    const input = dataset[i];    const resA = results[i][0];    const resB = results[i][1];    // Flip a coin to reduce persistent position bias    let a;    let b;    let predA;    let predB;    if (Math.random() < 0.5) {      predA = resA;      predB = resB;      a = "a";      b = "b";    } else {      predA = resB;      predB = resA;      a = "b";      b = "a";    }    const evalRes = await evalChain.evaluateStringPairs({      input,      prediction: predA.output || predA.toString(),      predictionB: predB.output || predB.toString(),    });    if (evalRes.value === "A") {      preferences.push(a);    } else if (evalRes.value === "B") {      preferences.push(b);    } else {      preferences.push("None"); // No preference    }  }  return preferences;}function wilsonScoreInterval(  preferences: string[],  which = "a",  z = 1.96): [number, number] {  const totalPreferences = preferences.filter(    (p) => p === "a" || p === "b"  ).length;  const ns = preferences.filter((p) => p === which).length;  if (totalPreferences === 0) {    return [0, 0];  }  const pHat = ns / totalPreferences;  const denominator = 1 + z ** 2 / totalPreferences;  const adjustment =    (z / denominator) *    Math.sqrt(      (pHat * (1 - pHat)) / totalPreferences +        z ** 2 / (4 * totalPreferences ** 2)    );  const center = (pHat + z ** 2 / (2 * totalPreferences)) / denominator;  const lowerBound = Math.min(Math.max(center - adjustment, 0.0), 1.0);  const upperBound = Math.min(Math.max(center + adjustment, 0.0), 1.0);  return [lowerBound, upperBound];}

#### API Reference:

*   [loadEvaluator](https://api.js.langchain.com/functions/langchain_evaluation.loadEvaluator.html) from `langchain/evaluation`
*   [initializeAgentExecutorWithOptions](https://api.js.langchain.com/functions/langchain_agents.initializeAgentExecutorWithOptions.html) from `langchain/agents`
*   [ChatOpenAI](https://api.js.langchain.com/classes/langchain_openai.ChatOpenAI.html) from `@langchain/openai`
*   [ChainValues](https://api.js.langchain.com/types/langchain_core_utils_types.ChainValues.html) from `@langchain/core/utils/types`
*   [SerpAPI](https://api.js.langchain.com/classes/langchain_community_tools_serpapi.SerpAPI.html) from `@langchain/community/tools/serpapi`

1.  Note: Automated evals are still an open research topic and are best used alongside other evaluation approaches. LLM preferences exhibit biases, including banal ones like the order of outputs. In choosing preferences, "ground truth" may not be taken into account, which may lead to scores that aren't grounded in utility.\_

* * *

#### Help us out by providing feedback on this documentation page:

[

Previous

Examples

](/v0.1/docs/guides/evaluation/examples/)[

Next

Extending LangChain.js

](/v0.1/docs/guides/extending_langchain/)

Community

*   [Discord](https://discord.gg/cU2adEyC7w)
*   [Twitter](https://twitter.com/LangChainAI)

GitHub

*   [Python](https://github.com/langchain-ai/langchain)
*   [JS/TS](https://github.com/langchain-ai/langchainjs)

More

*   [Homepage](https://langchain.com)
*   [Blog](https://blog.langchain.dev)

Copyright © 2024 LangChain, Inc.