Chat
For performing any chat specific tasks you can define the following function and execute it with your desired prompts.
Python3
# function that takes in string argument as parameter def chat(MSGS, MaxToken = 50 , outputs = 3 ): # We use the Chat Completion endpoint for chat like inputs response = openai.ChatCompletion.create( # model used here is ChatGPT # You can use all these models for this endpoint: # gpt-4, gpt-4-0314, gpt-4-32k, gpt-4-32k-0314, # gpt-3.5-turbo, gpt-3.5-turbo-0301 model = "gpt-3.5-turbo" , messages = MSGS, # max_tokens generated by the AI model # maximu value can be 4096 tokens for "gpt-3.5-turbo" max_tokens = MaxToken, # number of output variations to be generated by AI model n = outputs, ) return response.choices[ 0 ].message # Messages must consist of a collection of message objects, # each of which includes a role (either "system," "user," or "assistant") # and a content field for the message's actual text. # Conversations might last only 1 message or span several pages. MSGS = [ { "role" : "system" , "content" : "<message generated by system>" }, { "role" : "user" , "content" : "<message generated by user>" }, { "role" : "assistant" , "content" : "<message generated by assistant>" } ] |
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:
- model [required]: ID of the appropriate model. For information on which models are compatible with the Chat API, go to the model endpoint compatibility table (https://platform.openai.com/docs/models/model-endpoint-compatibility).
- message: A chronological list of the conversation’s messages. The list of objects must contain dictionary with the following parameters only:
- Role: The role of the author of this message. It can be either of “system”, “user”, or “assistant”.
- Content: The contents of the message.
- max_tokens: The maximum number of tokens to generate in the completion. The default value is 16.
- temperature: Sampling temperature ranges between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.
- n: number of completions to generate for each prompt.
Examples for Chat Completion
Now Let’s see some examples to gain a better understanding of the Chat Completion API
Prompt 1:
Python3
ch = [ { "role" : "user" , "content" : "When did India win the world cup" } ] chat(ch, MaxToken = 500 , outputs = 1 ) |
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
ch = [ { "role" : "user" , "content" : "Do you live with Snow White?" }, { "role" : "assistant" , "content" : "No, I live with Willy Wonka in the chocoalte factory." }, { "role" : "user" , "content" : "Can you tell me some good chocolate names?" } ] chat(ch, MaxToken = 500 , outputs = 1 ) |
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" }
OpenAI Python API – Complete Guide
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 Content
- What is OpenAI?
- What is OpenAI API?
- Generate OpenAI API key
- Installation of OpenAI package
- Prompt Engineering
- Text
- Chat
- Image
- Audio
- Embeddings
- Fine-Tuning
- API Error Codes
- Conclusion
- FAQs on OpenAI Python API
Contact Us