-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodelgraph.js
More file actions
211 lines (185 loc) · 6.89 KB
/
modelgraph.js
File metadata and controls
211 lines (185 loc) · 6.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { finalizeEvent, generateSecretKey, getPublicKey } from '../lib/esm/pure.js';
import { Relay, useWebSocketImplementation } from '../lib/esm/relay.js';
import {
NewSubspaceCreateEvent,
ValidateSubspaceCreateEvent,
NewSubspaceJoinEvent,
ValidateSubspaceJoinEvent,
toNostrEvent,
setParents
} from '../lib/esm/cip/subspace.js';
import { KindSubspaceCreate, DefaultCommonPrjOps, DefaultSubspaceOps, ModelGraphSubspaceOps } from '../lib/esm/cip/constants.js'
import {
newModelEvent,
newDatasetEvent,
newComputeEvent,
newAlgoEvent,
newValidEvent,
newFinetuneEvent,
newConversationEvent,
newSessionEvent,
toNostrEvent as toNostrEventModel
} from '../lib/esm/cip/cip03/modelgraph.js'
import WebSocket from 'ws';
useWebSocketImplementation(WebSocket);
// 0. Connect to the relay for test
const relayURL = 'ws://161.97.129.166:10547'
const relay = await Relay.connect(relayURL);
console.log(`connected to ${relay.url}`);
// Generate keys
let sk = generateSecretKey();
let pk = getPublicKey(sk);
// Subscribe to events
relay.subscribe([
{
kinds: [KindSubspaceCreate],
limit: 1,
},
], {
onevent(event) {
console.log('====================================');
console.log('Subscribe got event:', event);
}
})
// 1. Create a new subspace
const subspaceName = 'TestModelGraph';
const rules = 'rule1';
const description = 'This is a test subspace for model graph';
const imageURL = 'http://example.com/image.png';
const subspaceEvent = NewSubspaceCreateEvent(
subspaceName,
DefaultCommonPrjOps + "," + DefaultSubspaceOps + "," + ModelGraphSubspaceOps,
rules,
description,
imageURL
);
ValidateSubspaceCreateEvent(subspaceEvent);
// Sign and publish the subspace creation event
const signedSubspaceEvent = finalizeEvent(toNostrEvent(subspaceEvent), sk);
await relay.publish(signedSubspaceEvent);
console.log('====================================');
console.log('Subspace creation event published:', signedSubspaceEvent);
// 2. Join the subspace
const joinEvent = NewSubspaceJoinEvent(subspaceEvent.subspaceID, "Join");
ValidateSubspaceJoinEvent(joinEvent);
// Sign and publish the subspace join event
const signedJoinEvent = finalizeEvent(toNostrEvent(joinEvent), sk);
await relay.publish(signedJoinEvent);
console.log('====================================');
console.log('Subspace join event published:', signedJoinEvent);
// 3. Create a model
const modelEvent = await newModelEvent(subspaceEvent.subspaceID, "Create a new model")
if (!modelEvent) {
throw new Error('Failed to create model event')
}
modelEvent.setParent('parent-hash-123')
modelEvent.setContributions('0.5,0.3,0.2')
// Sign and publish the model event
const signedModelEvent = finalizeEvent(toNostrEventModel(modelEvent), sk);
await relay.publish(signedModelEvent);
console.log('====================================');
console.log('Model event published:', signedModelEvent);
// 4. Create a dataset
const datasetEvent = await newDatasetEvent(subspaceEvent.subspaceID, "Create a new dataset")
if (!datasetEvent) {
throw new Error('Failed to create dataset event')
}
datasetEvent.setDatasetInfo(
'proj-001',
'task-001',
'training',
'json',
['user1', 'user2']
)
// Sign and publish the dataset event
const signedDatasetEvent = finalizeEvent(toNostrEventModel(datasetEvent), sk);
await relay.publish(signedDatasetEvent);
console.log('====================================');
console.log('Dataset event published:', signedDatasetEvent);
// 5. Create a compute event
const computeEvent = await newComputeEvent(subspaceEvent.subspaceID, "Create a new compute event")
if (!computeEvent) {
throw new Error('Failed to create compute event')
}
computeEvent.setComputeType('gpu')
// Sign and publish the compute event
const signedComputeEvent = finalizeEvent(toNostrEventModel(computeEvent), sk);
await relay.publish(signedComputeEvent);
console.log('====================================');
console.log('Compute event published:', signedComputeEvent);
// 6. Create an algo event
const algoEvent = await newAlgoEvent(subspaceEvent.subspaceID, "Create a new algo event")
if (!algoEvent) {
throw new Error('Failed to create algo event')
}
algoEvent.setAlgoType('gradient-descent')
// Sign and publish the algo event
const signedAlgoEvent = finalizeEvent(toNostrEventModel(algoEvent), sk);
await relay.publish(signedAlgoEvent);
console.log('====================================');
console.log('Algo event published:', signedAlgoEvent);
// 7. Create a valid event
const validEvent = await newValidEvent(subspaceEvent.subspaceID, "Create a new valid event")
if (!validEvent) {
throw new Error('Failed to create valid event')
}
validEvent.setValidResult('accuracy=0.95')
// Sign and publish the valid event
const signedValidEvent = finalizeEvent(toNostrEventModel(validEvent), sk);
await relay.publish(signedValidEvent);
console.log('====================================');
console.log('Valid event published:', signedValidEvent);
// 8. Create a finetune event
const finetuneEvent = await newFinetuneEvent(subspaceEvent.subspaceID, "Create a new finetune event")
if (!finetuneEvent) {
throw new Error('Failed to create finetune event')
}
finetuneEvent.setFinetuneInfo(
'proj-001',
'task-001',
'dataset-001',
'provider-001',
'gpt-3.5'
)
// Sign and publish the finetune event
const signedFinetuneEvent = finalizeEvent(toNostrEventModel(finetuneEvent), sk);
await relay.publish(signedFinetuneEvent);
console.log('====================================');
console.log('Finetune event published:', signedFinetuneEvent);
// 9. Create a conversation event
const conversationEvent = await newConversationEvent(subspaceEvent.subspaceID, "Create a new conversation event")
if (!conversationEvent) {
throw new Error('Failed to create conversation event')
}
conversationEvent.setConversationInfo(
'session-001',
'user-001',
'model-001',
Date.now().toString(),
'interaction-hash-123'
)
// Sign and publish the conversation event
const signedConversationEvent = finalizeEvent(toNostrEventModel(conversationEvent), sk);
await relay.publish(signedConversationEvent);
console.log('====================================');
console.log('Conversation event published:', signedConversationEvent);
// 10. Create a session event
const sessionEvent = await newSessionEvent(subspaceEvent.subspaceID, "Create a new session event")
if (!sessionEvent) {
throw new Error('Failed to create session event')
}
const startTime = Date.now()
const endTime = startTime + 3600000 // 1 hour later
sessionEvent.setSessionInfo(
'session-001',
'start',
'user-001',
startTime.toString(),
endTime.toString()
)
// Sign and publish the session event
const signedSessionEvent = finalizeEvent(toNostrEventModel(sessionEvent), sk);
await relay.publish(signedSessionEvent);
console.log('====================================');
console.log('Session event published:', signedSessionEvent);
relay.close();