Files
LangChainPractice/chapter3/p66_chatbot.py
T

28 lines
908 B
Python

# from langchain_core.output_parsers import StructuredOutputParser, ResponseSchema
from langchain import PromptTemplate, LLMChain
from langchain_ollama import OllamaLLM
from pydantic import BaseModel, Field
from langchain_core.prompts import ChatPromptTemplate
class Response(BaseModel):
answer: str = Field(description="The answer to the question")
source: str = Field(description="The source of the answer")
# Example usage with llm
llm = OllamaLLM(model="llama3.2", base_url="http://studio1.dorceus.local:11434" )
structured_llm = llm.with_structured_output(Response)
template = """
You are an exeprt in answering questions about a pizza restaurant
Here are some relevant reviews: {reviews}
Here is the question to answer: {question}
"""
prompt = ChatPromptTemplate.from_template(template)
chain = prompt | structured_llm
result = chain.invoke({"reviews": reviews, "question": question})