Skip to content

Commit

Permalink
feat(adapters): allow passing client options for OpenAI and AzureOpenAI
Browse files Browse the repository at this point in the history
Signed-off-by: Tomas Dvorak <[email protected]>
(cherry picked from commit 60b04bc)
  • Loading branch information
Tomas2D committed Jan 14, 2025
1 parent 6b2f418 commit bdaa49a
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/adapters/openai/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { shallowCopy } from "@/serializer/utils.js";
import { ChatLLM, ChatLLMGenerateEvents, ChatLLMOutput } from "@/llms/chat.js";
import { BaseMessage, RoleType } from "@/llms/primitives/message.js";
import { Emitter } from "@/emitter/emitter.js";
import { ClientOptions, OpenAI, AzureOpenAI } from "openai";
import { ClientOptions, OpenAI, AzureOpenAI, AzureClientOptions } from "openai";
import { GetRunContext } from "@/context.js";
import { promptTokensEstimate } from "openai-chat-tokens";
import { Serializer } from "@/serializer/serializer.js";
Expand Down Expand Up @@ -98,6 +98,7 @@ export class OpenAIChatLLMOutput extends ChatLLMOutput {
interface Input {
modelId?: ChatModel;
client?: OpenAI | AzureOpenAI;
clientOptions?: ClientOptions | AzureClientOptions;
parameters?: Partial<Parameters>;
executionOptions?: ExecutionOptions;
cache?: LLMCache<OpenAIChatLLMOutput>;
Expand All @@ -118,7 +119,15 @@ export class OpenAIChatLLM extends ChatLLM<OpenAIChatLLMOutput> {
public readonly client: OpenAI | AzureOpenAI;
public readonly parameters: Partial<Parameters>;

constructor({ client, modelId, parameters, executionOptions = {}, cache, azure }: Input = {}) {
constructor({
client,
modelId,
parameters,
executionOptions = {},
clientOptions = {},
cache,
azure,
}: Input = {}) {
super(modelId || "gpt-4o-mini", executionOptions, cache);
if (client) {
this.client = client;
Expand All @@ -128,9 +137,10 @@ export class OpenAIChatLLM extends ChatLLM<OpenAIChatLLMOutput> {
endpoint: process.env.AZURE_OPENAI_API_ENDPOINT,
apiVersion: process.env.AZURE_OPENAI_API_VERSION,
deployment: process.env.AZURE_OPENAI_API_DEPLOYMENT,
...clientOptions,
});
} else {
this.client = new OpenAI();
this.client = new OpenAI(clientOptions);
}
this.parameters = parameters ?? { temperature: 0 };
}
Expand Down

0 comments on commit bdaa49a

Please sign in to comment.