From c205286dccdbc6feacfd2aeeca0e0ba449631a57 Mon Sep 17 00:00:00 2001 From: Dimitri Kennedy Date: Tue, 27 Feb 2024 10:56:18 -0500 Subject: [PATCH] updating deps + picking up stream meta and types (#125) --- .changeset/small-tomatoes-scream.md | 5 +++++ bun.lockb | Bin 274805 -> 274805 bytes package.json | 2 +- src/instructor.ts | 12 ++++++++++-- src/types/index.ts | 8 ++++++-- tests/inference.test.ts | 5 +++-- 6 files changed, 25 insertions(+), 7 deletions(-) create mode 100644 .changeset/small-tomatoes-scream.md diff --git a/.changeset/small-tomatoes-scream.md b/.changeset/small-tomatoes-scream.md new file mode 100644 index 00000000..4b0795d2 --- /dev/null +++ b/.changeset/small-tomatoes-scream.md @@ -0,0 +1,5 @@ +--- +"@instructor-ai/instructor": major +--- + +Updating zod-stream major and stream output types - this change moves the internal properties tacked onto the stream output from many \_properties to one \_meta object with the properties nested - this also adds explicit types so when used in ts projects it doesnt yell. diff --git a/bun.lockb b/bun.lockb index 42d4470d2045f18de57cfbe0500776015f621bd2..db6e4e78693e9ed87ac6bc8fbcf75ecaffeb1447 100755 GIT binary patch delta 155 zcmezRNZ{)ufeCsFr(CvpO!ofdAT0QB_l`buFOyP*|BL=)Fv!Z*n186Av}B_nlP)9U zxbb5NLbvn0#7mPS}kkefxm!Pf51PPM_ZChSm#h-!OA? zao+dLzBXxt>dZRxBlSA907x*IhiB|8rX%}C6rCLMHu}%Ul z0RWTHGdHt{Efw5AsVl)Y!dio~vDGwNF9U8T5edTJz=|*u^`OE28vP^JxQvcSkA{1@ zqr&U;a@=>Tk)0r@8b>Y2&8J+fc)O(phusMQx7`T>Puc}AE-)@Qw=v`bgL?%qE-)@Q Jx4B;fywS4`N%sH% diff --git a/package.json b/package.json index 47a2f5d0..25c9e3a8 100644 --- a/package.json +++ b/package.json @@ -51,7 +51,7 @@ }, "homepage": "https://github.com/instructor-ai/instructor-js#readme", "dependencies": { - "zod-stream": "0.0.8", + "zod-stream": "1.0.0", "zod-validation-error": "^2.1.0" }, "peerDependencies": { diff --git a/src/instructor.ts b/src/instructor.ts index 2b713d63..199fd1a9 100644 --- a/src/instructor.ts +++ b/src/instructor.ts @@ -6,7 +6,13 @@ import { } from "@/types" import OpenAI from "openai" import { z } from "zod" -import ZodStream, { OAIResponseParser, OAIStream, withResponseModel, type Mode } from "zod-stream" +import ZodStream, { + CompletionMeta, + OAIResponseParser, + OAIStream, + withResponseModel, + type Mode +} from "zod-stream" import { fromZodError } from "zod-validation-error" import { @@ -186,7 +192,9 @@ class Instructor { max_retries, response_model, ...params - }: ChatCompletionCreateParamsWithModel): Promise, void, unknown>> { + }: ChatCompletionCreateParamsWithModel): Promise< + AsyncGenerator & { _meta: CompletionMeta }, void, unknown> + > { if (max_retries) { this.log("warn", "max_retries is not supported for streaming completions") } diff --git a/src/types/index.ts b/src/types/index.ts index 3cf212e1..6f532599 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,7 +1,11 @@ import OpenAI from "openai" import { Stream } from "openai/streaming" import { z } from "zod" -import { type Mode as ZMode, type ResponseModel as ZResponseModel } from "zod-stream" +import { + CompletionMeta, + type Mode as ZMode, + type ResponseModel as ZResponseModel +} from "zod-stream" export type LogLevel = "debug" | "info" | "warn" | "error" @@ -33,7 +37,7 @@ export type ReturnTypeBasedOnParams

= response_model: ResponseModel } ) ? - Promise>, void, unknown>> + Promise> & { _meta: CompletionMeta }, void, unknown>> : P extends { response_model: ResponseModel } ? Promise> : P extends { stream: true } ? Stream : OpenAI.Chat.Completions.ChatCompletion diff --git a/tests/inference.test.ts b/tests/inference.test.ts index 5a72be18..dd4b1721 100644 --- a/tests/inference.test.ts +++ b/tests/inference.test.ts @@ -12,6 +12,7 @@ import OpenAI from "openai" import { Stream } from "openai/streaming" import { type } from "ts-inference-check" import { z } from "zod" +import { CompletionMeta } from "zod-stream" describe("Inference Checking", () => { const UserSchema = z.object({ @@ -78,7 +79,7 @@ describe("Inference Checking", () => { Partial<{ name: string age: number - }>, + }> & { _meta: CompletionMeta }, void, unknown > @@ -102,7 +103,7 @@ describe("Inference Checking", () => { Partial<{ name: string age: number - }>, + }> & { _meta: CompletionMeta }, void, unknown >