langchain.smith.evaluation.runner_utils
.arun_on_dataset¶
- async langchain.smith.evaluation.runner_utils.arun_on_dataset(client: Optional[Client], dataset_name: str, llm_or_chain_factory: Union[Callable[[], Union[Chain, Runnable]], BaseLanguageModel, Callable[[dict], Any], Runnable, Chain], *, evaluation: Optional[RunEvalConfig] = None, dataset_version: Optional[Union[datetime, str]] = None, concurrency_level: int = 5, project_name: Optional[str] = None, project_metadata: Optional[Dict[str, Any]] = None, verbose: bool = False, revision_id: Optional[str] = None, **kwargs: Any) Dict[str, Any] [source]¶
在数据集上运行链或语言模型,并将追踪存储到指定的项目名称。
- 参数
dataset_name (str) - 运行链的数据集名称。
llm_or_chain_factory (联合[可调用[[], 联合[链, 可运行对象]], 基础语言模型, 可调用[[字典], 任何], 可运行对象, 链]) – 用于在数据集上运行的语料库或链构造函数。链构造函数用于允许在每次示例上独立调用而不携带状态。
evaluation (可选[运行评估配置]) – 评估器在链的结果上运行的配置
concurrency_level (整数) – 同时运行的异步任务数量。
project_name (可选[字符串]) – 将跟踪的跟踪存储在的项目名称。默认为 {dataset_name}-{chain class name}-{datetime}。
project_metadata (可选[字典[字符串, 任何]]) – 可选的附加到项目中的元数据。用于存储有关测试变体的信息。(提示版本,模型版本等。)
client (可选[客户端]) – LangSmith客户端用于访问数据集,以记录反馈和运行跟踪。
verbose (布尔值) – 是否打印进度。
tags – 添加到项目中每个运行的标签。
revision_id (可选[字符串]) – 可选的修订标识符以跟踪不同版本的系统性能。
dataset_version (可选[联合[datetime, 字符串]]) –
kwargs (任何) –
- 返回
包含运行项目名称和模型的输出的字典。
- 返回类型
字典[字符串,任何]
有关此函数的(通常更快的)异步版本,请参阅
arun_on_dataset()
。示例
from langsmith import Client from langchain_openai import ChatOpenAI from langchain.chains import LLMChain from langchain.smith import smith_eval.RunEvalConfig, run_on_dataset # Chains may have memory. Passing in a constructor function lets the # evaluation framework avoid cross-contamination between runs. def construct_chain(): llm = ChatOpenAI(temperature=0) chain = LLMChain.from_string( llm, "What's the answer to {your_input_key}" ) return chain # Load off-the-shelf evaluators via config or the EvaluatorType (string or enum) evaluation_config = smith_eval.RunEvalConfig( evaluators=[ "qa", # "Correctness" against a reference answer "embedding_distance", smith_eval.RunEvalConfig.Criteria("helpfulness"), smith_eval.RunEvalConfig.Criteria({ "fifth-grader-score": "Do you have to be smarter than a fifth grader to answer this question?" }), ] ) client = Client() await arun_on_dataset( client, dataset_name="<my_dataset_name>", llm_or_chain_factory=construct_chain, evaluation=evaluation_config, )
您还可以通过继承
StringEvaluator
或 LangSmith 的 RunEvaluator 类来创建自定义评估器。from typing import Optional from langchain.evaluation import StringEvaluator class MyStringEvaluator(StringEvaluator): @property def requires_input(self) -> bool: return False @property def requires_reference(self) -> bool: return True @property def evaluation_name(self) -> str: return "exact_match" def _evaluate_strings(self, prediction, reference=None, input=None, **kwargs) -> dict: return {"score": prediction == reference} evaluation_config = smith_eval.RunEvalConfig( custom_evaluators = [MyStringEvaluator()], ) await arun_on_dataset( client, dataset_name="<my_dataset_name>", llm_or_chain_factory=construct_chain, evaluation=evaluation_config, )