forked from nvms/wingman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
koboldcpp.ts
142 lines (118 loc) · 4.33 KB
/
koboldcpp.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
/* eslint-disable unused-imports/no-unused-vars */
import * as vscode from "vscode";
import { type PostableViewProvider, type ProviderResponse, type Provider } from ".";
import { Client, DEFAULT_TEMPLATE, DEFAULT_CTX, type KoboldInferParams } from "./sdks/koboldcpp";
import { type Command } from "../templates/render";
import { handleResponseCallbackType } from "../templates/runner";
import { displayWarning, formatPrompt, getConfig, getSelectionInfo, llamaMaxTokens } from "../utils";
let lastMessage: string | undefined;
let lastTemplate: Command | undefined;
let lastSystemMessage: string | undefined;
export class KoboldcppProvider implements Provider {
viewProvider: PostableViewProvider | undefined;
instance: Client | undefined;
conversationTextHistory: string | undefined;
_abort: AbortController = new AbortController();
async create(provider: PostableViewProvider, template: Command) {
const { apiBaseUrl } = {
apiBaseUrl: getConfig("openai.apiBaseUrl") as string | undefined,
};
this.viewProvider = provider;
this.conversationTextHistory = undefined;
this.instance = new Client("", { apiUrl: apiBaseUrl });
}
destroy() {
this.instance = undefined;
this.conversationTextHistory = undefined;
}
abort() {
this._abort.abort();
this._abort = new AbortController();
}
async send(message: string, systemMessage?: string, template?: Command): Promise<void | ProviderResponse> {
let isFollowup = false;
lastMessage = message;
if (template) {
lastTemplate = template;
}
if (!template && !lastTemplate) {
return;
}
if (!template) {
template = lastTemplate!;
isFollowup = true;
}
if (systemMessage) {
lastSystemMessage = systemMessage;
}
if (!systemMessage && !lastSystemMessage) {
return;
}
if (!systemMessage) {
systemMessage = lastSystemMessage!;
}
let prompt;
if (!isFollowup) {
this.viewProvider?.postMessage({ type: "newChat" });
// The first message should have the system message prepended
prompt = `${message}`;
} else {
// followups should have the conversation history prepended
prompt = `${this.conversationTextHistory ?? ""}${message}`;
}
const modelTemplate = template?.completionParams?.template ?? DEFAULT_TEMPLATE;
const samplingParameters: KoboldInferParams = {
prompt: formatPrompt(prompt, modelTemplate, systemMessage),
...template?.completionParams,
temperature: template?.completionParams?.temperature ?? (getConfig("openai.temperature") as number),
max_length: llamaMaxTokens(prompt, DEFAULT_CTX),
};
console.log("Params", samplingParameters);
try {
this.viewProvider?.postMessage({ type: "requestMessage", value: message });
const editor = vscode.window.activeTextEditor!;
const selection = getSelectionInfo(editor);
let partialText = "";
await this.instance!.completeStream(samplingParameters, {
onOpen: (response) => {
console.log("Opened stream, HTTP status code", response.status);
},
onUpdate: (partialResponse: string) => {
partialText += partialResponse;
// console.log("P", partialText);
const msg = this.toProviderResponse(partialText);
// console.log("MSG:", msg.text);
this.viewProvider?.postMessage({
type: "partialResponse",
value: msg,
});
},
signal: this._abort.signal,
});
// Reformat the API response into a ProvderResponse
const response = this.toProviderResponse(partialText);
// Append the last response to the conversation history
this.conversationTextHistory = `${this.conversationTextHistory ?? ""}${prompt} ${response.text}`;
this.viewProvider?.postMessage({ type: "responseFinished", value: response });
if (!isFollowup) {
handleResponseCallbackType(template, editor, selection, response.text);
}
} catch (error) {
displayWarning(String(error));
}
}
async repeatLast() {
if (!lastMessage || !lastSystemMessage || !lastTemplate) {
return;
}
await this.send(lastMessage, lastSystemMessage, lastTemplate);
}
toProviderResponse(text: string) {
return {
text,
parentMessageId: "",
converastionId: "",
id: "",
};
}
}