-
Notifications
You must be signed in to change notification settings - Fork 807
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add changes needed for SAP Generative AI Hub
- Loading branch information
Showing
7 changed files
with
265 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { ModelProvider } from "../.."; | ||
import OpenAI from "./OpenAI"; | ||
|
||
class SAPGenAIHub extends OpenAI { | ||
private tokenCache: { token: string; expiry: number } | null = null; | ||
|
||
static providerName: ModelProvider = "sap-gen-ai-hub"; | ||
|
||
private _getTokenParams(): { authURL: string; clientID: string; clientSecret: string } { | ||
if (!this.authURL || !this.clientID || !this.clientSecret) { | ||
throw new Error("Authentication parameters (authURL, clientID, clientSecret) are undefined"); | ||
} | ||
return { | ||
authURL: this.authURL.endsWith("/oauth/token") ? this.authURL : `${this.authURL}/oauth/token`, | ||
clientID: this.clientID, | ||
clientSecret: this.clientSecret, | ||
}; | ||
} | ||
|
||
private async fetchWithTimeout(url: string, options: RequestInit, timeout: number): Promise<Response> { | ||
return new Promise((resolve, reject) => { | ||
const timer = setTimeout(() => reject(new Error('Request timed out')), timeout); | ||
|
||
fetch(url, options) | ||
.then(response => { | ||
clearTimeout(timer); | ||
resolve(response); | ||
}) | ||
.catch(err => { | ||
clearTimeout(timer); | ||
reject(err); | ||
}); | ||
}); | ||
} | ||
|
||
private async fetchOAuthToken(): Promise<string> { | ||
const params = this._getTokenParams(); | ||
const response = await this.fetchWithTimeout(params.authURL, { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, | ||
body: new URLSearchParams({ | ||
client_id: params.clientID, | ||
client_secret: params.clientSecret, | ||
grant_type: 'client_credentials', | ||
}).toString(), | ||
}, 10000); // 10-second timeout | ||
|
||
const data = await response.json(); | ||
return data.access_token; | ||
} | ||
|
||
private async ensureToken(): Promise<void> { | ||
if (!this.tokenCache || Date.now() >= this.tokenCache.expiry) { | ||
const token = await this.fetchOAuthToken(); | ||
const expiry = Date.now() + 3600 * 1000; // Consider making this configurable | ||
this.tokenCache = { token, expiry }; | ||
} | ||
} | ||
|
||
protected async _getRequestHeaders(): Promise<Record<string, string>> { | ||
await this.ensureToken(); | ||
const header: Record<string, string> = { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${this.tokenCache?.token}`, | ||
"api-key": this.apiKey || "", | ||
"AI-Resource-Group": this.resourceGroup || "default", | ||
}; | ||
return header; | ||
} | ||
|
||
protected _getChatUrl() { | ||
if (this.apiType === "azure") { | ||
return `${this.apiBase}/chat/completions?api-version=${this.apiVersion}`; | ||
} else { | ||
let url = this.apiBase; | ||
if (!url) { | ||
throw new Error( | ||
"No API base URL provided. Please set the 'apiBase' option in config.json" | ||
); | ||
} | ||
if (url.endsWith("/")) { | ||
url = url.slice(0, -1); | ||
} | ||
|
||
if (!url.endsWith("/v1")) { | ||
url += "/v1"; | ||
} | ||
return url + "/chat/completions"; | ||
} | ||
} | ||
|
||
protected _getCompletionUrl() { | ||
if (this.apiType === "azure") { | ||
return `${this.apiBase}/completions?api-version=${this.apiVersion}`; | ||
} else { | ||
let url = this.apiBase; | ||
if (!url) { | ||
throw new Error( | ||
"No API base URL provided. Please set the 'apiBase' option in config.json" | ||
); | ||
} | ||
if (url.endsWith("/")) { | ||
url = url.slice(0, -1); | ||
} | ||
if (!url.endsWith("/v1")) { | ||
url += "/v1"; | ||
} | ||
return url + "/completions"; | ||
} | ||
} | ||
|
||
} | ||
|
||
export default SAPGenAIHub; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.