in OpenAI-adapters, use only username, api_key auth

This commit is contained in:
Nate 2025-05-28 18:15:38 -07:00
parent 2623f90b45
commit a8cfddd3c8
2 changed files with 74 additions and 41 deletions

View file

@ -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"],
// };
// }
}
}

View file

@ -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<typeof WatsonXConfigSchema>;