Relative Content

Tag Archive for pythonartificial-intelligencelangchainlanggrah

Best way to stream using langchain `llm.with_structured_output`

import asyncio from langchain_core.pydantic_v1 import BaseModel, Field from langchain_openai import ChatOpenAI class Joke(BaseModel): setup: str = Field(description=”The setup of the joke”) punchline: str = Field(description=”The punchline to the joke”) # Define the model with structured output model = ChatOpenAI(model=”gpt-3.5-turbo-0125″, temperature=0, streaming=True) structured_llm = model.with_structured_output(Joke) def stream_joke(): # Invoke the model and stream the response response […]