🦜🕸️ Using Composio With LangGraph

Integrate Composio with LangGraph agents to let them seamlessly interact with external apps

Star A Repository on Github

In this example, we will use LangGraph Agent to star a repository on Github using Composio Tools

1

Install Packages

$pip install composio-langgraph
2

Import Libraries & Initialize ComposioToolSet & LLM

You can get all the tools for a given app as shown below, but you can get specific actions and filter actions using usecase & tags. Learn more here

1from typing import Literal
2from langchain_openai import ChatOpenAI
3from langgraph.graph import MessagesState, StateGraph
4from langgraph.prebuilt import ToolNode
5from composio_langgraph import Action, ComposioToolSet, App
6
7composio_toolset = ComposioToolSet()
8tools = composio_toolset.get_tools(
9 apps=[App.GITHUB]
10)
11
12tool_node = ToolNode(tools)
13
14model = ChatOpenAI(temperature=0, streaming=True)
15model_with_tools = model.bind_tools(tools)
3

Define the model calling function

1def call_model(state: MessagesState):
2 """
3 Process messages through the LLM and return the response
4 """
5 messages = state["messages"]
6 response = model_with_tools.invoke(messages)
7 return {"messages": [response]}
4

Define the decision function for workflow routing

1def should_continue(state: MessagesState) -> Literal["tools", "__end__"]:
2 """
3 Determine if the conversation should continue to tools or end
4 Returns:
5 - "tools" if the last message contains tool calls
6 - "__end__" otherwise
7 """
8 messages = state["messages"]
9 last_message = messages[-1]
10 if last_message.tool_calls:
11 return "tools"
12 return "__end__"
5

Define the workflow graph

1workflow = StateGraph(MessagesState)
2
3workflow.add_node("agent", call_model)
4workflow.add_node("tools", tool_node)
5workflow.add_edge("__start__", "agent")
6workflow.add_conditional_edges(
7 "agent",
8 should_continue,
9)
10workflow.add_edge("tools", "agent")
11
12app = workflow.compile()
6

Execute the workflow

1for chunk in app.stream(
2 {
3 "messages": [
4 (
5 "human",
6 "Star the Github Repository composiohq/composio",
7 )
8 ]
9 },
10 stream_mode="values",
11):
12 chunk["messages"][-1].pretty_print()