Skip to content

Commit 424db24

Browse files
committed
feat: add sqlite component
1 parent 3beffe1 commit 424db24

File tree

2 files changed

+53
-22
lines changed

2 files changed

+53
-22
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
apiVersion: dapr.io/v1alpha1
2+
kind: Component
3+
metadata:
4+
name: dapr-sqlite
5+
spec:
6+
type: state.sqlite
7+
version: v1
8+
metadata:
9+
- name: connectionString
10+
value: "data.db"
Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import os
2-
from langchain_openai import ChatOpenAI
3-
from langgraph.graph import MessagesState
4-
from langchain_core.messages import HumanMessage, SystemMessage
5-
from langgraph.graph import START, StateGraph
6-
from langgraph.prebuilt import tools_condition, ToolNode
7-
from dapr.ext.langgraph import DaprCheckpointer
82

3+
from dapr.ext.langgraph import DaprCheckpointer
94
from dotenv import load_dotenv
5+
from langchain_core.messages import HumanMessage, SystemMessage
6+
from langchain_openai import ChatOpenAI
7+
from langgraph.graph import START, MessagesState, StateGraph
8+
from langgraph.prebuilt import ToolNode, tools_condition
109

1110
load_dotenv()
1211

12+
1313
def add(a: int, b: int) -> int:
1414
"""Adds a and b.
1515
@@ -19,7 +19,8 @@ def add(a: int, b: int) -> int:
1919
"""
2020
return a + b
2121

22-
def multiply(a: int, b: int) -> int:
22+
23+
def multiply(a: int, b: int) -> int:
2324
"""Multiply a and b.
2425
2526
Args:
@@ -28,39 +29,59 @@ def multiply(a: int, b: int) -> int:
2829
"""
2930
return a * b
3031

32+
3133
tools = [add, multiply]
32-
llm = ChatOpenAI(model="gpt-4o", api_key=os.environ["OPENAI_API_KEY"])
34+
llm = ChatOpenAI(model='gpt-4o', api_key=os.environ['OPENAI_API_KEY'])
3335
llm_with_tools = llm.bind_tools(tools)
3436

35-
sys_msg = SystemMessage(content="You are a helpful assistant tasked with performing arithmetic on a set of inputs.")
37+
sys_msg = SystemMessage(
38+
content='You are a helpful assistant tasked with performing arithmetic on a set of inputs.'
39+
)
40+
3641

3742
def assistant(state: MessagesState):
38-
return {"messages": [llm_with_tools.invoke([sys_msg] + state["messages"])]}
43+
return {'messages': [llm_with_tools.invoke([sys_msg] + state['messages'])]}
44+
3945

4046
builder = StateGraph(MessagesState)
4147

42-
builder.add_node("assistant", assistant)
43-
builder.add_node("tools", ToolNode(tools))
48+
builder.add_node('assistant', assistant)
49+
builder.add_node('tools', ToolNode(tools))
4450

45-
builder.add_edge(START, "assistant")
51+
builder.add_edge(START, 'assistant')
4652
builder.add_conditional_edges(
47-
"assistant",
53+
'assistant',
4854
tools_condition,
4955
)
50-
builder.add_edge("tools", "assistant")
56+
builder.add_edge('tools', 'assistant')
5157
react_graph = builder.compile()
5258

53-
memory = DaprCheckpointer(store_name="langgraph_checkpoint", key_prefix="dapr")
59+
memory = DaprCheckpointer(store_name='dapr-redis', key_prefix='dapr')
5460
react_graph_memory = builder.compile(checkpointer=memory)
5561

56-
config = {"configurable": {"thread_id": "1"}}
62+
config = {'configurable': {'thread_id': '1'}}
5763

58-
messages = [HumanMessage(content="Add 3 and 4.")]
59-
messages = react_graph_memory.invoke({"messages": messages},config)
64+
messages = [HumanMessage(content='Add 3 and 4.')]
65+
messages = react_graph_memory.invoke({'messages': messages}, config)
6066
for m in messages['messages']:
6167
m.pretty_print()
6268

63-
messages = [HumanMessage(content="Multiply that by 2.")]
64-
messages = react_graph_memory.invoke({"messages": messages}, config)
69+
messages = [HumanMessage(content='Multiply that by 2.')]
70+
messages = react_graph_memory.invoke({'messages': messages}, config)
6571
for m in messages['messages']:
66-
m.pretty_print()
72+
m.pretty_print()
73+
74+
memory = DaprCheckpointer(store_name='dapr-sqlite', key_prefix='dapr')
75+
react_graph_memory = builder.compile(checkpointer=memory)
76+
77+
config = {'configurable': {'thread_id': '2'}}
78+
79+
messages = [HumanMessage(content='Add 5 and 6.')]
80+
messages = react_graph_memory.invoke({'messages': messages}, config)
81+
for m in messages['messages']:
82+
m.pretty_print()
83+
84+
messages = [HumanMessage(content='Multiply that by 3.')]
85+
messages = react_graph_memory.invoke({'messages': messages}, config)
86+
for m in messages['messages']:
87+
m.pretty_print()

0 commit comments

Comments
 (0)