Skip to content

feat(oracle): Add rerank and guardrails support#1540

Open
fede-kamel wants to merge 10 commits intoPortkey-AI:mainfrom
fede-kamel:feat/oracle-genai-extended
Open

feat(oracle): Add rerank and guardrails support#1540
fede-kamel wants to merge 10 commits intoPortkey-AI:mainfrom
fede-kamel:feat/oracle-genai-extended

Conversation

@fede-kamel
Copy link

@fede-kamel fede-kamel commented Feb 26, 2026

Summary

Adds two new endpoints for Oracle GenAI:

  • Rerank endpoint for document relevance ranking (RAG pipelines)
  • Guardrails for content moderation, PII detection, and prompt injection

Note: Commits 0018e3f4, 9935cd93, ed943795 are from PR #1537. Once #1537 merges, this branch will be rebased to remove the duplicates.

New Features

Rerank Endpoint

  • Maps Cohere rerank API format to OCI GenAI /actions/rerankText
  • Supports cohere.rerank-v3.5, cohere.rerank-multilingual-v3.1, cohere.rerank-english-v3.1
  • Model validation to prevent unsupported models
  • Note: Requires DEDICATED cluster (ON_DEMAND retired for rerank)

Guardrails (Content Moderation)

  • Maps OpenAI /v1/moderations format to OCI /actions/applyGuardrails
  • Content moderation: HATE, VIOLENCE, SEXUAL, HARASSMENT, SELF_HARM
  • PII detection: EMAIL, PHONE, SSN, CREDIT_CARD, IP_ADDRESS, etc.
  • Prompt injection detection
  • Works with ON_DEMAND serving (no dedicated cluster required)

Model Listing

  • Utility to list available OCI GenAI models
  • Transforms to OpenAI /v1/models format

Testing

Unit Tests

  • rerank.test.ts - 26 tests
  • guardrails.test.ts - 13 tests

Integration Tests (Real API)

  • guardrails.integration.test.ts - 4 tests against real OCI GenAI API
  • oracle-rerank.integration.test.ts - Transform validation tests

Run integration tests:

OCI_PROFILE=<profile> npx jest src/providers/oracle/guardrails.integration.test.ts --no-coverage

Files Changed (new in this PR)

File Description
src/providers/oracle/rerank.ts Rerank endpoint implementation
src/providers/oracle/rerank.test.ts Rerank unit tests
src/providers/oracle/guardrails.ts Guardrails implementation
src/providers/oracle/guardrails.test.ts Guardrails unit tests
src/providers/oracle/guardrails.integration.test.ts Real API integration tests
src/providers/oracle/listModels.ts Model listing utility

… tests

Oracle GenAI Provider Enhancements:

- Add embeddings support (embed.ts) with Cohere model compatibility
- Add model configuration system (modelConfig.ts) for model-specific behaviors
- Add model handler classes for different families (Meta, Gemini, GPT, Grok, Cohere)
- Support reasoningContent for GPT-OSS reasoning models
- Handle content in multiple formats (array, string, reasoningContent)
- Add oracleEndpointId to Options type for dedicated serving mode

Testing:
- Add comprehensive unit tests (61 tests passing)
- Add integration tests for chat completions and embeddings
- Add run-tests.sh script for easy test execution with OCI profiles

Models tested:
- Google Gemini 2.5 (flash, flash-lite, pro)
- OpenAI GPT-OSS (20b, 120b)
- Meta Llama Maverick
- Cohere embeddings (v4.0, v3.0 variants)
- Add usesMaxCompletionTokens() helper for GPT-5+ model detection
- Update chatRequest transform to use maxCompletionTokens for GPT-5 models
- Add model configs for openai.gpt-5 and openai.gpt-4o families
- Add OracleStreamChunk type definition for streaming responses
- Set appropriate minTokens (500) for GPT-5 reasoning models
- Test multiple tool calls in a single response (weather, time, stock)
- Test parallel tool calls with streaming
- Verify models can call 2+ tools simultaneously
Implement /v1/rerank endpoint for Oracle GenAI, enabling document
relevance ranking for RAG pipelines.

- Add rerankHandler for standard /v1/rerank route
- Add Oracle rerank request/response transforms
- Map to Oracle's /actions/rerankText API
- Support top_n, return_documents, max_chunks_per_doc params

The rerank endpoint type was already defined in Portkey's
endpointStrings, making this a safe addition that extends
the existing API surface.
- Add unit tests for OracleRerankConfig transformations
- Add unit tests for OracleRerankResponseTransform
- Add integration test scaffold for /v1/rerank endpoint
- Update test resources with rerank constants and templates
- Add Oracle-specific test configuration for rerank

Unit tests verify:
- Query to input transformation
- Document array transformations (string and object formats)
- ServingMode configuration with ON_DEMAND default
- CompartmentId extraction from provider options
- Parameter mappings (top_n, return_documents, max_chunks_per_doc)
- Response transformation for success and error cases
Integration test for Oracle GenAI rerank endpoint that validates:
- Request parameter transformations (query, documents, servingMode)
- Response transformations (results, scores, documents)
- Error response handling

Note: Cohere rerank models (cohere.rerank-v3.5, etc.) were retired
for ON_DEMAND serving in OCI GenAI as of May 2025. Live API tests
require a DEDICATED AI cluster with RERANK_COHERE shape.

Run with: npx tsx src/tests/oracle-rerank.integration.test.ts
Only Cohere rerank models are supported in Oracle GenAI:
- cohere.rerank-v3.5
- cohere.rerank-multilingual-v3.1
- cohere.rerank-english-v3.1

The validateRerankModel function throws an error if:
- Model is undefined or empty
- Model doesn't start with "cohere.rerank"

This prevents confusing 404 errors when using unsupported models.
As of Feb 2026, OCI GenAI rerank models are only available via
DEDICATED AI clusters. ON_DEMAND serving has been retired.

Changes:
- Add documentation about DEDICATED cluster requirement
- Support oracleEndpointId for DEDICATED mode
- Add tests for DEDICATED mode with endpointId

To use rerank:
1. Create a dedicated AI cluster with RERANK_COHERE shape
2. Set oracleServingMode: 'DEDICATED'
3. Set oracleEndpointId to your endpoint OCID
- Add guardrails endpoint for content moderation, PII detection, and prompt injection
- Transform OpenAI moderation format to/from Oracle guardrails API
- Add getProxyEndpoint to handle /moderations proxy requests
- Add listModels utility for model discovery
- Include comprehensive unit tests (13 tests)
- Fix response interface to use camelCase keys matching actual OCI API
  (contentModeration, personallyIdentifiableInformation, promptInjection)
- Update hasPII check to use Array.isArray() for proper detection
- Update unit tests to match real API response format
- Add integration tests that call real OCI GenAI Guardrails API
  (skipped by default, run with OCI_PROFILE=<profile>)
@fede-kamel fede-kamel changed the title feat(oracle): Add rerank, guardrails, and extended GenAI support feat(oracle): Add rerank and guardrails support Feb 26, 2026
@fede-kamel fede-kamel marked this pull request as ready for review February 26, 2026 21:23
@roh26it roh26it requested review from Copilot and narengogi February 27, 2026 14:15
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds comprehensive rerank and guardrails (content moderation) support to the Oracle GenAI provider, building upon the foundation established in PR #1537. The implementation provides OpenAI-compatible interfaces for Oracle's GenAI services.

Changes:

  • Adds rerank endpoint mapping Cohere's API format to OCI GenAI /actions/rerankText
  • Implements guardrails for content moderation, PII detection, and prompt injection detection
  • Enhances chat completion with improved tool call handling and multimodal content support

Reviewed changes

Copilot reviewed 38 out of 39 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
src/providers/oracle/rerank.ts Implements rerank endpoint with Cohere model validation
src/providers/oracle/guardrails.ts Implements content moderation and PII detection
src/providers/oracle/listModels.ts Utility for listing available OCI GenAI models
src/providers/oracle/modelConfig.ts Centralized model configuration and capabilities
src/providers/oracle/handlers/ Model-specific handlers for different families
src/providers/oracle/chatComplete.ts Enhanced with tool calls, multimodal content, and GPT-5 support
src/providers/oracle/embed.ts Embeddings implementation with serving mode support
src/handlers/rerankHandler.ts New handler for rerank endpoint
src/index.ts Registers /v1/rerank route
src/utils/env.ts Fixes module loading to use synchronous require
src/types/requestBody.ts Adds Oracle-specific types and rerank parameters
Test files Comprehensive unit and integration tests for all new features

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +14 to +27
export const createOracleHeaders = (oracleConfig: OracleConfig) => {
return {
'x-portkey-provider': 'oracle',
'Content-Type': 'application/json',
'x-portkey-oracle-tenancy': oracleConfig.tenancy,
'x-portkey-oracle-user': oracleConfig.user,
'x-portkey-oracle-fingerprint': oracleConfig.fingerprint,
'x-portkey-oracle-private-key': oracleConfig.privateKey,
'x-portkey-oracle-region': oracleConfig.region,
'x-portkey-oracle-compartment-id': oracleConfig.compartmentId,
...(oracleConfig.servingMode && {
'x-portkey-oracle-serving-mode': oracleConfig.servingMode,
}),
};
Copy link

Copilot AI Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The createOracleHeaders function does not include the oracleEndpointId header (x-portkey-oracle-endpoint-id) which is required for DEDICATED mode rerank operations. This means the rerank tests won't be able to pass the endpoint ID through headers even if it's set in the OracleConfig. Add support for the endpoint ID header similar to how servingMode is handled.

Copilot uses AI. Check for mistakes.
privateKey: string;
region: string;
compartmentId: string;
servingMode?: string;
Copy link

Copilot AI Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The OracleConfig interface is missing the endpointId field which is required for DEDICATED mode operations (rerank, potentially embeddings). This field should be added to the interface and populated from environment variables similar to other Oracle configuration fields.

Suggested change
servingMode?: string;
servingMode?: string;
endpointId?: string;

Copilot uses AI. Check for mistakes.
Comment on lines +9 to +15
async function loadNodeModules() {
if (isNodeInstance && !modulesLoaded) {
path = await import('path');
fs = await import('fs');
modulesLoaded = true;
}
}
Copy link

Copilot AI Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The async loadNodeModules function is defined but never called or used. It's redundant since the modules are loaded synchronously below it. This unused function should be removed to avoid confusion.

Copilot uses AI. Check for mistakes.
Comment on lines +107 to +116
if (servingMode === 'DEDICATED') {
return {
servingType: 'DEDICATED',
endpointId: provider.oracleEndpointId,
};
}
return {
servingType: 'ON_DEMAND',
modelId: params.model,
};
Copy link

Copilot AI Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When using DEDICATED serving mode without an endpointId, the code should either throw an error or provide a clear warning. Currently, it silently falls back to using modelId, which may result in an API error. This could be confusing for users who explicitly set DEDICATED mode but forget to provide the endpointId. Consider adding validation to ensure endpointId is present when DEDICATED mode is used.

Copilot uses AI. Check for mistakes.
Comment on lines +95 to +108
const servingType = providerOptions.oracleServingMode || 'ON_DEMAND';

// For DEDICATED mode, use endpointId instead of modelId
if (servingType === 'DEDICATED' && providerOptions.oracleEndpointId) {
return {
servingType: 'DEDICATED',
endpointId: providerOptions.oracleEndpointId,
};
}

return {
servingType,
modelId: params.model,
};
Copy link

Copilot AI Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the embed.ts issue, when DEDICATED serving mode is used without an endpointId, the code silently falls back to using modelId. This could cause confusing API errors. Consider adding validation to ensure endpointId is provided when DEDICATED mode is explicitly set, or document this fallback behavior clearly.

Copilot uses AI. Check for mistakes.
Comment on lines +188 to +189
const flagged: boolean =
overallScore > 0.5 || promptInjectionScore > 0.5 || hasPII;
Copy link

Copilot AI Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The threshold of 0.5 for determining if content is flagged is hardcoded and not configurable. Different use cases may require different sensitivity levels. Consider making this threshold configurable through request parameters or provider options, allowing users to adjust the sensitivity of the moderation system based on their requirements.

Copilot uses AI. Check for mistakes.
@fede-kamel
Copy link
Author

fede-kamel commented Feb 27, 2026

@narengogi let's start reviewing #1537 better - this one comesafter

@fede-kamel
Copy link
Author

Rebased check done: branch is already up to date with main (no conflicts).

Validation run:

  • Unit: tests/unit/src/providers/oracle/chatComplete.test.ts, tests/unit/src/providers/oracle/modelConfig.test.ts, src/providers/oracle/guardrails.test.ts, src/providers/oracle/rerank.test.ts (PASS)
  • Integration (OCI profile LUIGI_FRA_API):
    • tests/integration/src/providers/oracle/run-tests.sh ... all (PASS; chat+embed)
    • src/providers/oracle/guardrails.integration.test.ts (PASS)
    • src/tests/oracle-rerank.integration.test.ts (PASS config/transform checks; live API portion intentionally skipped because it requires ORACLE_SERVING_MODE=DEDICATED + ORACLE_ENDPOINT_ID)

Re-pinging for review.

@fede-kamel
Copy link
Author

fede-kamel commented Mar 6, 2026

Retested everything end-to-end and we are good to go here.

Latest validation:

  • Reworked src/tests/oracle-rerank.integration.test.ts into a proper Jest integration suite
  • npx jest src/tests/oracle-rerank.integration.test.ts --no-cache --runInBand ✅ (3 passed, live dedicated test skipped unless endpoint vars are set)
  • Oracle live integration suites with OCI profile
    • tests/integration/src/providers/oracle/chatComplete.integration.test.ts
    • tests/integration/src/providers/oracle/embed.integration.test.ts
    • src/providers/oracle/guardrails.integration.test.ts

All good from my side for merge.

@fede-kamel
Copy link
Author

@narengogi quick ping when you have a moment: everything has been rebased and retested end-to-end, and this is ready from my side. Let’s make the OCI <> Portkey integration great together 🚀

@fede-kamel
Copy link
Author

Friendly follow-up on this one when you have a moment: @narengogi @VisargD\n\nWould appreciate a review. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants