Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
11 views
Exploring The ChatGPT API With Python
Exploring the ChatGPT API with Python
Uploaded by
Marcos Luis
AI-enhanced title
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save Exploring the ChatGPT API with Python For Later
Download
Save
Save Exploring the ChatGPT API with Python For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
11 views
Exploring The ChatGPT API With Python
Exploring the ChatGPT API with Python
Uploaded by
Marcos Luis
AI-enhanced title
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save Exploring the ChatGPT API with Python For Later
Carousel Previous
Carousel Next
Save
Save Exploring the ChatGPT API with Python For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 11
Search
Fullscreen
s115i24, 8:41 AM Exploring the ChatGPT API with Python | MLExpert- Get Things Done with Al Bootcamp Blog > Chatgpt API Exploring the ChatGPT API with Python Exploring the ChatGPT API with Python: Multiple Completions, Max Tokens, ... OpenAl recently introduced? the ChatGPT API along with the Whisper API, which has already been integrated into some popular apps such as Snapchat and Instacart. The new ChatGPT API features a new model called gpt-3.5-turbo that has replaced the previously most commonly used model, text-davinci-e03 Join the AI BootCamp! Ready to dive into the world of Al and Machine Learning? Join the Al BootCamp to transform your career with the latest skills and hands-on project experience. Learn about LLMs, ML best practices, and much more! JOIN NOW ntpshwwwmlexperoxblogichatgpt-ap wtrsa, e:1aM Explering the ChaGPT API wth Python | MLEeper - Get Things Done wth AI Botcamp This new model is the exact same model used in the ChatGPT web app. Also, itis about 10x cheaper! The OpenAl team recommends the new model as a replacement for all previously used models. This part will utilize the official Python library provided by OpenAl, which includes a new component called ChatCompletion specifically designed for ChatGPT. Let's get started! © In this tutorial, we will be using Jupyter Notebook to run the code. If you prefer to follow along, you can access the notebook here: open the notebook Get API Key To use the API, you will need to register for an account on the OpenAl platform and generate an API key from this page: https://platform.openai.com/account/api-keys Setup You'll need the openai @ library, version @.27.0 . We'll also use the tiktoken 3 to count number of tokens in a given text: pip install -qqq openai==0.27.0 pip install -qqq tiktoken==0.3.0 Next, let's add imports and set the API key: import openai import tiktoken from IPython.display import display, Markdown openai.api_key = "YOUR API KEY" https shwwn-mlexpertiafblogichatgpt-ap| ams115i24, 8:41 AM Exploring the ChatGPT API with Python | MLExpert- Get Things Done with Al Bootcamp Call the API To start with the APIS, we'll need a prompt. Let's reuse the prompt from the prompt engineering guide: prompt = Write me a plan of how to invest 10,0@ USD with the goal of making maximum profit in 1 Give specific investments and allocation percentages. Next, we'll make the API call: result = openai.ChatCompletion.create( model="gpt-3.5-turbo", message: {"role": "system", “content”: "You're an expert personal finance advisor."}, {"role": "user", "content": prompt} 1. d result The model parameter specifies the version of the GPT model to use, in this case gpt- 3.5-turbo . The messages parameter is a list of two dictionaries representing the conversation between the user and the model. The first dictionary has a system role and provides a statement to set the context for the conversation. The second dictionary has a user role and contains the prompt. The API response is in JSON format and contains information regarding the response generated by ChatGPT: { "choices": [ { “#inish_reason": “stop, "index": @, "message": { ntps:hwwwmlexperofblogichatgpt-ap aits115i24, 8:41 AM Exploring the ChatGPT API with Python | MLExpert- Get Things Done with Al Bootcamp "content": "\n\nBefore creating an investment plan, it\u2019s important to note assistant” 1678135181, hatcmp] -6rBuPqnXPFt fdLNOcnkvAoldGykQ3" , 1 "gpt-3.5-turbo-03e1", "object": "chat.completion”, “usage”: { “completion_tokens": 397, “prompt_tokens": 54, “total_tokens": 451 Let's have a look at two key-value pairs © choices: an array that contains an object with the response message, index, and finish_reason * usage: an object that provides the number of tokens used to generate the response In this case, the choices array contains only one object, which represents the response message generated by ChatGPT. The message contains a recommended investment plan and some advice for the user. The usage object indicates the number of tokens used to generate the response, which can be useful to track usage and costs. We will use a helper function to extract the message content and display it in Markdown format def show_choice(choice) display (Markdown (choice[ "message"]["content"])) show_choice(result["choices"][@]) [Bi chatcrt Response Conversations https shwww-mlexpertiafblogichatgpt-ap| amssi, sat aM Espoing the ChatGPT AP with Python| MLExpert- Got Things Done wt Al Booteamp The messages parameter is designed to contain the whole conversation between the user and ChatGPT. It's important to note that the API does not have the ability to record or remember previous conversations. To create a conversation with ChatGPT, we can use the result object from the previous API call and add the generated response to the messages list. Then, we can make another API call using this extended messages list to generate a tweet about the personalized finance plan: answer = result[*choices"][]["nessage”][ "content" ] format_prompt "Write the plan as a tweet” result = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ “system”, “content”: “You're an expert personal finance advisor."}, "user", “content”: prompt}, “assistant”, “content”: answer}, “user”, "content": format_prompt}, show_choice(result[ "choices" ][@]) Hi chatGrr Response Investing 60% in stocks, 20% in alternative investments, 10-20% in cash reserves, and remaining 10-20% in bonds can maximize profit in 1 year for $10K portfolio. However, always do your research and seek professional advice. #personal finance #investing #portfolioallocation API Options The API has a wide array of options’, We'll have a look at limiting the number of tokens, getting multiple completions and adjusting temperature. Maximum Tokens https shwwn-mlexpertiafblogichatgpt-ap| sits115i24, 8:41 AM Exploring the ChatGPT API with Python | MLExpert- Get Things Done with Al Bootcamp ChatGPT's pricing is token-based, meaning the cost is determined by the number of tokens you use. There is a maximum token limit of 4096 per API request. Both input and output tokens count toward this limit and your payment. To control the maximum number of tokens generated by the model, the max_tokens parameter can be specified response = openai .chatCompletion.create( model="gpt-3.5~turbo" messages=[ (role ‘You're an expert personal finance advisor. (role pronpt} L max_tokens=256 ) response { "choices": [ { “finish_reason": "Length", "index": @, “message t content": "\n\nAs an AI language model, I cannot provide investment advice or assistant” 1678135229, id”: "chatempl-6rBvBDH2qjSFhv@lwPqB1az7dBTU" , model": "gpt-3.5-turbo-0301", "object": "chat.completion”, sage": { ‘completion_tokens": 256, prompt_tokens": 54, total_tokens": 310 https shwwn-mlexpertiafblogichatgpt-ap| eit9115724, 841 AM Exploring the ChatGPT API with Python | MLExpert- Get Things Done with Al Bootcamp Note that the finish_reason isnow stop and completion_tokens is equal to the number of max_tokens - 256. If you want to know how many tokens your prompts contain, you can use the "tiktoken" library to create this function: def num_tokens_fron_messages (messages): Returns the number of tokens used by a list of messages. Code from: https: //github.com/openai /openai -cookbook encoding = tiktoken.encoding_for_model("gpt-3.5-turbo") num_tokens = @ for message in messages num_tokens += 4 # every message follows
{role/name}\n{content}
assistant return num_tokens The num_tokens_from_messages function takes a list of messages and returns the number of tokens that would be used by ChatGPT. Let's check the last messages: messages = [ » "content": "You're an expert personal finance advisor."}, content": prompt} num_tokens_from_messages (messages) 54 The number 54 is the exact same value returned by the usage.prompt_tokens attribute of the API response. This attribute indicates the number of tokens used by the prompt provided in the API request. ntps:hwwwmlexperofblogichatgpt-ap mms115i24, 8:41 AM Exploring the ChatGPT API with Python | MLExpert- Get Things Done with Al Bootcamp Generate Multiple Responses To generate multiple completions for a prompt using ChatGPT API, you can set the n parameter to specify the number of completions you want result = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ “content”: "You're an expert personal finance advisor."}, prompt} for i, choice in enumerate(result["choices"]): print(f"Response {i + 1}:") show_choice(choice) print("\n") [HB chatort Response Temperature You can adjust the level of randomness and creativity in the responses generated by ChatGPT using the temperature parameter. The temperature ranges from 0 to 2, with a default value of 1. A lower temperature will result in more focused and predictable responses, while a higher temperature will result in more diverse and unpredictable responses: result = openai.chatCompletion.create( model="gpt-3.5-turbo", messages=[ "system", : "user", " "You're an expert personal finance advisor."}, : prompt} iy temperature=0 d show_choice(result["choices"][@]) https shwwn-mlexpertiafblogichatgpt-ap| amt115i24, 8:41 AM Exploring the ChatGPT API with Python | MLExpert- Get Things Done with Al Bootcamp HB chatarr Response result = openai.ChatCompletion.create( model="gpt-3.5-turbo" messages=[ {("role": "system", “content”: "You're an expert personal finance advisor."}, {role": "user", “content”: prompt} iy ‘temperature=1 ) show_choice(result, choices” [e]) HB chatert Response result = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ role "content": "You're an expert personal finance advisor. ("role content": prompt} L temperatun’ ) show_choice(result["choices"][@]) Hi chaterr Response response = openai .chatCompletion.create( gpt-3.5-turbo", system", “content user", ‘You're an expert personal finance advisor content": prompt} temperature ) show_choice(response["choices"][@]) [HB chaterr Response ntps:hwwwmlexperofblogichatgpt-ap omns, :41aM Explring the ChaGPT API wth Python | MLEeper- Get Things Done wth Al Boicamp Gradually increasing the temperature parameter can lead to more random and nonsensical completions, while finding the right balance of values can result in more focused and relevant responses Conclusion In conclusion, we have explored various features and functionalities of OpenAl's ChatGPT API. We have seen how to control the number of tokens, get multiple completions, adjust the temperature, and extract message content using helper functions. Compared to the web app, the ChatGPT API offers significantly more customization options. One particularly interesting area to explore is creating and modifying conversations, which can potentially result in better response generation. 3,000+ people already joined Join the The State of Al Newsletter Every week, receive a curated collection of cutting-edge Al developments, practical tutorials, and analysis, empowering you to stay ahead in the rapidly evolving field of AL Your Email Address SUBSCRIBE Iwon't send you any spam, ever! References 1. ChatGPT and Whisper APIs @ 2. Qpenai Python Library © https shwwn-mlexpertiafblogichatgpt-ap| somssi, st aM Espoing the ChatGPT AP with Python| MLExpert- Got Things Done wt Al Booteamp Tiktoken Library & ChatGPT API Tutorial © 5. ChatGPT API Reference © hitpsswww-mlexper ioblog/chatgpt-api awit
You might also like
OpenAI Official Prompt Engineering Guide
PDF
No ratings yet
OpenAI Official Prompt Engineering Guide
17 pages
Chat GPT
PDF
92% (72)
Chat GPT
34 pages
openai-workingcourse-introduction-to-chatgpt-api-chatgpt-api-parameters
PDF
No ratings yet
openai-workingcourse-introduction-to-chatgpt-api-chatgpt-api-parameters
11 pages
python chatgpt
PDF
No ratings yet
python chatgpt
3 pages
Python code explanation
PDF
No ratings yet
Python code explanation
4 pages
Chatbot Development With ChatGPT & LangChain A Context-Aware Approach DataCamp
PDF
No ratings yet
Chatbot Development With ChatGPT & LangChain A Context-Aware Approach DataCamp
18 pages
API OpenAI
PDF
No ratings yet
API OpenAI
25 pages
API Reference - OpenAI API
PDF
No ratings yet
API Reference - OpenAI API
116 pages
Additional Tips
PDF
No ratings yet
Additional Tips
1 page
medium_com
PDF
No ratings yet
medium_com
2 pages
Autogen Components Guide
PDF
No ratings yet
Autogen Components Guide
26 pages
Open AI Python
PDF
No ratings yet
Open AI Python
1 page
An Advanced Guide - How To Use ChatGPT API in Python - Kanaries
PDF
No ratings yet
An Advanced Guide - How To Use ChatGPT API in Python - Kanaries
5 pages
How To Build Your Own Custom ChatGPT Bot With Custom Knowledge Base - Better Programming
PDF
No ratings yet
How To Build Your Own Custom ChatGPT Bot With Custom Knowledge Base - Better Programming
8 pages
Telegram Bot с ChatGpt На Python. - UPROGER | Программирование
PDF
No ratings yet
Telegram Bot с ChatGpt На Python. - UPROGER | Программирование
2 pages
Finxter OpenAI Python API
PDF
No ratings yet
Finxter OpenAI Python API
1 page
Chatbot_Project_Guide
PDF
No ratings yet
Chatbot_Project_Guide
3 pages
Chatbot_Project_Guide
PDF
No ratings yet
Chatbot_Project_Guide
5 pages
Chat GPT
PDF
No ratings yet
Chat GPT
3 pages
A1 Usage
PDF
No ratings yet
A1 Usage
6 pages
How To Create A ChatGPT API Using Python Code
PDF
No ratings yet
How To Create A ChatGPT API Using Python Code
10 pages
OpenAI Playground Cheat Sheet 2.1
PDF
No ratings yet
OpenAI Playground Cheat Sheet 2.1
3 pages
Unlocking_the_Potential_of_ChatGPT_for_Programmers_by_Paul_Malaj_Icons8
PDF
No ratings yet
Unlocking_the_Potential_of_ChatGPT_for_Programmers_by_Paul_Malaj_Icons8
26 pages
Github - Acheong08 - Chatgpt - Reverse Engineered Chatgpt API
PDF
No ratings yet
Github - Acheong08 - Chatgpt - Reverse Engineered Chatgpt API
2 pages
Langchain Onepager
PDF
No ratings yet
Langchain Onepager
1 page
المستند (3)h
PDF
No ratings yet
المستند (3)h
2 pages
AI Phae 2 Project
PDF
No ratings yet
AI Phae 2 Project
8 pages
GPT-4o API Deep Dive Text Generation Vision and Function Calling
PDF
No ratings yet
GPT-4o API Deep Dive Text Generation Vision and Function Calling
21 pages
60 ChatGPT Prompts Ebook
PDF
100% (3)
60 ChatGPT Prompts Ebook
37 pages
OpenAI Compatible Server - VLLM
PDF
No ratings yet
OpenAI Compatible Server - VLLM
27 pages
2339_m3_demo5_v1_w7m_cy33m1lg
PDF
No ratings yet
2339_m3_demo5_v1_w7m_cy33m1lg
25 pages
ChatBot PDF
PDF
No ratings yet
ChatBot PDF
109 pages
Test Cases
PDF
No ratings yet
Test Cases
3 pages
LLM Review
PDF
No ratings yet
LLM Review
31 pages
Assistants API Overview (Python SDK) OpenAI Cookbook
PDF
No ratings yet
Assistants API Overview (Python SDK) OpenAI Cookbook
21 pages
How To Use ChatGPT
PDF
No ratings yet
How To Use ChatGPT
3 pages
Ai Phase 3 Project
PDF
No ratings yet
Ai Phase 3 Project
18 pages
LangChain Chatbot Guide
PDF
No ratings yet
LangChain Chatbot Guide
5 pages
Autogen OpenAi Class
PDF
No ratings yet
Autogen OpenAi Class
12 pages
Intro To OpenAI GPT API - Intro To OpenAI GPT API Cheatsheet - Codecademy
PDF
No ratings yet
Intro To OpenAI GPT API - Intro To OpenAI GPT API Cheatsheet - Codecademy
7 pages
ChatGPT Guide Use These Prompt Strategies To Maximize Your Results
PDF
No ratings yet
ChatGPT Guide Use These Prompt Strategies To Maximize Your Results
12 pages
AI 050T00A ENU PowerPoint 02
PDF
No ratings yet
AI 050T00A ENU PowerPoint 02
14 pages
The Complete Beginner's Guide to Coding with ChatGPT
PDF
No ratings yet
The Complete Beginner's Guide to Coding with ChatGPT
8 pages
ChatGPT User Guide
PDF
No ratings yet
ChatGPT User Guide
9 pages
AI Chatbot: Green University of Bangladesh
PDF
100% (2)
AI Chatbot: Green University of Bangladesh
20 pages
How To Build Your Own AI Chatbot With ChatGPT API
PDF
No ratings yet
How To Build Your Own AI Chatbot With ChatGPT API
16 pages
Chaptgpt Use
PDF
No ratings yet
Chaptgpt Use
6 pages
slides
PDF
No ratings yet
slides
63 pages
Allie K. Miller - ChatGPT+and+the+Professional's+Guide+to+Using+AI
PDF
No ratings yet
Allie K. Miller - ChatGPT+and+the+Professional's+Guide+to+Using+AI
50 pages
GroqCloud-api-reference
PDF
No ratings yet
GroqCloud-api-reference
7 pages
Quickstart Openai
PDF
No ratings yet
Quickstart Openai
8 pages
Raquan Mobley - ChatGPT For Dummies. The Smart Way 2023-Independently Published (2024)
PDF
No ratings yet
Raquan Mobley - ChatGPT For Dummies. The Smart Way 2023-Independently Published (2024)
216 pages
Lab1 Installation
PDF
No ratings yet
Lab1 Installation
8 pages
Prerequisites For All Programs: Install Required Packages
PDF
No ratings yet
Prerequisites For All Programs: Install Required Packages
5 pages
3.1 12. OpenAI Playground @RecursosCompartidos
PDF
No ratings yet
3.1 12. OpenAI Playground @RecursosCompartidos
1 page
Phase 5
PDF
No ratings yet
Phase 5
9 pages
chatGPT Chatbot Training
PDF
No ratings yet
chatGPT Chatbot Training
13 pages
ChatGPT User Guide
PDF
100% (1)
ChatGPT User Guide
12 pages
Chatgpt
PDF
No ratings yet
Chatgpt
9 pages
name
PDF
No ratings yet
name
27 pages
Paddle OCR EN
PDF
No ratings yet
Paddle OCR EN
16 pages
How To Build A Python GUI Application With Wxpython
PDF
No ratings yet
How To Build A Python GUI Application With Wxpython
17 pages
A Python Book Beginning Python Advanced Python and Python Exercises
PDF
No ratings yet
A Python Book Beginning Python Advanced Python and Python Exercises
261 pages
Support For GraphQL in generateDS
PDF
No ratings yet
Support For GraphQL in generateDS
6 pages
ERNIE
PDF
No ratings yet
ERNIE
7 pages
Create GUI Python Programs
PDF
No ratings yet
Create GUI Python Programs
2 pages
PaddlePaddle Generative Adversarial Network CN
PDF
No ratings yet
PaddlePaddle Generative Adversarial Network CN
5 pages
Writing & Blogging
PDF
No ratings yet
Writing & Blogging
8 pages
A Cross-Platform ChatGPT Gemini UI
PDF
No ratings yet
A Cross-Platform ChatGPT Gemini UI
15 pages
Learning Different Languages
PDF
No ratings yet
Learning Different Languages
9 pages
Therapist GPT
PDF
No ratings yet
Therapist GPT
2 pages
Learning Assistant
PDF
No ratings yet
Learning Assistant
6 pages
Agents
PDF
No ratings yet
Agents
4 pages
Kwai Agents
PDF
No ratings yet
Kwai Agents
7 pages
Awesome AI Agents
PDF
No ratings yet
Awesome AI Agents
35 pages
LLaVA - Large Multimodal Model
PDF
No ratings yet
LLaVA - Large Multimodal Model
15 pages
Private Chatbot With Local LLM (Falcon 7B) and LangChain
PDF
No ratings yet
Private Chatbot With Local LLM (Falcon 7B) and LangChain
14 pages
Flux.1-Dev - Photorealistic (And Cute) Images
PDF
100% (1)
Flux.1-Dev - Photorealistic (And Cute) Images
15 pages
Auto GPT
PDF
No ratings yet
Auto GPT
7 pages
Document Classification With LayoutLMv3
PDF
No ratings yet
Document Classification With LayoutLMv3
25 pages
Llama 3 - Open Model That Is Truly Useful
PDF
No ratings yet
Llama 3 - Open Model That Is Truly Useful
19 pages
MemGPT - Unlimited Context (Memory) For LLMs
PDF
No ratings yet
MemGPT - Unlimited Context (Memory) For LLMs
11 pages
Fine-Tuning Llama 2 On A Custom Dataset
PDF
No ratings yet
Fine-Tuning Llama 2 On A Custom Dataset
22 pages
Awesome Japanese NLP Resources
PDF
No ratings yet
Awesome Japanese NLP Resources
32 pages
Chat With Multiple PDFs Using Llama 2 and LangChain
PDF
No ratings yet
Chat With Multiple PDFs Using Llama 2 and LangChain
17 pages
Prompts For Large Language Models
PDF
No ratings yet
Prompts For Large Language Models
6 pages
LangChain QuickStart With Llama 2
PDF
No ratings yet
LangChain QuickStart With Llama 2
16 pages
ChatGPT-repositories ZH
PDF
No ratings yet
ChatGPT-repositories ZH
81 pages