Skip to content

Commit 771d175

Browse files
authored
fix linter + updating exports and build (#90)
1 parent 99af6d1 commit 771d175

16 files changed

+100
-78
lines changed

.changeset/strange-boxes-raise.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@instructor-ai/instructor": patch
3+
---
4+
5+
Updating build and exports for wider range of support
+15-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1+
/** @type {import("eslint").Linter.Config} */
2+
13
module.exports = {
24
$schema: "https://json.schemastore.org/eslintrc",
35
root: true,
4-
parserOptions: {
5-
project: true,
6-
tsconfigRootDir: __dirname
7-
},
6+
parser: "@typescript-eslint/parser",
7+
plugins: ["@typescript-eslint", "prettier"],
88
extends: [
9-
"eslint:recommended",
109
"plugin:@typescript-eslint/recommended",
11-
"prettier",
10+
"plugin:prettier/recommended"
1211
],
13-
parser: '@typescript-eslint/parser',
14-
plugins: ['@typescript-eslint'],
1512
rules: {
13+
"import/no-anonymous-default-export": "off",
14+
"prettier/prettier": "error",
1615
"linebreak-style": "off",
1716
"no-prototype-builtins": "off",
1817
"semi": "off",
@@ -28,13 +27,19 @@ module.exports = {
2827
}
2928
]
3029
},
30+
overrides: [
31+
{
32+
extends: ["plugin:@typescript-eslint/disable-type-checked"],
33+
files: ["./**/*.mjs", "*.js"]
34+
}
35+
],
3136
ignorePatterns: [
3237
"node_modules/",
3338
"dist/",
3439
"coverage/",
3540
"docs/",
36-
".eslintrc.js",
41+
".eslintrc.cjs",
3742
"package.json",
3843
"tsconfig.json"
3944
]
40-
}
45+
}

.prettierrc

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{
2-
"$schema": "https://json.schemastore.org/prettierrc",
32
"endOfLine": "lf",
43
"semi": false,
54
"trailingComma": "none",
@@ -29,5 +28,6 @@
2928
],
3029
"printWidth": 100,
3130
"proseWrap": "never",
32-
"quoteProps": "consistent"
31+
"quoteProps": "consistent",
32+
"experimentalTernaries": true
3333
}

.vscode/extensions.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{
2-
"recommendations": ["dbaeumer.vscode-eslint", "bradlc.vscode-tailwindcss"]
3-
}
2+
"recommendations": [
3+
"dbaeumer.vscode-eslint",
4+
"bradlc.vscode-tailwindcss"
5+
]
6+
}

.vscode/settings.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
"editor.formatOnType": false,
55
"editor.formatOnPaste": false,
66
"editor.trimAutoWhitespace": true,
7-
"editor.formatOnSave": true,
7+
"editor.formatOnSave": false,
88
"[javascript, javascriptreact, typescript, typescriptreact]": {
99
"editor.formatOnSave": false
1010
},
1111
"editor.codeActionsOnSave": {
12-
"source.fixAll": "explicit"
12+
"source.fixAll": "always",
13+
"source.fixAll.eslint": "always"
1314
},
1415
"[html]": {
1516
"editor.defaultFormatter": "GitHub.copilot"
@@ -33,6 +34,9 @@
3334
"typescript",
3435
"typescriptreact"
3536
],
37+
"eslint.options": {
38+
"overrideConfigFile": "./.eslintrc.cjs"
39+
},
3640
"eslint.debug": true,
3741
"github-actions.workflows.pinned.workflows": [
3842
".github/workflows/release.yml"

bun.lockb

71.8 KB
Binary file not shown.

package.json

+17-14
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,21 @@
55
"publishConfig": {
66
"access": "public"
77
},
8-
"main": "./dist/instructor.js",
9-
"module": "./dist/instructor.mjs",
10-
"types": "./dist/instructor.d.ts",
8+
"type": "module",
9+
"main": "./dist/index.js",
10+
"module": "./dist/index.js",
1111
"exports": {
1212
".": {
13-
"import": "./dist/instructor.mjs",
14-
"require": "./dist/instructor.js"
15-
},
16-
"./*": "./dist/*.mjs"
13+
"require": "./dist/index.cjs",
14+
"import": "./dist/index.js",
15+
"default": "./dist/index.js",
16+
"types": "./dist/index.d.ts"
17+
}
1718
},
1819
"files": [
1920
"dist/**"
2021
],
21-
"sideEffects": false,
22+
"typings": "./dist/index.d.ts",
2223
"scripts": {
2324
"test": "echo \"Error: no test specified\" && exit 1",
2425
"build": "tsup",
@@ -50,7 +51,7 @@
5051
},
5152
"homepage": "https://github.com/instructor-ai/instructor-js#readme",
5253
"dependencies": {
53-
"zod-stream": "^0.0.4",
54+
"zod-stream": "^0.0.5",
5455
"zod-to-json-schema": "^3.22.3",
5556
"zod-validation-error": "^2.1.0"
5657
},
@@ -64,13 +65,15 @@
6465
"@ianvs/prettier-plugin-sort-imports": "4.1.0",
6566
"@types/bun": "^1.0.0",
6667
"@types/node": "^20.10.6",
67-
"@typescript-eslint/eslint-plugin": "latest",
68-
"@typescript-eslint/parser": "latest",
69-
"eslint": "latest",
70-
"eslint-config-prettier": "latest",
68+
"eslint-config-turbo": "^1.10.12",
69+
"eslint-config-prettier": "^9.0.0",
70+
"eslint-config": "^0.3.0",
71+
"eslint-plugin-prettier": "^5.1.2",
7172
"eslint-import-resolver-typescript": "^3.5.5",
7273
"eslint-plugin-import": "^2.27.5",
73-
"eslint-plugin-prettier": "latest",
74+
"eslint-plugin-only-warn": "^1.1.0",
75+
"@typescript-eslint/parser": "^6.11.0",
76+
"@typescript-eslint/eslint-plugin": "^6.11.0",
7477
"husky": "^8.0.3",
7578
"prettier": "latest",
7679
"tsup": "^8.0.1",

src/constants/modes.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import {
55
} from "@/oai/params"
66
import {
77
OAIResponseFnArgsParser,
8+
OAIResponseJSONParser,
89
OAIResponseJSONStringParser,
9-
OAIResponseToolArgsParser,
10-
OAIResponseJSONParser
10+
OAIResponseToolArgsParser
1111
} from "@/oai/parser"
1212

1313
export const MODE = {

src/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
export * as Instructor from "./instructor"
1+
import Instructor from "./instructor"
2+
3+
export default Instructor

src/instructor.ts

+5-11
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,7 @@ class Instructor {
9595
}
9696
}
9797

98-
this.log(
99-
"debug",
100-
response_model.name,
101-
"making completion call with params: ",
102-
resolvedParams
103-
)
98+
this.log("debug", response_model.name, "making completion call with params: ", resolvedParams)
10499

105100
const completion = await this.client.chat.completions.create(resolvedParams)
106101
const parser = MODE_TO_PARSER[this.mode]
@@ -111,7 +106,6 @@ class Instructor {
111106
} catch (error) {
112107
this.log("error", "failed to parse completion", parsedCompletion, this.mode)
113108
}
114-
115109
}
116110

117111
const makeCompletionCallWithRetries = async () => {
@@ -201,8 +195,7 @@ class Instructor {
201195
completions: {
202196
create: async <
203197
T extends z.AnyZodObject,
204-
P extends T extends z.AnyZodObject
205-
? ChatCompletionCreateParamsWithModel<T>
198+
P extends T extends z.AnyZodObject ? ChatCompletionCreateParamsWithModel<T>
206199
: OpenAI.ChatCompletionCreateParams & { response_model: never }
207200
>(
208201
params: P
@@ -214,8 +207,9 @@ class Instructor {
214207
return this.chatCompletionStandard(params) as ReturnTypeBasedOnParams<P>
215208
}
216209
} else {
217-
const result: OpenAI.Chat.Completions.ChatCompletion = this.isStandardStream(params)
218-
? await this.client.chat.completions.create(params)
210+
const result: OpenAI.Chat.Completions.ChatCompletion =
211+
this.isStandardStream(params) ?
212+
await this.client.chat.completions.create(params)
219213
: await this.client.chat.completions.create(params)
220214

221215
return result as ReturnTypeBasedOnParams<P>

src/oai/parser.ts

+6-8
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,13 @@ export function OAIResponseJSONStringParser(
8585
return text
8686
}
8787

88-
8988
/**
9089
* `OAIResponseJSONParser` parses a JSON string and extracts the JSON content.
91-
*
90+
*
9291
* @param {string} data - The JSON string to parse.
9392
* @returns {Object} - The extracted JSON content.
94-
*
95-
*
93+
*
94+
*
9695
*/
9796
export function OAIResponseJSONParser(
9897
data:
@@ -104,9 +103,8 @@ export function OAIResponseJSONParser(
104103
const text =
105104
parsedData.choices?.[0].delta?.content ?? parsedData?.choices[0]?.message?.content ?? null
106105

107-
const jsonRegex = /```json\n([\s\S]*?)\n```/;
108-
const match = text.match(jsonRegex);
109-
106+
const jsonRegex = /```json\n([\s\S]*?)\n```/
107+
const match = text.match(jsonRegex)
110108

111-
return match ? match[1] : text;
109+
return match ? match[1] : text
112110
}

src/types/index.ts

+12-11
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,18 @@ export type InstructorChatCompletionParams<T extends z.AnyZodObject> = {
2929
export type ChatCompletionCreateParamsWithModel<T extends z.AnyZodObject> =
3030
InstructorChatCompletionParams<T> & ChatCompletionCreateParams
3131

32-
export type ReturnWithoutModel<P> = P extends { stream: true }
33-
? Stream<OpenAI.Chat.Completions.ChatCompletionChunk>
32+
export type ReturnWithoutModel<P> =
33+
P extends { stream: true } ? Stream<OpenAI.Chat.Completions.ChatCompletionChunk>
3434
: OpenAI.Chat.Completions.ChatCompletion
3535

36-
export type ReturnTypeBasedOnParams<P> = P extends {
37-
stream: true
38-
response_model: ResponseModel<infer T>
39-
}
40-
? Promise<AsyncGenerator<Partial<z.infer<T>>, void, unknown>>
41-
: P extends { response_model: ResponseModel<infer T> }
42-
? Promise<z.infer<T>>
43-
: P extends { stream: true }
44-
? Stream<OpenAI.Chat.Completions.ChatCompletionChunk>
36+
export type ReturnTypeBasedOnParams<P> =
37+
P extends (
38+
{
39+
stream: true
40+
response_model: ResponseModel<infer T>
41+
}
42+
) ?
43+
Promise<AsyncGenerator<Partial<z.infer<T>>, void, unknown>>
44+
: P extends { response_model: ResponseModel<infer T> } ? Promise<z.infer<T>>
45+
: P extends { stream: true } ? Stream<OpenAI.Chat.Completions.ChatCompletionChunk>
4546
: OpenAI.Chat.Completions.ChatCompletion

tests/mode.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { MODE } from "@/constants/modes"
88

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

1313
const createTestCases = (): { model: string; mode: Mode }[] => {
1414
const { FUNCTIONS, JSON_SCHEMA, ...rest } = MODE

tests/zod-type.test.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ import { describe, expect, test } from "bun:test"
33
import OpenAI from "openai"
44
import { z } from "zod"
55

6-
async function extractUser({
7-
schema
8-
}) {
9-
6+
async function extractUser({ schema }) {
107
const oai = new OpenAI({
118
apiKey: process.env.OPENAI_API_KEY ?? undefined,
129
organization: process.env.OPENAI_ORG_ID ?? undefined

tsconfig.json

+15-7
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,33 @@
44
"strictNullChecks": true,
55
"noEmit": true,
66
"allowJs": true,
7-
"jsx": "preserve",
7+
"lib": ["es2020", "dom"],
88
"module": "esnext",
9-
"target": "ESNext",
9+
"target": "es2020",
1010
"skipLibCheck": true,
1111
"esModuleInterop": true,
1212
"moduleResolution": "bundler",
1313
"allowImportingTsExtensions": true,
1414
"moduleDetection": "force",
1515
"baseUrl": ".",
16-
"lib": ["ESNext", "dom", "dom.iterable", "esnext"],
1716
"forceConsistentCasingInFileNames": true,
1817
"resolveJsonModule": true,
1918
"isolatedModules": true,
2019
"incremental": true,
2120
"tsBuildInfoFile": "tsconfig.tsbuildinfo",
2221
"paths": {
23-
"@/*": ["./src/*"]
22+
"@/*": [
23+
"./src/*"
24+
]
2425
}
2526
},
26-
"include": ["**/*.ts", "**/*.tsx"],
27-
"exclude": ["node_modules", "dist"]
28-
}
27+
"include": [
28+
"**/*.ts",
29+
"**/*.tsx"
30+
],
31+
"exclude": [
32+
"node_modules",
33+
"dist",
34+
".eslintrc.cjs"
35+
]
36+
}

tsup.config.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import { defineConfig } from "tsup"
33
export default defineConfig(options => {
44
return {
55
splitting: true,
6-
entry: ["src/**/*.{ts,tsx}"],
7-
format: ["esm", "cjs"],
8-
dts: true,
6+
sourcemap: true,
97
minify: true,
8+
entry: ["src/index.ts"],
9+
target: "es2020",
10+
format: ["cjs", "esm"],
1011
clean: true,
12+
dts: true,
1113
external: ["openai", "zod"],
1214
...options
1315
}

0 commit comments

Comments
 (0)