-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
feat: Add Cloudflare AI Gateway support #821
base: develop
Are you sure you want to change the base?
feat: Add Cloudflare AI Gateway support #821
Conversation
- Add Cloudflare Gateway integration for OpenAI, Anthropic, and Groq - Implement gateway URL construction and fallback logic - Add comprehensive logging for debugging - Update configuration documentation with gateway setup instructions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems to force all openai like apis to use CF, I think it needs to be more optional. I love the utility, desperately needed but we need fallback to the old URLs before we can merge thie
CLOUDFLARE_GW_ENABLED= # Set to true to enable Cloudflare AI Gateway | ||
CLOUDFLARE_AI_ACCOUNT_ID= # Cloudflare AI Account ID - found in the Cloudflare Dashboard under AI Gateway | ||
CLOUDFLARE_AI_GATEWAY_ID= # Cloudflare AI Gateway ID - found in the Cloudflare Dashboard under AI Gateway |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@w3-bounty The addition of sensitive configuration variables such as CLOUDFLARE_AI_ACCOUNT_ID and CLOUDFLARE_AI_GATEWAY_ID in the example environment file could lead to security vulnerabilities if this file is exposed. Consider using placeholders or ensuring that sensitive information is not included in example files. For instance, use CLOUDFLARE_AI_ACCOUNT_ID=YOUR_ACCOUNT_ID instead.
function getCloudflareGatewayBaseURL(runtime: IAgentRuntime, provider: string): string | undefined { | ||
const isCloudflareEnabled = runtime.getSetting("CLOUDFLARE_GW_ENABLED") === "true"; | ||
const cloudflareAccountId = runtime.getSetting("CLOUDFLARE_AI_ACCOUNT_ID"); | ||
const cloudflareGatewayId = runtime.getSetting("CLOUDFLARE_AI_GATEWAY_ID"); | ||
|
||
elizaLogger.debug("Cloudflare Gateway Configuration:", { | ||
isEnabled: isCloudflareEnabled, | ||
hasAccountId: !!cloudflareAccountId, | ||
hasGatewayId: !!cloudflareGatewayId, | ||
provider: provider | ||
}); | ||
|
||
if (!isCloudflareEnabled) { | ||
elizaLogger.debug("Cloudflare Gateway is not enabled"); | ||
return undefined; | ||
} | ||
|
||
if (!cloudflareAccountId) { | ||
elizaLogger.warn("Cloudflare Gateway is enabled but CLOUDFLARE_AI_ACCOUNT_ID is not set"); | ||
return undefined; | ||
} | ||
|
||
if (!cloudflareGatewayId) { | ||
elizaLogger.warn("Cloudflare Gateway is enabled but CLOUDFLARE_AI_GATEWAY_ID is not set"); | ||
return undefined; | ||
} | ||
|
||
const baseURL = `https://gateway.ai.cloudflare.com/v1/${cloudflareAccountId}/${cloudflareGatewayId}/${provider.toLowerCase()}`; | ||
elizaLogger.info("Using Cloudflare Gateway:", { | ||
provider, | ||
baseURL, | ||
accountId: cloudflareAccountId, | ||
gatewayId: cloudflareGatewayId | ||
}); | ||
|
||
return baseURL; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@w3-bounty The function getCloudflareGatewayBaseURL does not handle potential errors when retrieving settings from runtime. If the runtime is not properly configured or if there are issues accessing the settings, it could lead to unexpected behavior. Consider adding error handling to manage these scenarios gracefully.
const baseURL = getCloudflareGatewayBaseURL(runtime, 'openai') || endpoint; | ||
elizaLogger.debug("OpenAI baseURL result:", { baseURL }); | ||
|
||
const openai = createOpenAI({ apiKey, baseURL }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@w3-bounty The baseURL is derived from getCloudflareGatewayBaseURL, which can return undefined. If this happens, the createOpenAI function will be called with an undefined baseURL, potentially leading to runtime errors. Ensure that a valid baseURL is always provided or handle the case where it is undefined.
const baseURL = getCloudflareGatewayBaseURL(runtime, 'anthropic'); | ||
elizaLogger.debug("Anthropic baseURL result:", { baseURL }); | ||
|
||
const anthropic = createAnthropic({ apiKey, baseURL }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@w3-bounty Similar to the previous comment, the baseURL for the createAnthropic function can also be undefined. This could lead to issues when the function is called. Implement a check to ensure that baseURL is valid before proceeding.
const baseURL = getCloudflareGatewayBaseURL(runtime, 'groq'); | ||
elizaLogger.debug("Groq baseURL result:", { baseURL }); | ||
|
||
const groq = createGroq({ apiKey, baseURL }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@w3-bounty Again, the baseURL for the createGroq function can be undefined. This inconsistency in handling undefined values across different model initializations can lead to bugs. Ensure that a valid baseURL is always passed to the create functions.
Cloudflare AI Gateway Integration
Relates to
Enhances model provider integration by adding support for Cloudflare AI Gateway
Risks
Low - Changes are isolated to model provider routing and include fallback mechanisms:
Background
What does this PR do?
Adds support for routing API calls through Cloudflare AI Gateway, providing:
Changes Made
packages/core/src/generation.ts
:getCloudflareGatewayBaseURL
utility function for consistent gateway URL constructiondocs/docs/guides/configuration.md
:.env.example
:What kind of change is this?
Feature (non-breaking change which adds functionality)
Documentation changes
Documentation has been updated in
configuration.md
with:Testing
Where should a reviewer start?
getCloudflareGatewayBaseURL
function ingeneration.ts
configuration.md
Detailed testing steps
Configure Cloudflare AI Gateway:
Test with different providers:
Test fallback behavior:
Expected Results
When gateway is properly configured:
When gateway is disabled/misconfigured:
Discord username
w3_bounty