From 0e990e048b406815b8eb426c1d4feabbaa7fbb4d Mon Sep 17 00:00:00 2001 From: umutbozdag Date: Fri, 16 Aug 2024 12:02:37 +0300 Subject: [PATCH 1/2] feat: expand analytics support in README and code for Cloudflare AI Gateway integration --- README.md | 2 +- src/models.test.ts | 21 +++++++++++++++++++++ src/models.ts | 14 +++++++++++++- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 78f57d8..45098e2 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Features: - (Optional) built-in Redis compatibility for fast chat history management - (Optional) built-in rate limiting - (Optional) disableRag option to use it as LLM + chat history -- (Optional) Analytics via [Helicone](https://www.helicone.ai/) and [Langsmith](https://www.langchain.com/langsmith) +- (Optional) Analytics via [Helicone](https://www.helicone.ai/), [Langsmith](https://www.langchain.com/langsmith) and [Cloudflare AI Gateway](https://developers.cloudflare.com/ai-gateway/) ## Getting started diff --git a/src/models.test.ts b/src/models.test.ts index b5a8f32..1113bd3 100644 --- a/src/models.test.ts +++ b/src/models.test.ts @@ -49,6 +49,27 @@ describe("Model inits", () => { }); }); + test("OpenAI client configuration with Cloudflare AI Gateway", () => { + const client = openai("gpt-3.5-turbo", { + analytics: { + name: "cloudflare", + token: "mock-cloudflare-token", + accountId: "mock-account-id", + gatewayName: "mock-gateway-name", + }, + }); + + //@ts-expect-error required for testing + expect(client.clientConfig.baseURL).toBe( + "https://gateway.ai.cloudflare.com/v1/mock-account-id/mock-gateway-name" + ); + //@ts-expect-error required for testing + expect(client.clientConfig.defaultHeaders).toEqual({ + Authorization: "Bearer mock-cloudflare-token", + "Content-Type": "application/json", + }); + }); + test("OpenAI client configuration without analytics", () => { const config = { apiKey: "no-key", diff --git a/src/models.ts b/src/models.ts index 8afb714..aed3dac 100644 --- a/src/models.ts +++ b/src/models.ts @@ -73,7 +73,8 @@ type Providers = | "mistral"; type AnalyticsConfig = | { name: "helicone"; token: string } - | { name: "langsmith"; token: string; apiUrl?: string }; + | { name: "langsmith"; token: string; apiUrl?: string } + | { name: "cloudflare"; token: string; accountId: string; gatewayName: string }; type ModelOptions = Omit & { analytics?: AnalyticsConfig; @@ -138,6 +139,17 @@ const setupAnalytics = ( } return { client: undefined }; } + + case "cloudflare": { + return { + baseURL: `https://gateway.ai.cloudflare.com/v1/${analytics.accountId}/${analytics.gatewayName}`, + defaultHeaders: { + Authorization: `Bearer ${analytics.token}`, + "Content-Type": "application/json", + }, + }; + } + default: { // eslint-disable-next-line @typescript-eslint/no-explicit-any throw new Error(`Unsupported analytics provider: ${JSON.stringify(analytics)}`); From 40e5a0a68a9be7af3176b126e880455cbe9c4bbe Mon Sep 17 00:00:00 2001 From: ogzhanolguncu Date: Fri, 23 Aug 2024 13:32:21 +0300 Subject: [PATCH 2/2] fix: change typo in authorization header for cloudflare gateway --- src/models.test.ts | 1 - src/models.ts | 7 +++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/models.test.ts b/src/models.test.ts index 1113bd3..d2a3953 100644 --- a/src/models.test.ts +++ b/src/models.test.ts @@ -53,7 +53,6 @@ describe("Model inits", () => { const client = openai("gpt-3.5-turbo", { analytics: { name: "cloudflare", - token: "mock-cloudflare-token", accountId: "mock-account-id", gatewayName: "mock-gateway-name", }, diff --git a/src/models.ts b/src/models.ts index aed3dac..d48abfa 100644 --- a/src/models.ts +++ b/src/models.ts @@ -74,7 +74,7 @@ type Providers = type AnalyticsConfig = | { name: "helicone"; token: string } | { name: "langsmith"; token: string; apiUrl?: string } - | { name: "cloudflare"; token: string; accountId: string; gatewayName: string }; + | { name: "cloudflare"; accountId: string; gatewayName: string }; type ModelOptions = Omit & { analytics?: AnalyticsConfig; @@ -139,12 +139,11 @@ const setupAnalytics = ( } return { client: undefined }; } - case "cloudflare": { return { - baseURL: `https://gateway.ai.cloudflare.com/v1/${analytics.accountId}/${analytics.gatewayName}`, + baseURL: `https://gateway.ai.cloudflare.com/v1/${analytics.accountId}/${analytics.gatewayName}/openai`, defaultHeaders: { - Authorization: `Bearer ${analytics.token}`, + Authorization: `Bearer ${providerApiKey}`, "Content-Type": "application/json", }, };