Master AI & Build your First Coding Portfolio with SkillReactor | Sign Up Now

Quickstart Guide: OpenAI

Get Started with OpenAI

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.

Install Dependency

To use OpenAI's API, first install the necessary package in your environment.

JavaScript

Install the OpenAI package using npm:

npm install openai

Python

Install the OpenAI package using pip:

pip install openai

Import and Configure Client

Set up your API client by importing the OpenAI package and configuring it with your API key and base URL.

JavaScript

import OpenAI from "openai";
const openai = new OpenAI({
	baseURL: {BASE_URL},
    apiKey: {YOUR_KEY}
});

Python

from openai import OpenAI

client = OpenAI(
    base_url={BASE_URL},
    api_key={YOUR_KEY}
)

Using the Chat Completions API

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.

JavaScript

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]);

Python

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)

Using Function Calling Feature

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.

JavaScript

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",
});

Python

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"
)