Open API call, realize all kinds of brain holes through API


On March 2, OpenAI released the API related to ChatGPT, and the call to ChatGPT can be realized through the API.

POST https://api.openai.com/v1/chat/completions

curl https://api.openai.com/v1/chat/completions \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer YOUR_API_KEY' \
  -d '{
  "model": "gpt-3.5-turbo",
  "messages": [{"role": "user", "content": "Hello!"}]
}'

import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")

completion = openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
    {
    
    "role": "user", "content": "Hello!"}
  ]
)

print(completion.choices[0].message)

const {
    
     Configuration, OpenAIApi } = require("openai");

const configuration = new Configuration({
    
    
  apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);

const completion = await openai.createChatCompletion({
    
    
  model: "gpt-3.5-turbo",
  messages: [{
    
    role: "user", content: "Hello world"}],
});
console.log(completion.data.choices[0].message);

Guess you like

Origin blog.csdn.net/wwlsm_zql/article/details/129298757