From a8cfddd3c8b45366459f4bd1dd1e4b539b6c71aa Mon Sep 17 00:00:00 2001 From: Nate Date: Wed, 28 May 2025 18:15:38 -0700 Subject: [PATCH] in OpenAI-adapters, use only username, api_key auth --- packages/openai-adapters/src/apis/WatsonX.ts | 114 ++++++++++++------- packages/openai-adapters/src/types.ts | 1 + 2 files changed, 74 insertions(+), 41 deletions(-) diff --git a/packages/openai-adapters/src/apis/WatsonX.ts b/packages/openai-adapters/src/apis/WatsonX.ts index 3962662d2..bcfc4cb9c 100644 --- a/packages/openai-adapters/src/apis/WatsonX.ts +++ b/packages/openai-adapters/src/apis/WatsonX.ts @@ -64,48 +64,80 @@ export class WatsonXApi implements BaseLlmApi { }; } else { // watsonx Software - if (!this.config.apiKey?.includes(":")) { - // Using ZenApiKey auth - return { - token: this.config.apiKey ?? "", - expiration: -1, - }; - } else { - // Using username/password auth - const userPass = this.config.apiKey?.split(":"); - const wxToken = (await ( - await customFetch(this.config.requestOptions)( - `${this.apiBase}/icp4d-api/v1/authorize`, - { - method: "POST", - headers: { - "Content-Type": "application/json", - Accept: "application/json", - }, - body: JSON.stringify({ - username: userPass[0], - password: userPass[1], - }), + // if (this.config.env.bearerTokenRequired) { + // In certain WatsonX environments, ZenApiKey authentication is disabled, + // and it's necessary to call this endpoint with username+api_key to get a bearer token. + // See the docs: https://www.ibm.com/docs/en/watsonx/w-and-w/2.1.0?topic=keys-generating-bearer-token + // Ask @sestinj why the rest is commented out. + const base64Decoded = Buffer.from( + this.config.apiKey ?? "", + "base64", + ).toString(); + const [username, api_key] = base64Decoded.split(":"); + + const wxToken = (await ( + await customFetch(this.config.requestOptions)( + `${this.apiBase}/icp4d-api/v1/authorize`, + { + method: "POST", + headers: { + "Content-Type": "application/json", + Accept: "application/json", }, - ) - ).json()) as any; - const wxTokenExpiry = (await ( - await customFetch(this.config.requestOptions)( - `${this.apiBase}/usermgmt/v1/user/tokenExpiry`, - { - method: "GET", - headers: { - Accept: "application/json", - Authorization: `Bearer ${wxToken["token"]}`, - }, - }, - ) - ).json()) as any; - return { - token: wxToken["token"], - expiration: wxTokenExpiry["exp"], - }; - } + body: JSON.stringify({ + username, + api_key, + }), + }, + ) + ).json()) as any; + + return { + token: wxToken["access_token"], + expiration: 0, + }; + // } else if (!this.config.apiKey?.includes(":")) { + // // Using ZenApiKey auth + // return { + // token: this.config.apiKey ?? "", + // expiration: -1, + // }; + // } else { + // // Using username/password auth + // const userPass = this.config.apiKey?.split(":"); + // const wxToken = (await ( + // await customFetch(this.config.requestOptions)( + // `${this.apiBase}/icp4d-api/v1/authorize`, + // { + // method: "POST", + // headers: { + // "Content-Type": "application/json", + // Accept: "application/json", + // }, + // body: JSON.stringify({ + // username: userPass[0], + // password: userPass[1], + // }), + // }, + // ) + // ).json()) as any; + // const wxTokenExpiry = (await ( + // await customFetch(this.config.requestOptions)( + // `${this.apiBase}/usermgmt/v1/user/tokenExpiry`, + // { + // method: "GET", + // headers: { + // Accept: "application/json", + // Authorization: `Bearer ${wxToken["token"]}`, + // }, + // }, + // ) + // ).json()) as any; + // return { + // token: wxToken["token"], + // expiration: wxTokenExpiry["exp"], + // }; + // } } } diff --git a/packages/openai-adapters/src/types.ts b/packages/openai-adapters/src/types.ts index fd7ad94db..cb352b241 100644 --- a/packages/openai-adapters/src/types.ts +++ b/packages/openai-adapters/src/types.ts @@ -106,6 +106,7 @@ export const WatsonXConfigSchema = BasePlusConfig.extend({ apiVersion: z.string().optional(), projectId: z.string().optional(), deploymentId: z.string().optional(), + bearerTokenRequired: z.boolean().optional(), }), }); export type WatsonXConfig = z.infer;