-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathprotocol.py
418 lines (363 loc) · 12.5 KB
/
protocol.py
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import asyncio
from typing import AsyncIterator, Dict, List, Optional, Union
import bittensor as bt
import pydantic
from starlette.responses import StreamingResponse
import sys
class IsAlive(bt.Synapse):
answer: Optional[str] = None
completion: str = pydantic.Field(
"",
title="Completion",
description="Completion status of the current StreamPrompting object. "
"This attribute is mutable and can be updated.",
)
class Bandwidth(bt.Synapse):
bandwidth_rpm: Optional[Dict[str, dict]] = None
class ImageResponse(bt.Synapse):
""" A class to represent the response for an image-related request. """
# https://platform.stability.ai/docs/api-reference#tag/v1generation/operation/textToImage
completion: Optional[Dict] = pydantic.Field(
None,
title="Completion",
description="The completion data of the image response."
)
messages: str = pydantic.Field(
...,
title="Messages",
description="Messages related to the image response."
)
provider: str = pydantic.Field(
default="OpenAI",
title="Provider",
description="The provider to use when calling for your response."
)
seed: int = pydantic.Field(
default=1234,
title="Seed",
description="The seed that which to generate the image with"
)
samples: int = pydantic.Field(
default=1,
title="Samples",
description="The number of samples to generate"
)
cfg_scale: float = pydantic.Field(
default=8.0,
title="cfg_scale",
description="The cfg_scale to use for image generation"
)
# (Available Samplers: ddim, plms, k_euler, k_euler_ancestral, k_heun, k_dpm_2, k_dpm_2_ancestral, k_dpmpp_2s_ancestral, k_lms, k_dpmpp_2m, k_dpmpp_sde)
sampler: str = pydantic.Field(
default="",
title="Sampler",
description="The sampler to use for image generation"
)
steps: int = pydantic.Field(
default=30,
title="Seed",
description="The steps to take in generating the image"
)
model: str = pydantic.Field(
default="dall-e-2",
title="Model",
description="The model used for generating the image."
)
style: str = pydantic.Field(
default="vivid",
title="Style",
description="The style of the image."
)
size: str = pydantic.Field(
default="1024x1024",
title="The size of the image, used for Openai generation. Options are 1024x1024, 1792x1024, 1024x1792 for dalle3",
description="The size of the image."
)
height: int = pydantic.Field(
default=1024,
title="Height used for non Openai images",
description="height"
)
width: int = pydantic.Field(
default=1024,
title="Width used for non Openai images",
description="width"
)
quality: str = pydantic.Field(
default="standard",
title="Quality",
description="The quality of the image."
)
uid: int = pydantic.Field(
default=3,
title="uid",
description="The UID to send the synapse to",
)
timeout: int = pydantic.Field(
default=60,
title="timeout",
description="The timeout for the dendrite of the synapse",
)
required_hash_fields: List[str] = pydantic.Field(
["messages"],
title="Required Hash Fields",
description="A list of fields required for the hash."
)
process_time: int = pydantic.Field(
default=9999,
title="process time",
description="processed time of querying dendrite.",
)
task_id: str = pydantic.Field(
default="9999"
)
def deserialize(self) -> Optional[Dict]:
""" Deserialize the completion data of the image response. """
return self.completion
class Embeddings(bt.Synapse):
""" A class to represent the embeddings request and response. """
provider: str = pydantic.Field(
default="OpenAI",
title="text",
description="Provider name by which embeddings are to be generated"
)
texts: List[str] = pydantic.Field(
...,
title="Text",
description="The list of input texts for which embeddings are to be generated."
)
model: str = pydantic.Field(
default="text-embedding-ada-002",
title="Model",
description="The model used for generating embeddings."
)
embeddings: Optional[List[List[float]]] = pydantic.Field(
None,
title="Embeddings",
description="The resulting list of embeddings, each corresponding to an input text."
)
uid: int = pydantic.Field(
default=60,
title="uid",
description="The UID to send the synapse to",
)
timeout: int = pydantic.Field(
default=60,
title="timeout",
description="The timeout for the dendrite of the synapse",
)
class StreamPrompting(bt.StreamingSynapse):
messages: List[Dict[str, Union[str, List[Dict[str, Union[str, Dict[str, str]]]]]]] = pydantic.Field(
...,
title="Messages",
description="A list of messages in the StreamPrompting scenario, "
"each containing a role and content. Immutable.",
allow_mutation=False,
)
required_hash_fields: List[str] = pydantic.Field(
["messages"],
title="Required Hash Fields",
description="A list of required fields for the hash.",
allow_mutation=False,
)
seed: int = pydantic.Field(
default=1234,
title="Seed",
description="Seed for text generation. This attribute is immutable and cannot be updated.",
)
temperature: float = pydantic.Field(
default=0.0001,
title="Temperature",
description="Temperature for text generation. "
"This attribute is immutable and cannot be updated.",
)
max_tokens: int = pydantic.Field(
default=2048,
title="Max Tokens",
description="Max tokens for text generation. "
"This attribute is immutable and cannot be updated.",
)
top_p: float = pydantic.Field(
default=0.001,
title="Top_p",
description="Top_p for text generation. The sampler will pick one of "
"the top p percent tokens in the logit distirbution. "
"This attribute is immutable and cannot be updated.",
)
top_k: int = pydantic.Field(
default=1,
title="Top_k",
description="Top_k for text generation. Sampler will pick one of "
"the k most probablistic tokens in the logit distribtion. "
"This attribute is immutable and cannot be updated.",
)
completion: str = pydantic.Field(
None,
title="Completion",
description="Completion status of the current StreamPrompting object. "
"This attribute is mutable and can be updated.",
)
provider: str = pydantic.Field(
default="OpenAI",
title="Provider",
description="The provider to use when calling for your response. "
"Options: OpenAI, Anthropic, Groq, Bedrock"
)
model: str = pydantic.Field(
default="gpt-3.5-turbo",
title="model",
description="""
The model to use when calling provider for your response.
For Provider OpenAI:
text_models = [
"davinci-002",
"gpt-4-1106-preview",
"gpt-4-turbo-preview",
"gpt-4-0125-preview",
"babbage-002",
"gpt-4",
"gpt-4-0613",
"gpt-3.5-turbo-16k",
"gpt-3.5-turbo-1106",
"gpt-3.5-turbo-instruct-0914",
"gpt-3.5-turbo-instruct",
"gpt-3.5-turbo-0301",
"gpt-3.5-turbo-0125",
"gpt-3.5-turbo",
"gpt-4-turbo-2024-04-09",
"gpt-4-turbo",
"gpt-3.5-turbo-0613",
"gpt-4o",
"gpt-4o-2024-05-13"
]
For Provider Anthropic: claude-3-opus-20240229, claude-3-sonnet-20240229, claude-3-haiku-20240307
For Provider Groq: gemma-7b-it, llama3-70b-8192, llama3-8b-8192, mixtral-8x7b-32768
For Provider Bedrock: anthropic.claude-3-sonnet-20240229-v1:0, cohere.command-r-v1:0, meta.llama2-70b-chat-v1,
amazon.titan-text-express-v1, mistral.mistral-7b-instruct-v0:2
last_updated = 17 June 2024
"""
)
uid: int = pydantic.Field(
default=3,
title="uid",
description="The UID to send the streaming synapse to",
)
timeout: int = pydantic.Field(
default=60,
title="timeout",
description="The timeout for the dendrite of the streaming synapse",
)
streaming: bool = pydantic.Field(
default=True,
title="streaming",
description="whether to stream the output",
)
deserialize_flag: bool = pydantic.Field(
default=True
)
task_id: str = pydantic.Field(
default="9999",
title="task_id",
description="task id of the request from this syanpse."
)
validator_info: dict = pydantic.Field(
default={},
title="validator_info",
)
miner_info: dict = pydantic.Field(
default={},
title="miner_info",
)
time_taken: float = pydantic.Field(
default=0,
title="time_taken",
)
block_num: int = pydantic.Field(
default=0,
title="block_num",
)
cycle_num: int = pydantic.Field(
default=0,
title="cycle_num",
)
epoch_num: int = pydantic.Field(
default=0,
title="epoch num",
)
score: float = pydantic.Field(
default=0,
title="score",
)
similarity: float = pydantic.Field(
default=0,
title="similarity",
)
def to_headers(self) -> dict:
headers = {"name": self.name, "timeout": str(self.timeout)}
# Adding headers for 'axon' and 'dendrite' if they are not None
if self.axon:
headers.update(
{
f"bt_header_axon_{k}": str(v)
for k, v in self.axon.dict().items()
if v is not None
}
)
if self.dendrite:
headers.update(
{
f"bt_header_dendrite_{k}": str(v)
for k, v in self.dendrite.dict().items()
if v is not None
}
)
headers[f"bt_header_input_obj_messages"] = "W10="
headers["header_size"] = str(sys.getsizeof(headers))
headers["total_size"] = str(self.get_total_size())
headers["computed_body_hash"] = self.body_hash
return headers
async def process_streaming_response(self, response: StreamingResponse, organic=True) -> AsyncIterator[str]:
if self.completion is None:
self.completion = ""
chunk_size = 100 if organic else 1000
remain_chunk = ""
try:
async for chunk in response.content.iter_chunked(chunk_size):
tokens = chunk.decode("utf-8")
remain_chunk = tokens
self.completion += tokens
yield tokens
except asyncio.TimeoutError as err:
self.completion += remain_chunk
yield remain_chunk
def extract_response_json(self, response: StreamingResponse) -> dict:
headers = {
k.decode("utf-8"): v.decode("utf-8")
for k, v in response.__dict__["_raw_headers"]
}
def extract_info(prefix: str) -> dict[str, str]:
return {
key.split("_")[-1]: value
for key, value in headers.items()
if key.startswith(prefix)
}
return {
"name": headers.get("name", ""),
"timeout": float(headers.get("timeout", 0)),
"total_size": int(headers.get("total_size", 0)),
"header_size": int(headers.get("header_size", 0)),
"dendrite": extract_info("bt_header_dendrite"),
"axon": extract_info("bt_header_axon"),
"messages": self.messages,
"completion": self.completion,
"provider": self.provider,
"model": self.model,
"seed": self.seed,
"max_tokens": self.max_tokens,
"temperature": self.temperature,
"top_p": self.top_p,
"top_k": self.top_k,
"timeout": self.timeout,
"streaming": self.streaming,
"uid": self.uid,
}