Skip to main content

Use Composio to get array of tools with LangGraph Agent Workflows

Project description

🦜🕸️ Using Composio With LangGraph

Integrate Composio with LangGraph Agentic workflows & enable them to interact seamlessly with external apps, enhancing their functionality and reach.

Goal

  • Star a repository on GitHub using natural language commands through a LangGraph Agent.

Installation and Setup

Ensure you have the necessary packages installed and connect your GitHub account to allow your agents to utilize GitHub functionalities.

# Install Composio LangGraph package
pip install composio-langgraph

# Connect your GitHub account
composio-cli add github

# View available applications you can connect with
composio-cli show-apps

Usage Steps

1. Import Base Packages

Prepare your environment by initializing necessary imports from LangGraph & LangChain for setting up your agent.

from typing import Literal

from langchain_openai import ChatOpenAI
from langgraph.graph import MessagesState, StateGraph
from langgraph.prebuilt import ToolNode

2. Fetch GitHub LangGraph Tools via Composio

Access GitHub tools provided by Composio for LangGraph, initialize a ToolNode with necessary tools obtaned from ComposioToolSet.

from composio_langgraph import Action, ComposioToolSet

# Initialize the toolset for GitHub
composio_toolset = ComposioToolSet()
tools = composio_toolset.get_actions(
    actions=[
        Action.GITHUB_ACTIVITY_STAR_REPO_FOR_AUTHENTICATED_USER,
        Action.GITHUB_USERS_GET_AUTHENTICATED,
    ])
tool_node = ToolNode(tools)

3. Prepare the model

Initialize the LLM class and bind obtained tools to the model.

model = ChatOpenAI(temperature=0, streaming=True)
model_with_tools = model.bind_tools(functions)

4. Define the Graph Nodes

LangGraph expects you to define different nodes of the agentic workflow as separate functions. Here we define a node for calling the LLM model.

def call_model(state: MessagesState):
    messages = state["messages"]
    response = model_with_tools.invoke(messages)
    return {"messages": [response]}

5. Define the Graph Nodes and Edges

To establish the agent's workflow, we begin by initializing the workflow with agent and tools node, followed by specifying the connecting edges between nodes, finally compiling the workflow. These edges can be straightforward or conditional, depending on the workflow requirements.

def should_continue(state: MessagesState) -> Literal["tools", "__end__"]:
    messages = state["messages"]
    last_message = messages[-1]
    if last_message.tool_calls:
        return "tools"
    return "__end__"


workflow = StateGraph(MessagesState)

# Define the two nodes we will cycle between
workflow.add_node("agent", call_model)
workflow.add_node("tools", tool_node)

workflow.add_edge("__start__", "agent")
workflow.add_conditional_edges(
    "agent",
    should_continue,
)
workflow.add_edge("tools", "agent")

app = workflow.compile()

6. Invoke & Check Response

After the compilation of workflow, we invoke the LLM with a task, and stream the response.

for chunk in app.stream(
    {
        "messages": [
            (
                "human",
                # "Star the Github Repository composiohq/composio",
                "Get my information.",
            )
        ]
    },
    stream_mode="values",
):
    chunk["messages"][-1].pretty_print()

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

composio_langgraph-0.7.13.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

composio_langgraph-0.7.13-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file composio_langgraph-0.7.13.tar.gz.

File metadata

  • Download URL: composio_langgraph-0.7.13.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for composio_langgraph-0.7.13.tar.gz
Algorithm Hash digest
SHA256 c3ef77cbb680a5dc1dcf9a089fc83cf59387e53baf2a4d3a4b2c279ed2b4c6ea
MD5 b3e0f1e3f91a85e3251b151c323e462f
BLAKE2b-256 4432c02233f8b554a3c024bc5f8ba5771e7531d17f2ea3c16f9cf83d611f14f0

See more details on using hashes here.

File details

Details for the file composio_langgraph-0.7.13-py3-none-any.whl.

File metadata

File hashes

Hashes for composio_langgraph-0.7.13-py3-none-any.whl
Algorithm Hash digest
SHA256 389011dbebc0b393cdfe13e85745f6fc1f3dc012de55d0f06dedbb3121f9e190
MD5 b6a8fa93f6918039ff4c92b2b5646270
BLAKE2b-256 f28443a2bfb0f0fd26df10d909955bb7d41c337edf1c3d9fc7459e22af34a20d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page