Fun With OpenAI and Laravel
In this series, you'll get your feet wet with a variety of fun examples that demonstrate how to interact with OpenAI using PHP and Laravel. We'll begin by assuming that you know nothing, and then slowly work our way up one episode at a time!
Progress
Series Info
- Episodes
- 10
- Run Time
- 2h 41m
- Difficulty
- Intermediate
- Last Updated
- Feb 16, 2024
- Version
- Latest
Series Episodes
- Chatting With OpenAI (7)
Hello, AI
In this initial lesson, we'll get our first taste of OpenAI by using Laravel's HTTP client to make a POST request to the OpenAI Completions API. We explore how to send a request to compose a poem, handle the JSON response, and display the AI-generated poem as part of a web page.Keep the Conversation Going
In this lesson, I delve into how to continue the conversation with OpenAI. I demonstrate how to maintain the context of the conversation by storing each message in an array and sending the entire conversation history back to OpenAI with each new request. We'll also introduce a newChatclass to better organize our interaction with OpenAI.Chat with AI From the Command Line
In this lesson, I demonstrate how to enhance ourChatclass by installing theopenai-php/laravelpackage, which offers a cleaner way to handle API requests. We will then create an Artisan command to interact with OpenAI, allowing for a conversational chat directly from the terminal.Ask AI to Speak. Boom, Roasted!
In this next episode, we'll learn how to ask OpenAI to convert a text message into spoken audio. For fun, we'll create a form that requests a particular topic from the user. Once submitted, AI will sarcastically make fun of the given subject.AI-Generated Images
In this next example, we explore how to generate images with OpenAI. I demonstrate how to iterate on an initial idea by merging messages into a single prompt.Use AI to Detect Comment Spam
If we wish, we can use OpenAI to detect and alert us to potential spam from a forum or comments section. However, we'll need to specify that any response OpenAI returns is formatted as proper JSON.Place OpenAI Call Within a Validation Rule Class
Upon the completion of the previous episode, it occurred to me that we should probably wrap it up with a bow before moving. Let's move all of the spam checking logic into a dedicated validation rule class.
- Create Your Own Assistant (3)
Feed OpenAI Your Docs
In this new chapter, we'll move on to the Assistants API. Imagine that you've developed a new Composer package and want to provide an AI assistant for newcomers. There's only one problem: OpenAI doesn't yet know anything about your package. To solve this problem, we'll learn how to feed it our Markdown documentation files.Refactor Procedural Code Into Behaviors
Before we move on to something new, let's discuss how you might go about refactoring the procedural code from the previous episode into a simple and clean API.Bonus: Polymorphic AI
This next episode is supplementary, but I wanted to demonstrate how you might, when needed, allow for swapping out the underlying AI service. We will need to extract all OpenAI-specific code into a dedicated class, and declare an interface.
