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

Add Vercel SDK to node package to support multiple models #46

Open
wants to merge 2 commits 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
11 changes: 4 additions & 7 deletions node-zerox/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
formatMarkdown,
isString,
} from "./utils";
import { getCompletion } from "./openAI";
import { getCompletion } from "./processor";
import { ModelOptions, ZeroxArgs, ZeroxOutput } from "./types";
import { validateLLMParams } from "./utils";
import fs from "fs-extra";
Expand All @@ -14,13 +14,13 @@ import path from "path";
import pLimit, { Limit } from "p-limit";

export const zerox = async ({
apiKey = "",
cleanup = true,
concurrency = 10,
filePath,
llmParams = {},
maintainFormat = false,
model = ModelOptions.gpt_4o_mini,
openaiAPIKey = "",
outputDir,
pagesToConvertAsImages = -1,
tempDir = os.tmpdir(),
Expand All @@ -34,9 +34,6 @@ export const zerox = async ({
llmParams = validateLLMParams(llmParams);

// Validators
if (!openaiAPIKey || !openaiAPIKey.length) {
throw new Error("Missing OpenAI API key");
}
if (!filePath || !filePath.length) {
throw new Error("Missing file path");
}
Expand Down Expand Up @@ -96,7 +93,7 @@ export const zerox = async ({
const imagePath = path.join(tempDirectory, image);
try {
const { content, inputTokens, outputTokens } = await getCompletion({
apiKey: openaiAPIKey,
apiKey,
imagePath,
llmParams,
maintainFormat,
Expand All @@ -122,7 +119,7 @@ export const zerox = async ({
const imagePath = path.join(tempDirectory, image);
try {
const { content, inputTokens, outputTokens } = await getCompletion({
apiKey: openaiAPIKey,
apiKey,
imagePath,
llmParams,
maintainFormat,
Expand Down
53 changes: 53 additions & 0 deletions node-zerox/src/models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { createAmazonBedrock } from "@ai-sdk/amazon-bedrock";
import { createAnthropic } from "@ai-sdk/anthropic";
import { createGoogleGenerativeAI } from "@ai-sdk/google";
import { createMistral } from "@ai-sdk/mistral";
import { createOpenAI } from "@ai-sdk/openai";

const MODEL_PROVIDERS = {
anthropic: {
models: [
"claude-3-5-sonnet-20240620",
"claude-3-haiku-20240307",
"claude-3-opus-20240229",
"claude-3-sonnet-20240229",
],
provider: createAnthropic,
},
bedrock: {
models: [
"anthropic.claude-3-5-sonnet-20240620-v1:0",
"anthropic.claude-3-haiku-20240307-v1:0",
"anthropic.claude-3-opus-20240229-v1:0",
"anthropic.claude-3-sonnet-20240229-v1:0",
],
provider: createAmazonBedrock,
},
gemini: {
models: [
"gemini-1.5-flash-latest",
"gemini-1.5-flash",
"gemini-1.5-pro-latest",
"gemini-1.5-pro",
],
provider: createGoogleGenerativeAI,
},
gpt: {
models: ["gpt-4-turbo", "gpt-4o-mini", "gpt-4o"],
provider: createOpenAI,
},
mistral: {
models: ["pixtral-12b-2409"],
provider: createMistral,
},
};

export const createProviderInstance = (model: string, apiKey: string) => {
const foundProvider = Object.values(MODEL_PROVIDERS).find((group) =>
group.models.includes(model)
);
if (foundProvider) {
return foundProvider.provider({ apiKey });
}
throw new Error(`Model '${model}' does not support image inputs`);
};
32 changes: 11 additions & 21 deletions node-zerox/src/openAI.ts → node-zerox/src/processor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CompletionArgs, CompletionResponse } from "./types";
import { convertKeysToSnakeCase, encodeImageToBase64 } from "./utils";
import axios from "axios";
import { createProviderInstance } from "./models";
import { generateText } from "ai";

export const getCompletion = async ({
apiKey,
Expand Down Expand Up @@ -41,27 +42,16 @@ export const getCompletion = async ({
});

try {
const response = await axios.post(
"https://api.openai.com/v1/chat/completions",
{
messages,
model,
...convertKeysToSnakeCase(llmParams ?? null),
},
{
headers: {
Authorization: `Bearer ${apiKey}`,
"Content-Type": "application/json",
},
}
);

const data = response.data;

const providerInstance = createProviderInstance(model, apiKey);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to sync ModelOptions type to MODEL_PROVIDERS in models.ts? making sure types are the same

const { text, usage } = await generateText({
model: providerInstance(model),
messages,
...convertKeysToSnakeCase(llmParams ?? null),
});
return {
content: data.choices[0].message.content,
inputTokens: data.usage.prompt_tokens,
outputTokens: data.usage.completion_tokens,
content: text,
inputTokens: usage.promptTokens,
outputTokens: usage.completionTokens,
};
} catch (err) {
console.error("Error in OpenAI completion", err);
Expand Down
2 changes: 1 addition & 1 deletion node-zerox/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
export interface ZeroxArgs {
apiKey?: string;
cleanup?: boolean;
concurrency?: number;
filePath: string;
llmParams?: LLMParams;
maintainFormat?: boolean;
model?: ModelOptions;
openaiAPIKey?: string;
Comment on lines +2 to -8
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update docs in README.md?

Copy link

@shrix1 shrix1 Nov 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about azure openai ?? @annapo23

outputDir?: string;
pagesToConvertAsImages?: number | number[];
tempDir?: string;
Expand Down
Loading