Skip to content

Commit be85b19

Browse files
authored
add support for custom baseurl in openai provider (#1025)
# why currently, we do not support passing cusotm baseurls for the openai provider # what changed baseUrl can now optionally be passed to the openai client within ai sdk # test plan tested locally
1 parent 6e57b8e commit be85b19

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

.changeset/slimy-cars-matter.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@browserbasehq/stagehand": patch
3+
---
4+
5+
add support for custom baseUrl within openai provider

lib/StagehandPage.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,12 @@ ${scriptContent} \
770770
const { action, modelName, modelClientOptions } = actionOrOptions;
771771

772772
if (this.api) {
773-
const opts = { ...actionOrOptions, frameId: this.rootFrameId };
773+
const opts = {
774+
...actionOrOptions,
775+
frameId: this.rootFrameId,
776+
modelClientOptions:
777+
modelClientOptions || this.stagehand["modelClientOptions"],
778+
};
774779
const result = await this.api.act(opts);
775780
this.stagehand.addToHistory("act", actionOrOptions, result);
776781
return result;
@@ -864,7 +869,12 @@ ${scriptContent} \
864869
} = options;
865870

866871
if (this.api) {
867-
const opts = { ...options, frameId: this.rootFrameId };
872+
const opts = {
873+
...options,
874+
frameId: this.rootFrameId,
875+
modelClientOptions:
876+
modelClientOptions || this.stagehand["modelClientOptions"],
877+
};
868878
const result = await this.api.extract<T>(opts);
869879
this.stagehand.addToHistory("extract", instructionOrOptions, result);
870880
return result;
@@ -968,7 +978,12 @@ ${scriptContent} \
968978
} = options;
969979

970980
if (this.api) {
971-
const opts = { ...options, frameId: this.rootFrameId };
981+
const opts = {
982+
...options,
983+
frameId: this.rootFrameId,
984+
modelClientOptions:
985+
modelClientOptions || this.stagehand["modelClientOptions"],
986+
};
972987
const result = await this.api.observe(opts);
973988
this.stagehand.addToHistory("observe", instructionOrOptions, result);
974989
return result;

lib/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ export class Stagehand {
393393
public readonly logInferenceToFile?: boolean;
394394
private stagehandLogger: StagehandLogger;
395395
private disablePino: boolean;
396-
private modelClientOptions: ClientOptions;
396+
protected modelClientOptions: ClientOptions;
397397
private _env: "LOCAL" | "BROWSERBASE";
398398
private _browser: Browser | undefined;
399399
private _isClosed: boolean = false;

lib/llm/LLMProvider.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ export function getAISDKLanguageModel(
9898
subProvider: string,
9999
subModelName: string,
100100
apiKey?: string,
101+
baseURL?: string,
101102
) {
102103
if (apiKey) {
103104
const creator = AISDKProvidersWithAPIKey[subProvider];
@@ -107,8 +108,12 @@ export function getAISDKLanguageModel(
107108
Object.keys(AISDKProvidersWithAPIKey),
108109
);
109110
}
110-
// Create the provider instance with the API key
111-
const provider = creator({ apiKey });
111+
// Create the provider instance with the API key and baseURL if provided
112+
const providerConfig: { apiKey: string; baseURL?: string } = { apiKey };
113+
if (baseURL) {
114+
providerConfig.baseURL = baseURL;
115+
}
116+
const provider = creator(providerConfig);
112117
// Get the specific model from the provider
113118
return provider(subModelName);
114119
} else {
@@ -166,6 +171,7 @@ export class LLMProvider {
166171
subProvider,
167172
subModelName,
168173
clientOptions?.apiKey,
174+
clientOptions?.baseURL,
169175
);
170176

171177
return new AISdkClient({

0 commit comments

Comments
 (0)