|
from dotenv import load_dotenv |
|
load_dotenv(); |
|
|
|
|
|
from langchain_community.utils.openai_functions import ( |
|
convert_pydantic_to_openai_function, |
|
) |
|
from langchain_core.prompts import ChatPromptTemplate |
|
from langchain_core.pydantic_v1 import BaseModel, Field, validator |
|
from langchain_openai import ChatOpenAI |
|
|
|
class Joke(BaseModel): |
|
"""Joke to tell user.""" |
|
|
|
setup: str = Field(description="question to set up a joke") |
|
punchline: str = Field(description="answer to resolve the joke") |
|
|
|
|
|
openai_functions = [convert_pydantic_to_openai_function(Joke)] |
|
model = ChatOpenAI(temperature=0) |
|
|
|
prompt = ChatPromptTemplate.from_messages( |
|
[("system", "You are helpful assistant"), ("user", "{input}")] |
|
) |
|
|
|
from langchain.output_parsers.openai_functions import JsonOutputFunctionsParser |
|
|
|
parser = JsonOutputFunctionsParser() |
|
chain = prompt | model.bind(functions=openai_functions) | parser |
|
print(chain.invoke({"input": "tell me a joke"})) |
|
|
|
|
|
|