如何使用openAI

342 阅读2分钟

一、准备工作 要有vpn,且是openai允许的服务器节点

二、注册api账号

1 www.storytrain.info/ 网站购买一个国外实体号码,一定要实体

2 创建一个OpenAI帐户或登录。

3 接下来,导航到API密钥页面并“创建新密钥”,可选地命名密钥。

三、

Step 1: Setting up Node

Install Node.js

To use the OpenAI Node.js library, you will need to ensure you have Node.js installed.

To download Node.js, head to the official Node website and download the LTS version. If you are installing Node.js for the first time, you can follow the official Node.js usage guide to get started.

Install the OpenAI Node.js library

Once you have Node.js installed, the OpenAI Node.js library can be installed. From the terminal / command line, run:

1
2
3
npm install --save openai
# or
yarn add openai

Step 2: Set up your API key

Set up your API key for all projects (recommended)

The main advantage to making your API key accessible for all projects is that our SDK will automatically detect it and use it without having to write any code.

MacOS

  1. Open Terminal: You can find it in the Applications folder or search for it using Spotlight (Command + Space).

  2. Edit bash profile: Use the command nano ~/.bash_profile or nano ~/.zshrc (for newer MacOS versions) to open the profile file in a text editor.

  3. Add Environment Variable: In the editor, add the line below, replacing your-api-key-here with your actual API key:

    export OPENAI_API_KEY='your-api-key-here'
    
  4. Save and exit: Press Ctrl+O to write the changes, followed by Ctrl+X to close the editor.

  5. Load your profile: Use the command source ~/.bash_profile or source ~/.zshrc to load the updated profile.

  6. Verification: Verify the setup by typing echo $OPENAI_API_KEY in the terminal. It should display your API key.

Windows

Step 3: Sending your first API request

Making an API request

After you have Node.js configured and set up an API key, the final step is to send a request to the OpenAI API using the Node.js library. To do this, create a file named openai-test.js using the terminal or an IDE.

Inside the file, copy and paste one of the examples below:

ChatCompletions

Select libraryChatCompletionsEmbeddingsImages

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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();

To run the code, enter node openai-test.js into the terminal / command line.

The Chat Completions example highlights just one area of strength for our models: creative ability. Explaining recursion (the programming topic) in a well formatted poem is something both the best developers and best poets would struggle with. In this case, gpt-3.5-turbo does it effortlessly.