-
Notifications
You must be signed in to change notification settings - Fork 833
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: few shot example optimzier (#1739)
optimize with few short examples ```py from ragas.metrics import AspectCritic from ragas.llms import llm_factory # define metric llm = llm_factory("gpt-4o") metric = AspectCritic( name="answer_correctness", definition="Given the user_input, reference and response. Is the response correct compared with the reference", llm=llm, ) # optimize with annotation from ragas.config import DemonstrationConfig demonstration_config = DemonstrationConfig() metric.train( "alignment_sample.json", demonstration_config=demonstration_config, ) ```
- Loading branch information
Showing
9 changed files
with
334 additions
and
57 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,27 +1,39 @@ | ||
from __future__ import annotations | ||
|
||
import typing as t | ||
|
||
from pydantic import BaseModel, Field | ||
from pydantic import BaseModel, Field, field_validator | ||
|
||
from ragas.embeddings import BaseRagasEmbeddings | ||
from ragas.llms import BaseRagasLLM | ||
from ragas.embeddings.base import BaseRagasEmbeddings | ||
from ragas.llms.base import BaseRagasLLM | ||
from ragas.losses import Loss | ||
from ragas.optimizers import GeneticOptimizer, Optimizer | ||
|
||
DEFAULT_OPTIMIZER_CONFIG = {"max_steps": 100} | ||
|
||
|
||
class DemonstrationConfig(BaseModel): | ||
embedding: t.Any # this has to be of type Any because BaseRagasEmbedding is an ABC | ||
enabled: bool = True | ||
top_k: int = 3 | ||
threshold: float = 0.7 | ||
technique: t.Literal["random", "similarity"] = "similarity" | ||
embedding: t.Optional[BaseRagasEmbeddings] = None | ||
|
||
@field_validator("embedding") | ||
def validate_embedding(cls, v): | ||
if not isinstance(v, BaseRagasEmbeddings): | ||
raise ValueError("embedding must be an instance of BaseRagasEmbeddings") | ||
return v | ||
|
||
|
||
class InstructionConfig(BaseModel): | ||
llm: BaseRagasLLM | ||
enabled: bool = True | ||
loss: t.Optional[Loss] = None | ||
optimizer: Optimizer = GeneticOptimizer() | ||
optimizer_config: t.Dict[str, t.Any] = Field( | ||
default_factory=lambda: DEFAULT_OPTIMIZER_CONFIG | ||
) | ||
llm: t.Optional[BaseRagasLLM] = None | ||
|
||
|
||
InstructionConfig.model_rebuild() |
This file contains 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
This file contains 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
Oops, something went wrong.