Repo: https://github.com/daveaneo/openai-conversation-manager
This script allows you to interact with OpenAI's API to generate responses based on predefined or custom prompts. It supports three modes of operation: JSON Config Mode, File Input Mode, and Direct Input Mode. You can also simulate recurring conversations by including a history of previous exchanges within each request.
Some of the benefits are automatic context management and reusable
This script sends prompts to the OpenAI API using various input modes, allowing for flexible, customizable interactions. It’s designed to:
npm install openai-conversation-manager
git clone https://github.com/daveaneo/openai-conversation-manager.git
.env
file (for security)..env
File ExampleOPENAI_API_KEY=your_openai_api_key_here CONFIG_PATH=path_to_config_file
The Conversation Manager is a comprehensive tool for interacting with the OpenAI API in a way that maintains context across conversations, handles logging, and manages system prompts. It enables developers to create, maintain, and retrieve conversations for users with ease.
To start a conversation, initialize the ConversationManager
class. If you're not saving any user data, you can instantiate it without parameters.
// Create a new conversation without saving user data const conversation = new ConversationManager(); // Create a conversation for a specific user const conversation = new ConversationManager('user123');
This loads the user data, including past conversations, if any.
By default, the ConversationManager
uses predefined system settings. There are three modes for changing this setup:
// Setting the system with direct input conversation.setSystem('direct', { agentPrompt: 'You are a helpful assistant.', model: 'gpt-3.5-turbo', temperature: 0.7, conversationMaxTokens: 1000, responseTokens: 150, }); // Setting the system from a file conversation.setSystem('file', { agentFilePath: 'path/to/agentFile.txt' }); // Setting the system using config mode conversation.setSystem('config', { modelId: 'empathy_coach' });
Use the addMessage
method to add a user message to the conversation.
conversation.addMessage('Can you provide me with some tips for productivity?');
The callAPI
method sends the current conversation history to OpenAI and appends the assistant's response.
const response = await conversation.callAPI(); console.log('Assistant response:', response);
The saveHistory
method saves the current conversation, while loadLatestConversation
loads the most recent conversation for a user.
conversation.saveHistory(); // Save the current conversation conversation.loadLatestConversation(); // Load the last conversation for the user
You can use the getHistory
method to retrieve the current conversation history, with an option to exclude system messages.
const history = conversation.getHistory(true); // Retrieve history without system messages console.log(history);
setActiveConversation
Usage: Loads a specific conversation by its unique conversationId
for a user, setting it as the active conversation.
Purpose: Enables resuming or continuing a specific previous conversation, maintaining context and continuity.
Example:
conversation.setActiveConversation("2"); // Loads conversation with ID "2" for the user
startNewConversation()
: Begins a new conversation for the user.deleteHistory()
: Deletes the current conversation history but retains system messages.import { ConversationManager } from 'path/to/ConversationManager.js'; (async () => { const conversation = new ConversationManager('user123'); conversation.setSystem('config', { modelId: 'empathy_coach' }); conversation.addMessage('How do I improve my communication skills?'); const response = await conversation.callAPI(); console.log('Response:', response); conversation.saveHistory(); })();
The ConversationManager
includes built-in error handling. Ensure that file paths are valid and the modelId
matches an entry in your configuration.
Clone the Repository:
git clone your-repo-url cd your-repo-name
Install Dependencies:
npm install dotenv node-fetch
Create .env
File:
.env
file as shown in Prerequisites. You may copy the template, env.example to .env to do so.Create config.json
:
The config.json
file contains predefined agent configurations with unique IDs, default settings, and file paths for agent prompts.
{ "defaults": { "model": "gpt-4o-mini", "conversationMaxTokens": 3000, "responseTokens": 200, "temperature": 0.7, "logPath": "logs" }, "models": { "poet": { "agent_file": "agents/poet.txt", "conversationMaxTokens": 300, "responseTokens": 75, "temperature": 0.6, "model": "gpt-4o-mini" }, "general_question_confirmer": { "agent_file": "agents/general_question_confirmer.txt", "conversationMaxTokens": 3000, "responseTokens": 200, "temperature": 0.6, "model": "gpt-4o-mini" } } }
Each models entry in the configuration should include:
id
: A unique identifier for the agent configuration.agent_file
: Path to the file containing the agent’s prompt.max_tokens
: Default maximum token count for responses.temperature
: Default randomness control for responses.model
: OpenAI model name.The ConversationManager
stores user data in a structured JSON format for easy retrieval and persistence. Each user has a dedicated file named username.json
that includes metadata and all their conversation histories. Below is the structure of the user data:
Each user's data is stored in a single JSON file named after their userId
, with the following structure:
{ "userId": "username", "totalConversations": 3, "conversations": [ { "conversationId": "1", "name": "How can I stay focused?", "timestamp": "2024-11-03T10:00:00Z", "messages": [ { "role": "system", "content": "Welcome to the session." }, { "role": "user", "content": "How can I stay focused?" }, { "role": "assistant", "content": "Set clear goals, eliminate distractions, and take breaks." } ] }, { "conversationId": "2", "name": "Productivity tips", "timestamp": "2024-11-04T14:30:00Z", "messages": [ { "role": "system", "content": "Productivity coaching session started." }, { "role": "user", "content": "Give me a quick productivity tip." }, { "role": "assistant", "content": "Try the two-minute rule for small tasks." } ] }, { "conversationId": "3", "name": "Empathy session", "timestamp": "2024-11-05T09:45:00Z", "messages": [ { "role": "system", "content": "Empathy coaching session initiated." }, { "role": "user", "content": "I need some empathy. Can you help me understand my feelings?" }, { "role": "assistant", "content": "Of course, I'm here for you. What are you feeling at the moment?" } ] } ] }
userId
: The unique identifier for the user.totalConversations
: The total number of conversations stored for the user.conversations
: An array of conversations, each containing:
conversationId
: The unique ID of the conversation.name
: A brief title derived from the first user message or system prompt.timestamp
: The timestamp of when the conversation was created.messages
: An array of message objects, each containing:
role
: The sender's role (system
, user
, assistant
).content
: The content of the message.This project is licensed under the MIT License.
There are no models linked
There are no datasets linked
There are no models linked
There are no datasets linked