![]() |
OpenAI is the leading company in the field of AI. With the public release of software like ChatGPT, DALL-E, GPT-3, and Whisper, the company has taken the entire AI industry by storm. Everyone has incorporated ChatGPT to do their work more efficiently and those who failed to do so have lost their jobs. The age of AI has started and people not adapting to AI could introduce some difficulties for them. In this article, we will be discussing how you can leverage the power of AI and make your day-to-day tasks a lot easier by using the OpenAI APIs (Application Programming Interface) that allow developers to easily access their AI models and Integrate them into their own applications using Python. Table of ContentWhat is OpenAI?OpenAI is a Leading Company in the field of Artificial Intelligence(AI). It was originally founded in 2015 by Sam Altman and Elon Musk as a Non-profit Organization. They primarily focus on AI-based Software products Such as ChatGPT 3, ChatGPT 4 and DALL-E etc. They develop next-generation AI products holding incredible capabilities, for example, OpenAIs GPT-3 which is a Content filtering model that allows you to implement advanced text classification, outline, question-answering, and other chatbot applications. To learn more about APIs, you can refer to this article. What is OpenAI API?OpenAI API is a powerful cloud-based platform, hosted on Microsoft’s Azure, designed to provide developers with seamless access to state-of-the-art, pre-trained artificial intelligence models. This API empowers developers to effortlessly integrate cutting-edge AI capabilities into their applications, regardless of the programming language they choose to work with. By leveraging the OpenAI Python API, developers can unlock advanced AI functionalities and enhance the intelligence and performance of their software solutions. Generate OpenAI API keyFor you to use OpenAI’s models in your Python environment, you must first generate an API key. You can follow the below steps to generate the API key:
Installation of OpenAI packageStep 1: Now open a text editor of your choosing or an online notebook like Google Colab or Jupyter Notebook. Here, we’re using a Google Colab notebook to run the command indicated below in order to install the Open AI library in Python. !pip install -q openai Step 2: Now import the OpenAI library in your Python environment and add your API key to the environment by executing the following lines of code in your text editor. Python3
Now you are all set to use OpenAI in your python environment. You can follow the below tutorials to try out OpenAI’s latest models. Prompt EngineeringGiving the AI brain a unique set of instructions to increase its intelligence and responsiveness is what AI prompt engineering entails. To comprehend what we want from AI models like ChatGPT or GPT-4, they need to be gently nudged in the right direction. Prompt engineering can help with it. The finest answers from the AI may be ensured by carefully structuring the prompts. Now, prompt engineering doesn’t only happen once. The process of adjusting and experimenting is continuing. When we ask the AI a question, we experiment with varied wording and the addition of unique rules. We seem to be concocting a miraculous concoction of instructions! Let’s take a look at some rules to construct good prompts to generate accurate results for AI. Some Rules of Prompt EngineeringRule 1: Use latest AI models for your tasks It is important to use latest AI models, because AI models do not tend to give accurate results when they are not updated with recent data. For example, ChatGPT was trained on data dated till September 2021 so it won’t be able to provide you with information after September 2021. Whereas, GPT-4 (or ChatGPT plus) is trained on latest data and is able to generate the latest information. Rule 2: Start your prompt with Instructions, and use “”” or ### to separate your input from your instruction. For example, Translate the below text to Hindi. Text: """ I work at Geeks for Geeks """ Rule 3: Be as precise, illustrative, and thorough as you can when describing the context, result, length, format, style, and other desired characteristics. For example, Write a short inspiring and funny poem about Geeks for Geeks, focusing on their recent launch of Full-stack development course in the style of Sandeep Jain. Rule 4: Try including examples to illustrate the desired output format. For example, Create a list of datasets that can be used to train a logistic regression model. Ensure that the datasets are available in CSV format. The objective is to use this dataset to learn about Supervised Learning. Also, provide links to the dataset if possible. Desired Output format: Dataset name | URL | dataset description | | Rule 5: Start with zero-shot learning, then few-shot learning. If neither of them worked, then fine-tune. Zero-shot: The ability to complete a task without being trained for it. Few-shot: The ability to complete a task after being trained with a limited number of examples. Fine-tune: By fine-tuning GPT-3 using training data, you can increase its performance on particular tasks. When the model is being fine-tuned, it reads text tokens from the training set and predicts which token will come next. The model’s internal weights are updated when it predicts incorrectly to increase the likelihood that it will forecast correctly the following time. The model will eventually learn to generate the pattern of tokens shown in your training data after receiving sufficient training. For example, Zero-Shot: Find the odd one out from the following: Text: """Fish, Octopus, Crab, Lion""" Answer: Few-Shot: Find the odd one out from the following: Text: """Fish, Octopus, Crab, Lion""" Answer: Lion Text: """Ostrich, Cheetah, Turtle""" Answer: Turtle Text: """square, rectangle, rhombus, circle""" Answer: Fine-Tune: You can see the best practices for fine-tuning here. Rule 6: Don’t add imprecise descriptions to your prompt For example, Write a paragraph of 3 to 5 lines explaining apple. Rule 7: Instead of mentioning “what not to do”, try including “what to do” to your prompt For example, Write a conversation between a father and a son, the conversation should be about future goals and refrain from involving any personal information. Instead of involving personal information, try directing the conversation towards college. Rule 8: Specifically for code generation: Use “leading words” to guide the model towards a certain pattern. For example, # Write a simple python function that # 1. Ask me for an integer number # 2. Generate a fibonacci series till the input number using top-down approach import The “import” instruction tells the model in the code snippet below to begin writing in Python. (Likewise, “SELECT” is a good starting point for a SQL statement.) You can use these rules to design some prompts and execute it on ChatGPT. You can see how different the results are from your regular prompts. TextFor performing any text-specific tasks you can define the following function and execute it with your desired prompts. Python3
Here we generated text for the user prompt using the Completions module from the OpenAI library. These are the crucial variables related to the Completions module:
Example prompts for Text GenerationNow let’s try some prompts for Text Generation using Completions module(Chat GPT with Python). Prompt 1: Python3
Output: ['Once upon a time, there lived a Rabbit and a Turtle. Both wanted to be the fastest, so they decided to have a race. The Turtle, despite being slow, never gave up and came first, surprising everyone, leaving the Rabbit behind.', 'Once upon a time, a rabbit and a turtle competed against each other for a goal. Both wanted to cross the same track in the shortest amount of time possible. By the end, the turtle had proved that no matter how fast or slow one may seem, true greatness is in the persistence to never give up.', 'Once upon a time, a Rabbit and Turtle were racing to the finish line for an important goal. While the Rabbit was fast, the Turtle was determined and never gave up. In the end, the Turtle proved that slow and steady wins the race.'] Prompt 2: Python3
Output: ['Client: "Oh taste and see that this wine I purchased is foul!"\nBusinessman: "Verily, mine eyes see naught of thine discontent." \nClient: "Man, what I\'m sayin\' is this is some trash wine!" \nBusinessman: "Careful thy words, the wisdom in silence is stout."'] Example prompts for Text CompletionNow let’s try some prompts for Text Completion using Completions module. Prompt 1: Python3
Output: ['Well, what about a cup? A lot of our customers use cups to drink water and it can be a great alternative to a water bottle!'] Prompt 2: Python3
Output: ['Midas, blinded by his greed, wished for anything he touched to turn to gold. \nThe angel granted his wish and Midas went to try it out on his daughter. As soon as he touched her, she immediately turned to gold. He was filled with horror and shame, realizing that his greed had robbed him of his daughter. \nHe pleaded for the angel to reverse the spell, and she reluctantly agreed. Midas learned his lesson well; never let greed overtake judgment and reason.'] Example prompts for TranslationNow let’s try some prompts for Translation using Completions module. Prompt 1: Python3
Output: ['Japanese:\nジークス・フォー・ジークスのデータ構造コースが最高です。\n\nHindi:\nजीक्स फॉर जीक्स डेटा स्ट्रक्चर्स कोर्स सबसे अच्छा है।'] Prompt 2: Python3
Output: ['German:\nHallo, ich bin Geeks for Geeks Chatbot.\n\nFrench:\nSalut, je suis le bot de chat Geeks for Geeks.'] Example prompts for SummarizationNow let’s try some prompts for Summarization using Completions module. Prompt 1: Python3
Output: ["The well-known story of the hare and the tortoise tells of the hare's overconfidence and the tortoise's determination and dedication which saw the tortoise win the race. The hare had gotten a head start but when he realized that the tortoise was behind, he took a nap which allowed the tortoise to reach the finish line first. The tortoise emerged victorious as the moral of the story is that hard work pays off and should be done with humility."] Prompt 2: Python3
Output: ['- Farmer asked son to take herd of sheep grazing\n- Boy got bored and started shouting “Wolf! Wolf!” to which the villagers ran to help him\n- Villagers realized there was no wolf and were furious\n- Boy shouted again the next day and villagers again realized there was no wolf, making them extremely angry\n- On same day, boy spotted an actual Wolf, but this time the villagers did not come to help as they thought he was playing a joke again'] Example prompts for Retrieving factual informationNow let’s try some prompts for Retrieving factual information using Completions module. Prompt 1: Python3
Output: ['India is divided up into 28 different states and 8 union territories: \n\nStates: Andhra Pradesh, Arunachal Pradesh, Assam, Bihar, Chhattisgarh, Goa, Gujarat, Haryana, Himachal Pradesh, Jammu & Kashmir, Jharkhand, Karnataka, Kerala, Madhya Pradesh, Maharashtra, Manipur, Meghalaya, Mizoram, Nagaland, Odisha, Punjab, Rajasthan, Sikkim, Tamil Nadu, Telangana, Tripura, Uttar Pradesh, Uttarakhand, and West Bengal. \n\nUnion Territories: Andaman & Nicobar Islands, Chandigarh, Dadra & Nagar Haveli, Daman & Diu, Delhi, Lakshadweep, Ladakh, Puducherry.'] Prompt 2: Python3
Output: ['According to Forbes, the highest paid actor in the world as of 2021 is Dwayne Johnson, with an estimated 2020 earnings of $87.5 million.'] Example prompts for Text ConversionNow let’s try some prompts for Text Conversion using Completions module. Prompt 1: Python3
Output: 1. ????♂️???? 2. ⏳???? 3. ???????? Prompt 2: Python3
Output: 1. Ɣ€€Ƙ§ 2. Ƒöř 3. Ɣ€€Ƙ§ ChatFor performing any chat specific tasks you can define the following function and execute it with your desired prompts. Python3
Here, we have used the Chat Completion module from OpenAI library to execute chat specific tasks using ChatGPT model. Here are the important parameters involved with Chat Completion module:
Examples for Chat CompletionNow Let’s see some examples to gain a better understanding of the Chat Completion API Prompt 1: Python3
Output: <OpenAIObject at 0x7fb078845710> JSON: { "content": "India has won the Cricket World Cup twice. The first time was in 1983 and the second time was in 2011.", "role": "assistant" } Prompt 2: Python3
Output: <OpenAIObject at 0x7fb078847c90> JSON: { "content": "Sure, here are some popular chocolate names:\n\n1. Lindt\n2. Ghirardelli\n3. Ferrero Rocher\n4. Toblerone\n5. Godiva\n6. Hershey's\n7. Cadbury\n8. Nestle\n9. Lindor \n10. Milka", "role": "assistant" } ImageWe can perform Image generation and Image editing using DALL-E model of OpenAI. Before we begin, let’s import some image-processing libraries. Python3
Now we construct a function to produce an image using the DALL E API’s “create” endpoint. Python3
An argument string is passed to the API endpoint by the aforementioned function. Other parameters are n (the “number of images generated using that prompt”) and size (the “size of the image generated”). Either Base64 or a URL can be used by the API to generate an image. As output, API provides the created image’s URL.
Examples for Image GenerationNow Let’s see some examples to gain a better understanding of Image Generation using DALL-E Prompt 1: Python3
Output: ![]() batman art in red and blue color Prompt 2: Python3
Output: ![]() A scenic view of moon shining light on a yacht Examples for Image EditingFor editing the image we use the create edit endpoint of the DALL-E API. In this section, a mask will be uploaded and a text prompt will be supplied in order to change an image. Where the image should be altered is indicated by the transparent portions of the mask, and the prompt should describe the entire new image rather than just the area that was erased. Make sure your image and mask are of the same size (square PNG) and less than 4MB in size before passing them as arguments to API. For this we use the below code. Python3
Also, write a prompt such that it describes the full new image not just the transparent area that needs to be replaced. Use the following lines of code to edit the image. Let’s see the example to gain a better understanding of Image Editing using DALL-E. Image 1: We will be using the below image as input: ![]() Input Image for Create Edit endpoint Below is the code for editing the image and generating 3 images as output. Python3
Output: AudioThere are 2 modules available for Whisper module: 1. Transcribe: This module transcribes your audio file into the input language. Model parameters for this module are:
# opening the audio file in read mode audio_file = open("FILE LOCATION", "rb") # calling the module using this line and passing the model name and audio file # there is only one model available for speech-to-text conversion transcript = openai.Audio.transcribe(file="audio file", model="whisper-1") transcript 2. Translate: This module translates your audio file into English language. Model parameters for this module are:
# opening the audio file in read mode audio_file = open("FILE LOCATION", "rb") # calling the module using this line and passing the model name and audio file # there is only one model available for speech-to-text conversion transcript = openai.Audio.translate(file="audio file", model="whisper-1") transcript
Examples for Audio TranscribingAudio we will be using for trying out Transcribe module: . We will execute the below code for transcribing this audio. Python3
Output: Which language is best for learning DSA? There are different types of people. If you are a patient programmer who is a patient person, with a lot of patients, then probably Java is the best language. Because in Java you can't write bad code and Python would even be better than Java. But if you really want to quickly compile and run and see the output of the program, C++ is good. So if we talk about the software industry, most of the places they use Java. And some of the places have been shifted to Python or shifting to Python. But still most of the industry runs on Java. Examples for Audio TranslationAudio we will be using for trying out Translate module: . We will execute the below code for translating this audio. Python3
Output: Prompt engineering is a word that you must have heard somewhere. But do you know what is its exact use? And where is it used exactly in the software industry? If not, then let's know. Number 1, Rapid innovation. So any company wants to develop and deploy its new product as soon as possible. And give new services to its customers as soon as possible. So that it remains competitive in its entire tech market. So here prompt engineering comes in a lot of use. Number 2 is cost saving. So prompt engineering allows any company to save its total time and cost. Apart from this, the entire development process streamlines it. Due to which the time to develop the product is reduced and its cost is reduced. Number 3 is demand for automation. So whatever you see in your environment today, everyone wants their entire process to be automated. And prompt engineering allows this. It allows to make such systems that totally automate the process that is going on in your company. So now you know the importance of prompt engineering. If you know more important things than this, then quickly comment below. EmbeddingsThe embeddings module is used to obtain a vector representation of a given input that machine learning models and algorithms can quickly consume. We will write a function to generate these vectors using OpenAI API. Our API employs the “text-embedding-ada-002” model, a member of OpenAI’s second generation of embedding models. This model produces embeddings that are 1536 bytes long. Python3
Let’s try this out on some sample text. Prompt 1: Python3
Output: [0.002064391039311886, -0.006849951110780239, -0.0022784520406275988, -0.034846529364585876, -0.010696562007069588, 0.01778651960194111, -0.0037395802792161703, -0.009561389684677124, -0.0055493684485554695, -0.006862924434244633, -0.02698465622961521, -0.015438336879014969, 0.008653252385556698, -0.010417633689939976, ...] As we can see, we have obtained a vector generated by OpenAI Embeddings module. You can use this vector with any machine learning or deep learning algorithm. Fine-TuningOpen AI allows developers to fine-tune their models for specific purposes using their fine-tunes API endpoint. Below listed are the models you can fine-tune using this API:
For fine-tuning a model, you first need to make sure that your dataset is in JSONL format and if you are working on free trial, your training bill should not exceed 5 dollars. To prepare your dataset, you can execute the below command in your jupyter notebook: !openai tools fine_tunes.prepare_data -f <dataset location> With the exception of a prompt and a completion column/key, this tool accepts a variety of forms. You can instruct it to store the output into a JSONL file suitable for fine-tuning by passing it a CSV, TSV, XLSX, JSON, or JSONL file. It will walk you through the process of proposed adjustments. Now, you can fine-tune a model by executing the below code in python environment: Python3
Example of Fine-tuningWe will be taking the first 100 rows of Spam Classification dataset here.
Step 1: First prepare the dataset using OpenAI’s in-built function. !openai tools fine_tunes.prepare_data -f "spam.csv" Output: Analyzing... - Based on your file extension, your file is formatted as a CSV file - Your file contains 100 prompt-completion pairs - The `prompt` column/key should be lowercase - The `completion` column/key should be lowercase - The input file should contain exactly two columns/keys per row. Additional columns/keys present are: ['Unnamed: 0'] WARNING: Some of the additional columns/keys contain `Unnamed: 0` in their name. These will be ignored, and the column/key `Unnamed: 0` will be used instead. This could also result from a duplicate column/key in the provided file. - Based on your data it seems like you're trying to fine-tune a model for classification - For classification, we recommend you try one of the faster and cheaper models, such as `ada` - For classification, you can estimate the expected model performance by keeping a held out dataset, which is not used for training - Your data does not contain a common separator at the end of your prompts. Having a separator string appended to the end of the prompt makes it clearer to the fine-tuned model where the completion should begin. See https://platform.openai.com/docs/guides/fine-tuning/preparing-your-dataset for more detail and examples. If you intend to do open-ended generation, then you should leave the prompts empty - The completion should start with a whitespace character (` `). This tends to produce better results due to the tokenization we use. See https://platform.openai.com/docs/guides/fine-tuning/preparing-your-dataset for more details Based on the analysis we will perform the following actions: - [Necessary] Your format `CSV` will be converted to `JSONL` - [Necessary] Lower case column name to `prompt` - [Necessary] Lower case column name to `completion` - [Necessary] Remove additional columns/keys: ['Unnamed: 0'] - [Recommended] Add a suffix separator ` ->` to all prompts [Y/n]: n - [Recommended] Add a whitespace character to the beginning of the completion [Y/n]: n - [Recommended] Would you like to split into training and validation set? [Y/n]: y Your data will be written to a new JSONL file. Proceed [Y/n]: y Wrote modified files to `/content/clean_spam_prepared_train (1).jsonl` and `/content/clean_spam_prepared_valid (1).jsonl` Feel free to take a look! Now use that file when fine-tuning: > openai api fine_tunes.create -t "/content/clean_spam_prepared_train (1).jsonl" -v "/content/clean_spam_prepared_valid (1).jsonl" --compute_classification_metrics --classification_positive_class "ham" Make sure to include `stop=["am"]` so that the generated texts ends at the expected place. Once your model starts training, it'll approximately take 4.73 minutes to train a `curie` model, and less for `ada` and `babbage`. Queue will approximately take half an hour per job ahead of you. Step 2: Upload your training and validation dataset to OpenAI using the following lines of code in python Python3
Output: <File file id=file-jTiNyDdGWLAvjP3RPN6RqumP at 0x7fefd161f150> JSON: { "bytes": 9822, "created_at": 1685693212, "filename": "file", "id": "file-jTiNyDdGWLAvjP3RPN6RqumP", "object": "file", "purpose": "fine-tune", "status": "uploaded", "status_details": null } Python3
Output: <File file id=file-u6qLHlRivL52CuEj7qnnREpS at 0x7fefd0d6ba10> JSON: { "bytes": 2211, "created_at": 1685693224, "filename": "file", "id": "file-u6qLHlRivL52CuEj7qnnREpS", "object": "file", "purpose": "fine-tune", "status": "uploaded", "status_details": null }
Step 3: Now we will fine-tune Davinci model. Python3
Output: Fine-tunning model with jobID: ft-nPNCod70qnSWBXjnQ5kbZXK2. Training Response: { "created_at": 1685693249, "events": [ { "created_at": 1685693249, "level": "info", "message": "Created fine-tune: ft-nPNCod70qnSWBXjnQ5kbZXK2", "object": "fine-tune-event" } ], "fine_tuned_model": null, "hyperparams": { "batch_size": 4, "learning_rate_multiplier": 0.133, "n_epochs": 10, "prompt_loss_weight": 0.01 }, "id": "ft-nPNCod70qnSWBXjnQ5kbZXK2", "model": "davinci", "object": "fine-tune", "organization_id": "org-Sb3Fliwt5e4Qvq5kjbBtmpd8", "result_files": [], "status": "pending", "training_files": [ { "bytes": 9822, "created_at": 1685693212, "filename": "file", "id": "file-jTiNyDdGWLAvjP3RPN6RqumP", "object": "file", "purpose": "fine-tune", "status": "processed", "status_details": null } ], "updated_at": 1685693249, "validation_files": [ { "bytes": 2211, "created_at": 1685693224, "filename": "file", "id": "file-u6qLHlRivL52CuEj7qnnREpS", "object": "file", "purpose": "fine-tune", "status": "processed", "status_details": null } ] } Training Status: pending As you can see the training status is pending, to know the status of your fine-tuning task you can execute the below code in your jupyter notebook. Python3
Output: Finetune job ft-nPNCod70qnSWBXjnQ5kbZXK2 finished with status: succeeded Checking other finetune jobs in the subscription. Found 2 finetune jobs. Once the fine-tuning process is finished, move to the next step. Step 4: Retrieve the model By executing the below code, you can retrieve the model and save it in a variable. Python3
Output: davinci:ft-personal-2023-06-02-08-17-42 Step 5: Use your custom model with any API endpoint. We are using the Completion API endpoint of OpenAI here to check whether a message is spam or not. Python3
Output: Spam API Error CodesThis guide includes an overview on error codes you might see from both the API and our official Python library. Each error code mentioned in the overview has a dedicated section with further guidance. API errors401 – Invalid Authentication Cause: Invalid Authentication Solution: Ensure the correct API key and requesting organization are being used. 401 – Incorrect API key provided Cause: The requesting API key is not correct. Solution: Ensure the API key used is correct, clear your browser cache, or generate a new one. 401 – You must be a member of an organization to use the API Cause: Your account is not part of an organization. Solution: Contact us to get added to a new organization or ask your organization manager to invite you to an organization. 429 – Rate limit reached for requests Cause: You are sending requests too quickly. Solution: Pace your requests. Read the Rate limit guide. 429 – You exceeded your current quota, please check your plan and billing details Cause: You have hit your maximum monthly spend (hard limit) which you can view in the account billing section. Solution: Apply for a quota increase. 429 – The engine is currently overloaded, please try again later Cause: Our servers are experiencing high traffic. Solution: Please retry your requests after a brief wait. 500 – The server had an error while processing your request Cause: Issue on our servers. Solution: Retry your request after a brief wait and contact us if the issue persists. Check the status page. ConclusionIn this article, we discussed all the major features provided by OpenAI API’s and their implementation with examples. Now you are very much capable of using OpenAI’s services effectively in your applications, day-to-day tasks and so much more. As always, there is a lot more to discover about this subject than we are able to discuss in a single blog post. Check out the official OpenAI documentation and community forums if you’re interested in learning more and getting support. Thank you for reading, Happy coding! OpenAI Python API – FAQs1. What is OpenAI and what is its goal?
2. What are some potential use cases for OpenAI API?
3. How does the pricing for OpenAI models work?
|
Reffered: https://www.geeksforgeeks.org
Data Science |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |