Skip to main content

Use Composio to get array of tools with LnagGraph 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 langchain.agents import create_openai_functions_agent, AgentExecutor
import json
import operator
from typing import Annotated, TypedDict, Sequence

from langchain_openai import ChatOpenAI
from langchain_core.utils.function_calling import convert_to_openai_function
from langchain_core.messages import BaseMessage, HumanMessage, FunctionMessage

from langgraph.graph import StateGraph, END
from langgraph.prebuilt import ToolInvocation, ToolExecutor

2. Fetch GitHub LangGraph Tools via Composio

Access GitHub tools provided by Composio for LangGraph, initialize a tool_executor and get OpenAI-format function schemas from the tools.

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]
)
tool_executor = ToolExecutor(tools)
functions = [convert_to_openai_function(t) for t in tools]

3. Prepare the model

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

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

4. Define the Graph Nodes

LangGraph expects you to define different nodes of the agentic workflow as separate functions.

Here we define one node for calling the LLM and another for executing the correct tool(function), with appropriate parameters.

def function_1(state):
    messages = state['messages']
    response = model.invoke(messages)
    return {"messages": [response]}


def function_2(state):
    messages = state['messages']
    last_message = messages[-1]

    parsed_function_call = last_message.additional_kwargs["function_call"]

    action = ToolInvocation(
        tool=parsed_function_call["name"],
        tool_input=json.loads(parsed_function_call["arguments"]),
    )

    response = tool_executor.invoke(action)

    function_message = FunctionMessage(content=str(response), name=action.tool)

    return {"messages": [function_message]}

5. Define the Graph Edges

To establish the agent's workflow, we begin by initializing the workflow with an AgentState to maintain state, followed by specifying the connecting edges between nodes. These edges can be straightforward or conditional, depending on the workflow requirements.

def where_to_go(state):
    messages = state['messages']
    last_message = messages[-1]

    if "function_call" in last_message.additional_kwargs:
        return "continue"
    else:
        return "end"


class AgentState(TypedDict):
    messages: Annotated[Sequence[BaseMessage], operator.add]


workflow = StateGraph(AgentState)
workflow.add_node("agent", function_1)
workflow.add_node("tool", function_2)
workflow.add_conditional_edges(
    "agent",
    where_to_go,
    {
        "continue": "tool",
        "end": END
    }
)
workflow.add_edge('tool', 'agent')
workflow.set_entry_point("agent")

app = workflow.compile()

6. Invoke & Check Response

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

inputs = {
    "messages": [
        HumanMessage(
            content="Star the Github repository sawradip/sawradip"
            )
        ]
    }
response = app.invoke(inputs)
print(response)

Project details


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.3.30.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

composio_langgraph-0.3.30-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: composio_langgraph-0.3.30.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for composio_langgraph-0.3.30.tar.gz
Algorithm Hash digest
SHA256 0f2914201d42dd486b48997cc461ab67c436f622b8166c7607f1dc91b1812f8c
MD5 223486a5b13304667255c58994a9bfcc
BLAKE2b-256 9e0e67d1e8847310c6f4c03889e702de370e47a6fa81b8d4fee7f3a53ac49f13

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for composio_langgraph-0.3.30-py3-none-any.whl
Algorithm Hash digest
SHA256 a33d6d3d6d0d4f9a4b430674bd0732d3ad2272dac9c21c41b8695422992639fb
MD5 c565ff450afd382b37cf2a8c2234f1ca
BLAKE2b-256 cd08215399ad066a95684ad4af10518b183f8cb6749c620a25aeff7da6ff5307

See more details on using hashes here.

Supported by

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