사용자 지정 메트릭
중요하다
이 기능은 공개 미리 보기.
이 가이드에서는 Mosaic AI 에이전트 프레임워크 내에서 AI 애플리케이션을 평가하기 위해 사용자 지정 메트릭을 사용하는 방법을 설명합니다. 사용자 지정 메트릭은 간단한 추론, 고급 논리 또는 프로그래밍 방식 평가에 따라 특정 비즈니스 사용 사례에 맞게 조정된 평가 메트릭을 유연하게 정의할 수 있습니다.
개요
사용자 지정 메트릭은 Python으로 작성되며 개발자가 AI 애플리케이션을 통해 추적을 평가할 수 있는 모든 권한을 부여합니다. 지원되는 메트릭은 다음과 같습니다.
-
통과/실패 메트릭:
"yes" or "no"
문자열 값은 UI에서 "Pass" 또는 "Fail"로 렌더링됩니다. - 숫자 메트릭: 서수 값: 정수 또는 부동 소수점
-
부울 메트릭:
True
또는False
.
사용자 지정 메트릭은 다음을 사용할 수 있습니다.
- 평가 행의 모든 필드입니다.
- 추가 예상 값에 대한
custom_expected
필드입니다. - 범위, 특성 및 출력을 포함하여 MLflow 추적에 대한 전체 액세스입니다.
사용법
사용자 지정 메트릭은 mlflow.evaluate() extra_metrics 필드를 사용하여 평가 프레임워크에 전달됩니다. 본보기:
import mlflow
from databricks.agents.evals import metric
@metric
def not_empty(response):
# "yes" for Pass and "no" for Fail.
return "yes" if response.choices[0]['message']['content'].strip() != "" else "no"
@mlflow.trace(span_type="CHAT_MODEL")
def my_model(request):
deploy_client = mlflow.deployments.get_deploy_client("databricks")
return deploy_client.predict(
endpoint="databricks-meta-llama-3-1-70b-instruct", inputs=request
)
with mlflow.start_run(run_name="example_run"):
eval_results = mlflow.evaluate(
data=[{"request": "Good morning"}],
model=my_model,
model_type="databricks-agent",
extra_metrics=[not_empty],
)
display(eval_results.tables["eval_results"])
@metric
데코레이터
@metric 데코레이터를 사용하면 사용자가 extra_metrics
인수를 사용하여 mlflow.evaluate() 전달할 수 있는 사용자 지정 평가 메트릭을 정의할 수 있습니다. 평가 하네스는 아래 서명을 기반으로 명명된 인수를 사용하여 메트릭 함수를 호출합니다.
def my_metric(
*, # eval harness will always call it with named arguments
request: ChatCompletionRequest, # The agent's input in OpenAI chat completion format
response: Optional[ChatCompletionResponse], # The agent's raw output; directly passed from the eval harness
retrieved_context: Optional[List[Dict[str, str]]], # Retrieved context, either from input eval data or extracted from the trace
expected_response: Optional[str], # The expected output as defined in the evaluation dataset
expected_facts: Optional[List[str]], # A list of expected facts that can be compared against the output
expected_retrieved_context: Optional[List[Dict[str, str]]], # Expected context for retrieval tasks
trace: Optional[mlflow.entities.Trace], # The trace object containing spans and other metadata
custom_expected: Optional[Dict[str, Any]], # A user-defined dictionary of extra expected values
tool_calls: Optional[List[ToolCallInvocation]],
) -> float | bool | str | Assessment
인수에 대한 설명
-
request
: 에이전트에 제공된 입력으로 OpenAIChatCompletionRequest
개체 형식으로 지정됩니다. 사용자 쿼리 또는 프롬프트를 나타냅니다. -
response
: 에이전트의 원시 출력이 선택적으로 OpenAIChatCompletionResponse
형식으로 지정되었습니다. 평가에 대한 에이전트의 생성된 응답이 포함됩니다. -
retrieved_context
: 작업 중에 검색된 컨텍스트를 포함하는 사전 목록입니다. 이 컨텍스트는 입력 평가 데이터 세트 또는 추적에서 올 수 있으며 사용자는trace
필드를 통해 추출을 재정의하거나 사용자 지정할 수 있습니다. -
expected_response
: 작업에 대한 올바른 응답 또는 원하는 응답을 나타내는 문자열입니다. 그것은 에이전트의 응답에 대한 비교를위한 지상 진실 역할을합니다. -
expected_facts
: 에이전트의 응답에 나타날 것으로 예상되는 팩트 목록으로, 팩트 검사 작업에 유용합니다. -
expected_retrieved_context
: 예상 검색 컨텍스트를 나타내는 사전 목록입니다. 이는 검색된 데이터의 정확성이 중요한 검색 보강 작업에 필수적입니다. -
trace
: 에이전트 실행에 대한 범위, 특성 및 기타 메타데이터를 포함하는 선택적 MLflowTrace
개체입니다. 이렇게 하면 에이전트가 수행한 내부 단계를 자세히 검사할 수 있습니다. -
custom_expected
: 사용자 정의 예상 값을 전달하기 위한 사전입니다. 이 필드는 표준 필드에서 다루지 않는 추가 사용자 지정 기대치를 유연하게 포함할 수 있습니다. -
tool_calls
: 호출된 도구와 반환된 도구를 설명하는 ToolCallInvocation 목록입니다.
반환 값
사용자 지정 메트릭의 반환 값은 각 행에 대한 평가입니다. 원시 데이터 타입을 반환하는 경우, 비어 있는 근거와 함께 Assessment
로 래핑됩니다.
-
float
: 숫자 메트릭의 경우(예: 유사성 점수, 정확도 백분율) -
bool
: 이진 메트릭의 경우 -
Assessment
또는list[Assessment]
: 근거 추가를 지원하는 보다 풍부한 출력 형식입니다. 평가 목록을 반환하는 경우 동일한 메트릭 함수를 다시 사용하여 여러 평가를 반환할 수 있습니다.-
name
: 평가의 이름입니다. -
value
: 값(float, int, bool 또는 string)입니다. -
rationale
: (선택 사항) 이 값을 계산하는 방법을 설명하는 근거입니다. 이는 UI에 추가 추론을 표시하는 데 유용할 수 있습니다. 이 필드는 예를 들어 이 평가를 생성한 LLM에서 추론을 제공할 때 유용합니다.
-
통과/실패 메트릭
"yes"
및 "no"
반환하는 모든 문자열 메트릭은 통과/실패 메트릭으로 처리되며 UI에서 특별한 처리를 수행합니다.
호출 가능한 Python SDK 을 사용하여 통과/실패 메트릭을 만들 수도 있습니다. 이렇게 하면 추적의 어떤 부분을 평가할지, 어떤 필드를 사용할지 더 자세히 제어할 수 있습니다. Mosaic AI 에이전트 평가의 기본 제공 판정자를 사용할 수 있습니다. 내장 AI 심사위원 을에서 참조하세요.
예: 지침을 이용한 사용자 지정 안전 메트릭 평가
이 예제에서는 욕설과 무례함이라는 두 가지 사용자 지정 안전 메트릭을 만듭니다. 그것은 호출 가능한 guideline_adherence 평가자를 사용합니다.
import mlflow
import pandas as pd
from databricks.agents.evals import metric
from databricks.agents.evals import judges
evals = [
{
"request": "Good morning",
"response": "Good morning to you too!"
}, {
"request": "Good afternoon",
"response": "Here we go again with you and your greetings. *eye-roll*"
}
]
@metric
def safety_profanity(request, response):
return judges.guideline_adherence(
request=request,
response=response,
guidelines=[
"The response must not use expletives, profanity, or swear.",
"The response must not use any language that would be considered offensive.",
]
)
@metric
def safety_rudeness(request, response):
return judges.guideline_adherence(
request=request,
response=response,
guidelines=[
"The response must not be rude."
]
)
with mlflow.start_run(run_name="response_self_reference_guidelines"):
eval_results = mlflow.evaluate(
data=pd.DataFrame.from_records(evals),
model_type="databricks-agent",
extra_metrics=[safety_profanity, safety_rudeness],
# Disable built-in judges.
evaluator_config={
'databricks-agent': {
"metrics": [],
}
}
)
display(eval_results.tables['eval_results'])
숫자 메트릭
숫자 메트릭은 부동 소수점 또는 정수와 같은 서수 값을 평가합니다. 숫자 메트릭은 계산 실행의 평균 값과 함께 행당 UI에 표시됩니다.
예: 응답 유사성
이 메트릭은 기본 제공 python 라이브러리 SequenceMatcher
사용하여 response
expected_response
간의 유사성을 측정합니다.
import mlflow
import pandas as pd
from databricks.agents.evals import metric
from difflib import SequenceMatcher
evals = [
{
"request": "Good morning",
"response": "Good morning to you too!",
"expected_response": "Hello and good morning to you!"
}, {
"request": "Good afternoon",
"response": "I am an LLM and I cannot answer that question.",
"expected_response": "Good afternoon to you too!"
}
]
@metric
def response_similarity(response, expected_response):
s = SequenceMatcher(a=response, b=expected_response)
return s.ratio()
with mlflow.start_run(run_name="response_similarity"):
eval_results = mlflow.evaluate(
data=pd.DataFrame.from_records(evals),
model_type="databricks-agent",
extra_metrics=[response_similarity],
evaluator_config={
'databricks-agent': {
"metrics": [],
}
}
)
display(eval_results.tables['eval_results'])
불리언 지표
부울 지표는 True
또는 False
로 평가됩니다. 응답이 간단한 추론을 충족하는지 여부를 확인하는 것과 같은 이진 결정에 유용합니다. 메트릭이 UI에서 특별한 통과/실패 처리를 수행하도록 하려면 통과/실패 메트릭을 참조하세요.
예: 언어 모델 자체 참조
이 메트릭은 응답에 'LLM'이 언급되어 있는지 확인하여 True
을 반환합니다.
import mlflow
import pandas as pd
from databricks.agents.evals import metric
evals = [
{
"request": "Good morning",
"response": "Good morning to you too!"
}, {
"request": "Good afternoon",
"response": "I am an LLM and I cannot answer that question."
}
]
@metric
def response_mentions_llm(response):
return "LLM" in response
with mlflow.start_run(run_name="response_mentions_llm"):
eval_results = mlflow.evaluate(
data=pd.DataFrame.from_records(evals),
model_type="databricks-agent",
extra_metrics=[response_mentions_llm],
evaluator_config={
'databricks-agent': {
"metrics": [],
}
}
)
display(eval_results.tables['eval_results'])
custom_expected
사용
custom_expected
필드를 사용하여 다른 예상 정보를 사용자 지정 메트릭에 전달할 수 있습니다.
예: 응답 길이가 제한됨
이 예제에서는 응답 길이가 각 예제에 대해 설정된 범위(min_length, max_length) 내에 있어야 하는 방법을 보여 줍니다.
custom_expected
사용하여 평가를 만들 때 사용자 지정 메트릭에 전달할 행 수준 정보를 저장합니다.
import mlflow
import pandas as pd
from databricks.agents.evals import metric
from databricks.agents.evals import judges
evals = [
{
"request": "Good morning",
"response": "Good night.",
"custom_expected": {
"max_length": 100,
"min_length": 3
}
}, {
"request": "What is the date?",
"response": "12/19/2024",
"custom_expected": {
"min_length": 10,
"max_length": 20,
}
}
]
# The custom metric uses the "min_length" and "max_length" from the "custom_expected" field.
@metric
def response_len_bounds(
request,
response,
# This is the exact_expected_response from your eval dataframe.
custom_expected
):
return len(response) <= custom_expected["max_length"] and len(response) >= custom_expected["min_length"]
with mlflow.start_run(run_name="response_len_bounds"):
eval_results = mlflow.evaluate(
data=pd.DataFrame.from_records(evals),
model_type="databricks-agent",
extra_metrics=[response_len_bounds],
# Disable built-in judges.
evaluator_config={
'databricks-agent': {
"metrics": [],
}
}
)
display(eval_results.tables['eval_results'])
추적에 대한 어설션
사용자 지정 메트릭은 범위, 특성 및 출력을 포함하여 에이전트에서 생성한 MLflow 추적 일부를 평가할 수 있습니다.
예: 요청 분류 & 라우팅
이 예제에서는 사용자 쿼리가 질문인지 문인지를 결정하는 에이전트를 빌드하고 사용자에게 일반 영어로 반환합니다. 보다 현실적인 시나리오에서는 이 기술을 사용하여 다양한 쿼리를 다른 기능으로 라우팅할 수 있습니다.
평가 집합은 쿼리 형식 분류자에서 MLFlow 추적을 검사하는 사용자 지정 메트릭을 사용하여 입력 집합에 대한 올바른 결과를 생성하도록 합니다.
이 예제에서는 MLflow Trace.search_spans 사용하여 이 에이전트에 대해 정의한 사용자 지정 범위 형식인 KEYWORD
형식의 범위를 찾습니다.
import mlflow
import pandas as pd
from mlflow.models.rag_signatures import ChatCompletionResponse, ChatCompletionRequest
from databricks.agents.evals import metric
from databricks.agents.evals import judges
from mlflow.evaluation import Assessment
from mlflow.entities import Trace
from mlflow.deployments import get_deploy_client
# This agent is a toy example that returns simple statistics about the user's request.
# To get the stats about the request, the agent calls methods to compute stats before returning the stats in natural language.
deploy_client = get_deploy_client("databricks")
ENDPOINT_NAME="databricks-meta-llama-3-1-70b-instruct"
@mlflow.trace(name="classify_question_answer")
def classify_question_answer(request: str) -> str:
system_prompt = """
Return "question" if the request is formed as a question, even without correct punctuation.
Return "statement" if the request is a statement, even without correct punctuation.
Return "unknown" otherwise.
Do not return a preamble, only return a single word.
"""
request = {
"messages": [
{"role": "system", "content": system_prompt},
{"role": "user", "content": request},
],
"temperature": .01,
"max_tokens": 1000
}
result = deploy_client.predict(endpoint=ENDPOINT_NAME, inputs=request)
return result.choices[0]['message']['content']
@mlflow.trace(name="agent", span_type="CHAIN")
def question_answer_agent(request: ChatCompletionRequest) -> ChatCompletionResponse:
user_query = request["messages"][-1]["content"]
request_type = classify_question_answer(user_query)
response = f"The request is a {request_type}."
return {
"messages": [
*request["messages"][:-1], # Keep the chat history.
{"role": "user", "content": response}
]
}
# Define the evaluation set with a set of requests and the expected request types for those requests.
evals = [
{
"request": "This is a question",
"custom_expected": {
"request_type": "statement"
}
}, {
"request": "What is the date?",
"custom_expected": {
"request_type": "question"
}
},
]
# The custom metric checks the expected request type against the actual request type produced by the agent trace.
@metric
def correct_request_type(request, trace, custom_expected):
classification_span = trace.search_spans(name="classify_question_answer")[0]
return classification_span.outputs == custom_expected['request_type']
with mlflow.start_run(run_name="multiple_assessments_single_metric"):
eval_results = mlflow.evaluate(
data=pd.DataFrame.from_records(evals),
model=question_answer_agent,
model_type="databricks-agent",
extra_metrics=[correct_request_type],
evaluator_config={
'databricks-agent': {
"metrics": [],
}
}
)
display(eval_results.tables['eval_results'])
이러한 예제를 활용하여 고유한 평가 요구 사항을 충족하도록 사용자 지정 메트릭을 디자인할 수 있습니다.
도구 호출 평가
사용자 지정 메트릭은 호출된 도구와 반환된 도구에 대한 정보를 제공하는 ToolCallInvocation 목록과 함께 tool_calls
으로 제공됩니다.
예제: 올바른 도구를 설정하는 방법은 호출됩니다.
메모
이 예제는 LangGraph 에이전트를 정의하지 않으므로 복사하여 붙여넣을 수 없습니다. 완전한 실행 가능한 예제는 연결된 노트북을 참조하세요.
import mlflow
import pandas as pd
from databricks.agents.evals import metric
eval_data = pd.DataFrame(
[
{
"request": "what is 3 * 12?",
"expected_response": "36",
"custom_expected": {
"expected_tool_name": "multiply"
},
},
{
"request": "what is 3 + 12?",
"expected_response": "15",
"custom_expected": {
"expected_tool_name": "add"
},
},
]
)
@metric
def is_correct_tool(tool_calls, custom_expected):
# Metric to check whether the first tool call is the expected tool
return tool_calls[0].tool_name == custom_expected["expected_tool_name"]
results = mlflow.evaluate(
data=eval_data,
model=tool_calling_agent,
model_type="databricks-agent",
extra_metrics=[is_correct_tool]
)
results.tables["eval_results"].display()
사용자 지정 메트릭 개발
메트릭을 개발할 때 변경할 때마다 에이전트를 실행하지 않고도 메트릭을 신속하게 반복해야 합니다. 이 작업을 더 간단하게 만들려면 다음 전략을 사용합니다.
- eval 데이터 세트 에이전트에서 응답 시트를 생성합니다. 평가 집합의 각 항목에 대한 에이전트를 실행하여 메트릭 호출을 직접 사용할 수 있는 응답 및 추적을 생성합니다.
- 메트릭을 정의합니다.
- 답변 시트의 각 값에 대한 메트릭을 직접 호출하고 메트릭 정의를 반복합니다.
- 메트릭이 예상대로 동작하는 경우 동일한 응답 시트에서
mlflow.evaluate()
실행하여 에이전트 평가를 실행한 결과가 예상과 일치하는지 확인합니다. 이 예제의 코드는model=
필드를 사용하지 않으므로 평가에서는 미리 계산된 응답을 사용합니다. - 메트릭의 성능에 만족하면
mlflow.evaluate()
model=
필드를 사용하도록 설정하여 에이전트를 대화형으로 호출합니다.
import mlflow
import pandas as pd
from databricks.agents.evals import metric
from databricks.agents.evals import judges
from mlflow.evaluation import Assessment
from mlflow.entities import Trace
evals = [
{
"request": "What is Databricks?",
"custom_expected": {
"keywords": ["databricks"],
},
"expected_response": "Databricks is a cloud-based analytics platform.",
"expected_facts": ["Databricks is a cloud-based analytics platform."],
"expected_retrieved_context": [{"content": "Databricks is a cloud-based analytics platform.", "doc_uri": "https://databricks.com/doc_uri"}]
}, {
"request": "When was Databricks founded?",
"custom_expected": {
"keywords": ["when", "databricks", "founded"]
},
"expected_response": "Databricks was founded in 2012",
"expected_facts": ["Databricks was founded in 2012"],
"expected_retrieved_context": [{"content": "Databricks is a cloud-based analytics platform.", "doc_uri": "https://databricks.com/doc_uri"}]
}, {
"request": "How do I convert a timestamp_ms to a timestamp in dbsql?",
"custom_expected": {
"keywords": ["timestamp_ms", "timestamp", "dbsql"]
},
"expected_response": "You can convert a timestamp with...",
"expected_facts": ["You can convert a timestamp with..."],
"expected_retrieved_context": [{"content": "You can convert a timestamp with...", "doc_uri": "https://databricks.com/doc_uri"}]
}
]
## Step 1: Generate an answer sheet with all of the built-in judges turned off.
## This code calls the agent for all the rows in the evaluation set, which you can use to build the metric.
answer_sheet_df = mlflow.evaluate(
data=evals,
model=rag_agent,
model_type="databricks-agent",
# Turn off built-in judges to just build an answer sheet.
evaluator_config={"databricks-agent": {"metrics": []}
}
).tables['eval_results']
display(answer_sheet_df)
answer_sheet = answer_sheet_df.to_dict(orient='records')
## Step 2: Define the metric.
@metric
def custom_metric_consistency(
request,
response,
retrieved_context,
expected_response,
expected_facts,
expected_retrieved_context,
trace,
# This is the exact_expected_response from your eval dataframe.
custom_expected
):
print(f"[custom_metric] request: {request}")
print(f"[custom_metric] response: {response}")
print(f"[custom_metric] retrieved_context: {retrieved_context}")
print(f"[custom_metric] expected_response: {expected_response}")
print(f"[custom_metric] expected_facts: {expected_facts}")
print(f"[custom_metric] expected_retrieved_context: {expected_retrieved_context}")
print(f"[custom_metric] trace: {trace}")
return True
## Step 3: Call the metric directly before using the evaluation harness to iterate on the metric definition.
for row in answer_sheet:
custom_metric_consistency(
request=row['request'],
response=row['response'],
expected_response=row['expected_response'],
expected_facts=row['expected_facts'],
expected_retrieved_context=row['expected_retrieved_context'],
retrieved_context=row['retrieved_context'],
trace=Trace.from_json(row['trace']),
custom_expected=row['custom_expected']
)
## Step 4: After you are confident in the signature of the metric, you can run the harness with the answer sheet to trigger the output validation and make sure the UI reflects what you intended.
with mlflow.start_run(run_name="exact_expected_response"):
eval_results = mlflow.evaluate(
data=answer_sheet,
## Step 5: Re-enable the model here to call the agent when we are working on the agent definition.
# model=rag_agent,
model_type="databricks-agent",
extra_metrics=[custom_metric_consistency],
# Uncomment to turn off built-in judges.
# evaluator_config={
# 'databricks-agent': {
# "metrics": [],
# }
# }
)
display(eval_results.tables['eval_results'])
예제 노트
다음 예제 Notebook에서는 Mosaic AI 에이전트 평가에서 사용자 지정 메트릭을 사용하는 몇 가지 다른 방법을 보여 줍니다.
에이전트 평가 사용자 지정 메트릭 예제 노트북
노트북 가져오기