DEV Community

Aditya Mathur
Aditya Mathur

Posted on • Updated on

Python and OpenAi API: Unleashing the Power of AI with Ease

OpenAI's ChatGPT API is a cutting-edge natural language processing tool that is changing the way we interact with technology. In this blog, let's take a deep dive into technical details of ChatGPT API, exploring its architecture, different models in ChatGPT and using the OpenAI's API with python.

The Architecture of ChatGPT

ChatGPT (Chat Generative Pre-trained Transformer) is based on transformer architecture, a deep learning technique which is highly effective in natural language processing tasks.
Transformer Architecture operates on self-attention mechanism, where each word in a sequence is assigned a weight based on its relationship to other words. The model then uses these weights to make predictions.

Models in Chat GPT

The OpenAI's API provides a family of Models with different capabilities. These models can also be customized as per the use case with fine-tuning.
Models

  1. GPT-3 - A set of models that can understand and generate natural language
  2. Codex - A set of models that can understand and generate code, including translating natural language to code
  3. Content Filter - A fine-tuned model that can detect whether text may be sensitive or unsafe

In this blog, we will be going through GPT-3.

GPT-3 has been sub models depending upon levels of power suitable for different tasks.

1. text-davinci-003

This is the most capable model of GPT-3 which can do all the tasks any other models can do but with higher quality, longer output and better instruction following.
This model is useful for the applications which requires a lot of understanding of the content.
These increased capabilities require more computer resources due to which this model costs more per API call and is not as faster than other models.

2. text-curie-001

This model is extremely powerful, yet fast. While Davinci is stronger when it comes to analyzing complicated texts, Curie is very capable when it comes to Language translation, complex classification, text sentiments, summarization.

3. text-babbage-001

Babbage can perform straightforward tasks like simple classification. This model can be used when it comes to Moderate classification, sematic search classification.

4. text-ada-001

Ada is usually the fastest model and can perform tasks like parsing text, address correction and certain kinds of classification tasks that don’t require too much nuance. Ada’s performance can often be improved by providing more context.

API Reference

Install the official Python bindings with the following command:

pip install openai

The OpenAI's API uses API keys for authentication. You can get the API key from here.

Now let's interact with the API:

import openai
openai.api_key = 'YOUR_API_KEY'
question = input("Ask me anything!!")
completion = openai.Completion.create(engine='text-davinci-003', prompt = question, max_tokens = 240, "temperature": 0)
Enter fullscreen mode Exit fullscreen mode

Understanding the API call:

max_tokens parameters sets the upper bound on how many tokens the API will return.

The settings of the API can be adjusted according to need. One of the most importing settings is temperature.

The model predicts which text is most likely to follow the text preceding it. Temperature is a float value that varies from 0 to 1 that let's you control how confident the model should be while making the predictions. Lowering the temperature means that it will take lesser risk and the output would be more accurate and deterministic. Increasing the temperature would result in more diverse results.

All the models process the text by breaking them down into smaller units called tokens. Depending upon the temperature the model decides the risk that can be taken for the prediction of the next token.

Understanding the Result from API

print(completion)

{
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "logprobs": null,
      "text": "bot\n\nA chatbot is a computer program designed to simulate conversation with human users, especially over the Internet. They can be powered by rules, artificial intelligence or a mixture of both. Chatbots can help with customer service, provide marketing content, suggest products and more."
    }
  ],
  "created": 1675063250,
  "id": "cmpl-6eIl8gmYaewmKw8V10rUpOfzgffgu",
  "model": "text-davinci-003",
  "object": "text_completion",
  "usage": {
    "completion_tokens": 55,
    "prompt_tokens": 3,
    "total_tokens": 58
  }
}
Enter fullscreen mode Exit fullscreen mode

print(completion.choices[0]['text'])

A chatbot is a computer program designed to simulate conversation with human users, especially over the Internet. They can be powered by rules, artificial intelligence or a mixture of both. Chatbots can help with customer service, provide marketing content, suggest products and more.
Enter fullscreen mode Exit fullscreen mode

References:

https://beta.openai.com/docs/introduction/overview

Also, if you enjoyed this content and would like to stay updated on future posts, feel free to connect with me on LinkedIn or X or check out my Github profile. I'll be sharing more tips and tricks on Django and other technologies, so don't miss out!

If you find my content valuable and would like to support me, you can also buy me a coffee. Your support helps me continue creating helpful and insightful content. Thank you!

Top comments (1)

Collapse
 
darkfire5442 profile image
Aditya Singh

Very Helpful.