Skip to content

Commit

Permalink
fix linter + updating exports and build (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
roodboi committed Jan 29, 2024
1 parent 99af6d1 commit 771d175
Show file tree
Hide file tree
Showing 16 changed files with 100 additions and 78 deletions.
5 changes: 5 additions & 0 deletions .changeset/strange-boxes-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@instructor-ai/instructor": patch
---

Updating build and exports for wider range of support
25 changes: 15 additions & 10 deletions .eslintrc.js → .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
/** @type {import("eslint").Linter.Config} */

module.exports = {
$schema: "https://json.schemastore.org/eslintrc",
root: true,
parserOptions: {
project: true,
tsconfigRootDir: __dirname
},
parser: "@typescript-eslint/parser",
plugins: ["@typescript-eslint", "prettier"],
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"prettier",
"plugin:prettier/recommended"
],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
rules: {
"import/no-anonymous-default-export": "off",
"prettier/prettier": "error",
"linebreak-style": "off",
"no-prototype-builtins": "off",
"semi": "off",
Expand All @@ -28,13 +27,19 @@ module.exports = {
}
]
},
overrides: [
{
extends: ["plugin:@typescript-eslint/disable-type-checked"],
files: ["./**/*.mjs", "*.js"]
}
],
ignorePatterns: [
"node_modules/",
"dist/",
"coverage/",
"docs/",
".eslintrc.js",
".eslintrc.cjs",
"package.json",
"tsconfig.json"
]
}
}
4 changes: 2 additions & 2 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"$schema": "https://json.schemastore.org/prettierrc",
"endOfLine": "lf",
"semi": false,
"trailingComma": "none",
Expand Down Expand Up @@ -29,5 +28,6 @@
],
"printWidth": 100,
"proseWrap": "never",
"quoteProps": "consistent"
"quoteProps": "consistent",
"experimentalTernaries": true
}
7 changes: 5 additions & 2 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"recommendations": ["dbaeumer.vscode-eslint", "bradlc.vscode-tailwindcss"]
}
"recommendations": [
"dbaeumer.vscode-eslint",
"bradlc.vscode-tailwindcss"
]
}
8 changes: 6 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
"editor.formatOnType": false,
"editor.formatOnPaste": false,
"editor.trimAutoWhitespace": true,
"editor.formatOnSave": true,
"editor.formatOnSave": false,
"[javascript, javascriptreact, typescript, typescriptreact]": {
"editor.formatOnSave": false
},
"editor.codeActionsOnSave": {
"source.fixAll": "explicit"
"source.fixAll": "always",
"source.fixAll.eslint": "always"
},
"[html]": {
"editor.defaultFormatter": "GitHub.copilot"
Expand All @@ -33,6 +34,9 @@
"typescript",
"typescriptreact"
],
"eslint.options": {
"overrideConfigFile": "./.eslintrc.cjs"
},
"eslint.debug": true,
"github-actions.workflows.pinned.workflows": [
".github/workflows/release.yml"
Expand Down
Binary file modified bun.lockb
Binary file not shown.
31 changes: 17 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@
"publishConfig": {
"access": "public"
},
"main": "./dist/instructor.js",
"module": "./dist/instructor.mjs",
"types": "./dist/instructor.d.ts",
"type": "module",
"main": "./dist/index.js",
"module": "./dist/index.js",
"exports": {
".": {
"import": "./dist/instructor.mjs",
"require": "./dist/instructor.js"
},
"./*": "./dist/*.mjs"
"require": "./dist/index.cjs",
"import": "./dist/index.js",
"default": "./dist/index.js",
"types": "./dist/index.d.ts"
}
},
"files": [
"dist/**"
],
"sideEffects": false,
"typings": "./dist/index.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "tsup",
Expand Down Expand Up @@ -50,7 +51,7 @@
},
"homepage": "https://github.com/instructor-ai/instructor-js#readme",
"dependencies": {
"zod-stream": "^0.0.4",
"zod-stream": "^0.0.5",
"zod-to-json-schema": "^3.22.3",
"zod-validation-error": "^2.1.0"
},
Expand All @@ -64,13 +65,15 @@
"@ianvs/prettier-plugin-sort-imports": "4.1.0",
"@types/bun": "^1.0.0",
"@types/node": "^20.10.6",
"@typescript-eslint/eslint-plugin": "latest",
"@typescript-eslint/parser": "latest",
"eslint": "latest",
"eslint-config-prettier": "latest",
"eslint-config-turbo": "^1.10.12",
"eslint-config-prettier": "^9.0.0",
"eslint-config": "^0.3.0",
"eslint-plugin-prettier": "^5.1.2",
"eslint-import-resolver-typescript": "^3.5.5",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-prettier": "latest",
"eslint-plugin-only-warn": "^1.1.0",
"@typescript-eslint/parser": "^6.11.0",
"@typescript-eslint/eslint-plugin": "^6.11.0",
"husky": "^8.0.3",
"prettier": "latest",
"tsup": "^8.0.1",
Expand Down
4 changes: 2 additions & 2 deletions src/constants/modes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import {
} from "@/oai/params"
import {
OAIResponseFnArgsParser,
OAIResponseJSONParser,
OAIResponseJSONStringParser,
OAIResponseToolArgsParser,
OAIResponseJSONParser
OAIResponseToolArgsParser
} from "@/oai/parser"

export const MODE = {
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * as Instructor from "./instructor"
import Instructor from "./instructor"

export default Instructor
16 changes: 5 additions & 11 deletions src/instructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,7 @@ class Instructor {
}
}

this.log(
"debug",
response_model.name,
"making completion call with params: ",
resolvedParams
)
this.log("debug", response_model.name, "making completion call with params: ", resolvedParams)

const completion = await this.client.chat.completions.create(resolvedParams)
const parser = MODE_TO_PARSER[this.mode]
Expand All @@ -111,7 +106,6 @@ class Instructor {
} catch (error) {
this.log("error", "failed to parse completion", parsedCompletion, this.mode)
}

}

const makeCompletionCallWithRetries = async () => {
Expand Down Expand Up @@ -201,8 +195,7 @@ class Instructor {
completions: {
create: async <
T extends z.AnyZodObject,
P extends T extends z.AnyZodObject
? ChatCompletionCreateParamsWithModel<T>
P extends T extends z.AnyZodObject ? ChatCompletionCreateParamsWithModel<T>
: OpenAI.ChatCompletionCreateParams & { response_model: never }
>(
params: P
Expand All @@ -214,8 +207,9 @@ class Instructor {
return this.chatCompletionStandard(params) as ReturnTypeBasedOnParams<P>
}
} else {
const result: OpenAI.Chat.Completions.ChatCompletion = this.isStandardStream(params)
? await this.client.chat.completions.create(params)
const result: OpenAI.Chat.Completions.ChatCompletion =
this.isStandardStream(params) ?
await this.client.chat.completions.create(params)
: await this.client.chat.completions.create(params)

return result as ReturnTypeBasedOnParams<P>
Expand Down
14 changes: 6 additions & 8 deletions src/oai/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,13 @@ export function OAIResponseJSONStringParser(
return text
}


/**
* `OAIResponseJSONParser` parses a JSON string and extracts the JSON content.
*
*
* @param {string} data - The JSON string to parse.
* @returns {Object} - The extracted JSON content.
*
*
*
*
*/
export function OAIResponseJSONParser(
data:
Expand All @@ -104,9 +103,8 @@ export function OAIResponseJSONParser(
const text =
parsedData.choices?.[0].delta?.content ?? parsedData?.choices[0]?.message?.content ?? null

const jsonRegex = /```json\n([\s\S]*?)\n```/;
const match = text.match(jsonRegex);

const jsonRegex = /```json\n([\s\S]*?)\n```/
const match = text.match(jsonRegex)

return match ? match[1] : text;
return match ? match[1] : text
}
23 changes: 12 additions & 11 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@ export type InstructorChatCompletionParams<T extends z.AnyZodObject> = {
export type ChatCompletionCreateParamsWithModel<T extends z.AnyZodObject> =
InstructorChatCompletionParams<T> & ChatCompletionCreateParams

export type ReturnWithoutModel<P> = P extends { stream: true }
? Stream<OpenAI.Chat.Completions.ChatCompletionChunk>
export type ReturnWithoutModel<P> =
P extends { stream: true } ? Stream<OpenAI.Chat.Completions.ChatCompletionChunk>
: OpenAI.Chat.Completions.ChatCompletion

export type ReturnTypeBasedOnParams<P> = P extends {
stream: true
response_model: ResponseModel<infer T>
}
? Promise<AsyncGenerator<Partial<z.infer<T>>, void, unknown>>
: P extends { response_model: ResponseModel<infer T> }
? Promise<z.infer<T>>
: P extends { stream: true }
? Stream<OpenAI.Chat.Completions.ChatCompletionChunk>
export type ReturnTypeBasedOnParams<P> =
P extends (
{
stream: true
response_model: ResponseModel<infer T>
}
) ?
Promise<AsyncGenerator<Partial<z.infer<T>>, void, unknown>>
: P extends { response_model: ResponseModel<infer T> } ? Promise<z.infer<T>>
: P extends { stream: true } ? Stream<OpenAI.Chat.Completions.ChatCompletionChunk>
: OpenAI.Chat.Completions.ChatCompletion
2 changes: 1 addition & 1 deletion tests/mode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { MODE } from "@/constants/modes"

const models_latest = ["gpt-3.5-turbo-1106", "gpt-4-1106-preview"]
const models_old = ["gpt-3.5-turbo", "gpt-4"]
const models_anyscale = ["Open-Orca/Mistral-7B-OpenOrca"]
const models_anyscale = ["mistralai/Mistral-7B-Instruct-v0.1"]

const createTestCases = (): { model: string; mode: Mode }[] => {
const { FUNCTIONS, JSON_SCHEMA, ...rest } = MODE
Expand Down
5 changes: 1 addition & 4 deletions tests/zod-type.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import { describe, expect, test } from "bun:test"
import OpenAI from "openai"
import { z } from "zod"

async function extractUser({
schema
}) {

async function extractUser({ schema }) {
const oai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY ?? undefined,
organization: process.env.OPENAI_ORG_ID ?? undefined
Expand Down
22 changes: 15 additions & 7 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,33 @@
"strictNullChecks": true,
"noEmit": true,
"allowJs": true,
"jsx": "preserve",
"lib": ["es2020", "dom"],
"module": "esnext",
"target": "ESNext",
"target": "es2020",
"skipLibCheck": true,
"esModuleInterop": true,
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"moduleDetection": "force",
"baseUrl": ".",
"lib": ["ESNext", "dom", "dom.iterable", "esnext"],
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"isolatedModules": true,
"incremental": true,
"tsBuildInfoFile": "tsconfig.tsbuildinfo",
"paths": {
"@/*": ["./src/*"]
"@/*": [
"./src/*"
]
}
},
"include": ["**/*.ts", "**/*.tsx"],
"exclude": ["node_modules", "dist"]
}
"include": [
"**/*.ts",
"**/*.tsx"
],
"exclude": [
"node_modules",
"dist",
".eslintrc.cjs"
]
}
8 changes: 5 additions & 3 deletions tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { defineConfig } from "tsup"
export default defineConfig(options => {
return {
splitting: true,
entry: ["src/**/*.{ts,tsx}"],
format: ["esm", "cjs"],
dts: true,
sourcemap: true,
minify: true,
entry: ["src/index.ts"],
target: "es2020",
format: ["cjs", "esm"],
clean: true,
dts: true,
external: ["openai", "zod"],
...options
}
Expand Down

0 comments on commit 771d175

Please sign in to comment.