Skip to content
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

Answer relevancy metric is worse in languages other than English #998

Closed
icejean opened this issue Jan 10, 2025 · 2 comments
Closed

Answer relevancy metric is worse in languages other than English #998

icejean opened this issue Jan 10, 2025 · 2 comments

Comments

@icejean
Copy link

icejean commented Jan 10, 2025

I set up Neo4j KGBuilder on my Ubuntu22, and find that the answer relevancy metric is worse with Chinese, and I find out at last that the cause is that the questions generated by LLM is in English, not in Chinese, so the embedding of them are quite different with the original question in Chinese.
I'v posted an issue and the solution to the ragas project's home page at here: Answer relevancy metric is worse in languages other than English, please read it for details.

ragas-8

I think a solution for all languages and all metrics is needed.

Best regards
Jean from China

@icejean
Copy link
Author

icejean commented Jan 19, 2025

Well, the issue can be addressed in this way: Adapting metrics to target language
For example:

from datasets import Dataset
import os, json
from langchain_openai import ChatOpenAI

llm2 = ChatOpenAI(model="gpt-4o") 

# messages is the message list of LangGraph state
question = messages[-4].content
print(question)
answer = json.dumps(messages[-1].content, ensure_ascii=False)
print(answer)
context = json.dumps(messages[-2].content, ensure_ascii=False)
print(context)
dataset = Dataset.from_dict(
      {"question": [question], "reference": [answer], "answer": [answer], "contexts": [[context]]}
        )

# from ragas.metrics import answer_relevancy, faithfulness,context_entity_recall
from ragas.metrics import faithfulness,context_entity_recall
from ragas import evaluate
from ragas.cost import get_token_usage_for_openai

from ragas.llms import LangchainLLMWrapper

# This LLM instance is used to translate the prompt into target language.
llm3 = LangchainLLMWrapper(llm2)


from ragas.metrics import AnswerRelevancy

answer_relevancy = AnswerRelevancy(
    name="answer_relevancy", strictness=3, embeddings=embeddings
)

prompts = answer_relevancy.get_prompts()

import asyncio

async def adapt_prompt():
    adapted_prompts = await answer_relevancy.adapt_prompts(language="chinese", llm=llm3)
    print(adapted_prompts)
    return adapted_prompts

# run adapt_prompt() with  asyncio.run()
adapted_prompts = asyncio.run(adapt_prompt())

answer_relevancy.set_prompts(**adapted_prompts)


score = evaluate(
    dataset=dataset,
    metrics=[answer_relevancy],
    llm=llm2,
    embeddings=embeddings,
)

df = score.to_pandas()[['answer_relevancy']]
df

@icejean icejean closed this as completed Jan 19, 2025
@kartikpersistent
Copy link
Collaborator

Thank you @icejean we will surely incoperate into our application

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants