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

give-me-answers

v1.0.3

Published

give-me-answers is a super simple wrapper around the OpenAI SDK that helps you get AI answers fast. It lets you send prompts and get structured, validated responses without all the hassle. Perfect for creating quick scripts that need OpenAI answers.

Downloads

13

Readme

give-me-answers

This lightweight utility is designed to help you quickly get AI-generated answers from OpenAI with minimal setup. It includes Zod schema validation, making it easy to ensure responses are structured the way you need.

Installation

You can install this package using npm:

npm install give-me-answers

Usage

Importing the Package

import { openAiRequestWithSchema, giveMeAiMessages, craftJsonSchemaPrompt } from 'give-me-answers';

Example Usage

import { z } from 'zod';
import { openAiRequestWithSchema, giveMeAiMessages } from 'give-me-answers';

// Define your schema using Zod
const responseSchema = z.object({
  name: z.string(),
  age: z.number()
});

// Create messages for the AI
const messages = giveMeAiMessages({
  system: "You are a helpful assistant.",
  user: "Tell me a bit about yourself."
});

// Make an OpenAI request and get the validated response
(async () => {
  const response = await openAiRequestWithSchema('gpt-4o', messages, responseSchema);
  console.log('Validated Response:', response);
})();

Available Functions

openAiRequestWithSchema<T>(model: string, messages: OpenAI.Chat.Completions.ChatCompletionMessageParam[], schema: z.ZodType<T>): Promise<T | null>

Sends a request to OpenAI and validates the response against a Zod schema.

  • model: The OpenAI model to use (e.g., 'gpt-4o').
  • messages: An array of system and user messages.
  • schema: A Zod schema for validating the response.

giveMeAiMessages({ system: string, user: string }): OpenAI.Chat.Completions.ChatCompletionMessageParam[]

Generates an array of OpenAI chat messages from a system and user prompt.

  • system: The system message for the assistant.
  • user: The user message or prompt for the assistant.

craftJsonSchemaPrompt({ instructions: string[], fieldDescriptions: string[], exampleJson: string }): string

Crafts a detailed JSON schema prompt from provided instructions, field descriptions, and an example JSON string.

  • instructions: Instructions for the AI.
  • fieldDescriptions: Descriptions of the fields in the expected output.
  • exampleJson: An example JSON string.

Example Use Cases

Get AI Answers with Validated Responses

Use this package to send prompts and get structured responses that match your Zod schema, making sure the returned data is in the format you expect.

Automated Prompt Generation

With craftJsonSchemaPrompt, you can easily generate detailed prompts with field descriptions and example JSON, useful for generating structured outputs from AI models.

Quick Chat Setup

Quickly create chat interactions with OpenAI using giveMeAiMessages, perfect for conversational agents or simple AI interactions.

License

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

Contributing

Feel free to open issues or submit pull requests to improve this package. Contributions are welcome!