├── stroutputparser1.py ├── jsonoutputparser.py ├── stroutputparser.py ├── structuredoutputparser.py └── pydanticoutputparser.py /stroutputparser1.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 | 9 | model = ChatOpenAI() 10 | 11 | # 1st prompt -> detailed report 12 | template1 = PromptTemplate( 13 | template='Write a detailed report on {topic}', 14 | input_variables=['topic'] 15 | ) 16 | 17 | # 2nd prompt -> summary 18 | template2 = PromptTemplate( 19 | template='Write a 5 line summary on the following text. /n {text}', 20 | input_variables=['text'] 21 | ) 22 | 23 | parser = StrOutputParser() 24 | 25 | chain = template1 | model | parser | template2 | model | parser 26 | 27 | result = chain.invoke({'topic':'black hole'}) 28 | 29 | print(result) 30 | 31 | -------------------------------------------------------------------------------- /jsonoutputparser.py: -------------------------------------------------------------------------------- 1 | from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint 2 | from dotenv import load_dotenv 3 | from langchain_core.prompts import PromptTemplate 4 | from langchain_core.output_parsers import JsonOutputParser 5 | 6 | load_dotenv() 7 | 8 | # Define the model 9 | llm = HuggingFaceEndpoint( 10 | repo_id="google/gemma-2-2b-it", 11 | task="text-generation" 12 | ) 13 | 14 | model = ChatHuggingFace(llm=llm) 15 | 16 | parser = JsonOutputParser() 17 | 18 | template = PromptTemplate( 19 | template='Give me 5 facts about {topic} \n {format_instruction}', 20 | input_variables=['topic'], 21 | partial_variables={'format_instruction': parser.get_format_instructions()} 22 | ) 23 | 24 | chain = template | model | parser 25 | 26 | result = chain.invoke({'topic':'black hole'}) 27 | 28 | print(result) 29 | 30 | -------------------------------------------------------------------------------- /stroutputparser.py: -------------------------------------------------------------------------------- 1 | from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint 2 | from dotenv import load_dotenv 3 | from langchain_core.prompts import PromptTemplate 4 | 5 | load_dotenv() 6 | 7 | llm = HuggingFaceEndpoint( 8 | repo_id="google/gemma-2-2b-it", 9 | task="text-generation" 10 | ) 11 | 12 | model = ChatHuggingFace(llm=llm) 13 | 14 | # 1st prompt -> detailed report 15 | template1 = PromptTemplate( 16 | template='Write a detailed report on {topic}', 17 | input_variables=['topic'] 18 | ) 19 | 20 | # 2nd prompt -> summary 21 | template2 = PromptTemplate( 22 | template='Write a 5 line summary on the following text. /n {text}', 23 | input_variables=['text'] 24 | ) 25 | 26 | prompt1 = template1.invoke({'topic':'black hole'}) 27 | 28 | result = model.invoke(prompt1) 29 | 30 | prompt2 = template2.invoke({'text':result.content}) 31 | 32 | result1 = model.invoke(prompt2) 33 | 34 | print(result1.content) 35 | 36 | -------------------------------------------------------------------------------- /structuredoutputparser.py: -------------------------------------------------------------------------------- 1 | from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint 2 | from dotenv import load_dotenv 3 | from langchain_core.prompts import PromptTemplate 4 | from langchain.output_parsers import StructuredOutputParser, ResponseSchema 5 | 6 | load_dotenv() 7 | 8 | # Define the model 9 | llm = HuggingFaceEndpoint( 10 | repo_id="google/gemma-2-2b-it", 11 | task="text-generation" 12 | ) 13 | 14 | model = ChatHuggingFace(llm=llm) 15 | 16 | schema = [ 17 | ResponseSchema(name='fact_1', description='Fact 1 about the topic'), 18 | ResponseSchema(name='fact_2', description='Fact 2 about the topic'), 19 | ResponseSchema(name='fact_3', description='Fact 3 about the topic'), 20 | ] 21 | 22 | parser = StructuredOutputParser.from_response_schemas(schema) 23 | 24 | template = PromptTemplate( 25 | template='Give 3 fact about {topic} \n {format_instruction}', 26 | input_variables=['topic'], 27 | partial_variables={'format_instruction':parser.get_format_instructions()} 28 | ) 29 | 30 | chain = template | model | parser 31 | 32 | result = chain.invoke({'topic':'black hole'}) 33 | 34 | print(result) -------------------------------------------------------------------------------- /pydanticoutputparser.py: -------------------------------------------------------------------------------- 1 | from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint 2 | from dotenv import load_dotenv 3 | from langchain_core.prompts import PromptTemplate 4 | from langchain_core.output_parsers import PydanticOutputParser 5 | from pydantic import BaseModel, Field 6 | 7 | load_dotenv() 8 | 9 | # Define the model 10 | llm = HuggingFaceEndpoint( 11 | repo_id="google/gemma-2-2b-it", 12 | task="text-generation" 13 | ) 14 | 15 | model = ChatHuggingFace(llm=llm) 16 | 17 | class Person(BaseModel): 18 | 19 | name: str = Field(description='Name of the person') 20 | age: int = Field(gt=18, description='Age of the person') 21 | city: str = Field(description='Name of the city the person belongs to') 22 | 23 | parser = PydanticOutputParser(pydantic_object=Person) 24 | 25 | template = PromptTemplate( 26 | template='Generate the name, age and city of a fictional {place} person \n {format_instruction}', 27 | input_variables=['place'], 28 | partial_variables={'format_instruction':parser.get_format_instructions()} 29 | ) 30 | 31 | chain = template | model | parser 32 | 33 | final_result = chain.invoke({'place':'sri lankan'}) 34 | 35 | print(final_result) 36 | --------------------------------------------------------------------------------