| 1 | # Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | # All rights reserved. |
| 3 | # |
| 4 | # This source code is licensed under the terms described in the LICENSE file in |
| 5 | # the root directory of this source tree. |
| 6 | |
| 7 | from typing import List, Protocol, Union |
| 8 | |
| 9 | from llama_models.schema_utils import json_schema_type, webmethod |
| 10 | |
| 11 | from pydantic import BaseModel |
| 12 | |
| 13 | from llama_models.llama3.api.datatypes import * # noqa: F403 |
| 14 | |
| 15 | |
| 16 | @json_schema_type |
| 17 | class ScoredMessage(BaseModel): |
| 18 | message: Message |
| 19 | score: float |
| 20 | |
| 21 | |
| 22 | @json_schema_type |
| 23 | class DialogGenerations(BaseModel): |
| 24 | dialog: List[Message] |
| 25 | sampled_generations: List[Message] |
| 26 | |
| 27 | |
| 28 | @json_schema_type |
| 29 | class ScoredDialogGenerations(BaseModel): |
| 30 | dialog: List[Message] |
| 31 | scored_generations: List[ScoredMessage] |
| 32 | |
| 33 | |
| 34 | @json_schema_type |
| 35 | class RewardScoringRequest(BaseModel): |
| 36 | """Request to score a reward function. A list of prompts and a list of responses per prompt.""" |
| 37 | |
| 38 | dialog_generations: List[DialogGenerations] |
| 39 | model: str |
| 40 | |
| 41 | |
| 42 | @json_schema_type |
| 43 | class RewardScoringResponse(BaseModel): |
| 44 | """Response from the reward scoring. Batch of (prompt, response, score) tuples that pass the threshold.""" |
| 45 | |
| 46 | scored_generations: List[ScoredDialogGenerations] |
| 47 | |
| 48 | |
| 49 | class RewardScoring(Protocol): |
| 50 | @webmethod(route="/reward_scoring/score") |
| 51 | def reward_score( |
| 52 | self, |
| 53 | dialog_generations: List[DialogGenerations], |
| 54 | model: str, |
| 55 | ) -> Union[RewardScoringResponse]: ... |