|
| 1 | +# OCI Gen AI Integration |
| 2 | + |
| 3 | +This guide shows how to use Oracle Cloud Infrastructure (OCI) Generative AI models with Ragas for evaluation. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +First, install the OCI dependency: |
| 8 | + |
| 9 | +```bash |
| 10 | +pip install ragas[oci] |
| 11 | +``` |
| 12 | + |
| 13 | +## Setup |
| 14 | + |
| 15 | +### 1. Configure OCI Authentication |
| 16 | + |
| 17 | +Set up your OCI configuration using one of these methods: |
| 18 | + |
| 19 | +#### Option A: OCI CLI Configuration |
| 20 | +```bash |
| 21 | +oci setup config |
| 22 | +``` |
| 23 | + |
| 24 | +#### Option B: Environment Variables |
| 25 | +```bash |
| 26 | +export OCI_CONFIG_FILE=~/.oci/config |
| 27 | +export OCI_PROFILE=DEFAULT |
| 28 | +``` |
| 29 | + |
| 30 | +#### Option C: Manual Configuration |
| 31 | +```python |
| 32 | +config = { |
| 33 | + "user": "ocid1.user.oc1..example", |
| 34 | + "key_file": "~/.oci/private_key.pem", |
| 35 | + "fingerprint": "your_fingerprint", |
| 36 | + "tenancy": "ocid1.tenancy.oc1..example", |
| 37 | + "region": "us-ashburn-1" |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +### 2. Get Required IDs |
| 42 | + |
| 43 | +You'll need: |
| 44 | +- **Model ID**: The OCI model ID (e.g., `cohere.command`, `meta.llama-3-8b`) |
| 45 | +- **Compartment ID**: Your OCI compartment OCID |
| 46 | +- **Endpoint ID** (optional): If using a custom endpoint |
| 47 | + |
| 48 | +## Usage |
| 49 | + |
| 50 | +### Basic Usage |
| 51 | + |
| 52 | +```python |
| 53 | +from ragas.llms import oci_genai_factory |
| 54 | +from ragas import evaluate |
| 55 | +from datasets import Dataset |
| 56 | + |
| 57 | +# Initialize OCI Gen AI LLM |
| 58 | +llm = oci_genai_factory( |
| 59 | + model_id="cohere.command", |
| 60 | + compartment_id="ocid1.compartment.oc1..example" |
| 61 | +) |
| 62 | + |
| 63 | +# Your dataset |
| 64 | +dataset = Dataset.from_dict({ |
| 65 | + "question": ["What is the capital of France?"], |
| 66 | + "answer": ["Paris"], |
| 67 | + "contexts": [["France is a country in Europe. Its capital is Paris."]], |
| 68 | + "ground_truth": ["Paris"] |
| 69 | +}) |
| 70 | + |
| 71 | +# Evaluate with OCI Gen AI |
| 72 | +result = evaluate( |
| 73 | + dataset, |
| 74 | + llm=llm, |
| 75 | + embeddings=None # You can use any embedding model |
| 76 | +) |
| 77 | +``` |
| 78 | + |
| 79 | +### Advanced Configuration |
| 80 | + |
| 81 | +```python |
| 82 | +from ragas.llms import oci_genai_factory |
| 83 | +from ragas.run_config import RunConfig |
| 84 | + |
| 85 | +# Custom OCI configuration |
| 86 | +config = { |
| 87 | + "user": "ocid1.user.oc1..example", |
| 88 | + "key_file": "~/.oci/private_key.pem", |
| 89 | + "fingerprint": "your_fingerprint", |
| 90 | + "tenancy": "ocid1.tenancy.oc1..example", |
| 91 | + "region": "us-ashburn-1" |
| 92 | +} |
| 93 | + |
| 94 | +# Custom run configuration |
| 95 | +run_config = RunConfig( |
| 96 | + timeout=60, |
| 97 | + max_retries=3 |
| 98 | +) |
| 99 | + |
| 100 | +# Initialize with custom config and endpoint |
| 101 | +llm = oci_genai_factory( |
| 102 | + model_id="cohere.command", |
| 103 | + compartment_id="ocid1.compartment.oc1..example", |
| 104 | + config=config, |
| 105 | + endpoint_id="ocid1.endpoint.oc1..example", # Optional |
| 106 | + run_config=run_config |
| 107 | +) |
| 108 | +``` |
| 109 | + |
| 110 | +### Using with Different Models |
| 111 | + |
| 112 | +```python |
| 113 | +# Cohere Command model |
| 114 | +llm_cohere = oci_genai_factory( |
| 115 | + model_id="cohere.command", |
| 116 | + compartment_id="ocid1.compartment.oc1..example" |
| 117 | +) |
| 118 | + |
| 119 | +# Meta Llama model |
| 120 | +llm_llama = oci_genai_factory( |
| 121 | + model_id="meta.llama-3-8b", |
| 122 | + compartment_id="ocid1.compartment.oc1..example" |
| 123 | +) |
| 124 | + |
| 125 | +# Using with different endpoints |
| 126 | +llm_endpoint = oci_genai_factory( |
| 127 | + model_id="cohere.command", |
| 128 | + compartment_id="ocid1.compartment.oc1..example", |
| 129 | + endpoint_id="ocid1.endpoint.oc1..example" |
| 130 | +) |
| 131 | +``` |
| 132 | + |
| 133 | +## Available Models |
| 134 | + |
| 135 | +OCI Gen AI supports various models including: |
| 136 | + |
| 137 | +- **Cohere**: `cohere.command`, `cohere.command-light` |
| 138 | +- **Meta**: `meta.llama-3-8b`, `meta.llama-3-70b` |
| 139 | +- **Mistral**: `mistral.mistral-7b-instruct` |
| 140 | +- **And more**: Check OCI documentation for the latest available models |
| 141 | + |
| 142 | +## Error Handling |
| 143 | + |
| 144 | +The OCI Gen AI wrapper includes comprehensive error handling: |
| 145 | + |
| 146 | +```python |
| 147 | +try: |
| 148 | + result = evaluate(dataset, llm=llm) |
| 149 | +except Exception as e: |
| 150 | + print(f"Evaluation failed: {e}") |
| 151 | +``` |
| 152 | + |
| 153 | +## Performance Considerations |
| 154 | + |
| 155 | +1. **Rate Limits**: OCI Gen AI has rate limits. Use appropriate retry configurations. |
| 156 | +2. **Timeout**: Set appropriate timeouts for your use case. |
| 157 | +3. **Batch Processing**: The wrapper supports batch processing for multiple completions. |
| 158 | + |
| 159 | +## Troubleshooting |
| 160 | + |
| 161 | +### Common Issues |
| 162 | + |
| 163 | +1. **Authentication Errors** |
| 164 | + ``` |
| 165 | + Error: OCI SDK authentication failed |
| 166 | + ``` |
| 167 | + Solution: Verify your OCI configuration and credentials. |
| 168 | + |
| 169 | +2. **Model Not Found** |
| 170 | + ``` |
| 171 | + Error: Model not found in compartment |
| 172 | + ``` |
| 173 | + Solution: Check if the model ID exists in your compartment. |
| 174 | + |
| 175 | +3. **Permission Errors** |
| 176 | + ``` |
| 177 | + Error: Insufficient permissions |
| 178 | + ``` |
| 179 | + Solution: Ensure your user has the necessary IAM policies for Generative AI. |
| 180 | + |
| 181 | +### Debug Mode |
| 182 | + |
| 183 | +Enable debug logging to troubleshoot issues: |
| 184 | + |
| 185 | +```python |
| 186 | +import logging |
| 187 | +logging.basicConfig(level=logging.DEBUG) |
| 188 | + |
| 189 | +# Your OCI Gen AI code here |
| 190 | +``` |
| 191 | + |
| 192 | +## Examples |
| 193 | + |
| 194 | +### Complete Evaluation Example |
| 195 | + |
| 196 | +```python |
| 197 | +from ragas import evaluate |
| 198 | +from ragas.llms import oci_genai_factory |
| 199 | +from ragas.metrics import faithfulness, answer_relevancy, context_precision |
| 200 | +from datasets import Dataset |
| 201 | + |
| 202 | +# Initialize OCI Gen AI |
| 203 | +llm = oci_genai_factory( |
| 204 | + model_id="cohere.command", |
| 205 | + compartment_id="ocid1.compartment.oc1..example" |
| 206 | +) |
| 207 | + |
| 208 | +# Create dataset |
| 209 | +dataset = Dataset.from_dict({ |
| 210 | + "question": [ |
| 211 | + "What is the capital of France?", |
| 212 | + "Who wrote Romeo and Juliet?" |
| 213 | + ], |
| 214 | + "answer": [ |
| 215 | + "Paris is the capital of France.", |
| 216 | + "William Shakespeare wrote Romeo and Juliet." |
| 217 | + ], |
| 218 | + "contexts": [ |
| 219 | + ["France is a country in Europe. Its capital is Paris."], |
| 220 | + ["Romeo and Juliet is a play by William Shakespeare."] |
| 221 | + ], |
| 222 | + "ground_truth": [ |
| 223 | + "Paris", |
| 224 | + "William Shakespeare" |
| 225 | + ] |
| 226 | +}) |
| 227 | + |
| 228 | +# Evaluate |
| 229 | +result = evaluate( |
| 230 | + dataset, |
| 231 | + metrics=[faithfulness, answer_relevancy, context_precision], |
| 232 | + llm=llm |
| 233 | +) |
| 234 | + |
| 235 | +print(result) |
| 236 | +``` |
| 237 | + |
| 238 | +### Custom Metrics with OCI Gen AI |
| 239 | + |
| 240 | +```python |
| 241 | +from ragas.metrics import MetricWithLLM |
| 242 | + |
| 243 | +# Create custom metric using OCI Gen AI |
| 244 | +class CustomMetric(MetricWithLLM): |
| 245 | + def __init__(self): |
| 246 | + super().__init__() |
| 247 | + self.llm = oci_genai_factory( |
| 248 | + model_id="cohere.command", |
| 249 | + compartment_id="ocid1.compartment.oc1..example" |
| 250 | + ) |
| 251 | + |
| 252 | +# Use in evaluation |
| 253 | +result = evaluate( |
| 254 | + dataset, |
| 255 | + metrics=[CustomMetric()], |
| 256 | + llm=llm |
| 257 | +) |
| 258 | +``` |
| 259 | + |
| 260 | +## Best Practices |
| 261 | + |
| 262 | +1. **Use Appropriate Models**: Choose models based on your evaluation needs. |
| 263 | +2. **Monitor Costs**: OCI Gen AI usage is billed. Monitor your usage. |
| 264 | +3. **Handle Errors**: Implement proper error handling for production use. |
| 265 | +4. **Use Caching**: Enable caching for repeated evaluations. |
| 266 | +5. **Batch Operations**: Use batch operations when possible for efficiency. |
| 267 | + |
| 268 | +## Support |
| 269 | + |
| 270 | +For issues specific to OCI Gen AI integration: |
| 271 | +- Check OCI documentation: https://docs.oracle.com/en-us/iaas/Content/generative-ai/ |
| 272 | +- OCI Python SDK: https://docs.oracle.com/en-us/iaas/tools/python/2.160.1/api/generative_ai.html |
| 273 | +- Ragas GitHub issues: https://github.com/explodinggradients/ragas/issues |
0 commit comments