定制模型
自定义模型
Ragas 可以使用 LLM 和/或嵌入模型进行评估和合成数据生成。这两个模型都可以根据您的可用性进行自定义。
注意
Ragas 支持 langchain 中所有可用的 LLMs 和 Embeddings
-
BaseRagasLLM
和BaseRagasEmbeddings
是 Ragas 内部用于 LLM 和嵌入模型的基类。任何自定义的 LLM 或嵌入模型都应该是这些基类的子类。 -
如果您使用的是 Langchain,您可以直接传递 Langchain 的 LLM 和嵌入模型,Ragas 会根据需要将其包装在
LangchainLLMWrapper
或LangchainEmbeddingsWrapper
中。
示例
Azure OpenAI
from langchain_openai.chat_models import AzureChatOpenAI
from langchain_openai.embeddings import AzureOpenAIEmbeddings
from ragas.llms import LangchainLLMWrapper
from ragas.embeddings import LangchainEmbeddingsWrapper
azure_configs = {
"base_url": "https://<your-endpoint>.openai.azure.com/",
"model_deployment": "your-deployment-name",
"model_name": "your-model-name",
"embedding_deployment": "your-deployment-name",
"embedding_name": "text-embedding-ada-002", # 最有可能
}
azure_llm = AzureChatOpenAI(
openai_api_version="2023-05-15",
azure_endpoint=azure_configs["base_url"],
azure_deployment=azure_configs["model_deployment"],
model=azure_configs["model_name"],
validate_base_url=False,
)
# 初始化用于 answer_relevancy、answer_correctness 和 answer_similarity 的嵌入模型
azure_embeddings = AzureOpenAIEmbeddings(
openai_api_version="2023-05-15",
azure_endpoint=azure_configs["base_url"],
azure_deployment=azure_configs["embedding_deployment"],
model=azure_configs["embedding_name"],
)
azure_llm = LangchainLLMWrapper(azure_llm)
azure_embeddings = LangchainEmbeddingsWrapper(azure_embeddings)
Google Vertex
import google.auth
from langchain_google_vertexai import ChatVertexAI, VertexAIEmbeddings
from ragas.llms import LangchainLLMWrapper
from ragas.embeddings import LangchainEmbeddingsWrapper
config = {
"project_id": "<your-project-id>",
"chat_model_id": "gemini-1.0-pro-002",
"embedding_model_id": "textembedding-gecko",
}
# 认证到 GCP
creds, _ = google.auth.default(quota_project_id=config["project_id"])
# 创建 Langchain LLM 和 Embeddings
vertextai_llm = ChatVertexAI(
credentials=creds,
model_name=config["chat_model_id"],
)
vertextai_embeddings = VertexAIEmbeddings(
credentials=creds, model_name=config["embedding_model_id"]
)
vertextai_llm = LangchainLLMWrapper(vertextai_llm)
vertextai_embeddings = LangchainEmbeddingsWrapper(vertextai_embeddings)
AWS Bedrock
from langchain_community.chat_models import BedrockChat
from langchain_community.embeddings import BedrockEmbeddings
from ragas.llms import LangchainLLMWrapper
from ragas.embeddings import LangchainEmbeddingsWrapper
config = {
"credentials_profile_name": "your-profile-name", # 例如 "default"
"region_name": "your-region-name", # 例如 "us-east-1"
"model_id": "your-model-id", # 例如 "anthropic.claude-v2"
"model_kwargs": {"temperature": 0.4},
}
bedrock_llm = BedrockChat(
credentials_profile_name=config["credentials_profile_name"],
region_name=config["region_name"],
endpoint_url=f"https://bedrock-runtime.{config['region_name']}.amazonaws.com",
model_id=config["model_id"],
model_kwargs=config["model_kwargs"],
)
# 初始化嵌入模型
bedrock_embeddings = BedrockEmbeddings(
credentials_profile_name=config["credentials_profile_name"],
region_name=config["region_name"],
)
bedrock_llm = LangchainLLMWrapper(bedrock_llm)
bedrock_embeddings = LangchainEmbeddingsWrapper(bedrock_embeddings)