Quickstart Guide: OpenAI
This guide provides the essential steps to get started with integrating OpenAI's API using JavaScript or Python. Follow the instructions below to install dependencies, set up your client, and use various features such as Chat Completions and Function Calling.
To use OpenAI's API, first install the necessary package in your environment.
Install the OpenAI package using npm:
npm install openai
Install the OpenAI package using pip:
pip install openai
Set up your API client by importing the OpenAI package and configuring it with your API key and base URL.
import OpenAI from "openai"; const openai = new OpenAI({ baseURL: {BASE_URL}, apiKey: {YOUR_KEY} });
from openai import OpenAI client = OpenAI( base_url={BASE_URL}, api_key={YOUR_KEY} )
The Chat Completions API by OpenAI is a powerful tool designed to generate conversational responses based on the input provided. This API utilizes advanced language models, like GPT-3.5 Turbo, to understand and generate text in a conversational context. Users can customize interactions by specifying roles and content, such as system instructions or user queries, and the model responds intelligently in real-time.
const completion = await openai.chat.completions.create({ messages: [ { role: "system", content: "You are a poetic assistant, skilled in explaining complex programming concepts with creative flair." }, { role: "user", content: "Compose a poem that explains the concept of recursion in programming."} ], model: "gpt-3.5-turbo", }); console.log(completion.choices[0]);
completion = client.chat.completions.create( model="gpt-3.5-turbo", messages=[ {"role": "system", "content": "You are a poetic assistant, skilled in explaining complex programming concepts with creative flair."}, {"role": "user", "content": "Compose a poem that explains the concept of recursion in programming."} ] ) print(completion.choices[0].message)
Function calling feature allows you to define functions within the API call. The model then intelligently decides when to generate JSON objects containing arguments for calling these functions. Note that the Chat Completions API does not execute the function; it only provides the JSON output, which you can use to execute the function in your own application.
const completion = await openai.chat.completions.create({ tools: [ { type: "function", function: { name: "get_city_name", description: "Get the city name from coordinates", parameters: { type: "object", properties: { long: { type: "number", description: "Longitude", }, lat: { type: "number", description: "Latitude", }, }, }, }, }, ], messages: [{ role: "user", content: "Which city is located at 51.509865 longitude and -0.118092 latitude" }], model: "gpt-3.5-turbo", });
completion = client.chat.completions.create( model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Which city is located at 51.509865 longitude and -0.118092 latitude"}], tools=[{ "type": "function", "function": { "name": "get_city_name", "description": "Get the city name from coordinates", "parameters": { "type": "object", "properties": { "long": { "type": "number", "description": "Longitude", }, "lat": { "type": "number", "description": "Latitude", }, }, }, } }], tool_choice="auto" )