composio-langgraph


Namecomposio-langgraph JSON
Version 0.5.34 PyPI version JSON
download
home_pagehttps://github.com/ComposioHQ/composio
SummaryUse Composio to get array of tools with LnagGraph Agent Workflows
upload_time2024-10-17 15:55:22
maintainerNone
docs_urlNone
authorSawradip
requires_python<4,>=3.9
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ## 🦜🕸️ 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.

```bash
# 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.

```python
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`.

```python
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.

```python
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.

```python
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.

```python
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.

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

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/ComposioHQ/composio",
    "name": "composio-langgraph",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4,>=3.9",
    "maintainer_email": null,
    "keywords": null,
    "author": "Sawradip",
    "author_email": "sawradip@composio.dev",
    "download_url": "https://files.pythonhosted.org/packages/a4/9b/05a951c2ac85877d99cc295615377ca09deac5560c70d5801978127a6796/composio_langgraph-0.5.34.tar.gz",
    "platform": null,
    "description": "## \ud83e\udd9c\ud83d\udd78\ufe0f Using Composio With LangGraph\n\nIntegrate Composio with LangGraph Agentic workflows & enable them to interact seamlessly with external apps, enhancing their functionality and reach.\n\n### Goal\n\n- **Star a repository on GitHub** using natural language commands through a LangGraph Agent.\n\n### Installation and Setup\n\nEnsure you have the necessary packages installed and connect your GitHub account to allow your agents to utilize GitHub functionalities.\n\n```bash\n# Install Composio LangGraph package\npip install composio-langgraph\n\n# Connect your GitHub account\ncomposio-cli add github\n\n# View available applications you can connect with\ncomposio-cli show-apps\n```\n\n### Usage Steps\n\n#### 1. Import Base Packages\n\nPrepare your environment by initializing necessary imports from LangGraph & LangChain for setting up your agent.\n\n```python\nfrom typing import Literal\n\nfrom langchain_openai import ChatOpenAI\nfrom langgraph.graph import MessagesState, StateGraph\nfrom langgraph.prebuilt import ToolNode\n```\n\n#### 2. Fetch GitHub LangGraph Tools via Composio\n\nAccess GitHub tools provided by Composio for LangGraph, initialize a `ToolNode` with necessary tools obtaned from `ComposioToolSet`.\n\n```python\nfrom composio_langgraph import Action, ComposioToolSet\n\n# Initialize the toolset for GitHub\ncomposio_toolset = ComposioToolSet()\ntools = composio_toolset.get_actions(\n    actions=[\n        Action.GITHUB_ACTIVITY_STAR_REPO_FOR_AUTHENTICATED_USER,\n        Action.GITHUB_USERS_GET_AUTHENTICATED,\n    ])\ntool_node = ToolNode(tools)\n```\n\n#### 3. Prepare the model\n\nInitialize the LLM class and bind obtained tools to the model.\n\n```python\nmodel = ChatOpenAI(temperature=0, streaming=True)\nmodel_with_tools = model.bind_tools(functions)\n```\n#### 4. Define the Graph Nodes\n\nLangGraph expects you to define different nodes of the agentic workflow as separate functions. Here we define a node for calling the LLM model.\n\n```python\ndef call_model(state: MessagesState):\n    messages = state[\"messages\"]\n    response = model_with_tools.invoke(messages)\n    return {\"messages\": [response]}\n```\n#### 5. Define the Graph Nodes and Edges\n\nTo 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.\n\n```python\ndef should_continue(state: MessagesState) -> Literal[\"tools\", \"__end__\"]:\n    messages = state[\"messages\"]\n    last_message = messages[-1]\n    if last_message.tool_calls:\n        return \"tools\"\n    return \"__end__\"\n\n\nworkflow = StateGraph(MessagesState)\n\n# Define the two nodes we will cycle between\nworkflow.add_node(\"agent\", call_model)\nworkflow.add_node(\"tools\", tool_node)\n\nworkflow.add_edge(\"__start__\", \"agent\")\nworkflow.add_conditional_edges(\n    \"agent\",\n    should_continue,\n)\nworkflow.add_edge(\"tools\", \"agent\")\n\napp = workflow.compile()\n```\n#### 6. Invoke & Check Response\n\nAfter the compilation of workflow, we invoke the LLM with a task, and stream the response.\n\n```python\nfor chunk in app.stream(\n    {\n        \"messages\": [\n            (\n                \"human\",\n                # \"Star the Github Repository composiohq/composio\",\n                \"Get my information.\",\n            )\n        ]\n    },\n    stream_mode=\"values\",\n):\n    chunk[\"messages\"][-1].pretty_print()\n```\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Use Composio to get array of tools with LnagGraph Agent Workflows",
    "version": "0.5.34",
    "project_urls": {
        "Homepage": "https://github.com/ComposioHQ/composio"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "58a4bb2f36917b84c1b3cdc3353644df37a0f4d1064ef12f41c32ad8cc53a89e",
                "md5": "c75cd616b501ed1254da7381cafd3cec",
                "sha256": "e7efe4c0ae3e5b2f7a002f26d02d7708eac1a2737408a05a392e25d6fd9a60c0"
            },
            "downloads": -1,
            "filename": "composio_langgraph-0.5.34-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "c75cd616b501ed1254da7381cafd3cec",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4,>=3.9",
            "size": 4220,
            "upload_time": "2024-10-17T15:55:20",
            "upload_time_iso_8601": "2024-10-17T15:55:20.479210Z",
            "url": "https://files.pythonhosted.org/packages/58/a4/bb2f36917b84c1b3cdc3353644df37a0f4d1064ef12f41c32ad8cc53a89e/composio_langgraph-0.5.34-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a49b05a951c2ac85877d99cc295615377ca09deac5560c70d5801978127a6796",
                "md5": "2db2a2f7e657e66ce434ba18bbb65396",
                "sha256": "8657a1b289e98d775b0f1dd0a717b0e66803603a402fc18dc13271cd9afa19f0"
            },
            "downloads": -1,
            "filename": "composio_langgraph-0.5.34.tar.gz",
            "has_sig": false,
            "md5_digest": "2db2a2f7e657e66ce434ba18bbb65396",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4,>=3.9",
            "size": 3747,
            "upload_time": "2024-10-17T15:55:22",
            "upload_time_iso_8601": "2024-10-17T15:55:22.052902Z",
            "url": "https://files.pythonhosted.org/packages/a4/9b/05a951c2ac85877d99cc295615377ca09deac5560c70d5801978127a6796/composio_langgraph-0.5.34.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-17 15:55:22",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "ComposioHQ",
    "github_project": "composio",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "composio-langgraph"
}
        
Elapsed time: 0.56682s