Installing langgraph

Before you start work on this assignment you will need to install the langgraph package. To do this, run the following commands in a terminal pane.

conda activate agents
pip install langgraph

Writing a joke evaluator

For this assignment and the next we will pretend that we are setting up a web site for comedians. As part of the sign-up process for the site we will ask users to write a joke. We will be using an LLM to evaluate the quality of the joke, and if the joke is not good enough we will ask the user to keep trying until they can produce a decent joke.

The langgraph graph

To implement the joke evaluation logic we will be using langgraph, since langgraph makes it relatively easy to set up logical structures featuring loops and decisions.

Here is the graph we will be using for our application.

Here is what the nodes in this graph will do.

  1. The prompt node will prompt the user to enter a joke.
  2. The categorize node will ask the LLM to categorize the joke as offensive, perplexing, or funny.
  3. We will use a conditional edge to determine what to do next, based on the categorization the LLM provided.
  4. The rewrite node will send the joke back to the LLM and ask the LLM to rewrite it to make it more funny. That node will then tell the user that the LLM can do better and print the LLM's version of the joke.
  5. The good node will print a message congratulating the user on writing a good joke.

Hints and suggestions

Use the following dictionary for your graph's state:

class JokeState(TypedDict):
    joke: str
    classification: Optional[str]
    rewrite: Optional[str]

Here is how to write the function for the prompt node:

def prompt_for_joke(state: JokeState) -> JokeState:
    state['joke'] = input("Why did the chicken cross the road? ")
    return state