From the course: Build with AI: Building a Project with the ChatGPT API
Solution: Enhance the movie script generator with Threads
From the course: Build with AI: Building a Project with the ChatGPT API
Solution: Enhance the movie script generator with Threads
(upbeat music) - [Instructor] Let me show you how I solved the challenge to update the movie script generator with conversation threads. I've navigated to the Jupiter notebook. Let me walk you through the first section. This is where I'm importing the necessary libraries that I need so that my code functions correctly. The next step, on line five, I am loading my API key from a local environment file and storing that key in api_key. Line 10, I'm raising a ValueError, if that key does not exist. This section of the solution should look familiar to you. We've seen this code before. This is where I create a tool for the movie plot generator. I import the necessary libraries like agent, runner, function tool and trace. Lines nine through 14 show the data class called PlotInfo. Line 16 through 17 show the function tool or the generate plot twist function that generates that plot twist by pulling a random value from this twist array. This next section is where I create the agent and register the tool. The name of my agent is movie plot generator. I give that agent very detailed instructions. You are a creative writing assistant who writes movie plots. If the user mentions a twist or a surprise, call that generate plot twist function and do not add a plot twist if the user doesn't mention one. Line 14, this is where I register the tool with the agent. And now this section here, the main function showcases conversation threads, and a thread is like a conversation container. It stores every user and assistant message, so that context is preserved across turns. On line 14, I am creating this unique conversation ID. This will hold the conversations. On line six, I'm adding a trace. I'm giving this workflow a name called conversation and notice for the group ID, I'm passing in this conversation ID that I just created. If you notice there are three conversation turns that will use the same conversation thread. The first, I'm storing end result, and the question I'm asking the agent is, what are your capabilities? I'm printing that result to the screen. The second turn in the conversation, continuing within the same thread, I'm asking the agent to write a sci-fi movie plot with a twist at the end. I'm storing that result in this new_input variable. I'm sending that and then I'm printing the response back. The third and final turn, continuing within that same conversation thread, I'm sending to the agent, 'I don't like this movie plot. Please start over and use a genre of comedy. No need for a plot twist.' I'm storing that value and new_input. I'm sending that to the movie plot agent, storing the result and then printing the result to the screen. Here, I'm kicking off that main function. The first question is, what are your capabilities? And let's look at the response? The agent returns, in the first conversation thread, 'I can craft original movie plots, and if you request, I can also add intriguing plot twist to make the story more exciting. Just let me know what kind of plot you're interested in.' The second turn in that conversation is write a sci-fi movie plot with a twist at the end. Here the agent includes the plot twist. 'It's the year 2145. Humanity has perfected time travel, but strictly regulates its use. The protagonist is Dr. Elara Voss. She is a rebellious physicist who secretly builds a time device.' This movie plot sounds like something I've seen on the Hollywood screens before. 'Her ambition drives her to explore agent earth's mysteries. And during her unauthorized journey to the year 2020, she encounters Alex, a young historian.' The story goes on to tell more about their journey together through time. And now here's the plot twist. 'In a shocking discovery, Elara learns that Alex is not who he seems. Beneath his friendly exterior, Alex is a mastermind from the future, orchestrating every move. His true goal is to destabilize time for his purposes using Elara's genius and emotions to achieve it.' Definitely didn't see that plot twist coming. That was the second turn in the conversation. And the last turn is, 'I don't like this movie plot. Please start over and use the genre of comedy. No need for a plot twist.' Okay, and so the new comedy movie plot, as I read through this, friends discover a dusty old time machine in the basement of their favorite local pub. This definitely sounds like a Hollywood movie that I've seen before. It goes on to talk about Barney and Lucy and Max. They had their first accidental excursion to the Renaissance. Barney becomes a muse for Leonardo da Vinci. And as I continue to read on, there is no plot twist as requested. Now you see the agent has woven this magical story, and that's just the power of conversation threads. It lets the agent remember and build on what was said before. If your solution looked different, that's totally fine. What matters is that you use the same thread ID or conversation ID across turns, passed in multiple messages and saw the assistant adapt its responses based on prior inputs. In the next chapter, we'll explore how to customize models using fine tuning so that they align more closely with your specific tone or data needs. But for now, you've added memory to your chatbot and taken a big step forward in building a conversational AI agent.