New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@arrangedev/jsonformer-ts

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@arrangedev/jsonformer-ts

Structured JSON outputs from LLMs

latest
npmnpm
Version
1.1.0
Version published
Maintainers
1
Created
Source

jsonformer-ts

A Typescript port of Jsonformer.

Overview

This is a port of the Jsonformer library originally written in Python. This repo aims to replicate it as close as possible, allowing Typescript developers to take advantage of high-quality structured JSON outputs from language models.

Like the original, the following schema types are supported:

  • number
  • string
  • boolean
  • array
  • object

Installation

NPM:

npm i @arrangedev/jsonformer-ts

yarn:

yarn add @arrangedev/jsonformer-ts

Example

Here's a basic example of jsonformer-ts in action:

async function main() {
  console.log("Loading model and tokenizer...");
  const model = await LlamaForCausalLM.from_pretrained(
    "Xenova/TinyLlama-1.1B-Chat-v1.0",
    { model_file_name: "model" },
  );
  const tokenizer = await LlamaTokenizer.from_pretrained(
    "Xenova/TinyLlama-1.1B-Chat-v1.0",
  );

  const schema = {
    type: "object",
    properties: {
      name: { type: "string" },
      age: { type: "number" },
      is_student: { type: "boolean" },
      courses: {
        type: "array",
        items: { type: "string" },
      },
    },
  };

  const prompt =
    "Generate a person's information based on the following schema:";

  console.log("Creating Jsonformer instance...");
  const jsonformer = new Jsonformer(model, tokenizer, schema, prompt, {
    debug: true,
  });

  console.log("Generating data...");
  const result = await jsonformer.generate();

  console.log("\nGenerated result:");
  console.log(JSON.stringify(result, null, 2));
}

Run the example with:

yarn example:basic

License

This project is MIT licensed, like the original. See here for more information.

FAQs

Package last updated on 30 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts