Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lesson 2 - Update langchain parser function with a real function #22

Closed
aprilspeight opened this issue Feb 14, 2025 · 4 comments
Closed
Assignees

Comments

@aprilspeight
Copy link
Contributor

aprilspeight commented Feb 14, 2025

In 02-explore-agentic-frameworks, the Use Modular Components section provides a code snippet which insists that Parser is a function within langchain. However, there is no Parser function within the langchain package.

I'd recommend using one of the other real functions available (ex: PydanticOutputParser). However, this would also require modifying the code snippet - it becomes quite lengthy but can be run if someone is using Azure OpenAI.

import os
from langchain.output_parsers import PydanticOutputParser
from langchain.prompts import PromptTemplate
from langchain_openai import AzureChatOpenAI
from pydantic import BaseModel, Field
from dotenv import load_dotenv

# Load environment variables
load_dotenv()

# Define a Pydantic model for structured data
class FlightBooking(BaseModel):
    origin: str = Field(description="Departure city")
    destination: str = Field(description="Arrival city")
    date: str = Field(description="Flight date")

# Initialize parser
parser = PydanticOutputParser(pydantic_object=FlightBooking)

# Define the prompt template
prompt = PromptTemplate(
    template="Extract structured flight details from the following text:\n{text}\n{format_instructions}",
    input_variables=["text"],
    partial_variables={"format_instructions": parser.get_format_instructions()},
)

# Initialize Azure OpenAI LLM
llm = AzureChatOpenAI(
    azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
    openai_api_version=os.getenv("AZURE_OPENAI_API_VERSION"),
    deployment_name=os.getenv("AZURE_OPENAI_DEPLOYMENT_NAME"),
    openai_api_key=os.getenv("AZURE_OPENAI_API_KEY"),
    temperature=0
)

# Chain together the prompt, model, and parser
chain = prompt | llm | parser

# Example input
user_input = "Book a flight from New York to London on July 15th"

# Invoke the chain
parsed_data = chain.invoke({"text": user_input})

print(parsed_data)
# Output: origin='New York' destination='London' date='July 15th'
Copy link

👋 Thanks for contributing @aprilspeight! We will review the issue and get back to you soon.

@aprilspeight aprilspeight changed the title Update langchain parser function with a real function Lesson 2 - Update langchain parser function with a real function Feb 14, 2025
@koreyspace
Copy link
Contributor

I agree @aprilspeight - would love to see a PR on this one!

@aprilspeight
Copy link
Contributor Author

I’ll submit one on Tuesday and will close the issue after it’s submitted.

@softchris
Copy link
Contributor

Closing this as code has been changed (i.e SK example per discussion with Lee)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants