AI Agents with LangGraph

Nov 12 2024 · Python 3.12, LangGraph 0.2.x, JupyterLab 4.2.4

Lesson 04: Enhancing Agent Capabilities

Localizer Project Demo

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In this demo, you’ll add memory, structured output, and human-in-the-loop interaction to the localizer project that you’ve been working on. Open the localizer.ipynb notebook in the Starter folder. You’ll find it in the same state as you left it at the end of the last lesson.

app_strings = read_yaml_file('save_puppy.yaml')
screenshot = encode_image('save_puppy.png')
class State(TypedDict):
  messages: Annotated[Sequence[BaseMessage], operator.add]
  contextualized: str
  translation: str
  advice: str
prompt = f"""You are an expert in mobile app UI/UX and also
cross-cultural communication.A translator has submitted a translation
for the strings of a UI layout. Check the translation for accuracy.
If the translation is good, reply with one word: "good". However, if
there is anything that is ambiguous or might be translated wrong,
reply with a paragraph expressing your concern.
{app_strings}
Here is the translation with contextual comments:
{translation}
"""
def translation_good(state):
  advice = state["advice"]
  print(f"translation: \n{state["translation"]}\n")
  print(f"advice: {advice}")
  if advice == "good":
    return "good"
  else:
    return "problem"
def human_review(state):
  return state
prompt = f"""The text below is a translation in YAML format.
  The first step is to remove the comments.
  The second step is to convert it to format the key-value pairs
  in a .strings file for iOS.
  Don't change the key names or the translated text at all.
  Don't make any commentary. Just give the output.
  Here is the text:

  {translation}
  """
graph.add_node("human_review", human_review)
graph.add_edge("human_review", "formatter")
graph.add_conditional_edges(
  "checker",
  translation_good,
  {
    "good": "formatter",
    "problem": "human_review"
  }
)
from langgraph.checkpoint.memory import MemorySaver

memory = MemorySaver()
app = graph.compile(checkpointer=memory, interrupt_before=["human_review"])
thread = {"configurable": {"thread_id": "1"}}
response = app.invoke(state, thread)
The translation for the "positive_button" as "Guardar" may cause confusion.
"Guardar" typically means "to save" in the context of storing or keeping
something, like a file, rather than rescuing or saving a life. A more
appropriate translation that conveys the idea of rescuing the puppy would
be "Salvar." This would ensure the intended meaning is clear to users.
user_input = input("Update any line that needs changing: ")
positive_button: Salvar
import re

key = user_input.split(":")[0]
pattern = rf"^{key}:.*$"
translation = app.get_state(thread).values["translation"]
updated = re.sub(pattern, user_input, translation, flags=re.MULTILINE)
app.update_state(thread, {"translation": updated})
result = app.invoke(None, thread)
formatted = app.get_state(thread).values["messages"][-1].content
print(formatted)
See forum comments
Cinema mode Download course materials from Github
Previous: Localizer Project Next: Conclusion