OCI 生成式 AI 集成
本指南展示了如何将 Oracle Cloud Infrastructure (OCI) 生成式 AI 模型与 Ragas 一起用于评估。
安装
首先,安装 OCI 依赖项
设置
1. 配置 OCI 身份验证
使用以下方法之一设置您的 OCI 配置
选项 A:OCI CLI 配置
选项 B:环境变量
选项 C:手动配置
config = {
"user": "ocid1.user.oc1..example",
"key_file": "~/.oci/private_key.pem",
"fingerprint": "your_fingerprint",
"tenancy": "ocid1.tenancy.oc1..example",
"region": "us-ashburn-1"
}
2. 获取所需的 ID
您需要: - 模型 ID:OCI 模型 ID (例如, cohere.command, meta.llama-3-8b) - 区间 ID:您的 OCI 区间 OCID - 端点 ID (可选):如果使用自定义端点
用法
基本用法
from ragas.llms import oci_genai_factory
from ragas import evaluate
from datasets import Dataset
# Initialize OCI Gen AI LLM
llm = oci_genai_factory(
model_id="cohere.command",
compartment_id="ocid1.compartment.oc1..example"
)
# Your dataset
dataset = Dataset.from_dict({
"question": ["What is the capital of France?"],
"answer": ["Paris"],
"contexts": [["France is a country in Europe. Its capital is Paris."]],
"ground_truth": ["Paris"]
})
# Evaluate with OCI Gen AI
result = evaluate(
dataset,
llm=llm,
embeddings=None # You can use any embedding model
)
高级配置
from ragas.llms import oci_genai_factory
from ragas.run_config import RunConfig
# Custom OCI configuration
config = {
"user": "ocid1.user.oc1..example",
"key_file": "~/.oci/private_key.pem",
"fingerprint": "your_fingerprint",
"tenancy": "ocid1.tenancy.oc1..example",
"region": "us-ashburn-1"
}
# Custom run configuration
run_config = RunConfig(
timeout=60,
max_retries=3
)
# Initialize with custom config and endpoint
llm = oci_genai_factory(
model_id="cohere.command",
compartment_id="ocid1.compartment.oc1..example",
config=config,
endpoint_id="ocid1.endpoint.oc1..example", # Optional
run_config=run_config
)
使用不同模型
# Cohere Command model
llm_cohere = oci_genai_factory(
model_id="cohere.command",
compartment_id="ocid1.compartment.oc1..example"
)
# Meta Llama model
llm_llama = oci_genai_factory(
model_id="meta.llama-3-8b",
compartment_id="ocid1.compartment.oc1..example"
)
# Using with different endpoints
llm_endpoint = oci_genai_factory(
model_id="cohere.command",
compartment_id="ocid1.compartment.oc1..example",
endpoint_id="ocid1.endpoint.oc1..example"
)
可用模型
OCI 生成式 AI 支持多种模型,包括
- Cohere:
cohere.command,cohere.command-light - Meta:
meta.llama-3-8b,meta.llama-3-70b - Mistral:
mistral.mistral-7b-instruct - 及其他:请查阅 OCI 文档以获取最新的可用模型
错误处理
OCI 生成式 AI 包装器包含全面的错误处理
性能注意事项
- 速率限制:OCI 生成式 AI 有速率限制。请使用适当的重试配置。
- 超时:根据您的使用场景设置适当的超时时间。
- 批量处理:该包装器支持对多个补全任务进行批量处理。
问题排查
常见问题
-
身份验证错误
解决方案:验证您的 OCI 配置和凭据。 -
模型未找到
解决方案:检查模型 ID 是否存在于您的区间中。 -
权限错误
解决方案:确保您的用户具有生成式 AI 所需的 IAM 策略。
调试模式
启用调试日志以排查问题
示例
完整评估示例
from ragas import evaluate
from ragas.llms import oci_genai_factory
from ragas.metrics import faithfulness, answer_relevancy, context_precision
from datasets import Dataset
# Initialize OCI Gen AI
llm = oci_genai_factory(
model_id="cohere.command",
compartment_id="ocid1.compartment.oc1..example"
)
# Create dataset
dataset = Dataset.from_dict({
"question": [
"What is the capital of France?",
"Who wrote Romeo and Juliet?"
],
"answer": [
"Paris is the capital of France.",
"William Shakespeare wrote Romeo and Juliet."
],
"contexts": [
["France is a country in Europe. Its capital is Paris."],
["Romeo and Juliet is a play by William Shakespeare."]
],
"ground_truth": [
"Paris",
"William Shakespeare"
]
})
# Evaluate
result = evaluate(
dataset,
metrics=[faithfulness, answer_relevancy, context_precision],
llm=llm
)
print(result)
使用 OCI Gen AI 自定义指标
from ragas.metrics import MetricWithLLM
# Create custom metric using OCI Gen AI
class CustomMetric(MetricWithLLM):
def __init__(self):
super().__init__()
self.llm = oci_genai_factory(
model_id="cohere.command",
compartment_id="ocid1.compartment.oc1..example"
)
# Use in evaluation
result = evaluate(
dataset,
metrics=[CustomMetric()],
llm=llm
)
最佳实践
- 使用合适的模型:根据您的评估需求选择模型。
- 监控成本:OCI 生成式 AI 的使用是计费的。请监控您的使用情况。
- 处理错误:为生产环境使用实现适当的错误处理。
- 使用缓存:为重复的评估启用缓存。
- 批量操作:尽可能使用批量操作以提高效率。
支持
对于 OCI 生成式 AI 集成的特定问题: - 查看 OCI 文档:https://docs.oracle.com/en-us/iaas/Content/generative-ai/ - OCI Python SDK:https://docs.oracle.com/en-us/iaas/tools/python/2.160.1/api/generative_ai.html - Ragas GitHub 问题:https://github.com/vibrantlabsai/ragas/issues