로컬에서 평가 실행하기

로컬에서 평가 실행하기 (Python 전용)

때로는 결과를 LangSmith에 업로드하지 않고 로컬에서 평가를 실행하는 것이 유용할 때가 있어요. 예를 들어 프롬프트를 빠르게 반복하면서 몇 개 예제로 스모크 테스트를 하고 싶거나, 대상(target) 및 평가자 함수가 올바르게 정의됐는지 검증하고 싶을 때 이러한 평가를 기록하고 싶지 않을 수 있어요.

LangSmith Python SDK를 사용하고 evaluate() / aevaluate()upload_results=False를 전달하면 이렇게 할 수 있어요.

이렇게 하면 애플리케이션과 평가자를 평소와 똑같이 실행하고 동일한 출력을 반환하지만, LangSmith에는 아무것도 기록되지 않아요. 실험 결과뿐 아니라 애플리케이션과 평가자 추적도 포함돼요.

참고: LangSmith에 결과를 업로드하면서도 스크립트에서 처리(품질 게이트, 커스텀 집계 등)해야 한다면 실험 결과 로컬에서 읽기를 참고하세요.

출처: 문서

본문

예시

예시를 살펴볼게요:

langsmith>=0.2.0 필요. 예시는 pandas도 사용해요.

from langsmith import Client

# 1. Create and/or select your dataset
ls_client = Client()
dataset = ls_client.clone_public_dataset(
    "https://smith.langchain.com/public/a63525f9-bdf2-4512-83e3-077dc9417f96/d"
)

# 2. Define an evaluator
def is_concise(outputs: dict, reference_outputs: dict) -> bool:
    return len(outputs["answer"]) < (3 * len(reference_outputs["answer"]))

# 3. Define the interface to your app
def chatbot(inputs: dict) -> dict:
    return {"answer": inputs["question"] + " is a good question. I don't know the answer."}

# 4. Run an evaluation
experiment = ls_client.evaluate(
    chatbot,
    data=dataset,
    evaluators=[is_concise],
    experiment_prefix="my-first-experiment",
    # 'upload_results' is the relevant arg.
    upload_results=False
)

# 5. Analyze results locally
results = list(experiment)

# Check if 'is_concise' returned False.
failed = [r for r in results if not r["evaluation_results"]["results"][0].score]

# Explore the failed inputs and outputs.
for r in failed:
    print(r["example"].inputs)
    print(r["run"].outputs)

# Explore the results as a Pandas DataFrame.
# Must have 'pandas' installed.
df = experiment.to_pandas()
df[["inputs.question", "outputs.answer", "reference.answer", "feedback.is_concise"]]
{'question': 'What is the largest mammal?'}
{'answer': "What is the largest mammal? is a good question. I don't know the answer."}
{'question': 'What do mammals and birds have in common?'}
{'answer': "What do mammals and birds have in common? is a good question. I don't know the answer."}
inputs.question outputs.answer reference.answer feedback.is_concise
0 What is the largest mammal? What is the largest mammal? is a good question. I don't know the answer. The blue whale False
1 What do mammals and birds have in common? What do mammals and birds have in common? is a good question. I don't know the answer. They are both warm-blooded False

더 알아보기