코드에서 애플리케이션을 평가하려면 애플리케이션을 실행할 방법이 필요합니다. evaluate() (Python/TypeScript)를 사용할 때 target function 인자를 전달하여 이를 수행합니다. 이는 dataset Example의 입력을 받아 애플리케이션 출력을 dict로 반환하는 함수입니다. 이 함수 내에서 원하는 방식으로 애플리케이션을 호출할 수 있습니다. 또한 원하는 방식으로 출력을 포맷할 수 있습니다. 핵심은 정의하는 모든 evaluator 함수가 target function에서 반환하는 출력 형식과 호환되어야 한다는 것입니다.
Copy
from langsmith import Client# 'inputs' will come from your dataset.def dummy_target(inputs: dict) -> dict: return {"foo": 1, "bar": "two"}# 'inputs' will come from your dataset.# 'outputs' will come from your target function.def evaluator_one(inputs: dict, outputs: dict) -> bool: return outputs["foo"] == 2def evaluator_two(inputs: dict, outputs: dict) -> bool: return len(outputs["bar"]) < 3client = Client()results = client.evaluate( dummy_target, # <-- target function data="your-dataset-name", evaluators=[evaluator_one, evaluator_two], ...)
evaluate()는 자동으로 target function을 추적합니다. 즉, target function 내에서 추적 가능한 코드를 실행하면 이것도 target trace의 하위 run으로 추적됩니다.
from langsmith import wrappersfrom openai import OpenAI# Optionally wrap the OpenAI client to automatically# trace all model calls.oai_client = wrappers.wrap_openai(OpenAI())def target(inputs: dict) -> dict: # This assumes your dataset has inputs with a 'messages' key. # You can update to match your dataset schema. messages = inputs["messages"] response = oai_client.chat.completions.create( messages=messages, model="gpt-4o-mini", ) return {"answer": response.choices[0].message.content}
from my_agent import agent # This is the function you will evaluate.def target(inputs: dict) -> dict: # This assumes your dataset has inputs with a `messages` key messages = inputs["messages"] # Replace `invoke` with whatever you use to call your agent response = agent.invoke({"messages": messages}) # This assumes your agent output is in the right format return response
dataset에 정의된 입력을 받아들이고 evaluator에서 사용하려는 출력 형식을 반환하는 LangGraph/LangChain agent가 있다면, 해당 객체를 target으로 직접 전달할 수 있습니다:
Copy
from my_agent import agentfrom langsmith import Clientclient = Client()client.evaluate(agent, ...)