Tag: Open AI

Embark on an AI API journey with OpenAI: A detailed Guide for Beginners

Are you ready to unleash your creativity and build amazing applications with the power of AI?

Artificial Intelligence based APIs are a way for developers to access the functionality of pre-trained AI models without the need for extensive knowledge of machine learning algorithms. They help break the barrier of entry into creating AI applications and facilitate the adoption of AI development for non-AI engineers. These APIs can be integrated into a variety of applications and are typically categorized based on their functionality. They enable us to create applications with smarter artificial intelligence features such as natural language processing, image recognition, data analysis, image creation and many other use cases. As of this writing, there are several APIs available in the market each offering a set of capabilities and models. In this blog, we will discuss OpenAI API, one of the top AI API offerings available as of today.

One of the hardest thing to do in technology is disrupt yourself

Matt Mullenwegg

What is OpenAI API? 

OpenAI API is a service that lets you access the most advanced and known artificial intelligence models in the world through programming interfaces. OpenAI is creator of the now very popular ChatGPT. They have a strong community and documentation around their APIs. It is fairly sophesticated with SDKs available for various technology platforms, which can be leveraged for individual use or for Enterprise applications. This makes the OpenAI API an excellent candidate for learning and leveraging, to create new applications or enhance existing applications.

These are some models which OpenAPI currently supports:

MODELSDESCRIPTION
GPT-4A set of models that improve on GPT-3.5 and can understand as well as generate natural language or code
GPT-3.5A set of models that improve on GPT-3 and can understand as well as generate natural language or code
DALL·EA model that can generate and edit images given a natural language prompt
WhisperA model that can convert audio into text
EmbeddingsA set of models that can convert text into a numerical form
ModerationA fine-tuned model that can detect whether text may be sensitive or unsafe
GPT-3LegacyA set of models that can understand and generate natural language
DeprecatedA full list of models that have been deprecated
Courtesy of OpenAI API documentation website.

You can further fine-tune the models with your own data to suit your specific needs and preferences. You can combine various models to create interesting outputs. The OpenAI API is easy to use and integrate with your existing workflows.

So now that we have established the value OpenAI API let’s understand how to get started with them.

How to register on the OpenAI API website? 

To use OpenAI API, follow these steps:

  1. Visit openai.com and sign up for an account.
  2. Provide your name, email address, and create a password.
  3. Agree to the terms of service and privacy policy.
  4. Check your email for a confirmation link.
  5. Click on the link to activate your account.
  6. Once activated, you will be redirected to the OpenAI dashboard.

How to generate a key for the API? 

  1. Go to the OpenAI dashboard.
  2. Click on “API keys” in the navigation menu.
  3. Locate the “Create an API key” button and click on it.
  4. A pop-up window will appear with your new API key.
  5. Copy the API key, which will look something like this: sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.
  6. Keep the API key safe and secure, as it grants access to your OpenAI API.
  7. You can now use this API key to authenticate your requests to the OpenAI API.

Remember, it’s important to never share your API key with anyone to maintain the security of your API access.

How does OpenAI charge for the usage of API? 

OpenAI API is not free, but it has a generous free tier that lets you experiment and learn without paying anything. The free tier gives you 5,000 tokens per month. A token is a unit of measurement that represents the amount of data processed by OpenAI API.

 If you exceed the free tier limit, you will be charged $0.06 per 1,000 tokens. You can monitor your usage and billing on the OpenAI dashboard. You can also set up alerts and limits to avoid unexpected charges. 

Let’s understand tokens a little bit more. A character is a single symbol or letter in a text, such as “a” or “.” A token is a group of characters that form a meaningful unit, such as “cat” or “hello”. Tokens can vary in length depending on the context and the model. For example, the word “cat” might be one token for some models, but two tokens for others if they split it into “c” and “at”. 

Created using DALL-E.

“A helpful rule of thumb is that one token generally corresponds to ~4 characters of text for common English text. This translates to roughly ¾ of a word (so 100 tokens ~= 75 words).”

A token can be as short as one character or as long as one word. For example, “ChatGPT is great!” is considered as five tokens: [“ChatGPT”, ” “, “is”, ” “, “great!”].

Remember, both input and output tokens count towards the total. For example, if you send 10 tokens as a prompt and receive 20 tokens as a response, you’ll be billed for 30 tokens.

The cost per token varies depending on your usage tier. You can find detailed pricing information on the OpenAI Pricing page.

Some interesting Use cases of Open AI API

OpenAI API provides a wide range of AI capabilities that can be used in various use cases. Some of the common use cases for OpenAI API include:

  1. Text generation: OpenAI API can generate human-like text, making it useful for applications such as chatbots, content creation, and language translation.
  2. Text analysis: OpenAI API can analyze text to extract information, classify text, and perform sentiment analysis. This can be useful for applications such as social media monitoring, customer feedback analysis, and market research.
  3. Image recognition: OpenAI API can recognize and classify images, making it useful for applications such as visual search, image tagging, and object detection.
  4. Speech recognition: OpenAI API can transcribe speech to text, making it useful for applications such as voice assistants, voice search, and voice commands.
  5. Data analysis: OpenAI API can analyze data to extract insights, make predictions, and perform anomaly detection. This can be useful for applications such as predictive maintenance, fraud detection, and customer segmentation.

Possibilities are endless based on the context and creativity, how you can leverage these to enhance existing solutions or create new solutions.

Start with a simple program

Once all the above steps have been taken, you are ready to start programming with them.

Open a code editor like Visual Studio Code and write the Open API calling application in your programming language of choice.

Here is a sample Python program which calls an OpenAI API. As you can see it’s very simple and straight forward. All Open AI APIs are centered around completion APIs. Please note you can use any programming language to call these API, however Python is widely used by OpenAI themselves to show examples and various blogs on the web. This makes it a very convenient language to try various models, templates and settings easily. Visual Studio Code is a popular IDE which is free and supports various languages including Python. You can easily install Python extension in Visual Studio Code and with few steps start programming in it.

If you haven’t already started learning Python you may want to start doing since it is one of the most popular programming languages being used today. Here is a survey result from StackOverflow conducted in 2022.

Courtesy Stack Overflow : https://survey.stackoverflow.co/2022#most-popular-technologies-language

Python will also likelihood remain a dominant player in the coming years due to widespread use in AI, ML, Data Science etc. We will not go into too much detail into how to set up Python locally, however here is a sample program how to make a call to Open AI completion API with Python- as you can see it’s very simple and straightforward.

import openai

openai.api_key = "your open api key here"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"

prompt = "Hello"
prompt += "\nTranslate this into 1.French, 2.Spanish and 3.Hindi:"
prompt += "\n1."

print(prompt)

response = openai.Completion.create(
    engine="davinci",
    prompt=prompt,
    temperature=0.1,
    max_tokens=100,
    top_p=1,
    frequency_penalty=0,
    presence_penalty=0
)
print(response)  # Print the server response
print(response['choices'][0]['text'])

Here is an output of the program above:

Why is prompt engineering critical in using OpenAI API? 

Prompt engineering is a skill that every developer should learn and master. Why? Because it can help you create amazing applications that leverage the power of natural language processing and artificial intelligence. It is the art and science of designing and optimizing prompts, which are inputs that guide the behavior of a natural language model. A prompt can be a question, a command, a statement, or anything else that elicits a response from the model. You are already using prompts if you are using ChatGPT or other similar AI tools.

By learning prompt engineering, you can unlock the full potential of natural language models customize the outputs of the models to suit your needs and preferences, such as tone, style, format, length, and content. Prompt engineering is not only useful, but also fun and creative. You can experiment with different prompts and see how the models react. You can also challenge yourself to create prompts that produce the best results for your specific goals and scenarios. 

Prompt engineering is the future of natural language processing and artificial intelligence. It’s a crucial skill for developers working with AI language models because the quality of the output heavily depends on the input prompt.

With Prompt Engineering you can get:

  1. Better Results: A well-crafted prompt can guide the model to generate more accurate and relevant responses.
  2. Efficiency: It can help reduce the number of tokens used, thereby saving costs.
  3. Control: It allows developers to have more control over the model’s behavior and output.

Prompt engineering involves understanding the model’s behavior, experimenting with different prompts, and iterating based on the results. It’s a blend of art and science, and mastering it can greatly enhance the effectiveness of your AI applications.

Use the Presets / Templates to learn

The OpenAI API Playground is a user-friendly interface that allows developers to interact with OpenAI’s powerful models. One of the key features of the Playground is the use of Presets or Templates. These are pre-configured settings that help users get started with the API without needing to manually set all the parameters. They also give you some basic ideas on how to constructing your prompts for desired results.

Presets or Templates are designed to provide a starting point for common tasks. They are set up with optimal parameters for specific use-cases, such as drafting an email, writing Python code, or translating languages. By selecting a preset, the Playground automatically fills in the necessary parameters, such as the prompt, temperature, and max tokens.

Here’s how you can use Presets/Templates:

  1. Go to OpenAI API platform’s playground page.
  2. Select a Preset: When you open the Playground, you’ll see a dropdown menu labeled ‘Presets’. Click on it to see a list of available templates.
  3. Choose Your Task: The list includes a variety of tasks. Select the one that best matches your needs. The Playground will automatically populate the parameters based on your selection.
  4. Customize If Needed: Although the preset provides a good starting point, you can still customize the parameters to better suit your specific needs. For instance, you can modify the prompt, adjust the temperature to make the output more or less random, or change the max tokens to control the length of the output.
  5. Run the Model: Once you’re satisfied with the settings, click ‘Run Model’ to see the results. You can experiment with different settings and run the model multiple times to get the best output.

So, Presets in the OpenAI API Playground are a great way to get started with the API. They simplify the process of setting up the model, allowing you to focus on getting the results you need. Whether you’re a seasoned developer or a beginner, these presets can help you make the most of the powerful capabilities of the OpenAI API.

Here is a Preset example of chat conversation to generate SQL :

The playground also gives you the Python code you can use for the Preset prompts and play with it in code:

So I think this is enough motivation and information to get started with OpenAI APIs. While this writing was more to generate interest in OpenAI APIs, there are various other APIs which offer individual capabilities and use cases. The best APIs which suit your scenario the most should be selected for production applications. Here are some popular platforms which provide AI APIs today:

  • Google Cloud AI Platform: This platform offers a suite of services specifically targeted at building, deploying, and managing machine learning models in the cloud. official website.
  • Amazon Rekognition: This is an image recognition software that uses machine learning to automate and lower the cost of image recognition and video analysis. official website.
  • IBM Watson: This is a question-answering computer system capable of answering questions posed in natural language. It was developed in IBM’s DeepQA project by a research team led by principal investigator David Ferrucci. official website.
  • Microsoft Azure Cognitive Services: This is a comprehensive suite of out-of-the-box and customizable AI tools, APIs, and models that help modernize your business processes faster. official website.
  • Clarifai: This is an artificial intelligence company that specializes in computer vision and uses machine learning and deep neural networks to identify and analyze images and videos. The company offers its solution via API, mobile SDK, and on-premise solutions. official website.
  • TensorFlow: This is an open-source software library for machine learning and artificial intelligence. It can be used across a range of tasks but has a particular focus on training and inference of deep neural networks. official website.

I hope this will motivate you to jump into AI programming with AI APIs.

Happy Programming!