OCI Gen AI Integration
This guide shows how to use Oracle Cloud Infrastructure (OCI) Generative AI models with Ragas for evaluation.
Installation
First, install the OCI dependency:
Setup
1. Configure OCI Authentication
Set up your OCI configuration using one of these methods:
Option A: OCI CLI Configuration
Option B: Environment Variables
Option C: Manual 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"
}
2. Get Required IDs
You'll need:
- Model ID: The OCI model ID (e.g., cohere.command, meta.llama-3-8b)
- Compartment ID: Your OCI compartment OCID
- Endpoint ID (optional): If using a custom endpoint
Usage
Basic Usage
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
)
Advanced Configuration
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
)
Using with Different Models
# 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"
)
Available Models
OCI Gen AI supports various models including:
- Cohere:
cohere.command,cohere.command-light - Meta:
meta.llama-3-8b,meta.llama-3-70b - Mistral:
mistral.mistral-7b-instruct - And more: Check OCI documentation for the latest available models
Error Handling
The OCI Gen AI wrapper includes comprehensive error handling:
Performance Considerations
- Rate Limits: OCI Gen AI has rate limits. Use appropriate retry configurations.
- Timeout: Set appropriate timeouts for your use case.
- Batch Processing: The wrapper supports batch processing for multiple completions.
Troubleshooting
Common Issues
-
Authentication Errors
Solution: Verify your OCI configuration and credentials. -
Model Not Found
Solution: Check if the model ID exists in your compartment. -
Permission Errors
Solution: Ensure your user has the necessary IAM policies for Generative AI.
Debug Mode
Enable debug logging to troubleshoot issues:
Examples
Complete Evaluation Example
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)
Custom Metrics with 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
)
Best Practices
- Use Appropriate Models: Choose models based on your evaluation needs.
- Monitor Costs: OCI Gen AI usage is billed. Monitor your usage.
- Handle Errors: Implement proper error handling for production use.
- Use Caching: Enable caching for repeated evaluations.
- Batch Operations: Use batch operations when possible for efficiency.
Support
For issues specific to OCI Gen AI integration: - Check OCI documentation: 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/explodinggradients/ragas/issues