|
| 1 | +# Adapting metrics to target language |
| 2 | + |
| 3 | +While using ragas to evaluate LLM application workflows, you may have applications to be evaluated that are in languages other than english. In this case, it is best to adapt your LLM powered evaluation metrics to the target language. One obivous way to do this is to manually change the instruction and demonstration, but this can be time consuming. Ragas here offers automatic language adaptation where you can automatically adapt any metrics to target language by using LLM itself. This notebook demonstrates this with simple example |
| 4 | + |
| 5 | +For the sake of this example, let's choose and metric and inspect the default prompts |
| 6 | + |
| 7 | + |
| 8 | +```python |
| 9 | +from ragas.metrics import SimpleCriteriaScoreWithReference |
| 10 | + |
| 11 | +scorer = SimpleCriteriaScoreWithReference( |
| 12 | + name="course_grained_score", definition="Score 0 to 5 by similarity" |
| 13 | +) |
| 14 | +``` |
| 15 | + |
| 16 | + |
| 17 | +```python |
| 18 | +scorer.get_prompts() |
| 19 | +``` |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | + {'multi_turn_prompt': <ragas.metrics._simple_criteria.MultiTurnSimpleCriteriaWithReferencePrompt at 0x7fcf409c3880>, |
| 25 | + 'single_turn_prompt': <ragas.metrics._simple_criteria.SingleTurnSimpleCriteriaWithReferencePrompt at 0x7fcf409c3a00>} |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | +As you can see, the instruction and demonstration are both in english. Setting up LLM to be used for this conversion |
| 30 | + |
| 31 | + |
| 32 | +```python |
| 33 | +from ragas.llms import llm_factory |
| 34 | + |
| 35 | +llm = llm_factory() |
| 36 | +``` |
| 37 | + |
| 38 | +To view the supported language codes |
| 39 | + |
| 40 | + |
| 41 | +```python |
| 42 | +from ragas.utils import RAGAS_SUPPORTED_LANGUAGE_CODES |
| 43 | + |
| 44 | +print(list(RAGAS_SUPPORTED_LANGUAGE_CODES.keys())) |
| 45 | +``` |
| 46 | + |
| 47 | + ['english', 'hindi', 'marathi', 'chinese', 'spanish', 'amharic', 'arabic', 'armenian', 'bulgarian', 'urdu', 'russian', 'polish', 'persian', 'dutch', 'danish', 'french', 'burmese', 'greek', 'italian', 'japanese', 'deutsch', 'kazakh', 'slovak'] |
| 48 | + |
| 49 | + |
| 50 | +Now let's adapt it to 'hindi' as the target language using `adapt` method. |
| 51 | +Language adaptation in Ragas works by translating few shot examples given along with the prompts to the target language. Instructions remains in english. |
| 52 | + |
| 53 | + |
| 54 | +```python |
| 55 | +adapted_prompts = await scorer.adapt_prompts(language="hindi", llm=llm) |
| 56 | +``` |
| 57 | + |
| 58 | +Inspect the adapted prompts and make corrections if needed |
| 59 | + |
| 60 | + |
| 61 | +```python |
| 62 | +adapted_prompts |
| 63 | +``` |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | + {'multi_turn_prompt': <ragas.metrics._simple_criteria.MultiTurnSimpleCriteriaWithReferencePrompt at 0x7fcf42bc40a0>, |
| 69 | + 'single_turn_prompt': <ragas.metrics._simple_criteria.SingleTurnSimpleCriteriaWithReferencePrompt at 0x7fcf722de890>} |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +set the prompts to new adapted prompts using `set_prompts` method |
| 74 | + |
| 75 | + |
| 76 | +```python |
| 77 | +scorer.set_prompts(**adapted_prompts) |
| 78 | +``` |
| 79 | + |
| 80 | +Evaluate using adapted metrics |
| 81 | + |
| 82 | + |
| 83 | +```python |
| 84 | +from ragas.dataset_schema import SingleTurnSample |
| 85 | + |
| 86 | +sample = SingleTurnSample( |
| 87 | + user_input="एफिल टॉवर कहाँ स्थित है?", |
| 88 | + response="एफिल टॉवर पेरिस में स्थित है।", |
| 89 | + reference="एफिल टॉवर मिस्र में स्थित है", |
| 90 | +) |
| 91 | + |
| 92 | +scorer.llm = llm |
| 93 | +await scorer.single_turn_ascore(sample) |
| 94 | +``` |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + 0 |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +Trace of reasoning and score |
| 104 | + |
| 105 | +`{ |
| 106 | + "reason": "प्रतिक्रिया और संदर्भ के उत्तर में स्थान के संदर्भ में महत्वपूर्ण भिन्नता है।", |
| 107 | + "score": 0 |
| 108 | +}` |
| 109 | + |
| 110 | + |
0 commit comments