|
|
|
from dotenv import load_dotenv |
|
load_dotenv() |
|
|
|
from langchain_core.output_parsers import StrOutputParser |
|
from langchain_core.prompts import ChatPromptTemplate |
|
from langchain_openai import ChatOpenAI |
|
|
|
from typing import List |
|
|
|
from langchain.prompts import PromptTemplate |
|
from langchain_core.output_parsers import JsonOutputParser |
|
from langchain_core.pydantic_v1 import BaseModel, Field |
|
from langchain_openai import ChatOpenAI |
|
|
|
|
|
|
|
class Joke(BaseModel): |
|
setup: str = Field(description="question to set up a joke") |
|
punchline: str = Field(description="answer to resolve the joke") |
|
|
|
parser = JsonOutputParser(pydantic_object=Joke) |
|
|
|
prompt = PromptTemplate( |
|
template="Answer the user query.\n{format_instructions}\n{query}\n", |
|
input_variables=["query"], |
|
partial_variables={"format_instructions": parser.get_format_instructions()}, |
|
) |
|
|
|
model = ChatOpenAI(model="gpt-4") |
|
output_parser = StrOutputParser() |
|
from langchain_core.output_parsers import JsonOutputParser |
|
|
|
|
|
chain = prompt | model | parser |
|
|
|
response=chain.invoke({"query": "ice cream"}) |
|
|
|
print(response) |
|
|
|
|