没有合适的资源?快使用搜索试试~ 我知道了~
给Python程序员的OpenAI GPT
5星 · 超过95%的资源 需积分: 5 26 下载量 165 浏览量
2023-05-10
13:46:06
上传
评论 3
收藏 9.45MB PDF 举报
温馨提示
试读
235页
OpenAI GPT应用指南。OpenAI 提供 API(应用程序编程接口)以访问他们的 AI。API 的目标是通过为所有版本创建通用接口来抽象底层模型,使用户无论版本如何都能使用 GPT。
资源推荐
资源详情
资源评论
OpenAI GPT For Python Developers
The art and science of developing intelligent apps with
OpenAI GPT-3, DALL·E 2, CLIP, and Whisper
A comprehensive and example-rich guide suitable for
learners of all levels
Aymen El Amri @eon01
This book is for sale at http://leanpub.com/openaigptforpythondevelopers
This version was published on 2023-02-27
This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing
process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and
many iterations to get reader feedback, pivot until you have the right book and build traction once
you do.
© 2023 Aymen El Amri @eon01
Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
ChatGPT, GPT, GPT-3, DALL·E, Codex? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
About This Guide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Keep in Touch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
How Does GPT Work? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Preparing the Development Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Installing Python, pip, and a Virtual Environment for Development . . . . . . . . . . . . . . 10
Get Your OpenAI API Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Installing the Official Python Bindings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Testing our API Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Available Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
The Three Main Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
GPT-3: Processing and Generating Natural Language . . . . . . . . . . . . . . . . . . . . . . . 15
Codex: Understanding and Generating Computer Code . . . . . . . . . . . . . . . . . . . . . 16
Content Filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Listing all Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Which Model to Use? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
What’s next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Using GPT Text Completions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
A Basic Completion Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Controlling the Output’s Token Count . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Logprobs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Controlling Creativity: The Sampling Temperature . . . . . . . . . . . . . . . . . . . . . . . . 33
Sampling with “top_p” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Streaming the Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Controlling Repetitivity: Frequency and Presence Penalties . . . . . . . . . . . . . . . . . . . 39
Controlling the Number of Outputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Getting the “best of” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
CONTENTS
Controlling When the Completion Stops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Using Suffix After Text Completion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Example: Extracting keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Example: Generating Tweets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Example: Generating a Rap Song . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Example: Generating a Todo List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Editing Text Using GPT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Example: Translating Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Instruction is Required, Input is Optional . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Editing Using the Completions Endpoint and Vice Versa . . . . . . . . . . . . . . . . . . . . . 60
Formatting the Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Creativity vs. Well-Defined Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Generating Multiple Edits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Advanced Text Manipulation Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Chaining Completions and Edits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Apple the Company vs. Apple the Fruit (Context Stuffing) . . . . . . . . . . . . . . . . . . . . 70
Getting Cryptocurrency Information Based on a User-Defined Schema (Context stuffing) 73
Creating a Chatbot Assistant to Help with Linux Commands . . . . . . . . . . . . . . . . . . 75
Embedding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Overview of Embedding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Use Cases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Understanding Text Embedding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Embeddings for Multiple Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Semantic Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Cosine Similarity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Advanced Embedding Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Predicting your Preferred Coffee . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Making a “fuzzier” Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Predicting News Category Using Embedding . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Evaluating the Accuracy of a Zero-Shot Classifier . . . . . . . . . . . . . . . . . . . . . . . . . 116
Fine Tuning & Best Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Few Shot Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Improving Few Shot Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Fine Tuning in Practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Datasets, Prompts, and Completions: What are the Best Practices? . . . . . . . . . . . . . . . 126
Advanced Fine Tuning: Drug Classification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
CONTENTS
Dataset Used in the Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Preparing the Data and Launching the Fine Tuning . . . . . . . . . . . . . . . . . . . . . . . . 135
Testing the Fine Tuned Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Advanced Fine Tuning: Creating a Chatbot Assistant . . . . . . . . . . . . . . . . . . . . . . . . 142
Interactive Classification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
How Will Everything Work? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Creating a Conversational Web App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Intelligent Speech Recognition Using OpenAI Whisper . . . . . . . . . . . . . . . . . . . . . . 158
What is Whisper? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
How to Get Started? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Transcribe and Translate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Context & Memory: Making AI More Real . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
No Context = Chaos of Randomness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
History = Context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
The Problem with Carrying Over History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Last in First out (LIFO) Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
The Problem with Last in First out Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Selective Context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Building Your AI-Based Alexa . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Recording the audio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Transcribing the Audio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Replying to User Request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
The Main Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Putting Everything Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Generating Better Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
Image Classification with OpenAI CLIP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
What is CLIP? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
How to Use CLIP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Reverse Stable Diffusion: Image to Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Generating Images Using DALL-E . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
A Basic Example of Image Generation From a Prompt . . . . . . . . . . . . . . . . . . . . . . 194
Generating Multiple Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Using Different Sizes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
Better Image Prompts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Building a Random Image Generator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
剩余234页未读,继续阅读
资源评论
- thebestuzi2023-07-29在这篇文件中,对于Python程序员而言,了解OpenAI GPT会是一个很有趣的探索。
- 士多霹雳酱2023-07-29这篇文件对于Python程序员来说真是太实用了,里面的内容让我受益匪浅。
- 莉雯Liwen2023-07-29这篇文件把Python与OpenAI GPT的结合讲得很清晰,对我来说很有帮助。
- 图像车间2023-07-29作者在介绍Python程序员使用OpenAI GPT时,提供了一些很实用的技巧和建议。
- 赵伊辰2023-07-29读完这篇文件之后,我对OpenAI GPT的理解更加深入了,很棒的一份资料。
Macya-zxt
- 粉丝: 0
- 资源: 17
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功