-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
bittensor.ts
196 lines (174 loc) Β· 5.43 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { BaseMessage, ChatMessage } from "@langchain/core/messages";
import { ChatResult, ChatGeneration } from "@langchain/core/outputs";
import {
BaseChatModel,
BaseChatModelParams,
} from "@langchain/core/language_models/chat_models";
export interface BittensorInput extends BaseChatModelParams {
systemPrompt?: string | null | 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 chat model powerd by Bittensor, a decentralized network
* full of different AI models.s
* To analyze API_KEYS and logs of you usage visit
* https://api.neuralinternet.ai/api-keys
* https://api.neuralinternet.ai/logs
*/
export class NIBittensorChatModel
extends BaseChatModel
implements BittensorInput
{
static lc_name(): string {
return "NIBittensorLLM";
}
systemPrompt: string;
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";
}
_combineLLMOutput() {
return [];
}
_llmType(): string {
return "NIBittensorLLM";
}
messageToOpenAIRole(message: BaseMessage) {
const type = message._getType();
switch (type) {
case "system":
return "system";
case "ai":
return "assistant";
case "human":
return "user";
default:
return "user";
}
}
stringToChatMessage(message: string): BaseMessage {
return new ChatMessage(message, "assistant");
}
/** Call out to NIBittensorChatModel's complete endpoint.
Args:
messages: The messages to pass into the model.
Returns: The chat response by the model.
Example:
const chat = new NIBittensorChatModel();
const message = new HumanMessage('What is bittensor?');
const res = await chat.call([message]);
*/
async _generate(messages: BaseMessage[]): Promise<ChatResult> {
const processed_messages = messages.map((message) => {
if (typeof message.content !== "string") {
throw new Error("NIBittensorChat does not support non-string output.");
}
return {
role: this.messageToOpenAIRole(message),
content: message.content,
};
});
const generations: ChatGeneration[] = [];
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",
};
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) {
for (const uid of uids) {
try {
const payload: ChatPayload = {
uids: [uid],
messages: [
{ role: "system", content: this.systemPrompt },
...processed_messages,
],
};
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) {
const generation: ChatGeneration = {
text: chatData.choices[0].message.content,
message: this.stringToChatMessage(
chatData.choices[0].message.content
),
};
generations.push(generation);
return { generations, llmOutput: {} };
}
} catch (error) {
continue;
}
}
}
} catch (error) {
const generation: ChatGeneration = {
text: "Sorry I am unable to provide response now, Please try again later.",
message: this.stringToChatMessage(
"Sorry I am unable to provide response now, Please try again later."
),
};
generations.push(generation);
return { generations, llmOutput: {} };
}
const generation: ChatGeneration = {
text: "Sorry I am unable to provide response now, Please try again later.",
message: this.stringToChatMessage(
"Sorry I am unable to provide response now, Please try again later."
),
};
generations.push(generation);
return { generations, llmOutput: {} };
}
identifyingParams(): {
systemPrompt: string | null | undefined;
} {
return {
systemPrompt: this.systemPrompt,
};
}
}