-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
bittensor.ts
178 lines (154 loc) Β· 4.92 KB
/
bittensor.ts
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
import { BaseLLMParams, LLM } from "@langchain/core/language_models/llms";
export interface BittensorInput extends BaseLLMParams {
systemPrompt?: string | null | undefined;
topResponses?: number | undefined;
}
interface Message {
role: string;
content: string;
}
interface ChatPayload {
uids?: string[];
top_n?: number;
messages: Message[];
}
interface APIKeyResponse {
api_key: string;
}
interface ChatResponse {
choices?: { message: Message }[];
}
/**
* Class representing the Neural Internet language model powerd by Bittensor, a decentralized network
* full of different AI models.
* To analyze API_KEYS and logs of you usage visit
* https://api.neuralinternet.ai/api-keys
* https://api.neuralinternet.ai/logs
*/
export class NIBittensorLLM extends LLM implements BittensorInput {
static lc_name(): string {
return "NIBittensorLLM";
}
systemPrompt: string;
topResponses: number | undefined;
constructor(fields?: BittensorInput) {
super(fields ?? {});
this.systemPrompt =
fields?.systemPrompt ??
"You are an assistant which is created by Neural Internet(NI) in decentralized network named as a Bittensor. Your task is to provide accurate response based on user prompt";
this.topResponses = fields?.topResponses;
}
_llmType(): string {
return "NIBittensorLLM";
}
/** Call out to NIBittensorLLM's complete endpoint.
Args:
prompt: The prompt to pass into the model.
Returns: The string generated by the model.
Example:
let response = niBittensorLLM.call("Tell me a joke.");
*/
async _call(prompt: string): Promise<string> {
try {
// Retrieve API KEY
const apiKeyResponse: Response = await fetch(
"https://test.neuralinternet.ai/admin/api-keys/"
);
if (!apiKeyResponse.ok) {
throw new Error("Network response was not ok");
}
const apiKeysData: APIKeyResponse[] = await apiKeyResponse.json();
const apiKey: string = apiKeysData[0].api_key;
const headers = {
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`,
"Endpoint-Version": "2023-05-19",
};
if (this.topResponses !== undefined) {
this.topResponses = this.topResponses > 100 ? 100 : this.topResponses;
} else {
this.topResponses = 0;
}
const minerResponse: Response = await fetch(
"https://test.neuralinternet.ai/top_miner_uids",
{ headers }
);
if (!minerResponse.ok) {
throw new Error("Network response was not ok");
}
const uids: string[] = await minerResponse.json();
if (Array.isArray(uids) && uids.length && this.topResponses === 0) {
for (const uid of uids) {
try {
const payload: ChatPayload = {
uids: [uid],
messages: [
{ role: "system", content: this.systemPrompt },
{ role: "user", content: prompt },
],
};
const response: Response = await fetch(
"https://test.neuralinternet.ai/chat",
{
method: "POST",
headers,
body: JSON.stringify(payload),
}
);
if (!response.ok) {
throw new Error("Network response was not ok");
}
const chatData: ChatResponse = await response.json();
if (chatData.choices) {
return chatData.choices[0].message.content;
}
} catch (error) {
continue;
}
}
}
// For top miner based on bittensor response
if (this.topResponses === 0) {
this.topResponses = 10;
}
const payload: ChatPayload = {
top_n: this.topResponses,
messages: [
{ role: "system", content: this.systemPrompt },
{ role: "user", content: prompt },
],
};
const response: Response = await fetch(
"https://test.neuralinternet.ai/chat",
{
method: "POST",
headers,
body: JSON.stringify(payload),
}
);
if (!response.ok) {
throw new Error("Network response was not ok");
}
const responseData: ChatResponse | string = await response.json();
if (this.topResponses) {
return <string>responseData;
} else if ((<ChatResponse>responseData).choices) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const temp: any = (<ChatResponse>responseData).choices;
return <string>temp[0].message.content;
}
} catch (error) {
return "Sorry I am unable to provide response now, Please try again later.";
}
return "default";
}
identifyingParams(): {
systemPrompt: string | null | undefined;
topResponses: number | undefined;
} {
return {
systemPrompt: this.systemPrompt,
topResponses: this.topResponses,
};
}
}