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

conversation-engine

v0.0.4

Published

A powerful wrapper around the OpenAI API, providing additional features and making it easier to interact with AI models. Seamlessly chat with your AI assistant, include context strings, and manage conversation history.

Downloads

2

Readme

Conversation Engine

A powerful wrapper around the OpenAI API, providing additional features and making it easier to interact with conversational models. Seamlessly chat with your AI assistant, include context strings, and manage conversation history.

Give it a message and it will handle the rest.

Features

  • Automatically handle chat history and context injection
  • Built-in support for summarizing long conversations
  • Customizable system instructions for controlling AI behavior
  • Works with OpenAI API, making it easy to integrate with existing projects
  • Built with TypeScript, providing type safety and autocompletion

Installation

Install conversation-engine with npm

npm install conversation-engine

Usage

Minimal Example

import { configureChat, chat, ModelName } from 'conversation-engine';

// Configure the chat
configureChat({
	apiKey: 'your-openai-api-key',
	modelSelection: ModelName.GPT_3_5_TURBO,
});

// Send a message
async function yourChatBot() {
	const userMessage = { role: 'user', content: 'Tell me a joke!' };
	const response = await chat(userMessage);

	/*
	// The response object
	{
	    role: 'assistant',
	    content: "Why couldn't the bicycle stand up by itself? Because it was two tired!"
	}
	*/

	console.log(response.content);
}

yourChatBot();

Advanced Example

import { configureChat, chat, ModelName } from 'conversation-engine';

// Configure the chat with more options
configureChat({
	apiKey: 'your-openai-api-key',
	modelSelection: ModelName.GPT_4,
	historyLength: 6,
	historySummarizationModel: ModelName.GPT_3_5_TURBO,
	openaiOptions: {
		temperature: 0.8,
		maxTokens: 500,
		presencePenalty: 0,
		frequencyPenalty: 0,
	},
});

async function yourAdvancedChatBot() {
	const userMessage = { role: 'user', content: 'What are two benefits of exercise?' };
	const context = [
		'Exercise can reduce your risk of heart diseases',
		'Exercise can improve your mental health and mood. ',
	];
	const systemMessageContent = 'You are an AI health expert.';

	const response = await chat(userMessage, context, systemMessageContent);
	console.log(response.content);
}

yourAdvancedChatBot();

Note You should use dotenv to provide your apiKey.

API Reference

Message

A message object containing a role and content.

| Property | Type | Description | | :-------- | :------- | :---------------------------------------------------------------- | | role | string | The role of the message sender (system, user, or assistant) | | content | string | The content of the message |

ModelName

Enum for AI model names.

| Value | Description | | :-------------- | :------------------ | | GPT_4 | GPT-4 Model | | GPT_4_32K | GPT-4-32K Model | | GPT_3_5_TURBO | GPT-3.5-Turbo Model |

ChatConfiguration

A chat configuration object containing various settings.

| Property | Type | Description | | :-------------------------- | :---------- | :------------------------------------------------------------- | | apiKey | string | The API key for accessing the AI service | | modelSelection | ModelName | The AI model to use for chat | | historyLength | number | The number of recent messages to include without summarization | | historySummarizationModel | ModelName | The AI model to use for history summarization | | messageHistory | Message[] | The array of message objects representing the chat history |

OpenAIOptions

Optional settings for the OpenAI API. Check out their documentation.

| Property | Type | Description | | :----------------- | :---------------------------- | :--------------------------------------------------------------------- | | temperature | number | The temperature for generating responses | | topP | number | The top_p value for nucleus sampling | | n | number | The number of responses to generate | | stream (broken) | boolean | Whether to use streaming mode. (Streaming does not work currently) | | stop | string \| string[] | A string or array of strings to stop the response generation | | maxTokens | number | The maximum number of tokens for the generated response | | presencePenalty | number | The presence penalty for the generated response | | frequencyPenalty | number | The frequency penalty for the generated response | | logitBias | { [token: string]: number } | An object of token strings and their biases | | user | string | A user identifier to associate with this chat completion |

FAQ

Q: Which AI Models Can I Use with This Module?

A: You can use any of the models available in the OpenAI API, such as GPT-4, GPT-4-32K, and GPT-3.5-Turbo. You can refer to the ModelName enum for a complete list of supported models.

Q: How Do I Provide Context for the Conversation?

A: You can provide context by passing an array of context strings to the chat() function. This will include the context messages in the conversation, making it easier for the AI to understand the context.

Q: How Does Message History Summarization Work?

A: The message history summarization feature helps to condense long conversations into a shorter, summarized form. This allows the AI to focus on the most relevant information and provide better responses. The summarization is done using the AI model specified in the ChatConfiguration object.

Future Features

  • Working streaming responses
  • Import past history
  • Add tests