𧫠LangChain IMDB Example
For this section we will do a code example, on how to use LangChain. LangChain provides an interface to chain calls together and abstracts away much of the dirty work such as calling the OpenAI API, and chaining commands.
You'll see that LangChain is extremely simple, and crucially, helps users write prompts with outside data. For outside data we will use the IMDB Dataset.
Also if you
Importsβ
Here we will import the requests library to call the IMDB API. Requests is the easiest way to call external APIs.
Next we will make calls into LangChain. First we will grab the PromptTemplate, to write prompts. This gives us a few features, namely input variables, with which we can provide the API with truth. Then we will get the OpenAI LLM. This is the interface with OpenAI.
Finally we will grab the os library to set our API key to the environment.
import requests
from langchain.prompts import PromptTemplate
from langchain.llms import OpenAI
from langchain.chains import LLMChain
import os
os.environ['OPENAI_API_KEY']='YOUR_API_KEY_HERE'
Call APIβ
def get_movie_info(movie_title, appid):
"""Get movie data """
url = 'http://www.omdbapi.com/'
request = requests.get(url=url, params=dict(t=movie_title, apikey=appid))
return request.json()
movie_title = "The Other Guys"
appid = "YOUR_API_KEY"
movie_info = get_movie_info(movie_title, appid)
Templateβ
This is the template. It is where we establish our prompt, and actually do the hard work of prompt engineering. In this case, we used role prompting to establish that the model should act as a New York Times film critic, and gave it a humorous tone. If we had reviews we really liked, we could have passed them into the prompt to provide better results.
We also passed in critical information about the film, such as Movie, Plot, genre etc.. We grabbed all of this from the API.
At the bottom of the code you'll see the PromptTemplate. Here we define the prompt and the input variables.
template = """
You are a New York Times film critic. You have a humourous take on films that resonates with the audience. You write reviews based on the following information:
(Movie: {movie_title}), (Plot: {movie_plot}). (Genre: {movie_genre}), (Director: {movie_director}), (Actors: {movie_actors}), (Awards: {movie_awards}), (Box Office: {movie_box_office}), (Year: {movie_year}), (MPA Rating: {movie_mpa_rating})
Your Assignment is to write a review for the movie {movie_title} based on the information provided. The Rotten Tomatoes rating is: {rotten_tomatoes} and The IMDB rating is: {imdb_rating}, only use this information to determine sentiment, do not mention it in the review. You can use the information provided and some additional context, but do not spoil the film. End the reivew with a rating out of 10."""
prompt = PromptTemplate(template=template, input_variables=[
"movie_title", "movie_plot", "rotten_tomatoes", "imdb_rating", "movie_genre", "movie_director", "movie_actors", "movie_awards", "movie_box_office", "movie_year", "movie_mpa_rating"
]
)
Chainβ
This is where the magic happens. Here we create the Chain with LLMChain, this creates the object the entire process of putting the template together and calling OpenAI. Then we use the predict call to pass the data to the template and
llm_chain = LLMChain(prompt=prompt, llm=OpenAI(temperature=0.5), verbose=True)
prediction = llm_chain.predict(movie_title=movie_info['Title'], movie_plot=movie_info['Plot'], rotten_tomatoes=movie_info['Ratings'][1]['Value'], imdb_rating=movie_info['imdbRating'], movie_genre=movie_info['Genre'], movie_director=movie_info['Director'], movie_actors=movie_info['Actors'], movie_awards=movie_info['Awards'], movie_box_office=movie_info['BoxOffice'], movie_year=movie_info['Year'], movie_mpa_rating=movie_info['Rated'])
print(prediction)
Resultβ
The Result is pretty good! The text reads well and is pretty smooth throughout. We've passed the most important information to GPT and have now been able to give it ==truth.== This is the most important piece of putting AI into production use.
Adam McKayβs The Other Guys is an action-packed, laugh-out-loud comedy that follows two mismatched New York City detectives as they attempt to step up like the city's top cops, whom they idolize. Will Ferrell and Mark Wahlberg make a great comedic duo as they stumble their way through the case. The film also features an unexpected cameo from Derek Jeter.
The Other Guys is a wild ride of laughs and suspense. The plot is cleverly crafted and the action sequences are well-executed. The film is surprisingly poignant and thought-provoking, making it a great watch for both comedy and crime fans.
The Other Guys is an enjoyable and entertaining movie that is sure to please. With an impressive box office gross of $119,219,978, it is clear that the audience agrees. The Other Guys is rated PG-13 and is a great choice for a family movie night.
Overall, The Other Guys is an action-packed, laugh-out-loud comedy that is sure to leave you in stitches. It is a great film that is sure to please fans of comedy and crime alike. I give The Other Guys a rating of 8/10.