# @ai-d/aid

> Aid provides a structured and type-safe way to interact with LLMs.

Latest version **0.1.5** (published 2023-12-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install @ai-d/aid
pnpm add @ai-d/aid
yarn add @ai-d/aid
bun add @ai-d/aid
```

## Health

**Score 40/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high maintenance score.

Warnings: low downloads; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.1.5 |
| Published | 2023-12-01 |
| First published | 2023-11-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 31.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | JacobLinCool |
| Maintainers | jacoblincool |
| Keywords | large-language-model, type-safety, zod, openai, schema-validation, json-schema, machine-learning, data-validation, developer-tools, programming-interface, software-development, natural-language-processing |

## Links

- npm: https://www.npmjs.com/package/@ai-d/aid
- Repository: https://github.com/JacobLinCool/aid
- Homepage: https://jacoblincool.github.io/aid
- Issues: https://github.com/JacobLinCool/aid/issues
- npm.io page: https://npm.io/package/@ai-d/aid

## Dependencies (2)

- [debug](https://npm.io/package/debug.md) ^4.3.4
- [zod-to-json-schema](https://npm.io/package/zod-to-json-schema.md) ^3.22.0

## Alternatives

- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@regle/core](https://npm.io/package/@regle/core.md) — 47.0K weekly downloads
- [typeof-arguments](https://npm.io/package/typeof-arguments.md) — 12.5K weekly downloads
- [@lokalise/projects-engine-contracts](https://npm.io/package/@lokalise/projects-engine-contracts.md) — 978 weekly downloads
- [@osjwnpm/nam-laboriosam-quibusdam](https://npm.io/package/@osjwnpm/nam-laboriosam-quibusdam.md) — 70 weekly downloads

## Recent versions

- 0.1.5 (latest) — 2023-12-01
- 0.1.4 — 2023-12-01
- 0.1.3 — 2023-11-27
- 0.1.2 — 2023-11-27
- 0.1.1 — 2023-11-27
- 0.1.0 — 2023-11-24
- 0.0.2 — 2023-11-24
- 0.0.1 — 2023-11-22
- 0.0.0 — 2023-11-22

## README

# Aid: TypeScript Library for Typed LLM Interactions

A.I. :D

**Aid** is a TypeScript library designed for developers working with Large Language Models (LLMs) such as OpenAI's GPT-4 (including Vision) and GPT-3.5. The library focuses on ensuring **consistent, typed outputs** from LLM queries, enhancing the reliability and usability of LLM responses. Advanced users can leverage few-shot examples for more sophisticated use cases. It provides a structured and type-safe way to interact with LLMs.

## Features

- **Typed Response**: Aid leverages TypeScript and JSON Schema to ensure consistent, reliable outputs from LLMs, adheres to the predefined schema.
- **Task Based**: Easily define custom tasks with specific input and output types, streamlining the process of LLM interactions.
- **Few-Shot Learning Support**: Allows for the provision of few-shot prompt examples to guide the LLM in producing the desired output.
- **Visual Task Support**: Includes support for visual tasks with image inputs, harnessing the power of OpenAI's GPT-4 Vision. [Example](https://github.com/JacobLinCool/aid/blob/main/packages/aid/tests/vision.test.ts)
- **OpenAI Integration**: Integrates with OpenAI's official library to provide a seamless experience.
- **Customizable**: Allows for customization LLM models, just implement the `QueryEngine` function. [Example](https://github.com/JacobLinCool/aid/blob/main/packages/aid/tests/cohere.test.ts)

## Installation

```sh
pnpm install @ai-d/aid
```

## Usage

### Basic Setup

First, import the necessary modules and set up your OpenAI instance:

```ts
import { OpenAI } from "openai";
import { Aid } from "@ai-d/aid";

const openai = new OpenAI({ apiKey: "your-api-key" });
const aid = Aid.from(openai, { model: "gpt-4-1106-preview" });
```

<details>
<summary>Using GPT-4 Vision</summary>

```ts
import { OpenAI } from "openai";
import { Aid, OpenAIQuery } from "@ai-d/aid";

const openai = new OpenAI({ apiKey: "your-api-key" });
const aid = Aid.vision(
    OpenAIQuery(openai, { model: "gpt-4-vision-preview", max_tokens: 2048 }),
);
```

</details>

<details>
<summary>Using Other LLM</summary>

For example, [Cohere](https://cohere.ai/)'s Command.

```ts
import { Aid, CohereQuery } from "@ai-d/aid";

const aid = Aid.chat(
    CohereQuery(COHERE_TOKEN, { model: "command" }),
);
```

> You can implement your own `QueryEngine` function.

</details>

### Creating a Custom Task

Define a custom task with expected output types:

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

const analyze = aid.task(
    "Summarize and extract keywords",
    z.object({
        summary: z.string().max(300),
        keywords: z.array(z.string().max(30)).max(10),
    }),
);
```

<details>
<summary>Visual Task Example</summary>

```ts
const analyze = aid.task(
    "Analyze the person in the image",
    z.object({
        gender: z.enum(["boy", "girl", "other"]),
        age: z.enum(["child", "teen", "adult", "elderly"]),
        emotion: z.enum(["happy", "sad", "angry", "surprised", "neutral"]),
        clothing: z.string().max(100),
        background: z.string().max(100),
    }),
);
```

</details>

### Executing a Task

Execute the task and handle the output:

```ts
const { result } = await analyze("Your input here, e.g. a news article");
console.log(result); // { summary: "...", keywords: ["...", "..."] }
```


<details>
<summary>Visual Task Example</summary>

```ts
const datauri = `data:image/png;base64,${fs.readFileSync("path/to/image.png" "base64")}`;

const { result } = await analyze({ images: [{ url: datauri }] });
console.log(result); // { "gender": "boy", "age": "teen", ... }
```

</details>

### Advanced Usage with Few-Shot Examples

For more complex scenarios, you can use few-shot examples:

```ts
const run_advanced_task = aid.task(
    "Some Advanced Task",
    z.object({
        // Define your output schema here
    }),
    {
        examples: [
            // Provide few-shot examples here
        ],
    }
);
```

## Formulation

Case Parameter -> (join) Task Defination -> (join) Format Constraint -> (perform) Query

`Query` and `Format Constraint` are defined and implemented by the `QueryEngine` and `FormatEngine`.

`Task Defination` is defined by the user with `task` method. Task Goal, Expected Schema, Examples, etc.

`Case Parameter` is defined by the user on each single call. Text, Image, etc.

## Contributing

Contributions are welcome! Please submit pull requests with any bug fixes or feature enhancements.

---
_Source: https://npm.io/package/@ai-d/aid · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
