2.15.5 • Published 3 months ago

multi-llm-ts v2.15.5

Weekly downloads
-
License
-
Repository
github
Last release
3 months ago

llm-ts

A Typescript library to use LLM providers APIs in a unified way.

Features include:

  • Models list
  • Chat completion
  • Chat streaming
  • Text Attachments
  • Vision model (image attachments)
  • Function calling
  • Usage reporting (tokens count)

Providers supported

ProvideridCompletion& StreamingVisionFunction callingReasoningParametrization1Usage reporting
Anthropicanthropicyesyesyesyesyesyes
Cerebrascerebrasyesnononoyesyes
DeepSeekdeepseekyesnoyesyesyesyes
Googlegoogleyesyesyesnoyesyes
Groqgroqyesyesyesnoyesyes
MistralAImistralaiyesyesyesnoyesyes
Ollamaollamayesyesyesyesyesyes
OpenAIopenaiyesyes2yes2yesyesyes
OpenRouteropenrouteryesyesyesnoyesyes
TogetherAI3openaiyesyes2yes2noyesyes
xAIxaiyesyesyesnoyesyes

See it in action

npm i
API_KEY=your-openai-api-key npm run example

You can run it for another provider:

npm i
API_KEY=your-anthropic_api_key ENGINE=anthropic MODEL=claude-3-haiku-20240307 npm run example

Usage

Installation

npm i multi-llm-ts

Loading models

You can download the list of available models for any provider.

const config = { apiKey: 'YOUR_API_KEY' }
const models = await loadModels('PROVIDER_ID', config)
console.log(models.chat)

Chat completion

const llm = igniteEngine('PROVIDER_ID', { apiKey: 'YOUR_API_KEY' })
const messages = [
  new Message('system', 'You are a helpful assistant'),
  new Message('user', 'What is the capital of France?'),
]
await llm.complete('MODEL_ID', messages)

Chat streaming

const llm = igniteEngine('PROVIDER_ID', { apiKey: 'YOUR_API_KEY' })
const messages = [
  new Message('system', 'You are a helpful assistant'),
  new Message('user', 'What is the capital of France?'),
]
const stream = llm.generate('MODEL_ID', messages)
for await (const chunk of stream) {
  console.log(chunk)
}

Function calling

const llm = igniteEngine('PROVIDER_ID', { apiKey: 'YOUR_API_KEY' })
llm.addPlugin(new MyPlugin())
const messages = [
  new Message('system', 'You are a helpful assistant'),
  new Message('user', 'What is the capital of France?'),
]
const stream = llm.generate('MODEL_ID', messages)
for await (const chunk of stream) {
  // use chunk.type to decide what to do
  // type == 'tool' => tool usage status information
  // type == 'content' => generated text
  console.log(chunk)
}

You can easily implement Image generation using DALL-E with a Plugin class such as:

export default class extends Plugin {

  constructor(config: PluginConfig) {
    super(config)
  }

  isEnabled(): boolean {
    return config?.apiKey != null
  }

  getName(): string {
    return 'dalle_image_generation'
  }

  getDescription(): string {
    return 'Generate an image based on a prompt. Returns the path of the image saved on disk and a description of the image.'
  }

  getPreparationDescription(): string {
    return this.getRunningDescription()
  }
      
  getRunningDescription(): string {
    return 'Painting pixels…'
  }

  getParameters(): PluginParameter[] {

    const parameters: PluginParameter[] = [
      {
        name: 'prompt',
        type: 'string',
        description: 'The description of the image',
        required: true
      }
    ]

    // rest depends on model
    if (store.config.engines.openai.model.image === 'dall-e-2') {

      parameters.push({
        name: 'size',
        type: 'string',
        enum: [ '256x256', '512x512', '1024x1024' ],
        description: 'The size of the image',
        required: false
      })

    } else if (store.config.engines.openai.model.image === 'dall-e-3') {

      parameters.push({
        name: 'quality',
        type: 'string',
        enum: [ 'standard', 'hd' ],
        description: 'The quality of the image',
        required: false
      })

      parameters.push({
        name: 'size',
        type: 'string',
        enum: [ '1024x1024', '1792x1024', '1024x1792' ],
        description: 'The size of the image',
        required: false
      })

      parameters.push({
        name: 'style',
        type: 'string',
        enum: ['vivid', 'natural'],
        description: 'The style of the image',
        required: false
      })

    }

    // done
    return parameters
  
  }

   
  async execute(parameters: any): Promise<any> {

    // init
    const client = new OpenAI({
      apiKey: config.apiKey,
      dangerouslyAllowBrowser: true
    })

    // call
    console.log(`[openai] prompting model ${model}`)
    const response = await client.images.generate({
      model: 'dall-e-2',
      prompt: parameters?.prompt,
      response_format: 'b64_json',
      size: parameters?.size,
      style: parameters?.style,
      quality: parameters?.quality,
      n: parameters?.n || 1,
    })

    // return an object
    return {
      path: fileUrl,
      description: parameters?.prompt
    }

  }  

}

Tests

npm run test

2.11.0

4 months ago

2.11.1

4 months ago

2.2.0

7 months ago

2.0.2

7 months ago

2.4.1

7 months ago

2.4.0

7 months ago

2.6.1

5 months ago

2.4.3

6 months ago

2.6.0

6 months ago

2.4.2

7 months ago

2.8.1

5 months ago

2.6.3

5 months ago

2.4.5

6 months ago

2.8.0

5 months ago

2.6.2

5 months ago

2.4.4

6 months ago

2.0.1

7 months ago

2.0.0

7 months ago

2.15.4

3 months ago

2.15.5

3 months ago

2.15.2

3 months ago

2.15.3

3 months ago

2.15.0

3 months ago

2.13.2

4 months ago

2.15.1

3 months ago

2.13.0

4 months ago

2.13.1

4 months ago

1.3.4

7 months ago

1.3.3

7 months ago

1.3.2

7 months ago

1.3.1

7 months ago

1.3.0

7 months ago

2.10.1

5 months ago

2.3.0

7 months ago

2.12.0

4 months ago

2.10.2

5 months ago

2.1.1

7 months ago

2.5.0

6 months ago

2.1.4

7 months ago

2.10.0

5 months ago

2.3.1

7 months ago

2.1.3

7 months ago

2.7.0

5 months ago

2.1.5

7 months ago

2.9.0

5 months ago

2.7.1

5 months ago

2.4.10

6 months ago

2.0.0-beta.1

7 months ago

2.4.11

6 months ago

2.1.0

7 months ago

2.8.3

5 months ago

2.6.5

5 months ago

2.4.7

6 months ago

2.8.2

5 months ago

2.6.4

5 months ago

2.4.6

6 months ago

2.14.3

3 months ago

2.8.5

5 months ago

2.4.9

6 months ago

2.14.4

3 months ago

2.8.4

5 months ago

2.4.8

6 months ago

2.14.1

4 months ago

2.10.5

4 months ago

2.14.2

4 months ago

2.10.6

4 months ago

2.8.6

5 months ago

2.12.1

4 months ago

2.10.3

5 months ago

2.14.0

4 months ago

2.10.4

4 months ago

1.2.0

8 months ago

1.1.2

8 months ago

1.1.1

8 months ago

1.1.0

8 months ago

1.0.2

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago