- Notifications
You must be signed in to change notification settings - Fork 22
Remove reduntant client type check#39
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -27,6 +27,7 @@ | ||
| from __future__ import annotations | ||
| import asyncio | ||
| import logging | ||
| from enum import Enum | ||
| from functools import cache | ||
| @@ -130,11 +131,11 @@ def _get_moderation_client() -> AsyncOpenAI: | ||
| return AsyncOpenAI() | ||
| async def _call_moderation_api(client: AsyncOpenAI, data: str) -> Any: | ||
| """Call the OpenAI moderation API. | ||
| async def _call_moderation_api_async(client: Any, data: str) -> Any: | ||
| """Call the OpenAI moderation API asynchronously. | ||
| Args: | ||
| client: The OpenAI client to use. | ||
| client: The async OpenAI or Azure OpenAI client to use. | ||
| data: The text to analyze. | ||
| Returns: | ||
| @@ -146,6 +147,22 @@ async def _call_moderation_api(client: AsyncOpenAI, data: str) -> Any: | ||
| ) | ||
| def _call_moderation_api_sync(client: Any, data: str) -> Any: | ||
| """Call the OpenAI moderation API synchronously. | ||
| Args: | ||
| client: The sync OpenAI or Azure OpenAI client to use. | ||
| data: The text to analyze. | ||
| Returns: | ||
| The moderation API response. | ||
| """ | ||
| return client.moderations.create( | ||
| model="omni-moderation-latest", | ||
| input=data, | ||
| ) | ||
| async def moderation( | ||
| ctx: Any, | ||
| data: str, | ||
| @@ -165,29 +182,32 @@ async def moderation( | ||
| Returns: | ||
| GuardrailResult: Indicates if tripwire was triggered, and details of flagged categories. | ||
| """ | ||
| client = None | ||
| if ctx is not None: | ||
| candidate = getattr(ctx, "guardrail_llm", None) | ||
| if isinstance(candidate, AsyncOpenAI): | ||
| client = candidate | ||
| # Try context client first (if provided), fall back on 404 | ||
| client = getattr(ctx, "guardrail_llm", None) if ctx is not None else None | ||
| # Try the context client first, fall back if moderation endpoint doesn't exist | ||
| if client is not None: | ||
| # Determine if client is async or sync | ||
| is_async = isinstance(client, AsyncOpenAI) | ||
steven10a marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. CopilotAI | ||
| try: | ||
| resp = await _call_moderation_api(client, data) | ||
| if is_async: | ||
| resp = await _call_moderation_api_async(client, data) | ||
| else: | ||
| # Sync client - run in thread pool to avoid blocking event loop | ||
| resp = await asyncio.to_thread(_call_moderation_api_sync, client, data) | ||
| except NotFoundError as e: | ||
| # Moderation endpoint doesn't exist on this provider (e.g., third-party) | ||
| # Fall back to the OpenAI client | ||
| # Moderation endpoint doesn't exist (e.g., Azure, third-party) | ||
| # Fall back to OpenAI client with OPENAI_API_KEY env var | ||
| logger.debug( | ||
| "Moderation endpoint not available on context client, falling back to OpenAI: %s", | ||
| e, | ||
| ) | ||
| client = _get_moderation_client() | ||
| resp = await _call_moderation_api(client, data) | ||
| resp = await _call_moderation_api_async(client, data) | ||
| else: | ||
| # No context client, use fallback | ||
| # No context client - use fallback OpenAI client | ||
| client = _get_moderation_client() | ||
| resp = await _call_moderation_api(client, data) | ||
| resp = await _call_moderation_api_async(client, data) | ||
| results = resp.results or [] | ||
| if not results: | ||
| return GuardrailResult( | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.