Skip to content

Commit

Permalink
Merge pull request #9 from RelevanceAI/hotfix/revert-type-changes
Browse files Browse the repository at this point in the history
Revert type changes
  • Loading branch information
danieljpalmer committed Jun 2, 2023
2 parents f52b217 + d0209f5 commit 6f74bfe
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 94 deletions.
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@relevanceai/chain",
"version": "0.1.1",
"version": "0.1.2",
"private": false,
"description": "The managed, developer-first SDK for building LLM chains.",
"main": "dist/index.js",
Expand Down Expand Up @@ -64,11 +64,10 @@
"cross-fetch": "^3.1.6",
"dotenv": "^16.0.3",
"json-schema": "^0.4.0",
"json-schema-to-ts": "^2.8.2",
"kleur": "^4.1.5",
"open": "^9.1.0",
"ora": "^6.3.0",
"tiny-invariant": "^1.3.1",
"zod": "^3.21.4",
"zod-to-json-schema": "^3.21.1"
"tiny-invariant": "^1.3.1"
}
}
33 changes: 11 additions & 22 deletions src/chain.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { describe, test, expect, expectTypeOf } from "vitest";
import { defineChain, jsonSchemaParam, zodParam } from "./chain";
import { defineChain } from "./chain";
import { InferChainInput, InferChainOutput } from "./types";
import { z } from "zod";

describe("example chains", () => {
test("basic pdf-qa chain", () => {
Expand All @@ -11,10 +10,12 @@ describe("example chains", () => {
publiclyTriggerable: true,

params: {
pdf_url: jsonSchemaParam<string>({
pdf_url: {
type: "string",
}),
question: z.string(),
},
question: {
type: "string",
},
},
setup({ params, step }) {
const { pdf_url, question } = params;
Expand Down Expand Up @@ -50,19 +51,6 @@ Answer:`,
},
});

const chainJson = chain.toJSON();

expect(chainJson.params_schema).toMatchObject({
properties: {
pdf_url: {
type: "string",
},
question: {
type: "string",
},
},
});

expect(chain.toJSON()).toMatchInlineSnapshot(`
{
"description": "",
Expand Down Expand Up @@ -128,11 +116,10 @@ Answer:`,
}
`);

expectTypeOf<InferChainInput<typeof chain>>().toEqualTypeOf<{
expectTypeOf<InferChainInput<typeof chain>>().toMatchTypeOf<{
pdf_url: string;
question: string;
}>();

expectTypeOf<InferChainOutput<typeof chain>>().toMatchTypeOf<{
answer: string;
}>();
Expand All @@ -141,7 +128,9 @@ Answer:`,
test("long pdf summarise", () => {
const chain = defineChain({
params: {
pdf_url: zodParam(z.string()),
pdf_url: {
type: "string",
},
},
setup({ params, step, foreach }) {
const { pdf_url } = params;
Expand Down Expand Up @@ -235,7 +224,7 @@ Summarise the above text.`,
`);
expect(chainJSON.transformations.steps[2]?.foreach).toBeDefined();

expectTypeOf<InferChainInput<typeof chain>>().toEqualTypeOf<{
expectTypeOf<InferChainInput<typeof chain>>().toMatchTypeOf<{
pdf_url: string;
}>();
expectTypeOf<InferChainOutput<typeof chain>>().toMatchTypeOf<{
Expand Down
35 changes: 3 additions & 32 deletions src/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,19 @@ import {
ArrayItem,
ChainConfig,
LooseAutoComplete,
ParamDefinitionInput,
ParamSchema,
ParamSchemaMetadata,
ParamsToTypedObject,
Prettify,
TransformationInput,
TransformationOutput,
TransformationStep,
TransformationsMap,
TypedJsonSchema,
} from "./types";
import { jsonClone } from "./utils";
import { JsCodeTransformationOutput } from "./generated/transformation-types";
import { ZodType } from "zod";
import { zodToJsonSchema as _zodToJsonSchema } from "zod-to-json-schema";

const zodToJsonSchema = (schema: ZodType<any>) => {
const s = _zodToJsonSchema(schema);
delete s.$schema;
return s;
};

export const jsonSchemaParam = <T extends any>(schema: ParamSchema<T>) =>
schema as unknown as TypedJsonSchema<T>;

export const zodParam = <T extends any>(
schema: ZodType<T>,
metadata: ParamSchemaMetadata = {}
) => {
const jsonSchema = zodToJsonSchema(schema) as unknown as TypedJsonSchema<T>;
return { ...jsonSchema, ...metadata };
};

export class Chain<
ParamsDefinition extends ParamDefinitionInput,
ParamsDefinition extends Record<string, ParamSchema>,
OutputDefinition extends Record<string, any>
> {
$RELEVANCE_CHAIN_BRAND = true;
Expand Down Expand Up @@ -86,14 +64,7 @@ export class Chain<
);
}

this.params = Object.fromEntries(
Object.entries(params).map(([key, value]) => {
if (value instanceof ZodType) {
return [key, zodToJsonSchema(value)];
}
return [key, value];
})
);
this.params = params;
return createVariable<Prettify<ParamsToTypedObject<InnerParamsDefinition>>>(
{
path: "params",
Expand Down Expand Up @@ -290,7 +261,7 @@ return (() => {
}

static define = <
ChainParamsDefinition extends ParamDefinitionInput,
ChainParamsDefinition extends Record<string, ParamSchema>,
ChainOutputDefinition extends Record<string, any>
>(input: {
title?: string;
Expand Down
2 changes: 0 additions & 2 deletions src/generated/transformation-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ export interface SearchArrayInput {
array: any[];
query: string;
page_size?: number;
field?: string;
}

export interface SearchArrayOutput {
Expand Down Expand Up @@ -381,7 +380,6 @@ export interface SplitTextInput {
text: string;
method: "tokens" | "separator";
num_tokens?: number;
num_tokens_to_slide_window?: number;
sep?: string;
}

Expand Down
47 changes: 13 additions & 34 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { JSONSchema4 } from "json-schema";
import type { JSONSchema, FromSchema } from "json-schema-to-ts";
import { BuiltinTransformations } from "./generated/transformation-types";
import { UnwrapVariable } from "./variable";
import type { Chain } from "./chain";

export type ChainConfig = {
_id?: string;
Expand All @@ -10,7 +11,7 @@ export type ChainConfig = {
project?: string;
/** JSONSchema object that user parameters will be validated against */
params_schema: {
properties: Record<string, ParamSchema<any>>;
properties: Record<string, ParamSchema>;
};
/** Version */
version?: string;
Expand Down Expand Up @@ -82,17 +83,15 @@ export type SchemaMetadata = {
};
};

export type ParamSchemaMetadata = {
export type ParamSchema = JSONSchema4 & {
/**
* The order in which the parameter should be displayed in the form.
*/
order?: number | undefined;
metadata?: SchemaMetadata | undefined;
items?: JSONSchema4 | undefined;
order?: number;
metadata?: SchemaMetadata;
items?: JSONSchema4 & { type: string };
};

export type ParamSchema<T> = JSONSchema4 & ParamSchemaMetadata;

export type TransformationStepIfConditionValue = string | boolean | null;

export type TransformationStep = {
Expand All @@ -112,26 +111,8 @@ export type Prettify<TType> = TType extends any[] | Date
? TType
: { [K in keyof TType]: TType[K] };

export type TypedJsonSchema<T> = ParamSchema<T> & { $tsType: T };
export type InferTypedJsonSchema<T extends TypedJsonSchema<any>> = T["$tsType"];

export type ZodLike<T> =
| {
parse: (input: any) => T;
}
| { _output: T };

export type ParamDefinitionInput = Record<
string,
TypedJsonSchema<any> | ParamSchema<any> | ZodLike<any>
>;

export type ParamsToTypedObject<T extends ParamDefinitionInput> = {
[K in keyof T]: T[K] extends ZodLike<infer U>
? U
: T[K] extends TypedJsonSchema<infer U>
? U
: any;
export type ParamsToTypedObject<T extends Record<string, ParamSchema>> = {
[K in keyof T]: T[K] extends JSONSchema ? FromSchema<T[K]> : any;
};

/**
Expand Down Expand Up @@ -176,15 +157,13 @@ export type TransformationOutput<
export type LooseAutoComplete<T extends string> = T | (string & {});

// Using this instead of `Chain<any,any>` for the types because TypeScript doesn't like it
export type ChainRunnable = {
run: (params: any) => Promise<Record<"output", Record<string, any>>>;
};
export type ChainRunnable = { run: (params: any) => Record<string, any> };
export type InferChainInput<Chain extends ChainRunnable> = Parameters<
Chain["run"]
>[0];
export type InferChainOutput<Chain extends ChainRunnable> = UnwrapVariable<
Awaited<ReturnType<Chain["run"]>>["output"]
>;
export type InferChainOutput<Chain extends ChainRunnable> = Awaited<
ReturnType<Chain["run"]>
>["output"];

export type RunChainOptions<ReturnState extends boolean = boolean> = {
return_state?: ReturnState;
Expand Down

0 comments on commit 6f74bfe

Please sign in to comment.