avatarLaxfed Paulacy

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

2674

Abstract

ork City get its name?" result = qa_chain({"query": question}) result["result"]</pre></div><p id="d3db">Once the QA chain is ready, Ragas metrics can be imported and evaluators can be set up. The following code helps to create evaluator chains and run the evaluations:</p><div id="cfd3"><pre><span class="hljs-keyword">from</span> ragas.metrics <span class="hljs-keyword">import</span> faithfulness, answer_relevancy, context_relevancy, context_recall <span class="hljs-keyword">from</span> ragas.langchain <span class="hljs-keyword">import</span> RagasEvaluatorChain

<span class="hljs-comment"># make eval chains</span> eval_chains = { m.name: RagasEvaluatorChain(metric=m) <span class="hljs-keyword">for</span> m <span class="hljs-keyword">in</span> [faithfulness, answer_relevancy, context_relevancy, context_recall] }

<span class="hljs-comment"># evaluate</span> <span class="hljs-keyword">for</span> name, eval_chain <span class="hljs-keyword">in</span> eval_chains.items(): score_name = <span class="hljs-string">f"<span class="hljs-subst">{name}</span>_score"</span> <span class="hljs-built_in">print</span>(<span class="hljs-string">f"<span class="hljs-subst">{score_name}</span>: <span class="hljs-subst">{eval_chain(result)[score_name]}</span>"</span>)</pre></div><p id="e8da">To visualize the evaluations with LangSmith, the following code can be used to create a test dataset, run the evaluations, and view the results:</p><div id="164a"><pre><span class="hljs-keyword">from</span> langsmith import<span class="hljs-built_in"> Client </span><span class="hljs-keyword">from</span> langchain.smith import RunEvalConfig, run_on_dataset

<span class="hljs-comment"># test dataset</span> eval_questions = [ <span class="hljs-string">"What is the population of New York City as of 2020?"</span>, <span class="hljs-string">"Which borough of New York City has the highest population?"</span>, <span class="hljs-string">"What is the economic significance of New York City?"</span>, <span class="hljs-string">"How did New York City get its name?"</span>, <span class="hljs-string">"What is the significance of the Statue of Liberty in New York City?"</span>, ] eval_answers = [ <span class="hljs-string">"8,804,000"</span>, # incorrect answer <span class="hljs-string">"Queens"</span>, # incorrect answer <span class="hljs-string">"New York City's economic significance is vast, as it serves as the global financial capital, housing Wall Street and major financial institutions. Its diverse economy spans technology, media, healthcare, education, and more, making it resilient to economic fluctuations... "</span>,

Options

other answers

]

examples = [{<span class="hljs-string">"query"</span>: q, <span class="hljs-string">"ground_truths"</span>: [eval_answers[i]]} <span class="hljs-keyword">for</span> i, q <span class="hljs-keyword">in</span> enumerate(eval_questions)]

<span class="hljs-comment"># dataset creation</span><span class="hljs-built_in"> client </span>= Client() dataset_name = <span class="hljs-string">"NYC test"</span> dataset = client.create_dataset(<span class="hljs-attribute">dataset_name</span>=dataset_name, <span class="hljs-attribute">description</span>=<span class="hljs-string">"NYC test dataset"</span>) <span class="hljs-keyword">for</span> example <span class="hljs-keyword">in</span> examples: client.create_example(<span class="hljs-attribute">inputs</span>=example, <span class="hljs-attribute">dataset_id</span>=dataset.id)

<span class="hljs-comment"># factory function that returns a new qa chain</span> def create_qa_chain(<span class="hljs-attribute">return_context</span>=<span class="hljs-literal">True</span>): qa_chain = RetrievalQA.from_chain_type( llm, <span class="hljs-attribute">retriever</span>=index.vectorstore.as_retriever(), <span class="hljs-attribute">return_source_documents</span>=return_context, ) return qa_chain

<span class="hljs-comment"># configure and run the evaluation</span> evaluation_config = RunEvalConfig( custom_evaluators=[eval_chains.values()], <span class="hljs-attribute">prediction_key</span>=<span class="hljs-string">"result"</span>, ) result = run_on_dataset( client, dataset_name, create_qa_chain, <span class="hljs-attribute">evaluation</span>=evaluation_config, <span class="hljs-attribute">input_mapper</span>=lambda x: x, )</pre></div><div id="043b" class="link-block"> <a href="https://readmedium.com/langchain-integrating-chatgpt-with-google-drive-and-notion-data-7234420882af"> <div> <div> <h2>LANGCHAIN — Integrating ChatGPT with Google Drive and Notion Data</h2> <div><h3>Computer science is no more about computers than astronomy is about telescopes. — Edsger W. Dijkstra.</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="5c01">These code snippets illustrate how to leverage Ragas and LangSmith for evaluating QA systems, ensuring robustness and readiness for real-world applications.</p></article></body>

LANGCHAIN — Evaluating Rag Pipelines with Ragas Langsmith

Technology makes it possible for people to gain control over everything, except over technology. — John Tudor.

Ragas and LangSmith offer a robust combination for evaluating LLM-powered QA systems. Ragas provides metrics to evaluate retrieval and generation and calculates a single score, while LangSmith facilitates visualization and continuous evaluations.

To evaluate a QA chain built with Langchain over the NYC Wikipedia page, the following code can be used:

from langchain.document_loaders import WebBaseLoader
from langchain.indexes import VectorstoreIndexCreator
from langchain.chains import RetrievalQA
from langchain.chat_models import ChatOpenAI

# load the Wikipedia page and create index
loader = WebBaseLoader("https://en.wikipedia.org/wiki/New_York_City")
index = VectorstoreIndexCreator().from_loaders([loader])

# create the QA chain
llm = ChatOpenAI()
qa_chain = RetrievalQA.from_chain_type(
    llm, retriever=index.vectorstore.as_retriever(), return_source_documents=True
)

# testing it out
question = "How did New York City get its name?"
result = qa_chain({"query": question})
result["result"]

Once the QA chain is ready, Ragas metrics can be imported and evaluators can be set up. The following code helps to create evaluator chains and run the evaluations:

from ragas.metrics import faithfulness, answer_relevancy, context_relevancy, context_recall
from ragas.langchain import RagasEvaluatorChain

# make eval chains
eval_chains = {
    m.name: RagasEvaluatorChain(metric=m) 
    for m in [faithfulness, answer_relevancy, context_relevancy, context_recall]
}

# evaluate
for name, eval_chain in eval_chains.items():
    score_name = f"{name}_score"
    print(f"{score_name}: {eval_chain(result)[score_name]}")

To visualize the evaluations with LangSmith, the following code can be used to create a test dataset, run the evaluations, and view the results:

from langsmith import Client
from langchain.smith import RunEvalConfig, run_on_dataset

# test dataset
eval_questions = [
    "What is the population of New York City as of 2020?",
    "Which borough of New York City has the highest population?",
    "What is the economic significance of New York City?",
    "How did New York City get its name?",
    "What is the significance of the Statue of Liberty in New York City?",
]
eval_answers = [
    "8,804,000", # incorrect answer
    "Queens",    # incorrect answer
    "New York City's economic significance is vast, as it serves as the global financial capital, housing Wall Street and major financial institutions. Its diverse economy spans technology, media, healthcare, education, and more, making it resilient to economic fluctuations... ",
    # other answers
]

examples = [{"query": q, "ground_truths": [eval_answers[i]]} for i, q in enumerate(eval_questions)]

# dataset creation
client = Client()
dataset_name = "NYC test"
dataset = client.create_dataset(dataset_name=dataset_name, description="NYC test dataset")
for example in examples:
    client.create_example(inputs=example, dataset_id=dataset.id)

# factory function that returns a new qa chain
def create_qa_chain(return_context=True):
    qa_chain = RetrievalQA.from_chain_type(
        llm,
        retriever=index.vectorstore.as_retriever(),
        return_source_documents=return_context,
    )
    return qa_chain

# configure and run the evaluation
evaluation_config = RunEvalConfig(
    custom_evaluators=[eval_chains.values()],
    prediction_key="result",
)
result = run_on_dataset(
    client,
    dataset_name,
    create_qa_chain,
    evaluation=evaluation_config,
    input_mapper=lambda x: x,
)

These code snippets illustrate how to leverage Ragas and LangSmith for evaluating QA systems, ensuring robustness and readiness for real-world applications.

Pipelines
Ragas
Langchain
Evaluating
Rag
Recommended from ReadMedium