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

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,13 +1,11 @@
1
1
  import copy
2
- from typing import List, Any
2
+ from typing import Optional
3
3
 
4
4
  from langchain_core.runnables import RunnableConfig
5
5
  from langgraph.errors import NodeInterrupt
6
6
  from pydantic import BaseModel
7
7
 
8
8
  from hyperpocket.config import pocket_logger
9
- from hyperpocket.pocket_main import ToolLike
10
- from hyperpocket.prompts import pocket_extended_tool_description
11
9
 
12
10
  try:
13
11
  from langchain_core.messages import ToolMessage
@@ -24,7 +22,7 @@ except ImportError:
24
22
  "You need to install langgraph to use pocket langgraph"
25
23
  )
26
24
 
27
- from hyperpocket import Pocket, PocketAuth
25
+ from hyperpocket import Pocket
28
26
  from hyperpocket.tool import Tool as PocketTool
29
27
 
30
28
 
@@ -33,20 +31,16 @@ class PocketLanggraphBaseState(MessagesState):
33
31
 
34
32
 
35
33
  class PocketLanggraph(Pocket):
36
- langgraph_tools: dict[str, BaseTool]
34
+ def get_tools(self, use_profile: Optional[bool] = None):
35
+ if use_profile is not None:
36
+ self.use_profile = use_profile
37
+ return [self._get_langgraph_tool(tool_impl) for tool_impl in
38
+ self.core.tools.values()]
37
39
 
38
- def __init__(self,
39
- tools: List[ToolLike],
40
- auth: PocketAuth = None):
41
- super().__init__(tools, auth)
42
- self.langgraph_tools = {}
43
- for tool_name, tool_impl in self.core.tools.items():
44
- self.langgraph_tools[tool_name] = self._get_langgraph_tool(tool_impl)
40
+ def get_tool_node(self, should_interrupt: bool = False, use_profile: Optional[bool] = None):
41
+ if use_profile is not None:
42
+ self.use_profile = use_profile
45
43
 
46
- def get_tools(self):
47
- return [v for v in self.langgraph_tools.values()]
48
-
49
- def get_tool_node(self, should_interrupt: bool = False):
50
44
  async def _tool_node(state: PocketLanggraphBaseState, config: RunnableConfig) -> dict:
51
45
  thread_id = config.get("configurable", {}).get("thread_id", "default")
52
46
  last_message = state['messages'][-1]
@@ -63,12 +57,19 @@ class PocketLanggraph(Pocket):
63
57
  tool_call_id = _tool_call['id']
64
58
  tool_name = _tool_call['name']
65
59
  tool_args = _tool_call['args']
66
- body = tool_args.pop('body')
60
+
61
+ if self.use_profile:
62
+ body = tool_args.pop("body")
63
+ profile = tool_args.pop("profile", "default")
64
+ else:
65
+ body = tool_args
66
+ profile = "default"
67
+
67
68
  if isinstance(body, BaseModel):
68
69
  body = body.model_dump()
69
70
 
70
71
  prepare = await self.prepare_in_subprocess(tool_name, body=body, thread_id=thread_id,
71
- profile=tool_args.get("profile", "default"))
72
+ profile=profile)
72
73
  need_prepare |= True if prepare else False
73
74
 
74
75
  if prepare is None:
@@ -98,13 +99,19 @@ class PocketLanggraph(Pocket):
98
99
  tool_call_id = _tool_call['id']
99
100
  tool_name = _tool_call['name']
100
101
  tool_args = _tool_call['args']
101
- body = tool_args.pop('body')
102
+ if self.use_profile:
103
+ body = tool_args.pop("body")
104
+ profile = tool_args.pop("profile", "default")
105
+ else:
106
+ body = tool_args
107
+ profile = "default"
108
+
102
109
  if isinstance(body, BaseModel):
103
110
  body = body.model_dump()
104
111
 
105
112
  try:
106
113
  auth = await self.authenticate_in_subprocess(
107
- tool_name, body=body, thread_id=thread_id, profile=tool_args.get("profile", "default"))
114
+ tool_name, body=body, thread_id=thread_id, profile=profile)
108
115
  except Exception as e:
109
116
  pocket_logger.error(f"occur exception during authenticate. error : {e}")
110
117
  tool_messages.append(
@@ -131,18 +138,38 @@ class PocketLanggraph(Pocket):
131
138
  return _tool_node
132
139
 
133
140
  def _get_langgraph_tool(self, pocket_tool: PocketTool) -> BaseTool:
134
- def _invoke(body: Any, thread_id: str = 'default', profile: str = 'default', **kwargs) -> str:
141
+ def _invoke(**kwargs) -> str:
142
+ if self.use_profile:
143
+ body = kwargs["body"]
144
+ thread_id = kwargs.pop("thread_id", "default")
145
+ profile = kwargs.pop("profile", "default")
146
+ else:
147
+ body = kwargs
148
+ thread_id = "default"
149
+ profile = "default"
150
+
135
151
  if isinstance(body, BaseModel):
136
152
  body = body.model_dump()
153
+
137
154
  result, interrupted = self.invoke_with_state(pocket_tool.name, body, thread_id, profile, **kwargs)
138
155
  say = result
139
156
  if interrupted:
140
157
  say = f'{say}\n\nThe tool execution interrupted. Please talk to me to resume.'
141
158
  return say
142
159
 
143
- async def _ainvoke(body: Any, thread_id: str = 'default', profile: str = 'default', **kwargs) -> str:
160
+ async def _ainvoke(**kwargs) -> str:
161
+ if self.use_profile:
162
+ body = kwargs["body"]
163
+ thread_id = kwargs.pop("thread_id", "default")
164
+ profile = kwargs.pop("profile", "default")
165
+ else:
166
+ body = kwargs
167
+ thread_id = "default"
168
+ profile = "default"
169
+
144
170
  if isinstance(body, BaseModel):
145
171
  body = body.model_dump()
172
+
146
173
  result, interrupted = await self.ainvoke_with_state(pocket_tool.name, body, thread_id, profile, **kwargs)
147
174
  say = result
148
175
  if interrupted:
@@ -153,6 +180,6 @@ class PocketLanggraph(Pocket):
153
180
  func=_invoke,
154
181
  coroutine=_ainvoke,
155
182
  name=pocket_tool.name,
156
- description=pocket_extended_tool_description(pocket_tool.description),
157
- args_schema=pocket_tool.schema_model(),
183
+ description=pocket_tool.get_description(use_profile=self.use_profile),
184
+ args_schema=pocket_tool.schema_model(use_profile=self.use_profile),
158
185
  )
@@ -1,7 +1,7 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: hyperpocket-langgraph
3
- Version: 0.1.8
4
- Author-email: moon <moon@vessl.ai>
3
+ Version: 0.1.10
4
+ Author-email: Hyperpocket Team <hyperpocket@vessl.ai>
5
5
  Requires-Python: >=3.10
6
6
  Requires-Dist: hyperpocket>=0.0.3
7
7
  Requires-Dist: langgraph>=0.2.59
@@ -17,13 +17,13 @@ Description-Content-Type: text/markdown
17
17
  ## Get Pocket Subgraph
18
18
 
19
19
  ```python
20
- from hyperpocket.tool import from_git
20
+
21
21
 
22
22
  from hyperpocket_langgraph import PocketLanggraph
23
23
 
24
24
  pocket = PocketLanggraph(tools=[
25
- from_git("https://github.com/vessl-ai/hyperawesometools", "main", "managed-tools/slack/get-message"),
26
- from_git("https://github.com/vessl-ai/hyperawesometools", "main", "managed-tools/slack/post-message"),
25
+ "https://github.com/vessl-ai/hyperpocket/tree/main/tools/slack/get-message",
26
+ "https://github.com/vessl-ai/hyperpocket/tree/main/tools/slack/post-message",
27
27
  "https://github.com/my-org/some-awesome-tool",
28
28
  ])
29
29
 
@@ -36,14 +36,13 @@ pocket_node = pocket.get_tool_node()
36
36
  ```python
37
37
  import os
38
38
 
39
- from hyperpocket.tool import from_git
40
39
  from langchain_openai import ChatOpenAI
41
40
 
42
41
  from hyperpocket_langgraph import PocketLanggraph
43
42
 
44
43
  pocket = PocketLanggraph(tools=[
45
- from_git("https://github.com/vessl-ai/hyperawesometools", "main", "managed-tools/slack/get-message"),
46
- from_git("https://github.com/vessl-ai/hyperawesometools", "main", "managed-tools/slack/post-message"),
44
+ "https://github.com/vessl-ai/hyperpocket/tree/main/tools/slack/get-message",
45
+ "https://github.com/vessl-ai/hyperpocket/tree/main/tools/slack/post-message",
47
46
  ])
48
47
 
49
48
  # get tools from pocket to bind llm
@@ -60,7 +59,6 @@ llm_with_tools = llm.bind_tools(tools)
60
59
  import os
61
60
  from typing import Annotated
62
61
 
63
- from hyperpocket.tool import from_git
64
62
  from langchain_core.runnables import RunnableConfig
65
63
  from langchain_openai import ChatOpenAI
66
64
  from langgraph.checkpoint.memory import MemorySaver
@@ -73,8 +71,8 @@ from hyperpocket_langgraph import PocketLanggraph
73
71
 
74
72
  # Define pocket tools
75
73
  pocket = PocketLanggraph(tools=[
76
- from_git("https://github.com/vessl-ai/hyperawesometools", "main", "managed-tools/slack/get-message"),
77
- from_git("https://github.com/vessl-ai/hyperawesometools", "main", "managed-tools/slack/post-message"),
74
+ "https://github.com/vessl-ai/hyperpocket/tree/main/tools/slack/get-message",
75
+ "https://github.com/vessl-ai/hyperpocket/tree/main/tools/slack/post-message",
78
76
  ])
79
77
 
80
78
  # Get Pocket ToolNode
@@ -131,14 +129,14 @@ by setting the `should_interrupt` flag when calling `get_tool_node`
131
129
  Perform authentication in a multi-turn way
132
130
 
133
131
  ```python
134
- from hyperpocket.tool import from_git
132
+
135
133
 
136
134
  from hyperpocket_langgraph import PocketLanggraph
137
135
 
138
136
  # Define pocket tools
139
137
  pocket = PocketLanggraph(tools=[
140
- from_git("https://github.com/vessl-ai/hyperawesometools", "main", "managed-tools/slack/get-message"),
141
- from_git("https://github.com/vessl-ai/hyperawesometools", "main", "managed-tools/slack/post-message"),
138
+ "https://github.com/vessl-ai/hyperpocket/tree/main/tools/slack/get-message",
139
+ "https://github.com/vessl-ai/hyperpocket/tree/main/tools/slack/post-message",
142
140
  ])
143
141
 
144
142
  # Get Pocket ToolNode
@@ -148,14 +146,14 @@ pocket_node = pocket.get_tool_node(should_interrupt=False) # multi turn
148
146
  ### Human-in-the-loop Auth
149
147
 
150
148
  ```python
151
- from hyperpocket.tool import from_git
149
+
152
150
 
153
151
  from hyperpocket_langgraph import PocketLanggraph
154
152
 
155
153
  # Define pocket tools
156
154
  pocket = PocketLanggraph(tools=[
157
- from_git("https://github.com/vessl-ai/hyperawesometools", "main", "managed-tools/slack/get-message"),
158
- from_git("https://github.com/vessl-ai/hyperawesometools", "main", "managed-tools/slack/post-message"),
155
+ "https://github.com/vessl-ai/hyperpocket/tree/main/tools/slack/get-message",
156
+ "https://github.com/vessl-ai/hyperpocket/tree/main/tools/slack/post-message",
159
157
  ])
160
158
 
161
159
  # Get Pocket ToolNode
@@ -0,0 +1,5 @@
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,,
@@ -1,5 +0,0 @@
1
- hyperpocket_langgraph/__init__.py,sha256=0Az6FKsAvohOk8G4kUE99g6kyWz-Nf7dUuCuN3_pViY,97
2
- hyperpocket_langgraph/pocket_langgraph.py,sha256=6_KfStimtuvW7W7TzyUWN4WJ5veybDgJv_bGCAtxOt4,6852
3
- hyperpocket_langgraph-0.1.8.dist-info/METADATA,sha256=umosqoSEHq85vwsT7JiLfCMxAasNxLsvLFinxQ_sjeI,5827
4
- hyperpocket_langgraph-0.1.8.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
5
- hyperpocket_langgraph-0.1.8.dist-info/RECORD,,