Developing AI Agents using LangGraph - 1
LangGraph.com

Developing AI Agents using LangGraph - 1

LangGraph allows to orchestrate multiple agents that are needed in an AI project. Each agent has a specific work to complete.

We also need a few libraries: LangSmith (developing, debugging and monitoring LLMs), LangChain (chains, agents, retrieval logics), LangChain Groq (LPU: an alternative to GPU), LangChain Community (integrated point to external LLMs like OpenAI, Anthropic, etc), Annotated, TypeDict, and of course LangGraph (to develop multi-agents and support state management). I will explain each one of them in the next article.

The following diagram depicts how these pieces work together though we don't need to use all of them all the time:

From LangChain.com

In LLM, a GPT project like Chatbot might have many sources of information to glean before making a combined output. AI cannot and will not produce a good result magically without having fed with good data to it.

The best way of learning a new one is drawing a parallel from what we know already. The following scenario had happened to someone that I knew. It took months to diagnose the root cause of it. Why? Because, a symptom can appear due to tens if not hundreds of reasons. Let us see how we could have achieved it much faster by using AI/ML with GPT.

Let us take an example. A patient brings up a Chatbot from their healthcare provider. She says, "I have fainted a couple of times in the last one week. What could be the reason?"

With the task in hand, let us deep dive what LangGraph is all about, then we will develop an agent to handle it.

During the development of an app, there two main features of LangGraph are important:

  1. State Management
  2. Orchestration

The state management is necessary so that all the agents could refer each other or the orchestrator within the same session. The orchestration is for controlling a workflow in which multiple agents can work together to accomplish a common goal.

It has the flexibility to develop an agent's logics and to define the communication protocols.

Scalability: Using LangGraph, we can develop very large scale applications. It means that we could have hundreds of agents with very complex workflows.

Fault tolerance: It supports fault tolerance so that if an agents runs into any issue would not bring down the entire app.

Another important subject if you have studied in college math that would come in handy is graph theory. It helps to understand the nodes, and their relationships. Here is a link to it: Graph Theory. Just browse through it.

For example, our Chatbot app will have this basic architecture:


This simple app has four nodes with well established relationships among them.

Let us dive into the coding.

We need two API keys one from Groq and another one from LangSmith. You can sign up with them and create the API keys and store them in the colab's secrets.

!pip install langgraph langsmith
!pip install langchain langchain_groq langchain_community

from google.colab import userdata
groq_api_key=userdata.get('groq_api_key')
langsmith=userdata.get('langsmith_api_key')

import os
os.environ["LANGCHAIN_API_KEY"] = langsmith
os.environ["LANGCHAIN_TRACING_V2"]="true"
os.environ["LANGCHAIN_PROJECT"]="Langgraph_SVR"

from langchain_groq import ChatGroq

llm=ChatGroq(groq_api_key=groq_api_key, model_name="Gemma2-9b-It")

from typing import Annotated
from typing_extensions import TypedDict
from langgraph.graph import StateGraph,START,END
from langgraph.graph.message import add_messages

class State(TypedDict):
  # Messages have the type "list". The `add_messages` function
    # in the annotation defines how this state key should be updated
    # (in this case, it appends messages to the list, rather than overwriting them)
  messages:Annotated[list, add_messages]

graph_builder=StateGraph(State)

graph_builder

def chatbot(state:State):
  return {"messages":llm.invoke(state['messages'])}

graph_builder.add_node("chatbot",chatbot)

graph_builder.add_edge(START,"chatbot")
graph_builder.add_edge("chatbot",END)

graph=graph_builder.compile()

from IPython.display import Image, display
try:
  display(Image(graph.get_graph().draw_mermaid_png()))
except Exception:
  pass

while True:
  user_input=input("Patient: ")
  if user_input.lower() in ["quit","q"]:
    print("Good Bye")
    break
  for event in graph.stream({'messages':("user",user_input)}):
    print(event.values())
    for value in event.values():
      print(value['messages'])
      print("Assistant:",value["messages"].content)
        

Let us run it on colab.

  • On the code section copy/paste the above code
  • Run it

The output should be:

Patient: I have fainted a couple of times in the last one week. What could be the reason?
dict_values([{'messages': AIMessage(content="I'm sorry to hear that you've fainted a couple of times. I understand this is concerning, but I am an AI and cannot provide medical advice. \n\nFainting, also known as syncope, can have many causes, some serious and some not. \n\n**It's important to see a doctor as soon as possible to determine the underlying cause of your fainting spells.** They can perform a physical exam, ask about your medical history, and order tests if needed.\n\nHere are some **possible reasons** for fainting, but this is not an exhaustive list:\n\n* **Dehydration:** Not drinking enough fluids can lead to low blood volume and fainting.\n* **Low blood sugar:** Skipping meals or not eating enough can cause your blood sugar to drop, leading to fainting.\n* **Orthostatic hypotension:** This occurs when your blood pressure drops suddenly when you stand up.\n* **Heart problems:** Certain heart conditions can cause fainting, such as arrhythmias or heart valve problems.\n* **Neurological conditions:** Some neurological conditions, such as seizures or migraines, can cause fainting.\n* **Anemia:** Low iron levels can lead to reduced oxygen carrying capacity in the blood, causing fainting.\n* **Medication side effects:** Certain medications can cause fainting as a side effect.\n\n**In the meantime, here are some things you can do:**\n\n* **Stay hydrated:** Drink plenty of fluids, especially water.\n* **Eat regular meals:** Don't skip meals and make sure to include healthy snacks throughout the day.\n* **Get up slowly:** When standing up from a sitting or lying position, do so slowly to avoid a sudden drop in blood pressure.\n* **Avoid triggers:** If you know what triggers your fainting spells, try to avoid them.\n* **Wear comfortable clothing:** Loose-fitting clothing can help improve blood flow.\n\n**Please remember:** This information is not a substitute for professional medical advice. If you are experiencing fainting spells, it's essential to see a doctor to determine the cause and receive appropriate treatment.\n", response_metadata={'token_usage': {'completion_tokens': 426, 'prompt_tokens': 28, 'total_tokens': 454, 'completion_time': 0.774545455, 'prompt_time': 0.000420529, 'queue_time': 0.014398590000000001, 'total_time': 0.774965984}, 'model_name': 'Gemma2-9b-It', 'system_fingerprint': 'fp_10c08bf97d', 'finish_reason': 'stop', 'logprobs': None}, id='run-7b7899ae-32c0-4dda-a135-a815d3890888-0', usage_metadata={'input_tokens': 28, 'output_tokens': 426, 'total_tokens': 454})}])
content="I'm sorry to hear that you've fainted a couple of times. I understand this is concerning, but I am an AI and cannot provide medical advice. \n\nFainting, also known as syncope, can have many causes, some serious and some not. \n\n**It's important to see a doctor as soon as possible to determine the underlying cause of your fainting spells.** They can perform a physical exam, ask about your medical history, and order tests if needed.\n\nHere are some **possible reasons** for fainting, but this is not an exhaustive list:\n\n* **Dehydration:** Not drinking enough fluids can lead to low blood volume and fainting.\n* **Low blood sugar:** Skipping meals or not eating enough can cause your blood sugar to drop, leading to fainting.\n* **Orthostatic hypotension:** This occurs when your blood pressure drops suddenly when you stand up.\n* **Heart problems:** Certain heart conditions can cause fainting, such as arrhythmias or heart valve problems.\n* **Neurological conditions:** Some neurological conditions, such as seizures or migraines, can cause fainting.\n* **Anemia:** Low iron levels can lead to reduced oxygen carrying capacity in the blood, causing fainting.\n* **Medication side effects:** Certain medications can cause fainting as a side effect.\n\n**In the meantime, here are some things you can do:**\n\n* **Stay hydrated:** Drink plenty of fluids, especially water.\n* **Eat regular meals:** Don't skip meals and make sure to include healthy snacks throughout the day.\n* **Get up slowly:** When standing up from a sitting or lying position, do so slowly to avoid a sudden drop in blood pressure.\n* **Avoid triggers:** If you know what triggers your fainting spells, try to avoid them.\n* **Wear comfortable clothing:** Loose-fitting clothing can help improve blood flow.\n\n**Please remember:** This information is not a substitute for professional medical advice. If you are experiencing fainting spells, it's essential to see a doctor to determine the cause and receive appropriate treatment.\n" response_metadata={'token_usage': {'completion_tokens': 426, 'prompt_tokens': 28, 'total_tokens': 454, 'completion_time': 0.774545455, 'prompt_time': 0.000420529, 'queue_time': 0.014398590000000001, 'total_time': 0.774965984}, 'model_name': 'Gemma2-9b-It', 'system_fingerprint': 'fp_10c08bf97d', 'finish_reason': 'stop', 'logprobs': None} id='run-7b7899ae-32c0-4dda-a135-a815d3890888-0' usage_metadata={'input_tokens': 28, 'output_tokens': 426, 'total_tokens': 454}
Assistant: I'm sorry to hear that you've fainted a couple of times. I understand this is concerning, but I am an AI and cannot provide medical advice. 

Fainting, also known as syncope, can have many causes, some serious and some not. 

**It's important to see a doctor as soon as possible to determine the underlying cause of your fainting spells.** They can perform a physical exam, ask about your medical history, and order tests if needed.

Here are some **possible reasons** for fainting, but this is not an exhaustive list:

* **Dehydration:** Not drinking enough fluids can lead to low blood volume and fainting.
* **Low blood sugar:** Skipping meals or not eating enough can cause your blood sugar to drop, leading to fainting.
* **Orthostatic hypotension:** This occurs when your blood pressure drops suddenly when you stand up.
* **Heart problems:** Certain heart conditions can cause fainting, such as arrhythmias or heart valve problems.
* **Neurological conditions:** Some neurological conditions, such as seizures or migraines, can cause fainting.
* **Anemia:** Low iron levels can lead to reduced oxygen carrying capacity in the blood, causing fainting.
* **Medication side effects:** Certain medications can cause fainting as a side effect.

**In the meantime, here are some things you can do:**

* **Stay hydrated:** Drink plenty of fluids, especially water.
* **Eat regular meals:** Don't skip meals and make sure to include healthy snacks throughout the day.
* **Get up slowly:** When standing up from a sitting or lying position, do so slowly to avoid a sudden drop in blood pressure.
* **Avoid triggers:** If you know what triggers your fainting spells, try to avoid them.
* **Wear comfortable clothing:** Loose-fitting clothing can help improve blood flow.

**Please remember:** This information is not a substitute for professional medical advice. If you are experiencing fainting spells, it's essential to see a doctor to determine the cause and receive appropriate treatment.        

Yahoo! We have completed in developing an agent successfully. It uses one of the LLMs from Groq. Please note that though the patient has got the answer, there are two things that would not make them happy. The first one is too many causes. The second one is the final disclaimer paragraph.

How can we narrow it down further to pinpoint the problem? Without having access to the health history of the patient, it is very unlikely that we would be able to answer it. That is where enterprise dataset, vector embeddings, vector database and RAG come into the picture. In the next article, I will cover how to use them.


要查看或添加评论,请登录

社区洞察

其他会员也浏览了