Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support axios config #73

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"SECURITY.md"
],
"dependencies": {
"@types/axios": "^0.14.0",
"axios": "^1.4.0",
"typescript": "^5.1.3"
},
Expand Down
97 changes: 97 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 13 additions & 10 deletions src/model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios from "axios";
import axios, { CreateAxiosDefaults } from "axios";
import { Result, success, error } from "./result";

/**
Expand All @@ -23,6 +23,9 @@ export interface TypeChatLanguageModel {
complete(prompt: string): Promise<Result<string>>;
}

interface Options {
axiosConfg?: CreateAxiosDefaults
}
/**
* Creates a language model encapsulation of an OpenAI or Azure OpenAI REST API endpoint
* chosen by environment variables.
Expand All @@ -38,18 +41,18 @@ export interface TypeChatLanguageModel {
* If none of these key variables are defined, an exception is thrown.
* @returns An instance of `TypeChatLanguageModel`.
*/
export function createLanguageModel(env: Record<string, string | undefined>): TypeChatLanguageModel {
export function createLanguageModel(env: Record<string, string | undefined>, options?: Options): TypeChatLanguageModel {
if (env.OPENAI_API_KEY) {
const apiKey = env.OPENAI_API_KEY ?? missingEnvironmentVariable("OPENAI_API_KEY");
const model = env.OPENAI_MODEL ?? missingEnvironmentVariable("OPENAI_MODEL");
const endPoint = env.OPENAI_ENDPOINT ?? "https://api.openai.com/v1/chat/completions";
const org = env.OPENAI_ORGANIZATION ?? "";
return createOpenAILanguageModel(apiKey, model, endPoint, org);
return createOpenAILanguageModel(apiKey, model, endPoint, org, options);
}
if (env.AZURE_OPENAI_API_KEY) {
const apiKey = env.AZURE_OPENAI_API_KEY ?? missingEnvironmentVariable("AZURE_OPENAI_API_KEY");
const endPoint = env.AZURE_OPENAI_ENDPOINT ?? missingEnvironmentVariable("AZURE_OPENAI_ENDPOINT");
return createAzureOpenAILanguageModel(apiKey, endPoint);
return createAzureOpenAILanguageModel(apiKey, endPoint, options);
}
missingEnvironmentVariable("OPENAI_API_KEY or AZURE_OPENAI_API_KEY");
}
Expand All @@ -62,13 +65,13 @@ export function createLanguageModel(env: Record<string, string | undefined>): Ty
* @param org The OpenAI organization id.
* @returns An instance of `TypeChatLanguageModel`.
*/
export function createOpenAILanguageModel(apiKey: string, model: string, endPoint = "https://api.openai.com/v1/chat/completions", org = ""): TypeChatLanguageModel {
return createAxiosLanguageModel(endPoint, {
export function createOpenAILanguageModel(apiKey: string, model: string, endPoint = "https://api.openai.com/v1/chat/completions", org = "", options: Options = {}): TypeChatLanguageModel {
return createAxiosLanguageModel(endPoint, Object.assign({
headers: {
Authorization: `Bearer ${apiKey}`,
"OpenAI-Organization": org
}
}, { model });
},
}, options), { model });
}

/**
Expand All @@ -79,8 +82,8 @@ export function createOpenAILanguageModel(apiKey: string, model: string, endPoin
* @param apiKey The Azure OpenAI API key.
* @returns An instance of `TypeChatLanguageModel`.
*/
export function createAzureOpenAILanguageModel(apiKey: string, endPoint: string,): TypeChatLanguageModel {
return createAxiosLanguageModel(endPoint, { headers: { "api-key": apiKey } }, {});
export function createAzureOpenAILanguageModel(apiKey: string, endPoint: string, options: Options = {}): TypeChatLanguageModel {
return createAxiosLanguageModel(endPoint, Object.assign({ headers: { "api-key": apiKey } }, options), {});
}

/**
Expand Down