Chain Exercise

!pip install langchain openai python-dotenv
Requirement already satisfied: langchain in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (0.0.300)
Requirement already satisfied: openai in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (0.28.0)
Requirement already satisfied: python-dotenv in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (1.0.0)
Requirement already satisfied: PyYAML>=5.3 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from langchain) (6.0.1)
Requirement already satisfied: SQLAlchemy<3,>=1.4 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from langchain) (1.4.49)
Requirement already satisfied: aiohttp<4.0.0,>=3.8.3 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from langchain) (3.8.5)
Requirement already satisfied: anyio<4.0 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from langchain) (3.7.1)
Requirement already satisfied: dataclasses-json<0.7,>=0.5.7 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from langchain) (0.6.0)
Requirement already satisfied: jsonpatch<2.0,>=1.33 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from langchain) (1.33)
Requirement already satisfied: langsmith<0.1.0,>=0.0.38 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from langchain) (0.0.40)
Requirement already satisfied: numexpr<3.0.0,>=2.8.4 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from langchain) (2.8.6)
Requirement already satisfied: numpy<2,>=1 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from langchain) (1.25.2)
Requirement already satisfied: pydantic<3,>=1 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from langchain) (2.3.0)
Requirement already satisfied: requests<3,>=2 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from langchain) (2.31.0)
Requirement already satisfied: tenacity<9.0.0,>=8.1.0 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from langchain) (8.2.3)
Requirement already satisfied: tqdm in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from openai) (4.64.1)
Requirement already satisfied: attrs>=17.3.0 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (23.1.0)
Requirement already satisfied: charset-normalizer<4.0,>=2.0 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (3.2.0)
Requirement already satisfied: multidict<7.0,>=4.5 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (6.0.4)
Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (4.0.3)
Requirement already satisfied: yarl<2.0,>=1.0 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.9.2)
Requirement already satisfied: frozenlist>=1.1.1 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.4.0)
Requirement already satisfied: aiosignal>=1.1.2 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.3.1)
Requirement already satisfied: idna>=2.8 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from anyio<4.0->langchain) (3.4)
Requirement already satisfied: sniffio>=1.1 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from anyio<4.0->langchain) (1.3.0)
Requirement already satisfied: marshmallow<4.0.0,>=3.18.0 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from dataclasses-json<0.7,>=0.5.7->langchain) (3.20.1)
Requirement already satisfied: typing-inspect<1,>=0.4.0 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from dataclasses-json<0.7,>=0.5.7->langchain) (0.9.0)
Requirement already satisfied: jsonpointer>=1.9 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from jsonpatch<2.0,>=1.33->langchain) (2.4)
Requirement already satisfied: annotated-types>=0.4.0 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from pydantic<3,>=1->langchain) (0.5.0)
Requirement already satisfied: pydantic-core==2.6.3 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from pydantic<3,>=1->langchain) (2.6.3)
Requirement already satisfied: typing-extensions>=4.6.1 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from pydantic<3,>=1->langchain) (4.7.1)
Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from requests<3,>=2->langchain) (2.0.4)
Requirement already satisfied: certifi>=2017.4.17 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from requests<3,>=2->langchain) (2023.7.22)
Requirement already satisfied: greenlet!=0.4.17 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from SQLAlchemy<3,>=1.4->langchain) (2.0.2)
Requirement already satisfied: packaging>=17.0 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from marshmallow<4.0.0,>=3.18.0->dataclasses-json<0.7,>=0.5.7->langchain) (23.1)
Requirement already satisfied: mypy-extensions>=0.3.0 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from typing-inspect<1,>=0.4.0->dataclasses-json<0.7,>=0.5.7->langchain) (1.0.0)
import os

from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv()) # read local .env file

%load_ext dotenv
%dotenv ./openai.env
from langchain.chat_models import ChatOpenAI
from langchain.prompts import ChatPromptTemplate

OpenAIModel = "gpt-4"
llm = ChatOpenAI(model=OpenAIModel, temperature=0.1)
# @title #### Student Identity
student_id = "REA2023A" # @param {type:"string"}
name = "Ricky" # @param {type:"string"}
assignment_id = "00_langchain-exercise"
# Intalling Libs
%pip install rggrader
Requirement already satisfied: rggrader in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (0.1.6)
Requirement already satisfied: requests in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from rggrader) (2.31.0)
Requirement already satisfied: pandas in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from rggrader) (2.1.0)
Requirement already satisfied: Pillow in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from rggrader) (10.0.0)
Requirement already satisfied: numpy>=1.23.2 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from pandas->rggrader) (1.25.2)
Requirement already satisfied: python-dateutil>=2.8.2 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from pandas->rggrader) (2.8.2)
Requirement already satisfied: pytz>=2020.1 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from pandas->rggrader) (2023.3.post1)
Requirement already satisfied: tzdata>=2022.1 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from pandas->rggrader) (2023.3)
Requirement already satisfied: charset-normalizer<4,>=2 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from requests->rggrader) (3.2.0)
Requirement already satisfied: idna<4,>=2.5 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from requests->rggrader) (3.4)
Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from requests->rggrader) (2.0.4)
Requirement already satisfied: certifi>=2017.4.17 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from requests->rggrader) (2023.7.22)
Requirement already satisfied: six>=1.5 in /Users/ruangguru/Projects/ai-bootcamp/env/lib/python3.11/site-packages (from python-dateutil>=2.8.2->pandas->rggrader) (1.16.0)
Note: you may need to restart the kernel to use updated packages.

Simple Chain

Let’s create a simple chain that will generate a list of movie titles where we can input the movie category and/or movie description.

# @title #### 01. Simple Chain Exercise
from rggrader import submit

# Put your code here:
from langchain.chains import LLMChain

prompt = ChatPromptTemplate.from_template(
    "What is the best movie title to describe \
    a movie category {category}?, please give 1 name in unorder list"
)

chain = LLMChain(llm=llm, prompt=prompt)

product = "Action Comedy featuring Three Mice Musketeers"
answer = chain.run(product)

print(answer)

# ---- End of your code ----

# Submit Method
question_id = "01_simple-langchain-exercise"
submit(student_id, name, assignment_id, str(answer), question_id)
1. "Furrious Adventures: The Three Mice Musketeers"
'Assignment successfully submitted'

Sequential Chain

Let’s create a sequential chain from our previous answer this time we will ask the LLM to create a movie synopsis based on the title. If your previous answer have more than one option, please change the code to return only one answer.

# @title #### 02. Sequential Chain Exercise
from rggrader import submit

# Put your code here:
from langchain.chains import SimpleSequentialChain

# prompt template 2
second_prompt = ChatPromptTemplate.from_template(
    "Write a 20 words synopsis for the following \
    movie title:{title}"
)


# chain 2
chain_two = LLMChain(llm=llm, prompt=second_prompt, verbose=True)

overall_simple_chain = SimpleSequentialChain(chains=[chain, chain_two],verbose=False)

answer = overall_simple_chain.run(product)
print(answer)

# ---- End of your code ----

# Submit Method
question_id = "02_sequential-langchain-exercise"
submit(student_id, name, assignment_id, str(answer), question_id)


> Entering new LLMChain chain...
Prompt after formatting:
Human: Write a 20 words synopsis for the following     movie title:1. "Furrious Furry: The Three Mice Musketeers"

> Finished chain.
Three brave mice embark on a thrilling adventure, using their wit and courage to save their kingdom from a menacing cat.
'Assignment successfully submitted'

Self Exploration

Can you create more complex chains ?

Back to top