OpenAI GPT For Python Developers
Minimum price
Suggested price

OpenAI GPT For Python Developers

The art and science of building AI-powered apps with GPT-4, Whisper, Weaviate, and beyond

About the Book

The knowledge you'll acquire from this guide will be applicable to the current families of GPT models (GPT-3, GPT-3.5, GPT-4, etc.) and will likely also be relevant to GPT-5, should it ever be released.

OpenAI provides APIs (Application Programming Interfaces) to access their AI. The goal of an API is to abstract the underlying models by creating a universal interface for all versions, allowing users to use GPT regardless of its version.

This guide aims to provide a comprehensive, step-by-step tutorial on how to utilize GPT-3.5 and GPT-4 in your projects via this API. It also covers other models, such as Whisper and Text-to-Speech.

If you're developing a chatbot, an AI assistant, or a web application that utilizes AI-generated data, this guide will assist you in achieving your objectives.

If you have a basic understanding of the Python programming language and are willing to learn a few additional techniques, such as using Pandas Dataframes and some NLP methods, you possess all the necessary tools to start building intelligent systems with OpenAI tools.

Rest assured, you don't need to be a data scientist, machine learning engineer, or AI expert to comprehend and implement the concepts, techniques, and tutorials presented in this guide. The explanations provided are straightforward and easy to understand, featuring simple Python code, examples, and hands-on exercises.

This guide emphasizes practical, hands-on learning and is designed to assist readers in building real-world applications. It is example-driven and provides numerous practical examples to help readers understand the concepts and apply them to real-life scenarios to solve real-world problems.

By the end of your learning journey, you will have developed applications such as:

  • Fine-tuned, domain-specific chatbots.
  • An intelligent conversational system with memory and context.
  • A semantic modern search engine using RAG and other techniques.
  • An intelligent coffee recommendation system based on your taste.
  • A chatbot assistant to assist with Linux commands
  • A fine-tuned news category prediction system.
  • An AI-to-AI autonomous discussion system to simulate human-like conversations or solve problems
  • An AI-based mental health coach trained on a large dataset of mental health conversations
  • and more!

By reading this guide and following the examples, you will be able to:

  • Understand the different models available, and how and when to use each one.
  • Generate human-like text for various purposes, such as answering questions, creating content, and other creative uses.
  • Control the creativity of GPT models and adopt the best practices to generate high-quality text.
  • Transform and edit the text to perform translation, formatting, and other useful tasks.
  • Optimize the performance of GPT models using various parameters and options such as max_tokens, temperature, top_p, n, stream, logprobs, stop, presence_penalty, frequency_penalty, best_of, and others.
  • Stem, lemmatize and reduce your costs when using the API.
  • Understand Context Stuffing, chaining, and practice prompt engineering.
  • Implement a chatbot with memory and context.
  • Create prediction algorithms and zero-shot techniques and evaluate their accuracy.
  • Understand, practice, and improve few-shot learning.
  • Understand fine-tuning and leverage its power to create your own fine-tuned models.
  • Understand and use fine-tuning best practices
  • Practice training and classification techniques using GPT.
  • Understand embedding and how companies such as Tesla and Notion are using it.
  • Understand and implement semantic search, RAG, and other advanced tools and concepts.
  • Integrate a Vector Database (e.g.: Weaviate) with your intelligent systems.

About the Author

Aymen El Amri
Aymen El Amri

Aymen El Amri is an author, entrepreneur, trainer, and polymath software engineer who has excelled in a range of roles and responsibilities in the field of technology including DevOps & Cloud Native, Cloud Architecture, Python, NLP, Data Science, and more.

Aymen has trained hundreds of software engineers and written multiple books and courses read by thousands of other developers and software engineers.

Aymen El Amri has a practical approach to teaching based on breaking down complex concepts into easy-to-understand language and providing real-world examples that resonate with his audience.

Some projects he founded are FAUN,, and Marketto. You can find Aymen on Twitter and Linkedin.

Table of Contents

  • Preface
    • About the Author
  • The Story of OpenAI and ChatGPT
    • About This Guide
    • The Companion Toolkit
    • Stay Connected
  • How Does GPT Work?
  • Setting Up the Development Environment
    • Notes
    • Installing Python, pip, and a Virtual Development Environment
    • Obtain Your OpenAI API Keys
    • Install the Official Python Bindings
    • Test our API Keys
  • Understanding the Available Models and Which One to Use
    • OpenAI Available Models and Important Considerations
    • Which Model to Use?
    • OpenAI Model Series
      • GPT-4 Series
      • GPT-3.5 Series
      • InstructGPT-3 Series
      • Base GPT-3 Series
      • Codex Series
      • Content Filter
      • DALL-E Series
      • TTS Series
      • Whisper Model
      • Embedding Model
    • OpenAI Models and Pricing
    • What’s Next?
  • Using GPT Chat Completions
    • An Introductory Example
    • System, User, and Assistant Roles
      • The System Role
      • The User Role
      • The Assistant Role
    • Few-shot Learning with Chat Completions
    • Formatting the Output
    • Controlling the Output’s Token Count
    • Controlling When the Completion Output Stops
    • Temperature and Hallucination
    • Sampling with Top_p
    • Temperature vs Top_p: What’s the Difference? Which One Should I Use?
    • Streaming the API Response
    • Controlling Repetitiveness: Frequency and Presence Penalties
    • Frequency vs. Presence Penalty
    • Controlling the Number of Results from the API
    • Conclusion
  • Advanced Examples and Prompt Engineering
    • What is Prompt Engineering?
    • Few Shot Learning: A Key Prompt Engineering Technique
    • Overgeneration and Selection
    • General Knowledge Prompting (GKP): Generating a Rap Song
    • Context Stuffing: Is Apple a Fruit or a Company?
    • Dynamic Max Tokens
    • Creating an Interactive CLI-Based Assistant
    • What’s Next?
  • Embedding
    • What is an Embedding?
    • Use Cases: From Modern Search Engines to Self-Driving Cars
      • Tesla: How Embeddings Are Used in Self-Driving Cars
      • Kalendar AI: The Power of Embeddings in Sales Outreach
      • Notion: Enhanced Search Capabilities
      • DALL·E 2: Text-to-Image Conversion
    • Understanding Text Embedding
    • Embeddings for Multiple Inputs
    • Use case: Semantic Search
    • Cosine Similarity: A Deeper Look
    • Semantic Search and OpenAI’s Text Embeddings
    • Behind the Scenes: How Embeddings Work
  • Advanced Embedding Examples
    • Predicting Your Preferred Coffee
    • Creating a “Fuzzier” Search
    • Predicting News Category: Zero-Shot Classification with Embeddings
    • Evaluating the Accuracy of a Zero-Shot Classifier
    • Precision in Zero-Shot Classifier Applications: Examples
  • Fine-Tuning and Best Practices
    • Few-Shot Learning
    • Enhancing Few-Shot Learning
    • Practical Application of Fine-Tuning
    • Fine-Tuning Best Practices
      • Choosing the Model
      • Validating the Dataset
      • Token Limit
      • Dataset Size
      • Testing and Improving Training (Hyperparameters)
      • Epochs
        • Learning Rate Multiplier
      • Batch Size
      • Consider Estimated Costs
      • Dataset Quality
      • Combining Fine-Tuning with Other Techniques
      • Experiment and Learn
      • Use a Validation Set
      • Test the Model
      • Analyze the Results
  • Advanced Fine-Tuning: Mental Health Coach
    • Dataset Used in the Example
    • Preparing the Data
    • Using the Model in Real-World Applications and Challenges
  • Context & Memory: Making AI More Real
    • The Problem: No Memory
    • No Context = Chaos of Randomness and Confusion
    • History = Context
    • The Problem with Carrying Over History
    • Last In First Out (LIFO) Memory
    • The Problem with Last In, First Out Memory
    • Selective Context
  • Using a Vector Database with OpenAI
    • Introduction
    • What is a Vector Database?
    • Example 1: Using Weaviate to Make Our Model More Context-Aware
    • Example 2: Using Weaviate and OpenAI in Semantic Search
    • Example 3: Using Weaviate and OpenAI for Generative Search
  • Speech Recognition and Translation Using Whisper
    • What is Whisper?
    • How to Get Started?
    • Transcribe and Translate
    • Using Whisper SDK in Python
    • Using OpenAI Speech to Text API
      • Transcription API
      • Translation API
    • Improving Whisper Transcription
      • Cleaning the Audio
      • Using the Prompt Parameter
      • Post-Processing the Transcription
  • Text-to-Speech with OpenAI TTS Models
  • Autonomous AI-to-AI Discussion Using OpenAI, Weaviate, and AI Avatars
    • Generating the Audio Files
    • Using AI Avatar Models
    • What’s Next?
  • Afterword

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

80% Royalties. Earn $16 on a $20 book.

We pay 80% royalties. That's not a typo: you earn $16 on a $20 sale. If we sell 5000 non-refunded copies of your book or course for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earnedover $13 millionwriting, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub