-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[MLOB-6998] added pydantic documentation #35758
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jennm
wants to merge
6
commits into
master
Choose a base branch
from
jenn/MLOB-6998
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3db5216
added pydantic documentation
jennm 6662943
Update pydantic_evaluations.md
cswatt 5d40a42
added LLMJudge as example
jennm 1aa2a32
Merge branch 'jenn/MLOB-6998' of github.com:DataDog/documentation int…
jennm d8e25cd
rephrased to make more clear
jennm 5fca433
improved wording
jennm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
content/en/llm_observability/evaluations/deepeval_evaluations.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
content/en/llm_observability/evaluations/pydantic_evaluations.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| --- | ||
| title: Pydantic Evaluations | ||
| description: Use Pydantic evaluations with LLM Observability Experiments. | ||
| further_reading: | ||
| - link: "/llm_observability/evaluations/external_evaluations" | ||
| tag: "Documentation" | ||
| text: "Submit Evaluations" | ||
| --- | ||
|
|
||
| ## Overview | ||
|
|
||
| Pydantic is an open source framework that provides ready-to-use evaluations and allows for customizable LLM evaluations. For more information, see [Pydantic's documentation][3]. | ||
|
|
||
| You can use LLM Observability to run Pydantic evaluations and scalar Pydantic report evaluations in [Experiments][1]. Pydantic evaluation results appear as evaluator results tied to each instance in an [LLM Observability dataset][5]. Pydantic report evaluations run on an entire LLM Observability dataset and report one scalar result for the dataset. | ||
|
|
||
| ## Setup | ||
|
|
||
| 1. Set up an [LLM Observability Experiment][2] and an [LLM Observability Dataset][4]. | ||
| 2. Provide a Pydantic evaluator to the `evaluators` parameter in an LLMObs `Experiment` as demonstrated in the following code sample. (Optional) Provide a Pydantic report evaluator to the `summary_evaluators` parameter in an LLMObs `Experiment`. **Note**: Only Pydantic report evaluators that return a `ScalarResult` are supported. | ||
|
|
||
| ```python | ||
|
|
||
| from pydantic_evals.evaluators import ( | ||
| EqualsExpected, | ||
| EvaluationReason, | ||
| Evaluator, | ||
| EvaluatorContext, | ||
| EvaluatorOutput, | ||
| LLMJudge, | ||
| ReportEvaluator, | ||
| ReportEvaluatorContext, | ||
| ) | ||
| from pydantic_evals.reporting.analyses import ScalarResult | ||
|
|
||
| from ddtrace.llmobs import LLMObs | ||
|
|
||
|
|
||
| LLMObs.enable( | ||
| api_key="<YOUR_API_KEY>", # defaults to DD_API_KEY environment variable | ||
| app_key="<YOUR_APP_KEY>", # defaults to DD_APP_KEY environment variable | ||
| site="datadoghq.com", # defaults to DD_SITE environment variable | ||
| project_name="<YOUR_PROJECT>" # defaults to DD_LLMOBS_PROJECT_NAME environment variable, or "default-project" if the environment variable is not set | ||
| ) | ||
|
|
||
|
|
||
| # this can be any Pydantic evaluator | ||
| @dataclass | ||
| class ComprehensiveCheck(Evaluator): | ||
| def evaluate(self, ctx: EvaluatorContext) -> EvaluatorOutput: | ||
| format_valid = self._check_format(ctx.output) | ||
|
|
||
| to_return = { | ||
| 'valid_format': EvaluationReason( | ||
| value=format_valid, | ||
| reason='Valid JSON format' if format_valid else 'Invalid JSON format', | ||
| ), | ||
| 'quality_score': self._score_quality(ctx.output), | ||
| 'category': self._classify(ctx.output), | ||
| } | ||
| return to_return | ||
|
|
||
| def _check_format(self, output: str) -> bool: | ||
| return output.startswith('{') and output.endswith('}') | ||
|
|
||
| def _score_quality(self, output: str) -> float: | ||
| return len(output) / 100.0 | ||
|
|
||
| def _classify(self, output: str) -> str: | ||
| return 'short' if len(output) < 50 else 'long' | ||
|
|
||
| # This can be any Pydantic ReportEvaluator that returns ScalarResult | ||
| class TotalCasesEvaluator(ReportEvaluator): | ||
| def evaluate(self, ctx: ReportEvaluatorContext) -> ScalarResult: | ||
| return ScalarResult( | ||
| title='Total', | ||
| value=len(ctx.report.cases), | ||
| unit='cases', | ||
| ) | ||
|
|
||
| dataset = LLMObs.create_dataset( | ||
| dataset_name="capitals-of-the-world", | ||
| project_name="capitals-project", # optional, defaults to project_name used in LLMObs.enable | ||
| description="Questions about world capitals", | ||
| records=[ | ||
| { | ||
| "input_data": { | ||
| "question": "What is the capital of China?" | ||
| }, # required, JSON or string | ||
| "expected_output": "Beijing", # optional, JSON or string | ||
| "metadata": {"difficulty": "easy"}, # optional, JSON | ||
| }, | ||
| { | ||
| "input_data": { | ||
| "question": "Which city serves as the capital of South Africa?" | ||
| }, | ||
| "expected_output": "Pretoria", | ||
| "metadata": {"difficulty": "medium"}, | ||
| }, | ||
| ], | ||
| ) | ||
|
|
||
| def task(input_data: Dict[str, Any], config: Optional[Dict[str, Any]] = None, metadata: Optional[Dict[str, Any]] = None) -> str: | ||
| question = input_data['question'] | ||
| # Your LLM or processing logic here | ||
| return "Beijing" if "China" in question else "Unknown" | ||
|
|
||
|
|
||
| llm_judge = LLMJudge( | ||
| rubric='Response provides the same answer as expected, possibly with explanation', | ||
| include_input=True, | ||
| include_expected_output=True, | ||
| ) | ||
|
|
||
| experiment = LLMObs.experiment( | ||
| name="<EXPERIMENT_NAME>", | ||
| task=my_task, | ||
| dataset=dataset, | ||
| evaluators=[EqualsExpected(), ComprehensiveCheck(), llm_judge], | ||
| summary_evaluators=[TotalCasesEvaluator()], | ||
| description="<EXPERIMENT_DESCRIPTION>", | ||
| ) | ||
|
|
||
|
|
||
| results = experiment.run(jobs=4, raise_errors=True) | ||
|
|
||
| print(f"View experiment: {experiment.url}") | ||
| ``` | ||
|
|
||
| For a working example, see [Datadog's Pydantic demo in GitHub][6]. | ||
|
|
||
| ### Usage | ||
| After you run an experiment with a Pydantic evaluation, you can view the Pydantic evaluation results per instance in the corresponding experiment run in Datadog. In the following experiment, two Pydantic evaluations (a custom Pydantic evaluator with the name "ComprehensiveCheck" and a built-in evaluator with the name "EqualsExpected") and one Pydantic report evaluator (a custom Pydantic report evaluator with the name "TotalCasesEvaluator") were run: | ||
|
|
||
| {{< img src="llm_observability/pydantic-experiment-result.png" alt="An LLM Observability experiment with a Pydantic evaluator." style="width:100%;" >}} | ||
|
|
||
| ## Further reading | ||
|
|
||
| {{< partial name="whats-next/whats-next.html" >}} | ||
|
|
||
| [1]: /llm_observability/experiments | ||
| [2]: /llm_observability/experiments/setup#create-an-experiment | ||
| [3]: https://ai.pydantic.dev/evals/ | ||
| [4]: /llm_observability/experiments/setup#create-a-dataset | ||
| [5]: /llm_observability/experiments/datasets | ||
| [6]: https://github.com/DataDog/llm-observability/blob/main/experiments/eval-integrations/2-pydantic-demo.py | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.