hyperpocket-langgraph 0.1.10__py3-none-any.whl → 0.2.0__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  from hyperpocket_langgraph.pocket_langgraph import PocketLanggraph
2
2
 
3
- __all__ = ["PocketLanggraph"]
3
+ __all__ = ["PocketLanggraph"]
@@ -1,26 +1,21 @@
1
1
  import copy
2
2
  from typing import Optional
3
3
 
4
+ from hyperpocket.config import pocket_logger
4
5
  from langchain_core.runnables import RunnableConfig
5
6
  from langgraph.errors import NodeInterrupt
6
7
  from pydantic import BaseModel
7
8
 
8
- from hyperpocket.config import pocket_logger
9
-
10
9
  try:
11
10
  from langchain_core.messages import ToolMessage
12
11
  from langchain_core.tools import BaseTool, StructuredTool
13
12
  except ImportError:
14
- raise ImportError(
15
- "You need to install langchain to use pocket langgraph."
16
- )
13
+ raise ImportError("You need to install langchain to use pocket langgraph.")
17
14
 
18
15
  try:
19
16
  from langgraph.graph import MessagesState
20
17
  except ImportError:
21
- raise ImportError(
22
- "You need to install langgraph to use pocket langgraph"
23
- )
18
+ raise ImportError("You need to install langgraph to use pocket langgraph")
24
19
 
25
20
  from hyperpocket import Pocket
26
21
  from hyperpocket.tool import Tool as PocketTool
@@ -34,16 +29,22 @@ class PocketLanggraph(Pocket):
34
29
  def get_tools(self, use_profile: Optional[bool] = None):
35
30
  if use_profile is not None:
36
31
  self.use_profile = use_profile
37
- return [self._get_langgraph_tool(tool_impl) for tool_impl in
38
- self.core.tools.values()]
39
-
40
- def get_tool_node(self, should_interrupt: bool = False, use_profile: Optional[bool] = None):
32
+ return [
33
+ self._get_langgraph_tool(tool_impl)
34
+ for tool_impl in self.core.tools.values()
35
+ ]
36
+
37
+ def get_tool_node(
38
+ self, should_interrupt: bool = False, use_profile: Optional[bool] = None
39
+ ):
41
40
  if use_profile is not None:
42
41
  self.use_profile = use_profile
43
42
 
44
- async def _tool_node(state: PocketLanggraphBaseState, config: RunnableConfig) -> dict:
43
+ async def _tool_node(
44
+ state: PocketLanggraphBaseState, config: RunnableConfig
45
+ ) -> dict:
45
46
  thread_id = config.get("configurable", {}).get("thread_id", "default")
46
- last_message = state['messages'][-1]
47
+ last_message = state["messages"][-1]
47
48
  tool_calls = last_message.tool_calls
48
49
 
49
50
  # 01. prepare
@@ -54,9 +55,9 @@ class PocketLanggraph(Pocket):
54
55
  pocket_logger.debug(f"prepare tool {tool_call}")
55
56
  _tool_call = copy.deepcopy(tool_call)
56
57
 
57
- tool_call_id = _tool_call['id']
58
- tool_name = _tool_call['name']
59
- tool_args = _tool_call['args']
58
+ tool_call_id = _tool_call["id"]
59
+ tool_name = _tool_call["name"]
60
+ tool_args = _tool_call["args"]
60
61
 
61
62
  if self.use_profile:
62
63
  body = tool_args.pop("body")
@@ -68,27 +69,42 @@ class PocketLanggraph(Pocket):
68
69
  if isinstance(body, BaseModel):
69
70
  body = body.model_dump()
70
71
 
71
- prepare = await self.prepare_in_subprocess(tool_name, body=body, thread_id=thread_id,
72
- profile=profile)
72
+ prepare = await self.prepare_in_subprocess(
73
+ tool_name, body=body, thread_id=thread_id, profile=profile
74
+ )
73
75
  need_prepare |= True if prepare else False
74
76
 
75
77
  if prepare is None:
76
- prepare_done_list.append(ToolMessage(content="prepare done", tool_call_id=tool_call_id))
78
+ prepare_done_list.append(
79
+ ToolMessage(content="prepare done", tool_call_id=tool_call_id)
80
+ )
77
81
  else:
78
- prepare_list.append(ToolMessage(content=prepare, tool_call_id=tool_call_id))
82
+ prepare_list.append(
83
+ ToolMessage(content=prepare, tool_call_id=tool_call_id)
84
+ )
79
85
 
80
86
  if need_prepare:
81
87
  pocket_logger.debug(f"need prepare : {prepare_list}")
82
88
  if should_interrupt: # interrupt
83
- pocket_logger.debug(f"{last_message.name}({last_message.id}) is interrupt.")
84
- result = "\n\t" + "\n\t".join(set(msg.content for msg in prepare_list))
85
- raise NodeInterrupt(f'{result}\n\nThe tool execution interrupted. Please talk to me to resume.')
89
+ pocket_logger.debug(
90
+ f"{last_message.name}({last_message.id}) is interrupt."
91
+ )
92
+ result = "\n\t" + "\n\t".join(
93
+ set(msg.content for msg in prepare_list)
94
+ )
95
+ raise NodeInterrupt(
96
+ f"{result}\n\nThe tool execution interrupted. Please talk to me to resume."
97
+ )
86
98
 
87
99
  else: # multi turn
88
- pocket_logger.debug(f"{last_message.name}({last_message.id}) is multi-turn")
100
+ pocket_logger.debug(
101
+ f"{last_message.name}({last_message.id}) is multi-turn"
102
+ )
89
103
  return {"messages": prepare_done_list + prepare_list}
90
104
 
91
- pocket_logger.debug(f"no need prepare {last_message.name}({last_message.id})")
105
+ pocket_logger.debug(
106
+ f"no need prepare {last_message.name}({last_message.id})"
107
+ )
92
108
 
93
109
  # 02. authenticate and tool call
94
110
  tool_messages = []
@@ -96,9 +112,9 @@ class PocketLanggraph(Pocket):
96
112
  pocket_logger.debug(f"authenticate and call {tool_call}")
97
113
  _tool_call = copy.deepcopy(tool_call)
98
114
 
99
- tool_call_id = _tool_call['id']
100
- tool_name = _tool_call['name']
101
- tool_args = _tool_call['args']
115
+ tool_call_id = _tool_call["id"]
116
+ tool_name = _tool_call["name"]
117
+ tool_args = _tool_call["args"]
102
118
  if self.use_profile:
103
119
  body = tool_args.pop("body")
104
120
  profile = tool_args.pop("profile", "default")
@@ -111,27 +127,48 @@ class PocketLanggraph(Pocket):
111
127
 
112
128
  try:
113
129
  auth = await self.authenticate_in_subprocess(
114
- tool_name, body=body, thread_id=thread_id, profile=profile)
130
+ tool_name, body=body, thread_id=thread_id, profile=profile
131
+ )
115
132
  except Exception as e:
116
- pocket_logger.error(f"occur exception during authenticate. error : {e}")
133
+ pocket_logger.error(
134
+ f"occur exception during authenticate. error : {e}"
135
+ )
117
136
  tool_messages.append(
118
- ToolMessage(content=f"occur exception during authenticate. error : {e}", tool_name=tool_name,
119
- tool_call_id=tool_call_id))
137
+ ToolMessage(
138
+ content=f"occur exception during authenticate. error : {e}",
139
+ tool_name=tool_name,
140
+ tool_call_id=tool_call_id,
141
+ )
142
+ )
120
143
  continue
121
144
 
122
145
  try:
123
146
  result = await self.tool_call_in_subprocess(
124
- tool_name, body=body, envs=auth, thread_id=thread_id,
125
- profile=tool_args.get("profile", "default"))
147
+ tool_name,
148
+ body=body,
149
+ envs=auth,
150
+ thread_id=thread_id,
151
+ profile=tool_args.get("profile", "default"),
152
+ )
126
153
  except Exception as e:
127
- pocket_logger.error(f"occur exception during tool calling. error : {e}")
154
+ pocket_logger.error(
155
+ f"occur exception during tool calling. error : {e}"
156
+ )
128
157
  tool_messages.append(
129
- ToolMessage(content=f"occur exception during tool calling. error : {e}", tool_name=tool_name,
130
- tool_call_id=tool_call_id))
158
+ ToolMessage(
159
+ content=f"occur exception during tool calling. error : {e}",
160
+ tool_name=tool_name,
161
+ tool_call_id=tool_call_id,
162
+ )
163
+ )
131
164
  continue
132
165
 
133
166
  pocket_logger.debug(f"{tool_name} tool result : {result}")
134
- tool_messages.append(ToolMessage(content=result, tool_name=tool_name, tool_call_id=tool_call_id))
167
+ tool_messages.append(
168
+ ToolMessage(
169
+ content=result, tool_name=tool_name, tool_call_id=tool_call_id
170
+ )
171
+ )
135
172
 
136
173
  return {"messages": tool_messages}
137
174
 
@@ -151,10 +188,12 @@ class PocketLanggraph(Pocket):
151
188
  if isinstance(body, BaseModel):
152
189
  body = body.model_dump()
153
190
 
154
- result, interrupted = self.invoke_with_state(pocket_tool.name, body, thread_id, profile, **kwargs)
191
+ result, interrupted = self.invoke_with_state(
192
+ pocket_tool.name, body, thread_id, profile, **kwargs
193
+ )
155
194
  say = result
156
195
  if interrupted:
157
- say = f'{say}\n\nThe tool execution interrupted. Please talk to me to resume.'
196
+ say = f"{say}\n\nThe tool execution interrupted. Please talk to me to resume."
158
197
  return say
159
198
 
160
199
  async def _ainvoke(**kwargs) -> str:
@@ -170,10 +209,12 @@ class PocketLanggraph(Pocket):
170
209
  if isinstance(body, BaseModel):
171
210
  body = body.model_dump()
172
211
 
173
- result, interrupted = await self.ainvoke_with_state(pocket_tool.name, body, thread_id, profile, **kwargs)
212
+ result, interrupted = await self.ainvoke_with_state(
213
+ pocket_tool.name, body, thread_id, profile, **kwargs
214
+ )
174
215
  say = result
175
216
  if interrupted:
176
- say = f'{say}\n\nThe tool execution interrupted. Please talk to me to resume.'
217
+ say = f"{say}\n\nThe tool execution interrupted. Please talk to me to resume."
177
218
  return say
178
219
 
179
220
  return StructuredTool.from_function(
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: hyperpocket-langgraph
3
- Version: 0.1.10
3
+ Version: 0.2.0
4
4
  Author-email: Hyperpocket Team <hyperpocket@vessl.ai>
5
5
  Requires-Python: >=3.10
6
6
  Requires-Dist: hyperpocket>=0.0.3
@@ -0,0 +1,5 @@
1
+ hyperpocket_langgraph/__init__.py,sha256=Zqzel_ezEkzYOwVuzYWTQr4SJ737dd9hALXrIpMywEo,98
2
+ hyperpocket_langgraph/pocket_langgraph.py,sha256=Y1kj27T0NR7PFsO9NwQsZxBK9In-yQkwXTp_yDlPv80,8393
3
+ hyperpocket_langgraph-0.2.0.dist-info/METADATA,sha256=zmKuKzy8eSMrKaDaiCi3bkkTM92OtX1NSyqyFBAZLmc,5409
4
+ hyperpocket_langgraph-0.2.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
5
+ hyperpocket_langgraph-0.2.0.dist-info/RECORD,,
@@ -1,5 +0,0 @@
1
- hyperpocket_langgraph/__init__.py,sha256=0Az6FKsAvohOk8G4kUE99g6kyWz-Nf7dUuCuN3_pViY,97
2
- hyperpocket_langgraph/pocket_langgraph.py,sha256=y8GT64UrcriVr36gAf5NbTSFfnefl3NrcGnzFKqz6qY,7540
3
- hyperpocket_langgraph-0.1.10.dist-info/METADATA,sha256=EYoaPzvSBQ884u5LLA6iBwaJRb4QuDAYX6v8bl2VyWE,5410
4
- hyperpocket_langgraph-0.1.10.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
5
- hyperpocket_langgraph-0.1.10.dist-info/RECORD,,