OCI Gen AI 集成
本指南说明如何将 Oracle Cloud Infrastructure (OCI) Generative AI 模型与 Ragas 一起用于评测。
安装
首先,安装 OCI 依赖:
pip install ragas[oci]
设置
1. 配置 OCI 认证
使用以下方法之一设置 OCI 配置:
选项 A:OCI CLI 配置
oci setup config
选项 B:环境变量
export OCI_CONFIG_FILE=~/.oci/config
export OCI_PROFILE=DEFAULT
选项 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
你需要:
- Model ID:OCI 模型 ID(例如
cohere.command、meta.llama-3-8b) - Compartment ID:你的 OCI compartment OCID
- Endpoint ID(可选):如果使用自定义 endpoint
用法
基本用法
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 Gen AI 支持多种模型,包括:
- Cohere:
cohere.command、cohere.command-light - Meta:
meta.llama-3-8b、meta.llama-3-70b - Mistral:
mistral.mistral-7b-instruct - 以及更多:查看 OCI 文档了解最新可用模型
错误处理
OCI Gen AI wrapper 包含全面的错误处理:
try:
result = evaluate(dataset, llm=llm)
except Exception as e:
print(f"Evaluation failed: {e}")
性能考虑
- 速率限制:OCI Gen AI 有速率限制。请使用合适的重试配置。
- 超时:根据你的用例设置合适的超时。
- 批处理:该 wrapper 支持对多次补全进行批处理。
故障排除
常见问题
- 认证错误
text
Error: OCI SDK authentication failed
解决方案:核实你的 OCI 配置和凭证。
- 找不到模型
text
Error: Model not found in compartment
解决方案:检查该 model ID 是否存在于你的 compartment 中。
- 权限错误
text
Error: Insufficient permissions
解决方案:确保你的用户具备 Generative AI 所需的 IAM 策略。
调试模式
启用 debug 日志以排查问题:
import logging
logging.basicConfig(level=logging.DEBUG)
# Your OCI Gen AI code here
示例
完整评测示例
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 Gen AI 用量会计费。请监控用量。
- 处理错误:生产环境中实现妥善的错误处理。
- 使用缓存:对重复评测启用缓存。
- 批处理操作:尽可能使用批处理以提高效率。
支持
针对 OCI Gen 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 issues:https://github.com/vibrantlabsai/ragas/issues