Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Ready To Merge] Adds supporting of the OpenAI and AsyncOpenAI Clients #1140

Merged
merged 10 commits into from
Apr 2, 2025

Conversation

mrDzurb
Copy link
Member

@mrDzurb mrDzurb commented Mar 30, 2025

Description

This PR introduces two new client classes—AquaOpenAI (synchronous) and AsyncAquaOpenAI (asynchronous)—that extend the official OpenAI Python SDK to support OCI-AQUA-based model deployments. The changes are aimed at making our integration with OCI seamless and future‑proof by dynamically patching request headers and normalizing URL paths based on the deployment OCID.

Out Of Scope

OpenAI recently introduced a new API, but VLLM does not support it yet. For more details, refer to this issue. The AquaOpenAIClient can be enhanced to support the new OpenAI specification once VLLM adds compatibility.

Key Features

  • Common Request Patching Logic:

    • A new mixin class (AquaAIMixin) encapsulates all common logic for patching request headers and normalizing URL paths.
    • The mixin extracts the OCID from the URL and uses any segments following the OCID (skipping a leading "predict", if present) to build the "route" header dynamically.
    • The URL is normalized so that any trailing endpoint (e.g., /chat/completions, /embeddings, etc.) is replaced with /predict.
    • The mixin also sets the "enable-streaming" header based on whether the request JSON body contains "stream": true.
  • Auto-Creation of HTTP Clients:

    • If no HTTP client is provided to the synchronous client, it automatically creates one using ads.aqua.get_httpx_client().
    • Similarly, the asynchronous client auto-creates an httpx.AsyncClient using ads.aqua.get_async_httpx_client().
  • Support for Both Sync and Async Workflows:

    • AquaOpenAI and AsyncAquaOpenAI inherit from their respective OpenAI base classes and from the common mixin.

Testing

Sync

import ads
from ads.aqua.client.openai_client import OpenAI, AsyncOpenAI
ads.set_auth(auth="security_token")

client = OpenAI(
    base_url="https://modeldeployment.us-ashburn-1.oci.customer-oci.com/<OCID>/predict/v1",
)

response = client.chat.completions.create(
    model="odsc-llm",
    messages=[
        {
            "role": "user",
            "content": "Write a ten-sentence bedtime story about a unicorn.",
        }
    ],
    stream=True,
)

for event in response:
    print(event)

Async

import asyncio
import nest_asyncio
from ads.aqua.client.openai_client import OpenAI, AsyncOpenAI

async def test_async() -> None:
    client_async = AsyncOpenAI(
        base_url="https://modeldeployment.us-ashburn-1.oci.customer-oci.com/<OCID>/predict/v1",
    )
    response = await client_async.chat.completions.create(
        model="odsc-llm",
        messages=[{"role": "user", "content": "Tell me a long joke"}],
        stream=True
    )
    async for event in response:
        print(event)

asyncio.run(test_async())

@oracle-contributor-agreement oracle-contributor-agreement bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Mar 30, 2025
Copy link

📌 Cov diff with main:

Coverage-0%

📌 Overall coverage:

Coverage-58.68%

Copy link

📌 Cov diff with main:

Coverage-0%

📌 Overall coverage:

Coverage-58.68%

Copy link

github-actions bot commented Apr 1, 2025

📌 Cov diff with main:

Coverage-0%

📌 Overall coverage:

Coverage-58.73%

Copy link

github-actions bot commented Apr 1, 2025

📌 Cov diff with main:

Coverage-0%

📌 Overall coverage:

Coverage-58.73%

Copy link

github-actions bot commented Apr 1, 2025

📌 Cov diff with main:

Coverage-0%

📌 Overall coverage:

Coverage-58.73%

Copy link

github-actions bot commented Apr 1, 2025

📌 Cov diff with main:

Coverage-0%

📌 Overall coverage:

Coverage-58.73%

@mrDzurb mrDzurb changed the title [Experimental] Add AquaOpenAI and AsyncAquaOpenAI Clients [Experimental] Adds supporting of the OpenAI and AsyncOpenAI Clients Apr 2, 2025
@mrDzurb mrDzurb changed the title [Experimental] Adds supporting of the OpenAI and AsyncOpenAI Clients [Ready To Merge] Adds supporting of the OpenAI and AsyncOpenAI Clients Apr 2, 2025
Copy link

github-actions bot commented Apr 2, 2025

📌 Cov diff with main:

Coverage-0%

📌 Overall coverage:

Coverage-56.93%

Copy link

github-actions bot commented Apr 2, 2025

📌 Cov diff with main:

Coverage-0%

📌 Overall coverage:

Coverage-58.69%

Copy link
Member

@VipulMascarenhas VipulMascarenhas left a comment

Choose a reason for hiding this comment

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

lgtm 👍

@mrDzurb mrDzurb merged commit 68a529f into main Apr 2, 2025
21 checks passed
Copy link

github-actions bot commented Apr 2, 2025

📌 Cov diff with main:

Coverage-0%

📌 Overall coverage:

Coverage-58.69%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OCA Verified All contributors have signed the Oracle Contributor Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants