hyperpocket-llamaindex 0.1.8__tar.gz → 0.1.10__tar.gz

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,6 +14,7 @@ __pycache__/
14
14
  build/
15
15
  develop-eggs/
16
16
  dist/
17
+ !tools/**/dist/
17
18
  downloads/
18
19
  eggs/
19
20
  .eggs/
@@ -1,7 +1,7 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: hyperpocket-llamaindex
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: llama-index>=0.12.5
@@ -1,4 +1,6 @@
1
- from typing import List, Any
1
+ from typing import List, Optional
2
+
3
+ from pydantic import BaseModel
2
4
 
3
5
  try:
4
6
  from llama_index.core.tools import FunctionTool, BaseTool, ToolMetadata
@@ -12,12 +14,27 @@ from hyperpocket.tool import Tool
12
14
 
13
15
 
14
16
  class PocketLlamaindex(Pocket):
15
- def get_tools(self) -> List[BaseTool]:
17
+ def get_tools(self, use_profile: Optional[bool] = None) -> List[BaseTool]:
18
+ if use_profile is not None:
19
+ self.use_profile = use_profile
20
+
16
21
  tools = [self.get_tool(pk) for pk in self.core.tools.values()]
17
22
  return tools
18
23
 
19
24
  def get_tool(self, pocket_tool: Tool) -> BaseTool:
20
- def _invoke(body: Any, thread_id: str = 'default', profile: str = 'default', **kwargs) -> str:
25
+ def _invoke(**kwargs) -> str:
26
+ if self.use_profile:
27
+ body = kwargs["body"]
28
+ thread_id = kwargs.pop("thread_id", "default")
29
+ profile = kwargs.pop("profile", "default")
30
+ else:
31
+ body = kwargs
32
+ thread_id = "default"
33
+ profile = "default"
34
+
35
+ if isinstance(body, BaseModel):
36
+ body = body.model_dump()
37
+
21
38
  result, interrupted = self.invoke_with_state(pocket_tool.name, body=body, thread_id=thread_id,
22
39
  profile=profile, **kwargs)
23
40
  say = result
@@ -25,7 +42,19 @@ class PocketLlamaindex(Pocket):
25
42
  say = f'{say}\n\nThe tool execution interrupted. Please talk to me to resume.'
26
43
  return say
27
44
 
28
- async def _ainvoke(body: Any, thread_id: str = 'default', profile: str = 'default', **kwargs) -> str:
45
+ async def _ainvoke(**kwargs) -> str:
46
+ if self.use_profile:
47
+ body = kwargs["body"]
48
+ thread_id = kwargs.pop("thread_id", "default")
49
+ profile = kwargs.pop("profile", "default")
50
+ else:
51
+ body = kwargs
52
+ thread_id = "default"
53
+ profile = "default"
54
+
55
+ if isinstance(body, BaseModel):
56
+ body = body.model_dump()
57
+
29
58
  result, interrupted = await self.ainvoke_with_state(pocket_tool.name, body=body,
30
59
  thread_id=thread_id, profile=profile, **kwargs)
31
60
  say = result
@@ -37,8 +66,8 @@ class PocketLlamaindex(Pocket):
37
66
  fn=_invoke,
38
67
  async_fn=_ainvoke,
39
68
  tool_metadata=ToolMetadata(
40
- description=pocket_tool.description,
41
69
  name=pocket_tool.name,
42
- fn_schema=pocket_tool.schema_model(),
70
+ description=pocket_tool.get_description(use_profile=self.use_profile),
71
+ fn_schema=pocket_tool.schema_model(use_profile=self.use_profile),
43
72
  )
44
73
  )
@@ -1,9 +1,9 @@
1
1
 
2
2
  [project]
3
3
  name = "hyperpocket-llamaindex"
4
- version = "0.1.8"
4
+ version = "0.1.10"
5
5
  description = ""
6
- authors = [{ name = "moon", email = "moon@vessl.ai" }]
6
+ authors = [{ name = "Hyperpocket Team", email = "hyperpocket@vessl.ai" }]
7
7
  requires-python = ">=3.10"
8
8
  readme = "README.md"
9
9
  dependencies = ["llama-index>=0.12.5", "hyperpocket>=0.0.3"]
@@ -0,0 +1,137 @@
1
+ import ast
2
+ import json
3
+ from unittest.async_case import IsolatedAsyncioTestCase
4
+
5
+ from llama_index.agent.openai import OpenAIAgent
6
+ from llama_index.llms.openai import OpenAI
7
+ from pydantic import BaseModel
8
+
9
+ from hyperpocket.config import config, secret
10
+ from hyperpocket.tool import from_git
11
+ from hyperpocket_llamaindex import PocketLlamaindex
12
+
13
+
14
+ class TestPocketLlamaindexNoProfile(IsolatedAsyncioTestCase):
15
+
16
+ async def asyncSetUp(self):
17
+ config.public_server_port = "https"
18
+ config.public_hostname = "localhost"
19
+ config.public_server_port = 8001
20
+ config.internal_server_port = 8000
21
+ config.enable_local_callback_proxy = True
22
+
23
+ self.pocket = PocketLlamaindex(
24
+ tools=[
25
+ from_git("https://github.com/vessl-ai/hyperawesometools", "main",
26
+ "managed-tools/none/simple-echo-tool"),
27
+ self.add,
28
+ self.sub_pydantic_args
29
+ ],
30
+ use_profile=False
31
+ )
32
+
33
+ self.llm = OpenAI(model="gpt-4o", api_key=secret["OPENAI_API_KEY"])
34
+
35
+ async def asyncTearDown(self):
36
+ self.pocket._teardown_server()
37
+
38
+ async def test_agent_no_profile(self):
39
+ # given
40
+ agent = OpenAIAgent.from_tools(
41
+ tools=self.pocket.get_tools(),
42
+ llm=OpenAI(model="gpt-4o", api_key=secret["OPENAI_API_KEY"]),
43
+ verbose=True
44
+ )
45
+
46
+ # when
47
+ agent.query("add 1, 2")
48
+ agent.query("sub 1, 2")
49
+ agent.query("echo 'hello world'")
50
+
51
+ async def test_function_tool_no_profile(self):
52
+ # when
53
+ response = self.llm.chat_with_tools(user_msg="add 1, 2", tools=self.pocket.get_tools(), verbose=True)
54
+ message = response.message
55
+ tool_calls = message.additional_kwargs["tool_calls"]
56
+
57
+ tool_name = tool_calls[0].function.name
58
+ args = tool_calls[0].function.arguments
59
+ args = json.loads(args)
60
+
61
+ result = await self.pocket.ainvoke(tool_name=tool_name, body=args)
62
+
63
+ # then
64
+ self.assertEqual(tool_name, "add")
65
+ self.assertEqual(args, {
66
+ "a": 1,
67
+ "b": 2,
68
+ })
69
+ self.assertEqual(result, '3')
70
+
71
+ async def test_pydantic_function_tool_no_profile(self):
72
+ # when
73
+ response = self.llm.chat_with_tools(user_msg="sub 1, 2", tools=self.pocket.get_tools(), verbose=True)
74
+ tool_calls = response.message.additional_kwargs["tool_calls"]
75
+
76
+ tool_name = tool_calls[0].function.name
77
+ args = tool_calls[0].function.arguments
78
+ args = json.loads(args)
79
+
80
+ result = await self.pocket.ainvoke(tool_name=tool_name, body=args)
81
+
82
+ # then
83
+ self.assertEqual(tool_name, "sub_pydantic_args")
84
+ self.assertEqual(args, {
85
+ "a": {"first": 1},
86
+ "b": {"second": 2},
87
+ })
88
+ self.assertEqual(result, '-1')
89
+
90
+ async def test_wasm_tool_no_profile(self):
91
+ # when
92
+ response = self.llm.chat_with_tools(user_msg="echo 'hello world'", tools=self.pocket.get_tools(), verbose=True)
93
+ tool_calls = response.message.additional_kwargs["tool_calls"]
94
+
95
+ tool_name = tool_calls[0].function.name
96
+ args = tool_calls[0].function.arguments
97
+ args = json.loads(args)
98
+
99
+ result = await self.pocket.ainvoke(tool_name=tool_name, body=args)
100
+ output = ast.literal_eval(result)
101
+
102
+ # then
103
+ self.assertEqual(tool_name, "simple_echo_text")
104
+ self.assertEqual(args, {
105
+ "text": "hello world"
106
+ })
107
+ self.assertTrue(output["stdout"].startswith("echo message : hello world"))
108
+
109
+ @staticmethod
110
+ def add(a: int, b: int) -> int:
111
+ """
112
+ Add two numbers
113
+
114
+ Args:
115
+ a(int): first number
116
+ b(int): second number
117
+
118
+ """
119
+
120
+ return a + b
121
+
122
+ class FirstNumber(BaseModel):
123
+ first: int
124
+
125
+ class SecondNumber(BaseModel):
126
+ second: int
127
+
128
+ @staticmethod
129
+ def sub_pydantic_args(a: FirstNumber, b: SecondNumber):
130
+ """
131
+ sub two numbers
132
+
133
+ Args:
134
+ a(FirstNumber): first number
135
+ b(SecondNumber): second number
136
+ """
137
+ return a.first - b.second
@@ -1,3 +1,4 @@
1
+ import ast
1
2
  import json
2
3
  from unittest.async_case import IsolatedAsyncioTestCase
3
4
 
@@ -10,7 +11,7 @@ from hyperpocket.tool import from_git
10
11
  from hyperpocket_llamaindex import PocketLlamaindex
11
12
 
12
13
 
13
- class TestPocketLlamaindex(IsolatedAsyncioTestCase):
14
+ class TestPocketLlamaindexUseProfile(IsolatedAsyncioTestCase):
14
15
 
15
16
  async def asyncSetUp(self):
16
17
  config.public_server_port = "https"
@@ -21,22 +22,31 @@ class TestPocketLlamaindex(IsolatedAsyncioTestCase):
21
22
 
22
23
  self.pocket = PocketLlamaindex(
23
24
  tools=[
24
- from_git("https://github.com/vessl-ai/hyperawesometools", "main", "managed-tools/simple-echo-tool"),
25
+ from_git("https://github.com/vessl-ai/hyperawesometools", "main",
26
+ "managed-tools/none/simple-echo-tool"),
25
27
  self.add,
26
28
  self.sub_pydantic_args
27
29
  ],
30
+ use_profile=True
28
31
  )
29
32
 
30
- self.agent = OpenAIAgent.from_tools(
33
+ self.llm = OpenAI(model="gpt-4o", api_key=secret["OPENAI_API_KEY"])
34
+
35
+ async def asyncTearDown(self):
36
+ self.pocket._teardown_server()
37
+
38
+ async def test_agent_use_profile(self):
39
+ # given
40
+ agent = OpenAIAgent.from_tools(
31
41
  tools=self.pocket.get_tools(),
32
42
  llm=OpenAI(model="gpt-4o", api_key=secret["OPENAI_API_KEY"]),
33
43
  verbose=True
34
44
  )
35
45
 
36
- self.llm = OpenAI(model="gpt-4o", api_key=secret["OPENAI_API_KEY"])
37
-
38
- async def asyncTearDown(self):
39
- self.pocket._teardown_server()
46
+ # when
47
+ agent.query("add 1, 2")
48
+ agent.query("sub 1, 2")
49
+ agent.query("echo 'hello world'")
40
50
 
41
51
  async def test_function_tool(self):
42
52
  # when
@@ -87,13 +97,14 @@ class TestPocketLlamaindex(IsolatedAsyncioTestCase):
87
97
  args = json.loads(args)
88
98
 
89
99
  result = await self.pocket.ainvoke(tool_name=tool_name, **args)
100
+ output = ast.literal_eval(result)
90
101
 
91
102
  # then
92
103
  self.assertEqual(tool_name, "simple_echo_text")
93
104
  self.assertEqual(args["body"], {
94
105
  "text": "hello world"
95
106
  })
96
- self.assertTrue(result.startswith("echo message : hello world"))
107
+ self.assertTrue(output["stdout"].startswith("echo message : hello world"))
97
108
 
98
109
  @staticmethod
99
110
  def add(a: int, b: int) -> int: