|
| 1 | +from typing import TypedDict |
| 2 | + |
| 3 | +from IPython.display import Image, display |
1 | 4 | from langchain.prompts import ChatPromptTemplate |
2 | 5 | from langchain_openai import ChatOpenAI |
| 6 | +from langgraph.graph import END, StateGraph |
| 7 | + |
| 8 | +# EXAMPLE LANGCHAIN |
| 9 | +print("Example: Create a haiku with LangChain:\n\n") |
3 | 10 |
|
4 | 11 | llm = ChatOpenAI(temperature=0.9, model="gpt-4o-mini") |
5 | 12 | prompt = ChatPromptTemplate.from_template("Create a haiku from:\n\n{content}") |
6 | 13 | chain = prompt | llm |
7 | 14 |
|
8 | 15 | response = chain.invoke({"content": "The quick brown fox jumps over the lazy dog."}) |
9 | 16 | print(response.content) |
| 17 | + |
| 18 | +# EXAMPLE LANGGRAPH |
| 19 | +print( |
| 20 | + "\n\nExample: Determine if prompt is discussing colors or not with LangGraph:\n\n" |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +# Define the state of our graph |
| 25 | +class State(TypedDict): |
| 26 | + prompt: str |
| 27 | + is_color: bool |
| 28 | + result: str |
| 29 | + |
| 30 | + |
| 31 | +# Define our nodes |
| 32 | +def ask_for_prompt(state: State) -> State: |
| 33 | + prompt = input("Please enter a prompt to indentify if it mentions a color or not: ") |
| 34 | + return {"prompt": prompt, "is_color": False, "result": ""} |
| 35 | + |
| 36 | + |
| 37 | +def evaluate_prompt(state: State) -> State: |
| 38 | + prompt = state["prompt"] |
| 39 | + is_color = False |
| 40 | + |
| 41 | + llm = ChatOpenAI() |
| 42 | + prompt = ChatPromptTemplate.from_template( |
| 43 | + "Evaluate if this prompt discusses colors: '{prompt}'. Respond with 'Yes' or 'No'." |
| 44 | + ) |
| 45 | + response = llm.invoke(prompt.format_prompt(prompt=state["prompt"])) |
| 46 | + if isinstance(response.content, str): |
| 47 | + is_color = response.content.strip().lower() == "yes" |
| 48 | + return {**state, "is_color": is_color} |
| 49 | + |
| 50 | + |
| 51 | +def show_result(state: State) -> State: |
| 52 | + if state["is_color"]: |
| 53 | + result = f"The prompt '{state['prompt']}' contains a color!" |
| 54 | + else: |
| 55 | + result = f"The prompt '{state['prompt']}' does not contain a color." |
| 56 | + print(result) |
| 57 | + return {**state, "result": result} |
| 58 | + |
| 59 | + |
| 60 | +# Create the graph |
| 61 | +workflow = StateGraph(State) |
| 62 | + |
| 63 | +# Add nodes |
| 64 | +workflow.add_node("ask_prompt", ask_for_prompt) |
| 65 | +workflow.add_node("evaluate", evaluate_prompt) |
| 66 | +workflow.add_node("show_result", show_result) |
| 67 | + |
| 68 | +# Add edges |
| 69 | +workflow.add_edge("ask_prompt", "evaluate") |
| 70 | +workflow.add_edge("evaluate", "show_result") |
| 71 | +workflow.add_edge("show_result", END) |
| 72 | + |
| 73 | +# Set entry point |
| 74 | +workflow.set_entry_point("ask_prompt") |
| 75 | + |
| 76 | +# Compile the graph |
| 77 | +app = workflow.compile() |
| 78 | + |
| 79 | +display(Image(app.get_graph().draw_png())) # type: ignore |
| 80 | + |
| 81 | +# Run the graph |
| 82 | +for output in app.stream({"prompt": "", "is_color": False, "result": ""}): |
| 83 | + pass |
| 84 | + |
| 85 | +# Example usage: |
| 86 | +# This will run until a color is mentioned in the prompt |
0 commit comments