@devries-cameron20 / composio Mirror · read-only
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 03:37 UTC

next
py 62 lines 1.54 KB
Raw
1 from typing import Literal
2
3 from composio_langgraph import LanggraphProvider
4 from langchain_openai import ChatOpenAI
5 from langgraph.graph import MessagesState, StateGraph
6 from langgraph.prebuilt import ToolNode
7
8 from composio import Composio
9
10 composio = Composio(provider=LanggraphProvider())
11 tools = composio.tools.get(
12 user_id="default",
13 tools=[
14 "GITHUB_STAR_A_REPOSITORY_FOR_THE_AUTHENTICATED_USER",
15 "GITHUB_GET_THE_AUTHENTICATED_USER",
16 ],
17 )
18 tool_node = ToolNode(tools)
19 model_with_tools = ChatOpenAI(temperature=0, streaming=True).bind_tools(tools)
20
21
22 def should_continue(state: MessagesState) -> Literal["tools", "__end__"]:
23 messages = state["messages"]
24 last_message = messages[-1]
25 if last_message.tool_calls: # type: ignore
26 return "tools"
27 return "__end__"
28
29
30 def call_model(state: MessagesState):
31 messages = state["messages"]
32 response = model_with_tools.invoke(messages)
33 return {"messages": [response]}
34
35
36 workflow = StateGraph(MessagesState)
37
38 # Define the two nodes we will cycle between
39 workflow.add_node("agent", call_model)
40 workflow.add_node("tools", tool_node)
41
42 workflow.add_edge("__start__", "agent")
43 workflow.add_conditional_edges(
44 "agent",
45 should_continue,
46 )
47 workflow.add_edge("tools", "agent")
48
49 app = workflow.compile()
50
51 for chunk in app.stream(
52 {
53 "messages": [
54 ( # type: ignore
55 "human",
56 "Star the Github Repository composiohq/composio",
57 )
58 ]
59 },
60 stream_mode="values",
61 ):
62 chunk["messages"][-1].pretty_print()