Skip to content

Added integration with Amazon API Gateway + bug fix in _answer_relevance.py #327

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

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/ragas/llms/langchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from langchain.chat_models import AzureChatOpenAI, BedrockChat, ChatOpenAI, ChatVertexAI
from langchain.chat_models.base import BaseChatModel
from langchain.llms import AzureOpenAI, Bedrock, OpenAI, VertexAI
from langchain.llms import AmazonAPIGateway, AzureOpenAI, Bedrock, OpenAI, VertexAI
from langchain.llms.base import BaseLLM
from langchain.schema import LLMResult

Expand All @@ -25,6 +25,9 @@ def isOpenAI(llm: BaseLLM | BaseChatModel) -> bool:
def isBedrock(llm: BaseLLM | BaseChatModel) -> bool:
return isinstance(llm, Bedrock) or isinstance(llm, BedrockChat)

def isAmazonAPIGateway(llm: BaseLLM | BaseChatModel) -> bool:
return isinstance(llm, AmazonAPIGateway)


# have to specify it twice for runtime and static checks
MULTIPLE_COMPLETION_SUPPORTED = [
Expand Down Expand Up @@ -195,6 +198,8 @@ def generate(
temperature = 0.2 if n > 1 else 1e-8
if isBedrock(self.llm) and ("model_kwargs" in self.llm.__dict__):
self.llm.model_kwargs = {"temperature": temperature}
elif isAmazonAPIGateway(self.llm) and ("model_kwargs" in self.llm.__dict__):
self.llm.model_kwargs = {"temperature": temperature}
else:
self.llm.temperature = temperature

Expand Down
7 changes: 5 additions & 2 deletions src/ragas/metrics/_answer_relevance.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,11 @@ def _score_batch(

scores = []
for question, gen_questions in zip(questions, results):
cosine_sim = self.calculate_similarity(question, gen_questions)
scores.append(cosine_sim.mean())
if question is not None and question != "" and len(gen_questions) > 0:
cosine_sim = self.calculate_similarity(question, gen_questions)
scores.append(cosine_sim.mean())
else:
scores.append(0.0)

return scores

Expand Down