├── simple_chain.py ├── sequential_chain.py ├── conditional_chain.py └── parallel_chain.py /simple_chain.py: -------------------------------------------------------------------------------- 1 | from langchain_openai import ChatOpenAI 2 | from dotenv import load_dotenv 3 | from langchain_core.prompts import PromptTemplate 4 | from langchain_core.output_parsers import StrOutputParser 5 | 6 | load_dotenv() 7 | 8 | prompt = PromptTemplate( 9 | template='Generate 5 interesting facts about {topic}', 10 | input_variables=['topic'] 11 | ) 12 | 13 | model = ChatOpenAI() 14 | 15 | parser = StrOutputParser() 16 | 17 | chain = prompt | model | parser 18 | 19 | result = chain.invoke({'topic':'cricket'}) 20 | 21 | print(result) 22 | 23 | chain.get_graph().print_ascii() -------------------------------------------------------------------------------- /sequential_chain.py: -------------------------------------------------------------------------------- 1 | from langchain_openai import ChatOpenAI 2 | from dotenv import load_dotenv 3 | from langchain_core.prompts import PromptTemplate 4 | from langchain_core.output_parsers import StrOutputParser 5 | 6 | load_dotenv() 7 | 8 | prompt1 = PromptTemplate( 9 | template='Generate a detailed report on {topic}', 10 | input_variables=['topic'] 11 | ) 12 | 13 | prompt2 = PromptTemplate( 14 | template='Generate a 5 pointer summary from the following text \n {text}', 15 | input_variables=['text'] 16 | ) 17 | 18 | model = ChatOpenAI() 19 | 20 | parser = StrOutputParser() 21 | 22 | chain = prompt1 | model | parser | prompt2 | model | parser 23 | 24 | result = chain.invoke({'topic': 'Unemployment in India'}) 25 | 26 | print(result) 27 | 28 | chain.get_graph().print_ascii() -------------------------------------------------------------------------------- /conditional_chain.py: -------------------------------------------------------------------------------- 1 | from langchain_openai import ChatOpenAI 2 | from langchain_anthropic import ChatAnthropic 3 | from dotenv import load_dotenv 4 | from langchain_core.prompts import PromptTemplate 5 | from langchain_core.output_parsers import StrOutputParser 6 | from langchain.schema.runnable import RunnableParallel, RunnableBranch, RunnableLambda 7 | from langchain_core.output_parsers import PydanticOutputParser 8 | from pydantic import BaseModel, Field 9 | from typing import Literal 10 | 11 | load_dotenv() 12 | 13 | model = ChatOpenAI() 14 | 15 | parser = StrOutputParser() 16 | 17 | class Feedback(BaseModel): 18 | 19 | sentiment: Literal['positive', 'negative'] = Field(description='Give the sentiment of the feedback') 20 | 21 | parser2 = PydanticOutputParser(pydantic_object=Feedback) 22 | 23 | prompt1 = PromptTemplate( 24 | template='Classify the sentiment of the following feedback text into postive or negative \n {feedback} \n {format_instruction}', 25 | input_variables=['feedback'], 26 | partial_variables={'format_instruction':parser2.get_format_instructions()} 27 | ) 28 | 29 | classifier_chain = prompt1 | model | parser2 30 | 31 | prompt2 = PromptTemplate( 32 | template='Write an appropriate response to this positive feedback \n {feedback}', 33 | input_variables=['feedback'] 34 | ) 35 | 36 | prompt3 = PromptTemplate( 37 | template='Write an appropriate response to this negative feedback \n {feedback}', 38 | input_variables=['feedback'] 39 | ) 40 | 41 | branch_chain = RunnableBranch( 42 | (lambda x:x.sentiment == 'positive', prompt2 | model | parser), 43 | (lambda x:x.sentiment == 'negative', prompt3 | model | parser), 44 | RunnableLambda(lambda x: "could not find sentiment") 45 | ) 46 | 47 | chain = classifier_chain | branch_chain 48 | 49 | print(chain.invoke({'feedback': 'This is a beautiful phone'})) 50 | 51 | chain.get_graph().print_ascii() -------------------------------------------------------------------------------- /parallel_chain.py: -------------------------------------------------------------------------------- 1 | from langchain_openai import ChatOpenAI 2 | from langchain_anthropic import ChatAnthropic 3 | from dotenv import load_dotenv 4 | from langchain_core.prompts import PromptTemplate 5 | from langchain_core.output_parsers import StrOutputParser 6 | from langchain.schema.runnable import RunnableParallel 7 | 8 | load_dotenv() 9 | 10 | model1 = ChatOpenAI() 11 | 12 | model2 = ChatAnthropic(model_name='claude-3-7-sonnet-20250219') 13 | 14 | prompt1 = PromptTemplate( 15 | template='Generate short and simple notes from the following text \n {text}', 16 | input_variables=['text'] 17 | ) 18 | 19 | prompt2 = PromptTemplate( 20 | template='Generate 5 short question answers from the following text \n {text}', 21 | input_variables=['text'] 22 | ) 23 | 24 | prompt3 = PromptTemplate( 25 | template='Merge the provided notes and quiz into a single document \n notes -> {notes} and quiz -> {quiz}', 26 | input_variables=['notes', 'quiz'] 27 | ) 28 | 29 | parser = StrOutputParser() 30 | 31 | parallel_chain = RunnableParallel({ 32 | 'notes': prompt1 | model1 | parser, 33 | 'quiz': prompt2 | model2 | parser 34 | }) 35 | 36 | merge_chain = prompt3 | model1 | parser 37 | 38 | chain = parallel_chain | merge_chain 39 | 40 | text = """ 41 | Support vector machines (SVMs) are a set of supervised learning methods used for classification, regression and outliers detection. 42 | 43 | The advantages of support vector machines are: 44 | 45 | Effective in high dimensional spaces. 46 | 47 | Still effective in cases where number of dimensions is greater than the number of samples. 48 | 49 | Uses a subset of training points in the decision function (called support vectors), so it is also memory efficient. 50 | 51 | Versatile: different Kernel functions can be specified for the decision function. Common kernels are provided, but it is also possible to specify custom kernels. 52 | 53 | The disadvantages of support vector machines include: 54 | 55 | If the number of features is much greater than the number of samples, avoid over-fitting in choosing Kernel functions and regularization term is crucial. 56 | 57 | SVMs do not directly provide probability estimates, these are calculated using an expensive five-fold cross-validation (see Scores and probabilities, below). 58 | 59 | The support vector machines in scikit-learn support both dense (numpy.ndarray and convertible to that by numpy.asarray) and sparse (any scipy.sparse) sample vectors as input. However, to use an SVM to make predictions for sparse data, it must have been fit on such data. For optimal performance, use C-ordered numpy.ndarray (dense) or scipy.sparse.csr_matrix (sparse) with dtype=float64. 60 | """ 61 | 62 | result = chain.invoke({'text':text}) 63 | 64 | print(result) 65 | 66 | chain.get_graph().print_ascii() 67 | 68 | --------------------------------------------------------------------------------