๐Ÿฆœ๐Ÿ•ธ๏ธ Using Composio With LangGraph

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

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()
3

Connect Your GitHub Account

You need to have an active GitHub Integration. Learn how to do this here
$composio login
>composio add github

Donโ€™t forget to set your COMPOSIO_API_KEY and OPENAI_API_KEY in your environment variables.

4

Get And Bind Tools

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

1tools = composio_toolset.get_tools(
2 apps=[App.GITHUB]
3)
4tool_node = ToolNode(tools)
5model = ChatOpenAI(temperature=0, streaming=True)
6model_with_tools = model.bind_tools(tools)
5

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]}
6

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__"
7

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()
8

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()
Built with