Logo

dev-resources.site

for different kinds of informations.

How to use OpenAI API with JavaScript

Published at
12/3/2023
Categories
javascript
openai
typescript
gpt4
Author
hikaelis
Categories
4 categories in total
javascript
open
openai
open
typescript
open
gpt4
open
Author
8 person written this
hikaelis
open
How to use OpenAI API with JavaScript

(This article is translated from https://zenn.dev/hikaelis/articles/b93cc6f94a6bd3)
This shows how to access to GPT4/3.5 using openai v4 of JavaScript Library.
The usage was changed from version 4 . I tried it on node18.

1. Install the Library

Install openai v4 via npm or yarn

npm install --save openai
# or
yarn add openai
Enter fullscreen mode Exit fullscreen mode

2. Get API KEY of OpenAI

Get API KEY of OpenAI from OpenAI site. Please be careful it isn't the same as ChatGPT Plus.
https://openai.com/blog/openai-api

3. Coding

The usage was significantly changed from v4 (it became easier)
I learned from this video(is it ok? cuz he showed his API KEY...)
How to use the OpenAI API in NodeJS

The example of coding is below↓

const openai = new OpenAI({
    apiKey: OPENAI_API_KEY // API KEY
})
const message = "message to GPT"
const completion = await openai.chat.completions.create({
                model: "gpt-4", // GPT Model name
                messages: [{ "role": "user", "content": message }],
});
console.log(completion.choices[0].message.content); // GPT answer
Enter fullscreen mode Exit fullscreen mode

You can confirm available GPT models on this Document.
https://platform.openai.com/docs/models

4. Summary

If you just want to listen to GPT in chat format, I think this usage is sufficient.
I am actually using this usage to run the bot. (I will write an article about it later.)
https://github.com/hikaelis/nostr_shinjiro_bot_public

GPT is rapidly making progress, so usage will probably be changed when updates. So please check out openai-node documents.
https://github.com/openai/openai-node

gpt4 Article's
30 articles in total
Favicon
Benchmarking Pixtral 12B: MistralAI's New VLM
Favicon
Responsible AI: Evaluating truthfulness in Azure OpenAI model outputs
Favicon
Building Custom Chatbots with OpenAI's GPT-4: A Practical Guide
Favicon
Why Data Analysts Could Lose Their Jobs: The Impact of ChatGPT-4o
Favicon
GPT-4 Literally Doubles Your Learning Speed ... (According to GPT-4)
Favicon
Optimising Function Calling (GPT4 vs Opus vs Haiku vs Sonnet)
Favicon
Hacking out an AI spider with Node
Favicon
List of prompts for email marketing
Favicon
Evolution Of Generative AI: How Did We Get To GPT-4?
Favicon
How to Build an OpenAI GPT Agent in less than 15 mins
Favicon
Use Chat GPT 4 for Free ✨
Favicon
How to use ChatGPT4 for web development
Favicon
Can Chat GPT4 Be Used For Academic Writing?
Favicon
Screenshots optimization on OpenAI tokens
Favicon
From 12th Final Project to an ATM Management System: Leveraging ChatGPT 4 for PDF Analysis
Favicon
How to use gpt4 for free
Favicon
How to Write Prompts?
Favicon
Uncovering Generative Artificial Intelligence and LLMs: A Brief Introduction
Favicon
Custom GPT @ ChatGPT store
Favicon
How to Use GPT-4 for Free
Favicon
buckle up for GEMINI revolution
Favicon
OpenAI Model Deprecation Guide
Favicon
How To Use GPT-4 To Fix Video Transcription
Favicon
How to Augment GPT-4 with Qdrant to Elevate Its Poetry Composition Capabilities
Favicon
ChatGPT-4 Output Context Size Is Still Only 2048Β Tokens!
Favicon
Google Gemini is really terrible against GPT-4
Favicon
Google Gemini AI 🀯 Full Video - The Only Competitor Against ChatGPT / GPT-4 - #1 On Benchmarks
Favicon
How to use OpenAI API with JavaScript
Favicon
Review your requirements with AI
Favicon
GPT-4, 128K context - it is not big enough

Featured ones: