Skip to content

Commit

Permalink
Merge branch 'main' into main-class-update
Browse files Browse the repository at this point in the history
  • Loading branch information
roodboi committed Jan 2, 2024
2 parents aac1b97 + 9e81b2a commit 4405a88
Showing 1 changed file with 24 additions and 147 deletions.
171 changes: 24 additions & 147 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# instructor-js

This is a WIP of the [instructor](https://github.com/jxnl/instructor) project implemented in JavaScript.
I am not a JavaScript developer, so this is a learning project for me, if you're interested in contributing seriously to this project, please contact me on [Twitter](https://twitter.com/jxnlco)

This is a WIP of the port of the [Instructor Python Library](https://github.com/jxnl/instructor) by [@jxnlco](https://twitter.com/jxnlco).
This library brings structured prompting to LLMs. Instead of receiving text as output, Instructor will coax the LLM to output valid JSON that maps directly to the provided Ecto schema.
If the LLM fails to do so, or provides values that do not pass your validations, it will provide you utilities to automatically retry with the LLM to correct errors.
The simple goal of this project is to provide a simple, type-safe, and easy to use interface for the OpenAI API.

```ts
Expand Down Expand Up @@ -47,150 +47,27 @@ const user: User = response.model;

## Roadmap

- [ ] Patching with Tools and Functions
- [ ] Patching with JSON MODE
- [ ] Throwing Validation errors with ZOD
- [ ] Retrying Validations

## Prompting Ideas for Zod

To adapt these Python examples for TypeScript using Zod, you'll need to translate the Python class definitions and Pydantic models into Zod schemas. Zod is a TypeScript-first schema declaration and validation library that allows you to build complex schemas with great ease and efficiency. Here's how you can convert the given examples:

### Modular Chain of Thought

Translate the `Role` and `UserDetail` models to Zod schemas. Implement the chain of thought as a string field.

```typescript
import { z } from "zod";

const RoleSchema = z.object({
chain_of_thought: z
.string()
.nonempty("Think step by step to determine the correct title"),
title: z.string(),
});

const UserDetailSchema = z.object({
age: z.number(),
name: z.string(),
role: RoleSchema,
});
```

### Utilize Optional Attributes

For optional attributes, use Zod's `.optional()` method.

```typescript
const UserDetailSchema = z.object({
age: z.number(),
name: z.string(),
role: z.string().optional(),
});
```

### Handling Errors Within Function Calls

Implement a wrapper class for handling errors using Zod's `.union()` method.

```typescript
const MaybeUserSchema = z.union([
UserDetailSchema,
z.object({
error: z.boolean().default(false),
message: z.string().optional(),
}),
]);
```

### Tips for Enumerations

Use Zod's enum construct for standardized fields.

```typescript
const RoleEnum = z.enum(["PRINCIPAL", "TEACHER", "STUDENT", "OTHER"]);

const UserDetailSchema = z.object({
age: z.number(),
name: z.string(),
role: RoleEnum,
});
```

### Reiterate Long Instructions

Repeat complex instructions in the schema's descriptions.

```typescript
const RoleSchema = z.object({
instructions: z
.string()
.nonempty(
"Restate the instructions and rules to correctly determine the title."
),
title: z.string(),
});

const UserDetailSchema = z.object({
age: z.number(),
name: z.string(),
role: RoleSchema,
});
```

### Handle Arbitrary Properties

For arbitrary properties, use a list of key-value pairs.

```typescript
const PropertySchema = z.object({
key: z.string(),
value: z.string(),
});

const UserDetailSchema = z.object({
age: z.number(),
name: z.string(),
properties: z
.array(PropertySchema)
.max(5, "Extract any other properties that might be relevant."),
});
```

### Defining Relationships Between Entities

Explicitly define relationships in the schema.

```typescript
const UserDetailSchema = z.object({
id: z.number(),
age: z.number(),
name: z.string(),
friends: z.array(z.number()),
});

const UserRelationshipsSchema = z.object({
users: z.array(UserDetailSchema),
});
```

### Reusing Components with Different Contexts

Reuse components for different contexts.

```typescript
const TimeRangeSchema = z.object({
start_time: z.number(),
end_time: z.number(),
});

const UserDetailSchema = z.object({
id: z.number(),
age: z.number(),
name: z.string(),
work_time: TimeRangeSchema,
leisure_time: TimeRangeSchema,
});
```
## TODO

- [ ] Add `llm_validator`
- [ ] Logging for Distillation / Finetuning
- [ ] Support Streaming
- [ ] Optional/Maybe types
- [ ] Add Tutorials, include in docs
- [ ] Text Classification
- [ ] Self Critique
- [ ] Image Extracting Tables
- [ ] Moderation
- [ ] Citations
- [ ] Knowledge Graph
- [ ] Entity Resolution
- [ ] Search Queries
- [ ] Query Decomposition
- [ ] Recursive Schemas
- [ ] Table Extraction
- [ ] Action Item and Dependency Mapping
- [ ] Multi-File Code Generation
- [ ] PII Data Sanitization

These translations provide a structured approach to creating TypeScript schemas with Zod, mirroring the functionality and intent of the original Python examples.

0 comments on commit 4405a88

Please sign in to comment.