opengradient 0.3.20__py3-none-any.whl → 0.3.21__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
opengradient/__init__.py CHANGED
@@ -7,9 +7,9 @@ from typing import Dict, List, Optional, Tuple
7
7
  from .client import Client
8
8
  from .defaults import DEFAULT_INFERENCE_CONTRACT_ADDRESS, DEFAULT_RPC_URL
9
9
  from .types import InferenceMode, LlmInferenceMode, LLM, TEE_LLM
10
- from . import llm
11
10
 
12
- __version__ = "0.3.20"
11
+ from . import llm
12
+ from . import mltools
13
13
 
14
14
  _client = None
15
15
 
@@ -241,7 +241,7 @@ def generate_image(model: str, prompt: str, height: Optional[int] = None, width:
241
241
 
242
242
  __all__ = [
243
243
  'generate_image',
244
- 'list_files'
244
+ 'list_files',
245
245
  'login',
246
246
  'llm_chat',
247
247
  'llm_completion',
@@ -261,6 +261,7 @@ __pdoc__ = {
261
261
  'defaults': False,
262
262
  'exceptions': False,
263
263
  'llm': True,
264
+ 'mltools': True,
264
265
  'proto': False,
265
266
  'types': False,
266
267
  'utils': False
@@ -17,7 +17,7 @@ from langchain_core.callbacks.manager import CallbackManagerForLLMRun
17
17
  from langchain_core.tools import BaseTool
18
18
  from langchain_core.messages import ToolCall
19
19
 
20
- from opengradient import Client
20
+ from opengradient import Client, LlmInferenceMode
21
21
  from opengradient.defaults import DEFAULT_RPC_URL, DEFAULT_INFERENCE_CONTRACT_ADDRESS
22
22
 
23
23
 
@@ -99,7 +99,8 @@ class OpenGradientChatModel(BaseChatModel):
99
99
  messages=sdk_messages,
100
100
  stop_sequence=stop,
101
101
  max_tokens=self.max_tokens,
102
- tools=self.tools
102
+ tools=self.tools,
103
+ inference_mode=LlmInferenceMode.VANILLA
103
104
  )
104
105
 
105
106
  if "tool_calls" in chat_response and chat_response["tool_calls"]:
@@ -4,7 +4,6 @@ from opengradient.defaults import DEFAULT_RPC_URL, DEFAULT_INFERENCE_CONTRACT_AD
4
4
 
5
5
  from typing import List
6
6
  import time
7
- import json
8
7
  import uuid
9
8
 
10
9
  class OGCompletions(object):
@@ -0,0 +1,14 @@
1
+ """
2
+ OpenGradient AlphaSense Tools
3
+ """
4
+
5
+ from .model_tool import *
6
+
7
+ __all__ = [
8
+ 'create_og_model_tool',
9
+ 'ToolType'
10
+ ]
11
+
12
+ __pdoc__ = {
13
+ 'model_tool': False
14
+ }
@@ -0,0 +1,129 @@
1
+ from enum import Enum
2
+ from typing import Callable, Dict, Any, Type
3
+
4
+ from pydantic import BaseModel
5
+ from langchain_core.tools import BaseTool, StructuredTool
6
+ import opengradient as og
7
+
8
+ class ToolType(str, Enum):
9
+ """Indicates the framework the tool is compatible with."""
10
+
11
+ LANGCHAIN = "langchain"
12
+ SWARM = "swarm"
13
+
14
+ def __str__(self) -> str:
15
+ return self.value
16
+
17
+ def create_og_model_tool(
18
+ tool_type: ToolType,
19
+ model_cid: str,
20
+ tool_name: str,
21
+ input_getter: Callable,
22
+ output_formatter: Callable[..., str],
23
+ input_schema: Type[BaseModel] = None,
24
+ tool_description: str = "Executes the given ML model",
25
+ inference_mode: og.InferenceMode= og.InferenceMode.VANILLA) -> BaseTool:
26
+ """
27
+ Creates a tool that wraps an OpenGradient model for inference.
28
+
29
+ This function generates a tool that can be integrated into either a LangChain pipeline
30
+ or a Swarm system, allowing the model to be executed as part of a chain of operations.
31
+ The tool uses the provided input_getter function to obtain the necessary input data and
32
+ runs inference using the specified OpenGradient model.
33
+
34
+ Args:
35
+ tool_type (ToolType): Specifies the framework to create the tool for. Use
36
+ ToolType.LANGCHAIN for LangChain integration or ToolType.SWARM for Swarm
37
+ integration.
38
+ model_cid (str): The CID of the OpenGradient model to be executed.
39
+ tool_name (str): The name to assign to the created tool. This will be used to identify
40
+ and invoke the tool within the agent.
41
+ input_getter (Callable): A function that returns the input data required by the model.
42
+ The function should return data in a format compatible with the model's expectations.
43
+ output_formatter (Callable[..., str]): A function that takes the model output and
44
+ formats it into a string. This is required to ensure the output is compatible
45
+ with the tool framework.
46
+ input_schema (Type[BaseModel], optional): A Pydantic BaseModel class defining the
47
+ input schema. This will be used directly for LangChain tools and converted
48
+ to appropriate annotations for Swarm tools. Default is None.
49
+ tool_description (str, optional): A description of what the tool does. Defaults to
50
+ "Executes the given ML model".
51
+ inference_mode (og.InferenceMode, optional): The inference mode to use when running
52
+ the model. Defaults to VANILLA.
53
+
54
+ Returns:
55
+ BaseTool: For ToolType.LANGCHAIN, returns a LangChain StructuredTool.
56
+ Callable: For ToolType.SWARM, returns a decorated function with appropriate metadata.
57
+
58
+ Raises:
59
+ ValueError: If an invalid tool_type is provided.
60
+
61
+ Examples:
62
+ >>> from pydantic import BaseModel, Field
63
+ >>> class ClassifierInput(BaseModel):
64
+ ... query: str = Field(description="User query to analyze")
65
+ ... parameters: dict = Field(description="Additional parameters")
66
+ >>> def get_input():
67
+ ... return {"text": "Sample input text"}
68
+ >>> def format_output(output):
69
+ ... return str(output.get("class", "Unknown"))
70
+ >>> # Create a LangChain tool
71
+ >>> langchain_tool = create_og_model_tool(
72
+ ... tool_type=ToolType.LANGCHAIN,
73
+ ... model_cid="Qm...",
74
+ ... tool_name="text_classifier",
75
+ ... input_getter=get_input,
76
+ ... output_formatter=format_output,
77
+ ... input_schema=ClassifierInput
78
+ ... tool_description="Classifies text into categories"
79
+ ... )
80
+ """
81
+ # define runnable
82
+ def model_executor(**llm_input):
83
+ # Combine LLM input with input provided by code
84
+ combined_input = {
85
+ **llm_input,
86
+ **input_getter()
87
+ }
88
+
89
+ _, output = og.infer(
90
+ model_cid=model_cid,
91
+ inference_mode=inference_mode,
92
+ model_input=combined_input
93
+ )
94
+
95
+ return output_formatter(output)
96
+
97
+ if tool_type == ToolType.LANGCHAIN:
98
+ return StructuredTool.from_function(
99
+ func=model_executor,
100
+ name=tool_name,
101
+ description=tool_description,
102
+ args_schema=input_schema
103
+ )
104
+ elif tool_type == ToolType.SWARM:
105
+ model_executor.__name__ = tool_name
106
+ model_executor.__doc__ = tool_description
107
+ # Convert Pydantic model to Swarm annotations if provided
108
+ if input_schema:
109
+ model_executor.__annotations__ = _convert_pydantic_to_annotations(input_schema)
110
+ return model_executor
111
+ else:
112
+ raise ValueError(f"Invalid tooltype: {tool_type}")
113
+
114
+ def _convert_pydantic_to_annotations(model: Type[BaseModel]) -> Dict[str, Any]:
115
+ """
116
+ Convert a Pydantic model to function annotations format used by Swarm.
117
+
118
+ Args:
119
+ model: A Pydantic BaseModel class
120
+
121
+ Returns:
122
+ Dict mapping field names to (type, description) tuples
123
+ """
124
+ annotations = {}
125
+ for field_name, field in model.model_fields.items():
126
+ field_type = field.annotation
127
+ description = field.description or ""
128
+ annotations[field_name] = (field_type, description)
129
+ return annotations
@@ -1,8 +1,7 @@
1
- Metadata-Version: 2.4
1
+ Metadata-Version: 2.2
2
2
  Name: opengradient
3
- Version: 0.3.20
3
+ Version: 0.3.21
4
4
  Summary: Python SDK for OpenGradient decentralized model management & inference services
5
- Project-URL: Homepage, https://opengradient.ai
6
5
  Author-email: OpenGradient <oliver@opengradient.ai>
7
6
  License: MIT License
8
7
 
@@ -25,7 +24,8 @@ License: MIT License
25
24
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
25
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27
26
  SOFTWARE.
28
- License-File: LICENSE
27
+
28
+ Project-URL: Homepage, https://opengradient.ai
29
29
  Classifier: Development Status :: 3 - Alpha
30
30
  Classifier: Intended Audience :: Developers
31
31
  Classifier: License :: OSI Approved :: MIT License
@@ -33,13 +33,15 @@ Classifier: Programming Language :: Python :: 3.10
33
33
  Classifier: Programming Language :: Python :: 3.11
34
34
  Classifier: Programming Language :: Python :: 3.12
35
35
  Requires-Python: >=3.10
36
+ Description-Content-Type: text/markdown
37
+ License-File: LICENSE
36
38
  Requires-Dist: aiohappyeyeballs>=2.4.3
37
39
  Requires-Dist: aiohttp>=3.10.8
38
40
  Requires-Dist: aiosignal>=1.3.1
39
41
  Requires-Dist: annotated-types>=0.7.0
40
42
  Requires-Dist: attrs>=24.2.0
41
43
  Requires-Dist: bitarray>=2.9.2
42
- Requires-Dist: cachecontrol>=0.14.0
44
+ Requires-Dist: CacheControl>=0.14.0
43
45
  Requires-Dist: cachetools>=5.5.0
44
46
  Requires-Dist: certifi>=2024.8.30
45
47
  Requires-Dist: cffi>=1.17.1
@@ -53,7 +55,6 @@ Requires-Dist: cryptography>=43.0.1
53
55
  Requires-Dist: cytoolz>=0.12.3
54
56
  Requires-Dist: distlib>=0.3.8
55
57
  Requires-Dist: dulwich>=0.21.7
56
- Requires-Dist: eth-abi>=5.1.0
57
58
  Requires-Dist: eth-account>=0.13.4
58
59
  Requires-Dist: eth-hash>=0.7.0
59
60
  Requires-Dist: eth-keyfile>=0.8.1
@@ -61,6 +62,7 @@ Requires-Dist: eth-keys>=0.5.1
61
62
  Requires-Dist: eth-rlp>=2.1.0
62
63
  Requires-Dist: eth-typing>=5.0.0
63
64
  Requires-Dist: eth-utils>=5.0.0
65
+ Requires-Dist: eth_abi>=5.1.0
64
66
  Requires-Dist: fastjsonschema>=2.20.0
65
67
  Requires-Dist: fastparquet>=2024.5.0
66
68
  Requires-Dist: filelock>=3.16.1
@@ -75,11 +77,12 @@ Requires-Dist: google-cloud-storage>=2.18.2
75
77
  Requires-Dist: google-crc32c>=1.6.0
76
78
  Requires-Dist: google-resumable-media>=2.7.2
77
79
  Requires-Dist: googleapis-common-protos>=1.65.0
78
- Requires-Dist: grpcio-tools>=1.66.2
79
80
  Requires-Dist: grpcio>=1.66.2
81
+ Requires-Dist: grpcio-tools>=1.66.2
82
+ Requires-Dist: protobuf>=4.24.0
80
83
  Requires-Dist: hexbytes>=1.2.1
81
84
  Requires-Dist: idna>=3.10
82
- Requires-Dist: jaraco-classes>=3.4.0
85
+ Requires-Dist: jaraco.classes>=3.4.0
83
86
  Requires-Dist: jwcrypto>=1.5.6
84
87
  Requires-Dist: keyring>=24.3.1
85
88
  Requires-Dist: langchain>=0.3.7
@@ -96,25 +99,24 @@ Requires-Dist: pkce>=1.0.3
96
99
  Requires-Dist: pkginfo>=1.11.1
97
100
  Requires-Dist: platformdirs>=4.3.6
98
101
  Requires-Dist: proto-plus>=1.24.0
99
- Requires-Dist: protobuf>=4.24.0
100
102
  Requires-Dist: protobuf>=5.28.2
101
103
  Requires-Dist: ptyprocess>=0.7.0
102
104
  Requires-Dist: pyarrow>=17.0.0
103
- Requires-Dist: pyasn1-modules>=0.4.1
104
105
  Requires-Dist: pyasn1>=0.6.1
106
+ Requires-Dist: pyasn1_modules>=0.4.1
105
107
  Requires-Dist: pycparser>=2.22
106
108
  Requires-Dist: pycryptodome>=3.21.0
107
- Requires-Dist: pydantic-core>=2.23.4
108
109
  Requires-Dist: pydantic>=2.9.2
109
- Requires-Dist: pyproject-hooks>=1.2.0
110
+ Requires-Dist: pydantic_core>=2.23.4
111
+ Requires-Dist: pyproject_hooks>=1.2.0
110
112
  Requires-Dist: python-dateutil>=2.9.0.post0
111
113
  Requires-Dist: python-jwt>=4.1.0
112
114
  Requires-Dist: pytz>=2024.2
113
115
  Requires-Dist: pyunormalize>=16.0.0
114
- Requires-Dist: rapidfuzz>=3.10.0
116
+ Requires-Dist: RapidFuzz>=3.10.0
115
117
  Requires-Dist: regex>=2024.9.11
116
- Requires-Dist: requests-toolbelt>=1.0.0
117
118
  Requires-Dist: requests>=2.32.3
119
+ Requires-Dist: requests-toolbelt>=1.0.0
118
120
  Requires-Dist: rlp>=4.0.1
119
121
  Requires-Dist: rsa>=4.9
120
122
  Requires-Dist: shellingham>=1.5.4
@@ -123,14 +125,13 @@ Requires-Dist: tomlkit>=0.13.2
123
125
  Requires-Dist: toolz>=0.12.1
124
126
  Requires-Dist: trove-classifiers>=2024.9.12
125
127
  Requires-Dist: types-requests>=2.32.0.20240914
126
- Requires-Dist: typing-extensions>=4.12.2
128
+ Requires-Dist: typing_extensions>=4.12.2
127
129
  Requires-Dist: tzdata>=2024.2
128
130
  Requires-Dist: urllib3>=2.2.3
129
131
  Requires-Dist: web3>=7.3.0
130
132
  Requires-Dist: websockets>=13.1
131
133
  Requires-Dist: xattr>=1.1.0
132
134
  Requires-Dist: yarl>=1.13.1
133
- Description-Content-Type: text/markdown
134
135
 
135
136
  # OpenGradient Python SDK
136
137
  Python SDK for the OpenGradient platform provides decentralized model management & inference services. Python SDK allows programmatic access to our model repository and decentralized AI infrastructure.
@@ -272,4 +273,4 @@ Or you can use files instead of text input in order to simplify your command:
272
273
  opengradient chat --model "mistralai/Mistral-7B-Instruct-v0.3" --messages-file messages.json --tools-file tools.json --max-tokens 200
273
274
  ```
274
275
 
275
- For more information read the OpenGradient [documentation](https://docs.opengradient.ai/).
276
+ For more information read the OpenGradient [documentation](https://docs.opengradient.ai/).
@@ -1,4 +1,4 @@
1
- opengradient/__init__.py,sha256=2kBCVdEjuZvLDFjivX_aQSn7wnTLxYVTixy4jLQSXts,9262
1
+ opengradient/__init__.py,sha256=rwR7EMpzimOALAPP5dQa_aNQNAntefJeGal7jCQ3Iqc,9283
2
2
  opengradient/account.py,sha256=2B7rtCXQDX-yF4U69h8B9-OUreJU4IqoGXG_1Hn9nWs,1150
3
3
  opengradient/cli.py,sha256=niN8tlLaiVEpdtkdWEUbxidG75nxrlb6mMUfUAIjiVw,26400
4
4
  opengradient/client.py,sha256=axMbfqAQ6OWq3sM_D4bGVWrpAd-15Ru-bOHJ6R6GruA,35197
@@ -8,14 +8,17 @@ opengradient/types.py,sha256=-lGWv_yfXMN48bbvARKIFrj1L0AotIwr2c7GOv1JBcI,2464
8
8
  opengradient/utils.py,sha256=lUDPmyPqLwpZI-owyN6Rm3QvUjOn5pLN5G1QyriVm-E,6994
9
9
  opengradient/abi/inference.abi,sha256=MR5u9npZ-Yx2EqRW17_M-UnGgFF3mMEMepOwaZ-Bkgc,7040
10
10
  opengradient/llm/__init__.py,sha256=J1W_AKPntqlDqLeflhn2x7A0i-dkMT-ol3jlEdFgMWU,1135
11
- opengradient/llm/og_langchain.py,sha256=F32yN1o8EvRbzZSJkUwI0-FmSVAWMuMTI9ho7wgW5hk,4470
12
- opengradient/llm/og_openai.py,sha256=GilEkIVDac5Cacennb7YNXD6BKwUj69mfnMkvxkiW5Y,3865
11
+ opengradient/llm/og_langchain.py,sha256=-QBGFdQJ1cdSCTUUerajHXNvV5Xi9yaVpa3BeAqKZbQ,4541
12
+ opengradient/llm/og_openai.py,sha256=3oaQOQnA7RzQ-LcFw-AQRn2RON8tsprSa9bGtyD88OE,3853
13
+ opengradient/mltools/__init__.py,sha256=cfzf6lZmqLNN1efa5K_pCa7k1D5PnRkB6LNyJ9UeJvk,162
14
+ opengradient/mltools/model_tool.py,sha256=q1k3Zq7jmRe-8HqP_-ia-p7ocDRcSn18T4gbNM9xlD0,5237
13
15
  opengradient/proto/__init__.py,sha256=AhaSmrqV0TXGzCKaoPV8-XUvqs2fGAJBM2aOmDpkNbE,55
14
16
  opengradient/proto/infer.proto,sha256=13eaEMcppxkBF8yChptsX9HooWFwJKze7oLZNl-LEb8,1217
15
17
  opengradient/proto/infer_pb2.py,sha256=wg2vjLQCNv6HRhYuIqgj9xivi3nO4IPz6E5wh2dhDqY,3446
16
18
  opengradient/proto/infer_pb2_grpc.py,sha256=y5GYwD1EdNs892xx58jdfyA0fO5QC7k3uZOtImTHMiE,6891
17
- opengradient-0.3.20.dist-info/METADATA,sha256=ePeui6GaKauFQNAKa-ZRNx0VKWjVOKg0ZZYN77Mjiew,8744
18
- opengradient-0.3.20.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
19
- opengradient-0.3.20.dist-info/entry_points.txt,sha256=yUKTaJx8RXnybkob0J62wVBiCp_1agVbgw9uzsmaeJc,54
20
- opengradient-0.3.20.dist-info/licenses/LICENSE,sha256=xEcvQ3AxZOtDkrqkys2Mm6Y9diEnaSeQRKvxi-JGnNA,1069
21
- opengradient-0.3.20.dist-info/RECORD,,
19
+ opengradient-0.3.21.dist-info/LICENSE,sha256=xEcvQ3AxZOtDkrqkys2Mm6Y9diEnaSeQRKvxi-JGnNA,1069
20
+ opengradient-0.3.21.dist-info/METADATA,sha256=YWusI2vSVVDeXxCp7NBwqEWlEfsPI4Y93sHs1ISKWs4,8754
21
+ opengradient-0.3.21.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
22
+ opengradient-0.3.21.dist-info/entry_points.txt,sha256=yUKTaJx8RXnybkob0J62wVBiCp_1agVbgw9uzsmaeJc,54
23
+ opengradient-0.3.21.dist-info/top_level.txt,sha256=oC1zimVLa2Yi1LQz8c7x-0IQm92milb5ax8gHBHwDqU,13
24
+ opengradient-0.3.21.dist-info/RECORD,,
@@ -1,4 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: hatchling 1.27.0
2
+ Generator: setuptools (75.8.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ opengradient