0.2.20 • Published 4 months ago

jinaai v0.2.20

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
4 months ago

JinaAI JavaScript SDK

The JinaAI JavaScript SDK serves as an efficient conduit for incorporating Jina AI's suite of products — SceneXplain, PromptPerfect, Rationale, BestBanner, and JinaChat — into your JavaScript applications. It provides a potent interface to Jina AI's APIs, letting you craft and optimize prompts with ease, making it an indispensable tool for streamlined application development.

Installing

Package manager

Using npm:

$ npm install jinaai

Using Yarn:

$ yarn add jinaai

You can import the library using the import or require approach:

import JinaAI from 'jinaai';
const JinaAI = require('jinaai');

API secrets

To generate an API secret, you need to authenticate on each respective platform's API tab:

Note: Each secret is product-specific and cannot be interchanged. If you're planning to use multiple products, you'll need to generate a separate secret for each.

Example usage

Import the SDK and instantiate a new client with your authentication secret:

import JinaAI from 'jinaai';

const jinaai = new JinaAI({ secrets: {
    'promptperfect-secret': 'XXXXXX',
    'scenex-secret': 'XXXXXX',
    'rationale-secret': 'XXXXXX',
    'jinachat-secret': 'XXXXXX',
    'bestbanner-secret': 'XXXXXX',
}});

Describe images:

const descriptions = await jinaai.describe(
    'https://picsum.photos/200'
);

Evaluate situations:

const decisions = await jinaai.decide(
    'Going to Paris this summer', 
    { analysis: 'proscons' }
);

Optimize prompts:

const prompts = await jinaai.optimize(
    'Write an Hello World function in Typescript'
);

Generate complex answer:

const output = await jinaai.generate(
    'Give me a recipe for a pizza with pineapple'
);

Create images from text:

const output = await jinaai.imagine(
    'A controversial fusion of sweet pineapple and savory pizza.'
);

Use APIs together:

const situations = [
    'factory-1.png',
    'factory-2.png',
    'factory-3.png',
    'factory-4.png',
].map(i => toBase64(i));

const descriptions = await jinaai.describe(situations);

const prompt1 = [
    'Do any of those situations present a danger?',
    'Reply with [YES] or [NO] and explain why',
    ...descriptions.results.map(desc => 'SITUATION: ' + desc.output),
];

const analysis = await jinaai.generate(
    prompt1.join('\n')
);

const prompt2 = [
    'What should be done first to make those situations safer?',
    'I only want the most urgent situation',
    ...descriptions.results.map(desc => 'SITUATION: ' + desc.output),
];

const recommendation = await jinaai.generate(propmt2.join('\n'));

const swot = await jinaai.decide(
    recommendation.output,
    { analysis: 'swot' }
);

const banners = await jinaai.imagine(
    descriptions!.results.map(d => d.output)
);

Raw Output

You can retrieve the raw output of each APIs by passing raw: true in the options:

const descriptions = await jinaai.describe(
    'https://picsum.photos/200',
    { raw: true }
);

console.log(descriptions.raw)

Custom Base Urls

Custom base Urls can be passed directly in the client's constructor:

const jinaai = new JinaAI({
    baseUrls: {
        promptperfect: 'https://promptperfect-customurl.jina.ai',
        scenex: 'https://scenex-customurl.jina.ai',
        rationale: 'https://rationale-customurl.jina.ai',
        jinachat: 'https://jinachat-customurl.jina.ai',
        bestbanner: 'https://bestbanner-customurl.jina.ai',
    }
});

API Documentation

  • JinaAi.describe
JinaAI.describe(
    input: string | string[],
    options?: SceneXOptions
): Promise<SceneXOutput>
type SceneXAlgorithm = 'Aqua' | 'Bolt' | 'Comet' | 'Dune' | 'Ember' | 'Flash' | 'Glide' | 'Hearth' | 'Inception' | 'Jelly';
type SceneXFeatures = Array<'high_quality' | 'question_answer' | 'tts' | 'opt-out' | 'json'>;
type SceneXOptions = {
    algorithm?: SceneXAlgorithm,
    features?: SceneXFeatures,
    languages?: Array<Languages>,
    question?: string,
    style?: 'default' | 'concise' | 'prompt',
    output_length?: number | null,
    json_schema?: Object,
    callback_url?: string,
    reportProgress?: (videoIndex: number, progress: string)=> void,
};
type SceneXOutput = {
    results: Array<{
        output: string,
        i18n?: {
            [key: string]: string | SceneXStoryOutput | SceneXSVideoOutput
        },
        tts?: {
            [key: string]: string
        },
        ssml?: {
            [key: string]: string
        }
    }>
};
// used when algorithm is set to 'Hearth' 
type SceneXStoryOutput = Array<{
    isNarrator: boolean,
    message: string,
    name: string
}>; 
// used when algorithm is set to 'Inception' 
type SceneXSVideoOutput = {
    summary: string,
    events: Array<{
        description: string,
        timestamp: string
    }>
};
  • JinaAi.optimize
JinaAI.optimize(
    input: string | string[],
    options?: PromptPerfectOptions
): Promise<PromptPerfectOutput> 
type PromptPerfectOptions = {
    targetModel?: 'chatgpt' | 'gpt-4' | 'stablelm-tuned-alpha-7b' |
    'claude' | 'cogenerate' | 'text-davinci-003' | 'dalle' | 'sd' |
    'midjourney' | 'kandinsky' | 'lexica',
    features?: Array<
        'preview' | 'no_spam' | 'shorten' | 'bypass_ethics' |
        'same_language' | 'always_en' | 'high_quality' |
        'redo_original_image' | 'variable_subs' | 'template_run'
    >,
    iterations?: number,
    previewSettings?: {
        'temperature': number,
        'topP': number,
        'topK': number,
        'frequencyPenalty': number,
        'presencePenalty': number
    },
    previewVariables?: {
        [key: string]: string
    }
    timeout?: number,
    target_language?: Languages
};
type PromptPerfectOutput = {
    results: Array<{
        output: string,
    }>
};
  • JinaAI.decide
JinaAi.decide(
    input: string | string[],
    options?: RationaleOptions
): Promise<RationaleOutput>
type RationaleOptions = {
    analysis?: 'proscons' | 'swot' | 'multichoice' | 'outcomes',
    style?: 'concise' | 'professional' | 'humor' | 'sarcastic' | 'childish' | 'genZ',
    profileId?: string
};
export type RationaleOutput = {
    results: Array<{
        proscons?: RationaleProsConsOutput,
        swot?: RationaleSWOTOutput,
        multichoice?: RationaleMultichoiceOutput,
        outcomes?: RationaleOutcomesOutput
    }>
};
type RationaleProsConsOutput = {
    pros: {
        [key: string]: string
    },
    cons: {
        [key: string]: string
    },
    bestChoice: string,
    conclusion: string,
    confidenceScore: number
};
type RationaleSWOTOutput = {
    strengths: {
        [key: string]: string
    },
    weaknesses: {
        [key: string]: string
    },
    opportunities: {
        [key: string]: string
    },
    threats: {
        [key: string]: string
    },
    bestChoice: string,
    conclusion: string,
    confidenceScore: number
};
type RationaleMultichoiceOutput = {
    [key: string]: string
};
type RationaleOutcomesOutput = Array<{
    children: RationaleOutcomesOutput,
    labal: string,
    sentiment: string
}>;
  • JinaAI.generate
JinaAi.generate(
    input: string | string[],
    options?: JinaChatOptions
): Promise<JinaChatOutput>
type JinaChatOptions = {
    role?: 'user' | 'assistant'
    name?: string,
    chatId?: string,
    stream?: boolean,
    temperature?: number,
    top_p?: number,
    stop?: string | Array<string>,
    max_tokens?: number,
    presence_penalty?: number,
    frequency_penalty?: number,
    logit_bias?: { [key: string]: number },
    image?: string
};
type JinaChatOutput = {
    output: string,
    chatId: string
};
  • JinaAi.imagine
JinaAI.imagine(
    input: string | string[],
    options?: BestBannerOptions
): Promise<BestBannerOutput>
type BestBannerOptions = {
    style?: 'default' | 'photographic' | 'minimalist' | 'flat',
};
type BestBannerOutput = {
    results: Array<{
        output: Array<string>,
    }>
};
  • JinaAi.utils
JinaAI.utils.imageToBase64(filePath: string): string
JinaAI.utils.isUrl(str: string): boolean
JinaAI.utils.isBase64(str: string): boolean
0.2.20

4 months ago

0.2.19

6 months ago

0.2.18

7 months ago

0.2.17

8 months ago

0.2.16

9 months ago

0.2.15

9 months ago

0.2.14

9 months ago

0.2.13

9 months ago

0.2.12

9 months ago

0.2.11

9 months ago

0.2.10

9 months ago

0.2.9

9 months ago

0.2.8

9 months ago

0.2.7

9 months ago

0.2.6

10 months ago

0.2.5

10 months ago

0.2.4

10 months ago

0.2.3

10 months ago

0.2.2

11 months ago

0.2.1

11 months ago

0.2.0

11 months ago

0.1.13

11 months ago

0.1.12

11 months ago

0.1.11

11 months ago

0.1.10

11 months ago

0.1.9

11 months ago

0.1.8

11 months ago

0.1.7

11 months ago

0.1.6

11 months ago

0.1.5

11 months ago

0.1.4

11 months ago

0.1.3

11 months ago

0.1.2

11 months ago

0.1.1

11 months ago

0.1.0

12 months ago