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

happydata-ai

v1.0.6

Published

A collection of LLM/AI utility functions for happy data studio.

Downloads

106

Readme

HappyData AI Utils

Welcome to the HappyData AI Utils library! This library provides a set of utilities to interact with OpenAI's API, manage prompts, redact sensitive information, and validate JSON schemas using Zod. It is designed to streamline the process of building AI-driven applications with a focus on privacy and data integrity.

Features

  • HappyAI: Interacting with LLMs (OpenAI, and Ollama).
  • Prompt Management: Create and manage different types of prompts with variable extraction and replacement (User, System, Assistant).
  • Redactor: Automatically redact sensitive information such as emails, phone numbers, and more.
  • Schema Management: Enforce JSON outputs with LLMs using Zod schemas.
  • Markdown Utils: Interacting with Markdown content.

Installation

To install the library, use npm or yarn:

npm install happydata-ai
# or
yarn add happydata-ai

Usage

OpenAI Integration

To use the OpenAI integration, you need to initialize the HappyAI class with your OpenAI API key:

import OpenAI from "openai";
import { HappyAI } from "happydata-ai";

const openai = new OpenAI({
    apiKey: process.env.OPENAI_API_KEY
});

const HappyAI = new HappyAI(openai);

Chat with OpenAI

You can send messages to OpenAI using the chat method. This method accepts either a single string or an array of messages:

Using a String

const response = await HappyAI.chat("What is the capital of Indiana?");
console.log(response.content); // Outputs: "Indianapolis"

Using an Array of Messages

import { UserPrompt, SystemPrompt } from "happydata-ai";

const messages = [
    new SystemPrompt("You are a helpful assistant."),
    new UserPrompt("What is the capital of Indiana?")
];

const response = await HappyAI.chat(messages);
console.log(response.content); // Outputs: "Indianapolis"

JSON Output

To enforce a JSON structure in the response, use the json method with a Zod schema. First, create a schema using Zod, and then pass it to the json method. This method accepts either a single string or an array of messages:

With an Array of Messages

import { z } from "zod";
import { SystemPrompt, UserPrompt } from "happydata-ai";

// Define a Zod schema
const schema = z.object({
    city: z.string(),
    state: z.string(),
    yearFounded: z.number()
});

const messages = [
    new SystemPrompt("You are a helpful assistant."),
    new UserPrompt("What is the capital of Indiana?")
];

// Use the schema in the json method
const jsonResponse = await HappyAI.json(messages, { schema });
console.log(jsonResponse); // Outputs: { city: "Indianapolis", state: "Indiana", yearFounded: 1816 }

Redaction

To redact sensitive information from messages, enable the redact option:

const message = "My email is [email protected] and my phone is 123-456-7890.";
const redactedResponse = await HappyAI.chat(message, { redact: true });  // Sensitive information is redacted before sending to LLM
console.log(redactedResponse.content);// Sensitive information is repopulated in the response

Prompt Management

Prompt Documentation

Create and manage different types of prompts:

import { UserPrompt, SystemPrompt, AssistantPrompt } from "happydata-ai";

const userPrompt = new UserPrompt("User message");
const systemPrompt = new SystemPrompt("System message");
const assistantPrompt = new AssistantPrompt("Assistant message");

JSON Schema Validation

Validate JSON data against a Zod schema:

import { HDSchema } from "happydata-ai";

const schema = new HDSchema(z.object({
    name: z.string(),
    age: z.number()
}));

const validationResult = schema.validate({ name: "Alice", age: 30 });
console.log(validationResult.valid); // true

Contributing

Contributions are welcome! Please read the contributing guidelines for more information.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contact

For any questions or feedback, please contact us at [email protected].