Top Related Projects
The React Framework
The library for web and native user interfaces.
Node.js JavaScript runtime ✨🐢🚀✨
Fast, unopinionated, minimalist web framework for node.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
Promise based HTTP client for the browser and node.js
Quick Overview
The openai/openai-quickstart-node repository is a starter project for using the OpenAI API with Node.js. It provides a simple example of how to integrate OpenAI's powerful language models into a web application, demonstrating basic API usage and server-side integration.
Pros
- Easy to set up and understand for beginners
- Provides a working example of OpenAI API integration
- Includes a basic web interface for interaction
- Well-documented with clear instructions
Cons
- Limited in scope, only demonstrates basic functionality
- May require additional security measures for production use
- Lacks advanced error handling and input validation
- Not optimized for large-scale applications
Code Examples
- Setting up the OpenAI API client:
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
- Making a request to the OpenAI API:
const completion = await openai.createCompletion({
model: "text-davinci-002",
prompt: generatePrompt(req.body.animal),
temperature: 0.6,
});
res.status(200).json({ result: completion.data.choices[0].text });
- Generating a prompt based on user input:
function generatePrompt(animal) {
const capitalizedAnimal = animal[0].toUpperCase() + animal.slice(1).toLowerCase();
return `Suggest three names for an animal that is a superhero.
Animal: Cat
Names: Captain Whiskers, Agent Pawsome, The Incredible Feline
Animal: Dog
Names: Bark Knight, Wonder Woof, Super Sniffer
Animal: ${capitalizedAnimal}
Names:`;
}
Getting Started
-
Clone the repository:
git clone https://github.com/openai/openai-quickstart-node.git
-
Install dependencies:
cd openai-quickstart-node npm install
-
Set up your OpenAI API key:
cp .env.example .env
Then edit the
.env
file and add your OpenAI API key. -
Run the application:
npm run dev
-
Open
http://localhost:3000
in your browser to use the application.
Competitor Comparisons
The React Framework
Pros of Next.js
- More comprehensive framework for building full-stack React applications
- Extensive ecosystem with built-in features like routing, API routes, and server-side rendering
- Large community and extensive documentation
Cons of Next.js
- Steeper learning curve due to its more complex architecture
- May be overkill for simple projects or quick prototypes
- Requires more setup and configuration compared to a basic Node.js application
Code Comparison
Next.js (pages/api/example.js):
export default function handler(req, res) {
res.status(200).json({ message: 'Hello from Next.js!' })
}
openai-quickstart-node (index.js):
app.get('/api/example', (req, res) => {
res.json({ message: 'Hello from Express!' })
})
The Next.js example demonstrates its built-in API routes, while the openai-quickstart-node example uses Express for routing. Next.js provides a more streamlined approach for creating API endpoints within the same project structure.
While Next.js offers a more robust framework for building full-stack applications, openai-quickstart-node is better suited for quickly prototyping OpenAI-specific functionality without the overhead of a larger framework.
The library for web and native user interfaces.
Pros of React
- Extensive ecosystem with a large community and numerous third-party libraries
- Powerful and flexible component-based architecture for building complex UIs
- Well-documented and supported by Facebook, with regular updates and improvements
Cons of React
- Steeper learning curve, especially for developers new to component-based architectures
- Requires additional tools and libraries for a complete application setup
- Larger bundle size compared to lightweight alternatives
Code Comparison
React:
import React from 'react';
function App() {
return <h1>Hello, World!</h1>;
}
export default App;
openai-quickstart-node:
import { Configuration, OpenAIApi } from "openai";
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
Summary
React is a comprehensive library for building user interfaces, offering a rich ecosystem and powerful features. It's well-suited for large-scale applications but may be overkill for simpler projects. openai-quickstart-node, on the other hand, is a focused starter project for integrating OpenAI's API into Node.js applications. It's more lightweight and specific to AI-related tasks, making it easier to get started with OpenAI integration but less versatile for general web development compared to React.
Node.js JavaScript runtime ✨🐢🚀✨
Pros of node
- Comprehensive and mature JavaScript runtime environment
- Extensive ecosystem with a vast library of modules
- Actively maintained by a large community and major tech companies
Cons of node
- Steeper learning curve for beginners
- More complex setup and configuration required
- Larger codebase and resource footprint
Code Comparison
node:
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello, World!');
});
server.listen(3000, () => {
console.log('Server running on port 3000');
});
openai-quickstart-node:
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const completion = await openai.createCompletion({
model: "text-davinci-002",
prompt: "Hello world",
});
The node example demonstrates creating a basic HTTP server, while openai-quickstart-node focuses on integrating with the OpenAI API. node provides lower-level control and flexibility, whereas openai-quickstart-node offers a simpler, more focused approach for working with OpenAI's services.
Fast, unopinionated, minimalist web framework for node.
Pros of Express
- Mature, widely-used web application framework with extensive documentation and community support
- Highly flexible and customizable, allowing developers to structure applications as needed
- Offers a robust set of features for building full-stack web applications
Cons of Express
- Steeper learning curve for beginners compared to the simpler OpenAI quickstart
- Requires more setup and configuration for basic functionality
- May be overkill for small projects or simple API integrations
Code Comparison
Express:
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello World!');
});
OpenAI Quickstart:
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
Express is a full-featured web framework, while the OpenAI Quickstart is focused on integrating OpenAI's API. Express provides a more comprehensive solution for building web applications, while the OpenAI Quickstart offers a simpler entry point for working with AI-powered features. The choice between the two depends on the project's scope and requirements.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
Pros of TypeScript
- Larger, more established project with extensive documentation and community support
- Provides static typing and advanced language features for JavaScript development
- Widely adopted in the industry, used by many large-scale applications
Cons of TypeScript
- Steeper learning curve compared to the simpler setup of openai-quickstart-node
- Requires compilation step, which can add complexity to the development process
- May be overkill for small projects or quick prototypes
Code Comparison
TypeScript example:
interface User {
name: string;
age: number;
}
function greet(user: User): string {
return `Hello, ${user.name}! You are ${user.age} years old.`;
}
openai-quickstart-node example:
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
TypeScript offers static typing and interfaces, providing better code organization and error detection. openai-quickstart-node focuses on simplicity and quick setup for OpenAI API integration. While TypeScript is more suitable for large-scale projects, openai-quickstart-node is ideal for rapid prototyping and smaller applications utilizing OpenAI's services.
Promise based HTTP client for the browser and node.js
Pros of axios
- More comprehensive HTTP client with support for various request methods and configurations
- Widely adopted in the JavaScript community with extensive documentation and community support
- Can be used for general-purpose HTTP requests, not limited to OpenAI API interactions
Cons of axios
- Requires more setup and configuration for specific use cases like OpenAI API integration
- May introduce unnecessary complexity for simple API requests
- Larger package size compared to the lightweight OpenAI quickstart
Code Comparison
openai-quickstart-node:
import { Configuration, OpenAIApi } from "openai";
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
axios:
import axios from 'axios';
const openai = axios.create({
baseURL: 'https://api.openai.com/v1',
headers: { 'Authorization': `Bearer ${process.env.OPENAI_API_KEY}` }
});
The openai-quickstart-node provides a more straightforward setup specifically for OpenAI API interactions, while axios offers a more flexible configuration for general HTTP requests. The axios approach requires additional setup to tailor it for OpenAI API use.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
OpenAI API Quickstart - Node.js example app
This is an example chat app intended to get you started with your first OpenAI API project. It uses the Chat Completions API to create a simple general purpose chat app with streaming.
Basic request
To send your first API request with the OpenAI Node SDK, make sure you have the right dependencies installed and then run the following code:
import OpenAI from "openai";
const openai = new OpenAI();
async function main() {
const completion = await openai.chat.completions.create({
messages: [{ role: "system", content: "You are a helpful assistant." }],
model: "gpt-3.5-turbo",
});
console.log(completion.choices[0]);
}
main();
This quickstart app builds on top of the example code above, with streaming and a UI to visualize messages.
Setup
-
If you donât have Node.js installed, install it from nodejs.org (Node.js version >= 16.0.0 required)
-
Clone this repository
-
Navigate into the project directory
$ cd openai-quickstart-node
-
Install the requirements
$ npm install
-
Make a copy of the example environment variables file
On Linux systems:
$ cp .env.example .env
On Windows:
$ copy .env.example .env
-
Add your API key to the newly created
.env
file -
Run the app
$ npm run dev
You should now be able to access the app at http://localhost:3000! For the full context behind this example app, check out the tutorial.
Top Related Projects
The React Framework
The library for web and native user interfaces.
Node.js JavaScript runtime ✨🐢🚀✨
Fast, unopinionated, minimalist web framework for node.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
Promise based HTTP client for the browser and node.js
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot