openai

Open AI

What is Open AI?

OpenAI is a private research laboratory that aims to develop and direct artificial intelligence (AI) in ways that benefit humanity as a whole. The company was founded by Elon Musk, Sam Altman and others in 2015 and is headquartered in San Francisco.

The OpenAI API can be applied to virtually any task. We offer a range of models with different capabilities and price points, as well as the ability to fine-tune custom models.

Text generation models

OpenAI’s text generation models (often referred to as generative pre-trained transformers or “GPT” models for short), like GPT-4 and GPT-3.5, have been trained to understand natural and formal language. Models like GPT-4 allows text outputs in response to their inputs. The inputs to these models are also referred to as “prompts”. Designing a prompt is essentially how you “program” a model like GPT-4, usually by providing instructions or some examples of how to successfully complete a task. Models like GPT-4 can be used across a great variety of tasks including content or code generation, summarization, conversation, creative writing, and more. Read more in our introductory text generation guide and in our prompt engineering guide.

Assistants

Assistants refer to entities, which in the case of the OpenAI API are powered by large language models like GPT-4, that are capable of performing tasks for users. These assistants operate based on the instructions embedded within the context window of the model. They also usually have access to tools which allows the assistants to perform more complex tasks like running code or retrieving information from a file. Read more about assistants in our Assistants API Overview.

Embeddings

An embedding is a vector representation of a piece of data (e.g. some text) that is meant to preserve aspects of its content and/or its meaning. Chunks of data that are similar in some way will tend to have embeddings that are closer together than unrelated data. OpenAI offers text embedding models that take as input a text string and produce as output an embedding vector. Embeddings are useful for search, clustering, recommendations, anomaly detection, classification, and more. Read more about embeddings in our embeddings guide.

Tokens

Text generation and embeddings models process text in chunks called tokens. Tokens represent commonly occurring sequences of characters. For example, the string ” tokenization” is decomposed as ” token” and “ization”, while a short and common word like ” the” is represented as a single token. Note that in a sentence, the first token of each word typically starts with a space character. Check out our tokenizer tool to test specific strings and see how they are translated into tokens. As a rough rule of thumb, 1 token is approximately 4 characters or 0.75 words for English text.

One limitation to keep in mind is that for a text generation model the prompt and the generated output combined must be no more than the model’s maximum context length. For embeddings models (which do not output tokens), the input must be shorter than the model’s maximum context length. The maximum context lengths for each text generation and embeddings model can be found in the model index.

Quickstart for developer

Go to Open AI and then to  Quick Start

The OpenAI API provides a simple interface for developers to create an intelligence layer in their applications, powered by OpenAI’s state of the art models. The Chat Completions endpoint powers ChatGPT and provides a simple way to take text as input and use a model like GPT-4 to generate an output.

This quickstart is designed to help get your local development environment setup and send your first API request. If you are an experienced developer or want to just dive into using the OpenAI API, the API reference of GPT guide are a great place to start. Throughout this quickstart, you will learn:

  • How to setup your development environment
  • How to install the latest SDKs
  • Some of the basic concepts of the OpenAI API
  • How to send your first API request

Account setup

First, create an OpenAI account or sign in. Next, navigate to the API key page and “Create new secret key”, optionally naming the key. Make sure to save this somewhere safe and do not share it with anyone.

Quickstart language selection

Select the tool or language you want to get started using the OpenAI API with.

I have select Python.

Python is a popular programming language that is commonly used for data applications, web development, and many other programming tasks due to its ease of use. OpenAI provides a custom Python library which makes working with the OpenAI API in Python simple and efficient.

Step 1: Setup Python

Install the OpenAI Python library
Once you have Python 3.7.1 or newer installed and (optionally) a virtual environment setup, the OpenAI Python library can be installed. From the terminal / command line, run:
pip install --upgrade openai

Step 2: Setup your API key

Setup your API key for all projects (recommended)

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

Once you create the .gitignore and .env files using the terminal or an integrated development environment (IDE), copy your secret API key and set it as the OPENAI_API_KEY in your .env file. If you haven’t created a secret key yet, you can do so on the API key page.

The .env file should look like the following:

# Once you add your API key below, make sure to not share it with anyone! The API key should remain private.
OPENAI_API_KEY=abc123
The API key can be imported by running the code below:
from openai import OpenAI

client = OpenAI()
# defaults to getting the key using os.environ.get("OPENAI_API_KEY")
# if you saved the key under a different environment variable name, you can do something like:
# client = OpenAI(
#   api_key=os.environ.get("CUSTOM_ENV_NAME"),
# )

Step 3: Sending your first API request

Making an API request

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

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

from openai import OpenAI
client = OpenAI()

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)

To run the code, enter python openai-test.py into the terminal / command line.

In the context of interacting with the OpenAI API, when the request is successful and no error occurs, the correct response typically depends on the specific API endpoint being utilized and the data you’re retrieving or the action you’re performing.

Here’s an example of what a successful response might look like for text generation using the OpenAI GPT-3 API (this is just a hypothetical example and might not represent the actual response structure):

{
    "id": "generated-text-123",
    "model": "gpt-3.5-turbo",
    "text": "This is an example of generated text based on the prompt provided to the API."
}

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.

Conclusion

In this post we have talked about Open AI  which it provides a simple interface for developers to create an intelligence layer in their applications. The Chat Completions endpoint powers ChatGPT and provides a simple way to take text as input and use a model like GPT-4 to generate an output.

We take a quick start  and setup account,  select a language (python) , installed Python, setup Virtual Environment, setup API key,  and send first API request.

In the next step I am going to show OPENAI as API for your application

This post is part of  AI (Artificial Intelligence) step by step

Back to home page

 

Leave a Reply

Your email address will not be published. Required fields are marked *