-
Does guardrails project support chinese language? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Short answerYes! Guardrails acts between your content and the LLM, so as long as the LLM can understand Chinese you will be good. ...but I thought this was pretty interesting so I also wrote up a few examples! I always use Guardrails with pydantic models so that's what my examples below are all using, but I'm sure it works for everything. Long answer 1 (Asking questions in Chinese)In this example below, I have it analyze a short text in Chinese and extract/categorize information, along with validating the responses. import openai
from pydantic import BaseModel, Field
from guardrails.hub import ValidChoices
from guardrails import Guard
prompt = """
Comment to analyze: ${text}
${gr.complete_json_suffix_v2}
"""
class Comment(BaseModel):
food_name: str = Field(description="食品名",)
food_category: str = Field(description="食品类", validators=[
ValidChoices(choices=['肉类', '蔬菜', '水果', '面条/谷物'], on_fail='reask'),
])
sentiment: str = Field(description="情绪", validators=[
ValidChoices(choices=['喜欢', '不喜欢'], on_fail='reask'),
])
guard = Guard.from_pydantic(output_class=Comment, prompt=prompt)
result = guard(
llm_api=openai.chat.completions.create,
prompt_params={
'text': '我真的不喜欢吃西兰花'
},
num_reasks=3
) We can see the good result in {'food_name': '西兰花', 'food_category': '蔬菜', 'sentiment': '不喜欢'} How does Guardrails and the LLM know to do this correctly? It's because the
Long answer 2 (Chinese, always and only)But we can also make it more complicated! Let's say we want to make sure the responses are all in Chinese. There's a validator that says it does that but it doesn't work very well, so I wrote another one we can try out. It uses this library to check the language, and if the library is over 50% certain it's Chinese it says "this looks great." from typing import Dict
from lingua import Language, LanguageDetectorBuilder
from guardrails.validators import (
FailResult,
PassResult,
register_validator,
ValidationResult,
Validator
)
@register_validator(name="is-chinese", data_type="string")
class IsChineseLanguage(Validator):
def validate(self, value: str, metadata: Dict) -> ValidationResult:
# Uses https://github.com/pemistahl/lingua-py because it seems to be the best!
detector = LanguageDetectorBuilder.from_all_languages().with_preloaded_language_models().build()
# Get the Chinese score
scores = detector.compute_language_confidence_values(value)
chinese_score = next(filter(lambda e: e.language == Language.CHINESE, scores))
# Accept if it's over 50%
if chinese_score.value > 0.5:
return PassResult()
# Could use a fix value that's machine-translated...
return FailResult(
error_message=f"Text \"{value}\" should be provided in the Chinese language."
) Now I will use (almost) the same model as above, but include our new validator in every part of the response. I will also make the text to analyze be in English: import openai
from pydantic import BaseModel, Field
from guardrails.hub import ValidChoices
from guardrails import Guard
prompt = """
Comment to analyze: ${text}
${gr.complete_json_suffix_v2}
"""
is_chinese = IsChineseLanguage(on_fail='reask')
class Comment(BaseModel):
food_name: str = Field(description="食品名", validators=[is_chinese])
food_category: str = Field(description="食品类", validators=[
ValidChoices(choices=['肉类', '蔬菜', '水果', '面条/谷物'], on_fail='reask'),
is_chinese
])
sentiment: str = Field(description="情绪", validators=[
ValidChoices(choices=['喜欢', '不喜欢'], on_fail='reask'),
is_chinese
])
guard = Guard.from_pydantic(output_class=Comment, prompt=prompt)
result = guard(
llm_api=openai.chat.completions.create,
prompt_params={
'text': 'I love to eat buckwheat noodles'
},
num_reasks=3
) If we look at {'food_name': '荞麦面', 'food_category': '面条/谷物', 'sentiment': '喜欢'} It does this because even though the LLM provided "buckwheat noodles" at first, Guardrails used
Notice that it only had to ask for help with 荞麦面 - the LLM knew to put In conclusionIt really is like magic, but sorry 我不会中文 😅 |
Beta Was this translation helpful? Give feedback.
Short answer
Yes! Guardrails acts between your content and the LLM, so as long as the LLM can understand Chinese you will be good.
...but I thought this was pretty interesting so I also wrote up a few examples! I always use Guardrails with pydantic models so that's what my examples below are all using, but I'm sure it works for everything.
Long answer 1 (Asking questions in Chinese)
In this example below, I have it analyze a short text in Chinese and extract/categorize information, along with validating the responses.