Skip to content

Commit

Permalink
extra types (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
roodboi committed Jan 31, 2024
1 parent 89ae3a9 commit c7aec7c
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/stale-parents-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@instructor-ai/instructor": patch
---

Fixing inference on stream types when using npm or pnpm
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"files": [
"dist/**"
],
"typings": "./dist/index.d.ts",
"types": "./dist/index.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "tsup",
Expand Down
4 changes: 3 additions & 1 deletion src/instructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,9 @@ class Instructor {
): Promise<ReturnTypeBasedOnParams<P>> => {
if (this.isChatCompletionCreateParamsWithModel(params)) {
if (params.stream) {
return this.chatCompletionStream(params) as ReturnTypeBasedOnParams<P>
return this.chatCompletionStream(params) as ReturnTypeBasedOnParams<
P & { stream: true }
>
} else {
return this.chatCompletionStandard(params) as ReturnTypeBasedOnParams<P>
}
Expand Down
7 changes: 3 additions & 4 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import OpenAI from "openai"
import type { ChatCompletionCreateParams } from "openai/resources/index.mjs"
import { Stream } from "openai/streaming.mjs"
import { Stream } from "openai/streaming"
import { z } from "zod"

import { MODE } from "@/constants/modes"
Expand All @@ -27,10 +26,10 @@ export type InstructorChatCompletionParams<T extends z.AnyZodObject> = {
}

export type ChatCompletionCreateParamsWithModel<T extends z.AnyZodObject> =
InstructorChatCompletionParams<T> & ChatCompletionCreateParams
InstructorChatCompletionParams<T> & OpenAI.ChatCompletionCreateParams

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

export type ReturnTypeBasedOnParams<P> =
Expand Down
2 changes: 1 addition & 1 deletion tests/inference.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Instructor from "@/instructor"
import { describe, expect, test } from "bun:test"
import OpenAI from "openai"
import { Stream } from "openai/streaming.mjs"
import { Stream } from "openai/streaming"
import { type } from "ts-inference-check"
import { z } from "zod"

Expand Down

0 comments on commit c7aec7c

Please sign in to comment.