Skip to content

navedx/openai-conversation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OpenAI Conversations API - Node.js Application

A Node.js application demonstrating how to use OpenAI's Conversations API (/v1/conversations) to create and manage conversations.

Features

  • Create conversations with initial messages
  • Add messages to conversations
  • Update conversation metadata (e.g., title)
  • Delete conversations
  • Retrieve conversation history
    • List conversation items and fetch specific items

Prerequisites

  • Node.js (v18 or higher) - Required for native fetch API support
  • npm or yarn
  • OpenAI API key (Get one here)

Installation

  1. Clone or navigate to this repository
  2. Install dependencies:
npm install
  1. Create a .env file in the root directory:
cp .env.example .env
  1. Add your OpenAI API key to the .env file:
OPENAI_API_KEY=sk-your-api-key-here

Usage

Backend (CLI example):

npm start

Run the API server (for the React app):

npm run server

Run the React client (in another terminal):

npm run client

Or run both together (single package.json):

npm run dev:all

How It Works

The application demonstrates a complete conversation flow:

  1. Creates a Conversation: Creates a new conversation with optional initial messages and title
  2. Adds Messages: Sends user messages to the conversation
  3. Retrieves Conversation: Gets the full conversation details
  4. Lists Conversations: Shows all available conversations
  5. Retrieves Messages: Gets the conversation history

API Functions

The application includes the following functions:

  • createConversation(messages, title) - Creates a new conversation
  • getConversation(conversationId) - Gets a specific conversation
  • updateConversation(conversationId, updates) - Updates a conversation
  • deleteConversation(conversationId) - Deletes a conversation
  • addMessageToConversation(conversationId, role, content) - Adds a message to a conversation
  • getConversationMessages(conversationId) - Gets all messages from a conversation
  • listConversationItems(conversationId, options) - Lists items within a conversation
  • getConversationItem(conversationId, itemId) - Retrieves a specific item from a conversation

API Endpoint

This application uses the OpenAI Conversations API endpoint:

  • Base URL: https://api.openai.com/v1
  • Conversations Endpoint: POST /conversations, GET /conversations, etc.

The React app calls the local Express server routes:

  • POST /api/conversations
  • POST /api/conversations/:id/messages
  • GET /api/conversations/:id/items

Customization

You can modify the exampleConversation() function in index.js to:

  • Change the system message and instructions
  • Modify the user message
  • Add more messages to the conversation
  • Update conversation metadata

License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published