@lmnr-ai/lmnr
TypeScript icon, indicating that this package has built-in type declarations

0.5.2 • Public • Published

Laminar Typescript

JavaScript/TypeScript SDK for Laminar.

Laminar is an open-source platform for engineering LLM products. Trace, evaluate, annotate, and analyze LLM data. Bring LLM applications to production with confidence.

Check our open-source repo and don't forget to star it ⭐

NPM Version NPM Downloads

Quickstart

npm install @lmnr-ai/lmnr

And then in the code

# Only if you are using Next.js
export NEXT_OTEL_FETCH_DISABLED=1

import { Laminar as L } from '@lmnr-ai/lmnr'

L.initialize({ projectApiKey: '<PROJECT_API_KEY>' })

This will automatically instrument most of the LLM, Vector DB, and related calls with OpenTelemetry-compatible instrumentation.

Read docs to learn more.

Autoinstrumentations are provided by OpenLLMetry, open-source package by TraceLoop.

Instrumentation

In addition to automatic instrumentation, we provide a simple @observe() decorator. This can be useful if you want to trace a request handler or a function which combines multiple LLM calls.

Example

import { OpenAI } from 'openai';
import { Laminar as L, observe } from '@lmnr-ai/lmnr';

L.initialize({ projectApiKey: "<LMNR_PROJECT_API_KEY>" });

const client = new OpenAI({ apiKey: '<OPENAI_API_KEY>' });

const poemWriter = async (topic = "turbulence") => {
  const prompt = `write a poem about ${topic}`;
  const response = await client.chat.completions.create({
    model: "gpt-4o",
    messages: [
      { role: "system", content: "You are a helpful assistant." },
      { role: "user", content: prompt }
    ]
  });

  const poem = response.choices[0].message.content;
  return poem;
}

// Observe the function like this
await observe({name: 'poemWriter'}, async () => await poemWriter('laminar flow'))

Sending events

You can send laminar events using L.event(name, value).

Read our docs to learn more about events and examples.

Example

import { Laminar as L } from '@lmnr-ai/lmnr';
// ...
const poem = response.choices[0].message.content;

// this will register True or False value with Laminar
L.event('topic alignment', poem.includes(topic))

Evaluations

Quickstart

Install the package:

npm install @lmnr-ai/lmnr

Create a file named my-first-eval.ts with the following code:

import { evaluate } from '@lmnr-ai/lmnr';

const writePoem = ({topic}: {topic: string}) => {
    return `This is a good poem about ${topic}`
}

evaluate({
    data: [
        { data: { topic: 'flowers' }, target: { poem: 'This is a good poem about flowers' } },
        { data: { topic: 'cars' }, target: { poem: 'I like cars' } },
    ],
    executor: (data) => writePoem(data),
    evaluators: {
        containsPoem: (output, target) => target.poem.includes(output) ? 1 : 0
    },
    groupId: 'my_first_feature'
})

Run the following commands:

export LMNR_PROJECT_API_KEY=<LMNR_PROJECT_API_KEY>  # get from Laminar project settings
npx lmnr eval my-first-eval.ts

Visit the URL printed in the console to see the results.

Overview

Bring rigor to the development of your LLM applications with evaluations.

You can run evaluations locally by providing executor (part of the logic used in your application) and evaluators (numeric scoring functions) to evaluate function.

evaluate takes in the following parameters:

  • data – an array of Datapoint objects, where each Datapoint has two keys: target and data, each containing a key-value object.
  • executor – the logic you want to evaluate. This function must take data as the first argument, and produce any output.
  • evaluators – Object which maps evaluator names to evaluators. Each evaluator is a function that takes output of executor as the first argument, target as the second argument and produces numeric scores. Each function can produce either a single number or Record<string, number> of scores.
  • name – optional name for the evaluation. Automatically generated if not provided.
  • groupName – optional group name for evaluation. Evaluations within the same group can be compared visually side by side.
  • config – optional additional override parameters.

* If you already have the outputs of executors you want to evaluate, you can specify the executor as an identity function, that takes in data and returns only needed value(s) from it.

Read docs to learn more about evaluations.

Client for HTTP operations

Various interactions with Laminar API are available in LaminarClient

Agent

To run Laminar agent, you can invoke client.agent.run

import { LaminarClient } from '@lmnr-ai/lmnr';

const client = new LaminarClient({
  projectApiKey:"<YOUR_PROJECT_API_KEY>",
});

const response = await client.agent.run({
    prompt: "What is the weather in London today?",
});

// Be careful, `response` itself contains the state which may get large
console.log(response.result.content)

Streaming

Agent run supports streaming as well.

import { LaminarClient } from '@lmnr-ai/lmnr';

const client = new LaminarClient({
  projectApiKey:"<YOUR_PROJECT_API_KEY>",
});

const response = await client.agent.run({
    prompt: "What is the weather in London today?",
});

for await (const chunk of response) {
  console.log(chunk.chunkType)
  if (chunk.chunkType === 'step') {
    console.log(chunk.summary);
  } else if (chunk.chunkType === 'finalOutput') {
    // Be careful, `chunk.content` contains the state which may get large
    console.log(chunk.content.result);
  }
}

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
0.5.296latest
0.4.18-alpha.01alpha

Version History

VersionDownloads (Last 7 Days)Published
0.5.296
0.5.1173
0.5.02
0.4.42222
0.4.410
0.4.404
0.4.3949
0.4.3843
0.4.370
0.4.360
0.4.350
0.4.340
0.4.330
0.4.321
0.4.310
0.4.300
0.4.281
0.4.2711
0.4.260
0.4.250
0.4.2414
0.4.231
0.4.220
0.4.210
0.4.201
0.4.191
0.4.180
0.4.18-alpha.10
0.4.18-alpha.01
0.4.170
0.4.160
0.4.150
0.4.15-alpha.00
0.4.141
0.4.130
0.4.121
0.4.110
0.4.11-alpha.60
0.4.11-alpha.51
0.4.11-alpha.40
0.4.11-alpha.30
0.4.11-alpha.20
0.4.11-alpha.10
0.4.11-alpha0
0.4.100
0.4.10-alpha.50
0.4.10-alpha.40
0.4.10-alpha.30
0.4.10-alpha.20
0.4.10-alpha.10
0.4.10-alpha0
0.4.91
0.4.9-alpha0
0.4.80
0.4.8-alpha.00
0.4.71
0.4.7-alpha.10
0.4.7-alpha0
0.4.60
0.4.50
0.4.5-alpha.10
0.4.5-alpha0
0.4.40
0.4.31
0.4.20
0.4.10
0.4.00
0.3.33
0.3.20
0.3.11
0.3.00
0.2.62
0.2.50
0.2.41
0.2.30
0.2.20
0.2.10
0.2.00
0.1.00

Package Sidebar

Install

npm i @lmnr-ai/lmnr

Weekly Downloads

633

Version

0.5.2

License

Apache-2.0

Unpacked Size

955 kB

Total Files

18

Last publish

Collaborators

  • founders-lmnr.ai
  • dinmukhamedm