09. Custom generator

Generator function (ie yield Functions that use keywords and act like an emulator) can be used in the LCEL pipeline.

The signature of this generator Iterator[Input] -> Iterator[Output] Should be. In the case of asynchronous generators AsyncIterator[Input] -> AsyncIterator[Output] is.

This is useful for:

  • Custom output parser implementation

  • Maintain streaming while modifying output from previous steps

In the example below, we will implement a custom output parser for comma-separated lists.

Copy

%pip install -qU langchain langchain-openai

Copy

from typing import Iterator, List

from langchain.prompts.chat import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_openai import ChatOpenAI

prompt = ChatPromptTemplate.from_template(
    # List five companies similar to the given company, separated by commas.
    "Write a comma-separated list of 5 companies similar to: {company}"
)
# Initialize the ChatOpenAI model by setting the temperature to 0.0.
model = ChatOpenAI(temperature=0.0, model="gpt-4-turbo-preview")

# Create a chain by connecting prompts and models and applying a string output parser.
str_chain = prompt | model | StrOutputParser()

Chain below Stream Run with and output the result. Results are generated in real time.

Copy

Copy

invoke() Check the run results.

Copy

Copy

split_into_list The function takes the emulator of the LLM token as input and returns the emulator of the comma-separated string list.

  • The last chunk yield Returns by (generator).

Copy

  • str_chain Pipe the string of the variable ( | ) Using operators split_into_list Pass to the function.

  • split_into_list The function serves to split the string into list.

  • Split list list_chain Assigned to variables.

Copy

  • list_chain Object stream Input data by calling a method {"animal": "bear"} Generate output for.

  • stream The method returns the output in chunk units, and each chunk is processed through repeated statements.

  • Each chunk print It is output immediately using functions, flush=True This output takes place immediately without buffering through the option.

This code list_chain It shows the process of generating output for input data using, processing output in chunks and outputting immediately.

Copy

Copy

this time list_chain on invoke() Inject data into. Let's see if it works without problems.

Copy

Copy

Asynchronous

asplit_into_list Functions are asynchronous generator ( AsyncIterator[str] ) Asynchronous generator of string list ( AsyncIterator[List[str]] ).

Copy

Stream using asynchronous functions.

Copy

Copy

Invoke the data to the asynchronous chain to make sure it works without problems.

Copy

Copy

Last updated