npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ttj-client

v0.0.11

Published

the official client library for https://text-to-json.com

Downloads

417

Readme

ttj-client SDK for Node.js

The ttj-client SDK for Node.js lets you use the text-to-json.com API to LLM powered data extraction features for your applications.

For API docs see https://text-to-json.com/docs.

Before you begin

  1. Create an Account
  2. Setup payment

Install the SDK

npm install ttj-client

Initiate the TTJClient class

To use the ttj-client SDK for Node.js, create an instance of TTJClient by passing it your API Key. You can generate an API Key on the Usage page by clicking Create new API Key.

async function infer(){
    const { TTJClient } = await import('ttj-client');
    const ttjClient = new TTJClient(TTJ_API_TOKEN);
    //...
}

Querying the Document API

You can extract the data defined in your schema from any pdf, png, or jpeg file. Just pass the file as a Buffer or string as the first parameter and the mimetype as the second parameter. Define your schema as the third parameter.

async function infer(){
    //...
    const response = await ttjClient.inferDocumentBySchema(
        await fs.promises.readFile('tests/testfiles/invoice.jpg'),
         'image/jpeg', 
         {
            issuer_legal_name: '<string: the legal name of the company that issued the invoice>',
            issuer_street: '<string: the street and number of the issuer address>',
            issuer_zip: '<string: the zip code of the issuer address without the town>',
            issuer_town: '<string: the town of the issuer address without zip code>',
            issuer_tax_number: '<string: the tax number ("UID Nummer") of the issuer>',
            invoice_number: '<string: invoice number>',
            invoice_date: '<date: the date (without time) the invoice was issued>',
            total_gross_sum: '<float: the total gross sum of the invoice including VAT, null iff unsure>',
            total_net_sum: '<float: the total net sum of the invoice excluding VAT, null iff unsure>',
        }, 
        [{
            type: 'raw',
            name: 'openai/gpt-3.5-turbo',
            maxcount: 3
        },
        {
            type: 'padded',
            name: 'openai/gpt-3.5-turbo',
            maxcount: 3
        }], false);
}
console.log(JSON.stringify(response.results));
/* logs
{
    issuer_legal_name: 'some-company',
    issuer_street: 'a street',
    issuer_zip: '1234',
    issuer_town: 'a town'
    ...
}
*/

returnprobabilities

Set returnprobabilities (the fourth parameter) to true to get options and their probability for every value.

async function infer(){
    //...
    const response = await ttjClient.inferDocumentBySchema(
        await fs.promises.readFile('tests/testfiles/invoice.jpg'),
         'image/jpeg', 
         {
            issuer_legal_name: '<string: the legal name of the company that issued the invoice>',
            issuer_street: '<string: the street and number of the issuer address>',
            issuer_zip: '<string: the zip code of the issuer address without the town>',
            issuer_town: '<string: the town of the issuer address without zip code>',
            issuer_tax_number: '<string: the tax number ("UID Nummer") of the issuer>',
            invoice_number: '<string: invoice number>',
            invoice_date: '<date: the date (without time) the invoice was issued>',
            total_gross_sum: '<float: the total gross sum of the invoice including VAT, null iff unsure>',
            total_net_sum: '<float: the total net sum of the invoice excluding VAT, null iff unsure>',
        }, 
        [{
            type: 'raw',
            name: 'openai/gpt-3.5-turbo',
            maxcount: 3
        },
        {
            type: 'padded',
            name: 'openai/gpt-3.5-turbo',
            maxcount: 3
        }], true);// set returnproabilities to true
}
console.log(JSON.stringify(response.results));
/* logs
{
    issuer_legal_name: [{
        value: 'some-company',
        probability: 1
    }],
    issuer_street: [
        {
            value: 'a street',
            probability: 0.66
        },
        {
            value: 'different-value',
            probability: 0.33
        }
    ],
    issuer_zip: [{
        value: '1234',
        probability: 1
    }],
    issuer_town: [{
        value: 'a town',
        probability: 1
    }],
    ...
}
*/

Querying the Text API

You can extract the data defined in your schema from any text. Just pass the text as the first parameter and define your schema as the second parameter.

async function infer(){
    //...
    const response = await ttjClient.inferBySchema(
        'company name: Acme Corp\na street 123\n1234 Town', 
        {
            customer: {
                company_name: 'string',
                address: {
                    street: 'string',
                    zip_code: 'number',
                    city: 'string'
                }
            }
        }, 
        'openai/gpt-3.5-turbo');
    console.log(JSON.stringify(response));
}
/* logs
{
    customer: {
        company_name: 'Acme Corp',
        address: {
            street: 'a street 123',
            zip_code: 1234,
            city: 'Town'
        }
    }
}
*/

using a model defined on the website

You can also use a model defined on the website using inferByUUID.

async function infer(){
    //...
    const response = await ttjClient.inferByUUID('company name: acme corp\na street 123\n1234 Town', YOUR_UUID);
    console.log(JSON.stringify(response));
}

infer();
/* logs
{
    customer: {
        company_name: 'Acme Corp',
        address: {
            street: 'a street 123',
            zip_code: 1234,
            city: 'Town'
        }
    }
}
*/

Streaming Responses

You can also get an async generator that always returns the current state of the extraction by using the inferStreamingBySchema method.

async function infer(){
    //...
    for await (const response of ttjClient.inferStreamingBySchema(
        'company name: Acme Corp\na street 123\n1234 Town', 
        {
            customer: {
                company_name: 'string',
                address: {
                    street: 'string',
                    zip_code: 'number',
                    city: 'string'
                }
            }
        }, 
        'openai/gpt-3.5-turbo')) {
            
        console.log(JSON.stringify(response));
    }
}

infer();
/* logs
{"customer":{}}
{"customer":{"company_name":"Acme Corp"}}
{"customer":{"company_name":"Acme Corp","address":{}}}
{"customer":{"company_name":"Acme Corp","address":{"street":"a street 123"}}}
{"customer":{"company_name":"Acme Corp","address":{"street":"a street 123","zip_code":1234}}}
{"customer":{"company_name":"Acme Corp","address":{"street":"a street 123","zip_code":1234,"city":"Town"}}}
*/

using a model defined on the website

You can also use a model defined on the website using inferStreamingByUUID.

async function infer(){
    //...
    for await (const response of ttjClient.inferStreamingByUUID(
            'company name: Acme Corp\na street 123\n1234 Town',
            YOUR_UUID
        )) {
            
        console.log(JSON.stringify(response));
    }
}

infer();
/* logs
{"customer":{}}
{"customer":{"company_name":"Acme Corp"}}
{"customer":{"company_name":"Acme Corp","address":{}}}
{"customer":{"company_name":"Acme Corp","address":{"street":"a street 123"}}}
{"customer":{"company_name":"Acme Corp","address":{"street":"a street 123","zip_code":1234}}}
{"customer":{"company_name":"Acme Corp","address":{"street":"a street 123","zip_code":1234,"city":"Town"}}}
*/