Skip to content

Commit 19caa7a

Browse files
feat: Add OCI Gen AI Integration for Direct LLM Support (#2321)
## Issue Link / Problem Description This PR adds direct support for Oracle Cloud Infrastructure (OCI) Generative AI models in Ragas, enabling evaluation without requiring LangChain or LlamaIndex dependencies. Currently, users who want to use OCI Gen AI models must go through LangChain or LlamaIndex wrappers, which adds unnecessary complexity and dependencies. **Problem**: No direct OCI Gen AI integration exists in Ragas, forcing users to use indirect approaches through LangChain/LlamaIndex. **Solution**: Implement a native OCI Gen AI wrapper that uses the OCI Python SDK directly. ## Changes Made ### Core Implementation - **Add `OCIGenAIWrapper`** - New LLM wrapper class extending `BaseRagasLLM` - **Direct OCI SDK Integration** - Uses `oci.generative_ai.GenerativeAiClient` directly - **Factory Function** - `oci_genai_factory()` for easy initialization - **Async Support** - Full async/await implementation with proper error handling ### Model Support [Pretrained Foundational Models supported by OCI Generative AI](https://docs.oracle.com/en-us/iaas/Content/generative-ai/pretrained-models.htm) ### Dependencies & Configuration - **Optional Dependency** - Added `oci>=2.160.1` as optional dependency - **Import Safety** - Graceful handling when OCI SDK is not installed - **Configuration Options** - Support for OCI CLI config, environment variables, or manual config ### Testing & Quality - **Comprehensive Test Suite** - 15+ test cases with mocking - **Error Handling Tests** - Tests for authentication, model not found, permission errors - **Async Testing** - Full async operation testing - **Factory Testing** - Factory function validation ### Documentation & Examples - **Complete Integration Guide** - Step-by-step setup and usage - **Working Example Script** - `examples/oci_genai_example.py` - **Authentication Guide** - Multiple OCI auth methods - **Troubleshooting Section** - Common issues and solutions - **Updated Integration Index** - Added to main integrations page ### Analytics & Monitoring - **Usage Tracking** - Built-in analytics with `LLMUsageEvent` - **Error Logging** - Comprehensive error logging and debugging - **Performance Monitoring** - Request tracking and metrics ## Testing ### How to Test - [x] Automated tests added/updated - [x] Manual testing steps: 1. **Install OCI dependency**: `pip install ragas[oci]` 2. **Configure OCI authentication**: Set up OCI config file or environment variables 3. **Run example script**: `python examples/oci_genai_example.py` 4. **Test with different models**: Try Cohere, Meta, and xAI models 5. **Test async operations**: Verify async generation works correctly 6. **Test error handling**: Verify proper error messages for auth/model issues ### Test Coverage ```bash # Run OCI Gen AI specific tests pytest tests/unit/test_oci_genai_wrapper.py -v # Test syntax validation python -c "import ast; ast.parse(open('src/ragas/llms/oci_genai_wrapper.py').read())" ``` ## References - **OCI Python SDK**: https://docs.oracle.com/en-us/iaas/tools/python/2.160.1/api/generative_ai.html - **OCI Gen AI Documentation**: https://docs.oracle.com/en-us/iaas/Content/generative-ai/ - **Ragas LLM Patterns**: Follows existing `BaseRagasLLM` patterns - **Related Issues**: Direct OCI Gen AI support request ## Screenshots/Examples ### Basic Usage ```python from ragas.llms import oci_genai_factory from ragas import evaluate # Initialize OCI Gen AI LLM llm = oci_genai_factory( model_id="cohere.command", compartment_id="ocid1.compartment.oc1..example" ) # Evaluate with OCI Gen AI result = evaluate(dataset, llm=llm) ``` ### Advanced Configuration ```python # 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" } llm = oci_genai_factory( model_id="cohere.command", compartment_id="ocid1.compartment.oc1..example", config=config, endpoint_id="ocid1.endpoint.oc1..example" # Optional ) ``` ## Files Changed - `src/ragas/llms/oci_genai_wrapper.py` - Main implementation - `src/ragas/llms/__init__.py` - Export new classes - `pyproject.toml` - Add OCI optional dependency - `tests/unit/test_oci_genai_wrapper.py` - Comprehensive tests - `docs/howtos/integrations/oci_genai.md` - Complete documentation - `docs/howtos/integrations/index.md` - Updated integration index - `examples/oci_genai_example.py` - Working example script ## Breaking Changes None - This is a purely additive feature with no breaking changes. ## Dependencies - **New Optional Dependency**: `oci>=2.160.1` - **No Breaking Changes**: Existing functionality unchanged - **Backward Compatible**: All existing code continues to work
1 parent 4026f4f commit 19caa7a

File tree

8 files changed

+1005
-0
lines changed

8 files changed

+1005
-0
lines changed

docs/howtos/integrations/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ happy to look into it 🙂
1515
- [LlamaIndex for RAG](./_llamaindex.md) - LlamaIndex is a framework for building RAG applications, more information can be found [here](https://www.llamaindex.ai/).
1616
- [LlamaIndex for Agents](./llamaindex_agents.md) - LlamaIndex enables building intelligent, semi-autonomous agents, more information can be found [here](https://www.llamaindex.ai/).
1717
- [LlamaStack](./llama_stack.md) – A unified framework by Meta for building and deploying generative AI apps across local, cloud, and mobile; [docs](https://llama-stack.readthedocs.io/en/latest/)
18+
- [OCI Gen AI](./oci_genai.md) - Oracle Cloud Infrastructure Generative AI provides access to various LLM models including Cohere, Meta, and Mistral models for RAG evaluation.
1819
- [R2R](./r2r.md) - R2R is an all-in-one solution for AI Retrieval-Augmented Generation (RAG) with production-ready features, more information can be found [here](https://r2r-docs.sciphi.ai/introduction)
1920
- [Swarm](./swarm_agent_evaluation.md) - Swarm is a framework for orchestrating multiple AI agents, more information can be found [here](https://github.com/openai/swarm).
2021

Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
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

Comments
 (0)