opengradient 0.3.9__py3-none-any.whl → 0.3.11__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 +2 -1
- opengradient/llm/__init__.py +5 -0
- opengradient/llm/chat.py +119 -0
- {opengradient-0.3.9.dist-info → opengradient-0.3.11.dist-info}/METADATA +1 -2
- opengradient-0.3.11.dist-info/RECORD +17 -0
- opengradient-0.3.9.dist-info/RECORD +0 -15
- {opengradient-0.3.9.dist-info → opengradient-0.3.11.dist-info}/LICENSE +0 -0
- {opengradient-0.3.9.dist-info → opengradient-0.3.11.dist-info}/WHEEL +0 -0
- {opengradient-0.3.9.dist-info → opengradient-0.3.11.dist-info}/entry_points.txt +0 -0
- {opengradient-0.3.9.dist-info → opengradient-0.3.11.dist-info}/top_level.txt +0 -0
opengradient/__init__.py
CHANGED
|
@@ -3,8 +3,9 @@ from typing import Dict, List, Optional, Tuple
|
|
|
3
3
|
from .client import Client
|
|
4
4
|
from .defaults import DEFAULT_INFERENCE_CONTRACT_ADDRESS, DEFAULT_RPC_URL
|
|
5
5
|
from .types import InferenceMode, LLM
|
|
6
|
+
from . import llm
|
|
6
7
|
|
|
7
|
-
__version__ = "0.3.
|
|
8
|
+
__version__ = "0.3.11"
|
|
8
9
|
|
|
9
10
|
_client = None
|
|
10
11
|
|
opengradient/llm/chat.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
from typing import List, Dict, Optional, Any, Sequence, Union
|
|
2
|
+
import json
|
|
3
|
+
|
|
4
|
+
from langchain.chat_models.base import BaseChatModel
|
|
5
|
+
from langchain.schema import (
|
|
6
|
+
AIMessage,
|
|
7
|
+
HumanMessage,
|
|
8
|
+
SystemMessage,
|
|
9
|
+
BaseMessage,
|
|
10
|
+
ChatResult,
|
|
11
|
+
ChatGeneration,
|
|
12
|
+
)
|
|
13
|
+
from langchain_core.callbacks.manager import CallbackManagerForLLMRun
|
|
14
|
+
from langchain_core.tools import BaseTool
|
|
15
|
+
from langchain_core.messages import ToolCall
|
|
16
|
+
|
|
17
|
+
from opengradient import Client
|
|
18
|
+
from opengradient.defaults import DEFAULT_RPC_URL, DEFAULT_INFERENCE_CONTRACT_ADDRESS
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class OpenGradientChatModel(BaseChatModel):
|
|
22
|
+
"""OpenGradient adapter class for LangChain chat model"""
|
|
23
|
+
|
|
24
|
+
client: Client = None
|
|
25
|
+
model_cid: str = None
|
|
26
|
+
max_tokens: int = None
|
|
27
|
+
tools: List[Dict] = []
|
|
28
|
+
|
|
29
|
+
def __init__(self, private_key: str, model_cid: str, max_tokens: int = 300):
|
|
30
|
+
super().__init__()
|
|
31
|
+
self.client = Client(
|
|
32
|
+
private_key=private_key,
|
|
33
|
+
rpc_url=DEFAULT_RPC_URL,
|
|
34
|
+
contract_address=DEFAULT_INFERENCE_CONTRACT_ADDRESS,
|
|
35
|
+
email=None,
|
|
36
|
+
password=None)
|
|
37
|
+
self.model_cid = model_cid
|
|
38
|
+
self.max_tokens = max_tokens
|
|
39
|
+
|
|
40
|
+
@property
|
|
41
|
+
def _llm_type(self) -> str:
|
|
42
|
+
return "opengradient"
|
|
43
|
+
|
|
44
|
+
def bind_tools(
|
|
45
|
+
self,
|
|
46
|
+
tools: Sequence[Union[BaseTool, Dict]],
|
|
47
|
+
) -> "OpenGradientChatModel":
|
|
48
|
+
"""Bind tools to the model."""
|
|
49
|
+
tool_dicts = []
|
|
50
|
+
for tool in tools:
|
|
51
|
+
if isinstance(tool, BaseTool):
|
|
52
|
+
tool_dicts.append({
|
|
53
|
+
"type": "function",
|
|
54
|
+
"function": {
|
|
55
|
+
"name": tool.name,
|
|
56
|
+
"description": tool.description,
|
|
57
|
+
"parameters": tool.args_schema.schema() if hasattr(tool, "args_schema") else {}
|
|
58
|
+
}
|
|
59
|
+
})
|
|
60
|
+
else:
|
|
61
|
+
tool_dicts.append(tool)
|
|
62
|
+
|
|
63
|
+
self.tools = tool_dicts
|
|
64
|
+
return self
|
|
65
|
+
|
|
66
|
+
def _generate(
|
|
67
|
+
self,
|
|
68
|
+
messages: List[BaseMessage],
|
|
69
|
+
stop: Optional[List[str]] = None,
|
|
70
|
+
run_manager: Optional[CallbackManagerForLLMRun] = None,
|
|
71
|
+
**kwargs: Any,
|
|
72
|
+
) -> ChatResult:
|
|
73
|
+
chat_messages = []
|
|
74
|
+
for message in messages:
|
|
75
|
+
if isinstance(message, SystemMessage):
|
|
76
|
+
chat_messages.append({"role": "system", "content": message.content})
|
|
77
|
+
elif isinstance(message, HumanMessage):
|
|
78
|
+
chat_messages.append({"role": "user", "content": message.content})
|
|
79
|
+
elif isinstance(message, AIMessage):
|
|
80
|
+
chat_messages.append({"role": "assistant", "content": message.content})
|
|
81
|
+
|
|
82
|
+
tx_hash, finish_reason, chat_response = self.client.llm_chat(
|
|
83
|
+
model_cid=self.model_cid,
|
|
84
|
+
messages=chat_messages,
|
|
85
|
+
stop_sequence=stop,
|
|
86
|
+
max_tokens=self.max_tokens,
|
|
87
|
+
tools=self.tools
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
if "tool_calls" in chat_response:
|
|
91
|
+
tool_calls = []
|
|
92
|
+
for tool_call in chat_response["tool_calls"]:
|
|
93
|
+
tool_calls.append(
|
|
94
|
+
ToolCall(
|
|
95
|
+
id=tool_call.get("id", ""),
|
|
96
|
+
name=tool_call["name"],
|
|
97
|
+
args=json.loads(tool_call["arguments"])
|
|
98
|
+
)
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
message = AIMessage(
|
|
102
|
+
content='',
|
|
103
|
+
tool_calls=tool_calls
|
|
104
|
+
)
|
|
105
|
+
else:
|
|
106
|
+
message = AIMessage(content=chat_response["content"])
|
|
107
|
+
|
|
108
|
+
return ChatResult(
|
|
109
|
+
generations=[ChatGeneration(
|
|
110
|
+
message=message,
|
|
111
|
+
generation_info={"finish_reason": finish_reason}
|
|
112
|
+
)]
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
@property
|
|
116
|
+
def _identifying_params(self) -> Dict[str, Any]:
|
|
117
|
+
return {
|
|
118
|
+
"model_name": self.model_cid,
|
|
119
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: opengradient
|
|
3
|
-
Version: 0.3.
|
|
3
|
+
Version: 0.3.11
|
|
4
4
|
Summary: Python SDK for OpenGradient decentralized model management & inference services
|
|
5
5
|
Author-email: OpenGradient <oliver@opengradient.ai>
|
|
6
6
|
License: MIT License
|
|
@@ -87,7 +87,6 @@ Requires-Dist: keyring==24.3.1
|
|
|
87
87
|
Requires-Dist: more-itertools==10.5.0
|
|
88
88
|
Requires-Dist: msgpack==1.1.0
|
|
89
89
|
Requires-Dist: multidict==6.1.0
|
|
90
|
-
Requires-Dist: numpy==2.1.1
|
|
91
90
|
Requires-Dist: packaging==24.1
|
|
92
91
|
Requires-Dist: pandas==2.2.3
|
|
93
92
|
Requires-Dist: parsimonious==0.10.0
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
opengradient/__init__.py,sha256=D5AItNKf3klN25oWw3YAQMSYM9uOYBLGIaw7X6TD2-Q,3129
|
|
2
|
+
opengradient/account.py,sha256=2B7rtCXQDX-yF4U69h8B9-OUreJU4IqoGXG_1Hn9nWs,1150
|
|
3
|
+
opengradient/cli.py,sha256=0SRt9iQcCHxR1QmsF54-KhdpqHx9_va0UyckoPQcYwg,22937
|
|
4
|
+
opengradient/client.py,sha256=yaexPTr-CSPQXR5AOrhHDmygKCq37Q_FIVy8Mtq6THk,31253
|
|
5
|
+
opengradient/defaults.py,sha256=jweJ6QyzNY0oO22OUA8B-uJPA90cyedhMT9J38MmGjw,319
|
|
6
|
+
opengradient/exceptions.py,sha256=v4VmUGTvvtjhCZAhR24Ga42z3q-DzR1Y5zSqP_yn2Xk,3366
|
|
7
|
+
opengradient/types.py,sha256=kRCkxJ2xD6Y5eLmrrS61t66qm7jzNF0Qbnntl1_FMKk,2143
|
|
8
|
+
opengradient/utils.py,sha256=_dEIhepJXjJFfHLGTUwXloZJXnlQbvwqHSPu08548jI,6532
|
|
9
|
+
opengradient/abi/inference.abi,sha256=VMxv4pli9ESYL2hCpbU41Z_WweCBy_3EcTYkCWCb-rU,6623
|
|
10
|
+
opengradient/llm/__init__.py,sha256=n_11WFPoU8YtGc6wg9cK6gEy9zBISf1183Loip3dAbI,62
|
|
11
|
+
opengradient/llm/chat.py,sha256=Ia-K0og-DkZ1_IFgbgscn_gpLMXZX54F3fvfSrUq5M8,3849
|
|
12
|
+
opengradient-0.3.11.dist-info/LICENSE,sha256=xEcvQ3AxZOtDkrqkys2Mm6Y9diEnaSeQRKvxi-JGnNA,1069
|
|
13
|
+
opengradient-0.3.11.dist-info/METADATA,sha256=RDat9N9hKfSIWgrx9JOwXmBcKrLtvbDM8wqdCzSgPNA,7581
|
|
14
|
+
opengradient-0.3.11.dist-info/WHEEL,sha256=R06PA3UVYHThwHvxuRWMqaGcr-PuniXahwjmQRFMEkY,91
|
|
15
|
+
opengradient-0.3.11.dist-info/entry_points.txt,sha256=yUKTaJx8RXnybkob0J62wVBiCp_1agVbgw9uzsmaeJc,54
|
|
16
|
+
opengradient-0.3.11.dist-info/top_level.txt,sha256=oC1zimVLa2Yi1LQz8c7x-0IQm92milb5ax8gHBHwDqU,13
|
|
17
|
+
opengradient-0.3.11.dist-info/RECORD,,
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
opengradient/__init__.py,sha256=eExTI4XJMp1rPcUy5jH2xi_P4qKph0P98Qf4xJ3yQ4I,3110
|
|
2
|
-
opengradient/account.py,sha256=2B7rtCXQDX-yF4U69h8B9-OUreJU4IqoGXG_1Hn9nWs,1150
|
|
3
|
-
opengradient/cli.py,sha256=0SRt9iQcCHxR1QmsF54-KhdpqHx9_va0UyckoPQcYwg,22937
|
|
4
|
-
opengradient/client.py,sha256=yaexPTr-CSPQXR5AOrhHDmygKCq37Q_FIVy8Mtq6THk,31253
|
|
5
|
-
opengradient/defaults.py,sha256=jweJ6QyzNY0oO22OUA8B-uJPA90cyedhMT9J38MmGjw,319
|
|
6
|
-
opengradient/exceptions.py,sha256=v4VmUGTvvtjhCZAhR24Ga42z3q-DzR1Y5zSqP_yn2Xk,3366
|
|
7
|
-
opengradient/types.py,sha256=kRCkxJ2xD6Y5eLmrrS61t66qm7jzNF0Qbnntl1_FMKk,2143
|
|
8
|
-
opengradient/utils.py,sha256=_dEIhepJXjJFfHLGTUwXloZJXnlQbvwqHSPu08548jI,6532
|
|
9
|
-
opengradient/abi/inference.abi,sha256=VMxv4pli9ESYL2hCpbU41Z_WweCBy_3EcTYkCWCb-rU,6623
|
|
10
|
-
opengradient-0.3.9.dist-info/LICENSE,sha256=xEcvQ3AxZOtDkrqkys2Mm6Y9diEnaSeQRKvxi-JGnNA,1069
|
|
11
|
-
opengradient-0.3.9.dist-info/METADATA,sha256=ZAayPv7Ugn-NZqpjDEXTm56djR8iePhbHWr5Ajx-Ma8,7608
|
|
12
|
-
opengradient-0.3.9.dist-info/WHEEL,sha256=R06PA3UVYHThwHvxuRWMqaGcr-PuniXahwjmQRFMEkY,91
|
|
13
|
-
opengradient-0.3.9.dist-info/entry_points.txt,sha256=yUKTaJx8RXnybkob0J62wVBiCp_1agVbgw9uzsmaeJc,54
|
|
14
|
-
opengradient-0.3.9.dist-info/top_level.txt,sha256=oC1zimVLa2Yi1LQz8c7x-0IQm92milb5ax8gHBHwDqU,13
|
|
15
|
-
opengradient-0.3.9.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|