Skip to content

Commit

Permalink
Feat/LangGraph TS (#949)
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishekpatil4 authored Dec 5, 2024
1 parent b988f86 commit b61aa66
Showing 1 changed file with 52 additions and 12 deletions.
64 changes: 52 additions & 12 deletions docs/framework/langgraph.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ In this example, we will use LangGraph Agent to star a repository on Github usin
```bash Python
pip install composio-langgraph
```
```javascript JavaScript
coming soon
```bash TypeScript
npm install composio-core langchain langgraph
```
</CodeGroup>
</Step>
Expand All @@ -39,8 +39,22 @@ tool_node = ToolNode(tools)
model = ChatOpenAI(temperature=0, streaming=True)
model_with_tools = model.bind_tools(tools)
```
```javascript JavaScript
coming soon
```typescript TypeScript
import { LangGraphToolSet } from "composio-core";
import { ToolNode } from "@langchain/langgraph/prebuilt";
import { ChatOpenAI } from "@langchain/openai";
import { StateGraph, END, MessagesAnnotation, START } from "@langchain/langgraph";
import { HumanMessage } from "@langchain/core/messages";

const composioToolset = new LangGraphToolSet();

const tools = await composioToolset.getTools({
apps: ["github"],
});

const toolNode = new ToolNode(tools);

const model = new ChatOpenAI({ temperature: 0, apiKey:""}).bindTools(tools);
```
</CodeGroup>
</Step>
Expand All @@ -56,8 +70,12 @@ def call_model(state: MessagesState):
response = model_with_tools.invoke(messages)
return {"messages": [response]}
```
```javascript JavaScript
coming soon
```typescript TypeScript
async function callModal(state) {
const { messages } = state;
const response = await model.invoke(messages);
return { messages: [response] };
}
```
</CodeGroup>
</Step>
Expand All @@ -78,8 +96,17 @@ def should_continue(state: MessagesState) -> Literal["tools", "__end__"]:
return "tools"
return "__end__"
```
```javascript JavaScript
coming soon
```typescript TypeScript
async function shouldContinue(state) {
const { messages } = state;
const lastMessage = messages[messages.length - 1];

if (lastMessage.additional_kwargs.tool_calls) {
return "tools";
} else {
return END;
}
}
```
</CodeGroup>
</Step>
Expand All @@ -100,8 +127,15 @@ workflow.add_edge("tools", "agent")

app = workflow.compile()
```
```javascript JavaScript
coming soon
```typescript TypeScript
const workflow = new StateGraph(MessagesAnnotation)
.addNode("agent", callModal)
.addEdge(START, "agent")
.addNode("tools", toolNode)
.addConditionalEdges("agent", shouldContinue)
.addEdge("tools", "agent");

const app = workflow.compile();
```
</CodeGroup>
</Step>
Expand All @@ -122,8 +156,14 @@ for chunk in app.stream(
):
chunk["messages"][-1].pretty_print()
```
```javascript JavaScript
coming soon
```typescript TypeScript
const stream = await app.invoke({
messages: [
new HumanMessage("Star the Github Repository composiohq/composio"),
],
});

console.log(stream.messages[stream.messages.length - 1].content);
```
</CodeGroup>
</Step>
Expand Down

0 comments on commit b61aa66

Please sign in to comment.