Compare commits

..

5 commits

Author SHA1 Message Date
celeste
afbb3519f1 hehe 2025-03-19 22:53:10 -07:00
celeste
53ef92e076 bugfixes 2025-03-19 22:41:22 -07:00
celeste
518a90f587 Add Llama 3.1 405b (and remove cd2) autofill 2024-09-04 00:25:18 -07:00
celeste
93599ed192 Fix OpenRouter error reporting 2024-09-03 23:47:17 -07:00
celeste
788d91b2b1 Use cl100k for LLaMa 3.1 2024-09-03 21:23:25 -07:00
3 changed files with 276 additions and 140 deletions

412
main.ts
View file

@ -113,18 +113,6 @@ export default class LoomPlugin extends Plugin {
return callback(file);
}
saveAndRender() {
this.save();
if (this.rendering) return;
this.rendering = true;
this.renderLoomViews();
this.renderLoomSiblingsViews();
this.rendering = false;
}
thenSaveAndRender(callback: () => void) {
callback();
this.saveAndRender();
@ -136,20 +124,6 @@ export default class LoomPlugin extends Plugin {
});
}
renderLoomViews() {
const views = this.app.workspace
.getLeavesOfType("loom")
.map((leaf) => leaf.view) as LoomView[];
views.forEach((view) => view.render());
}
renderLoomSiblingsViews() {
const views = this.app.workspace
.getLeavesOfType("loom-siblings")
.map((leaf) => leaf.view) as LoomSiblingsView[];
views.forEach((view) => view.render());
}
initializeProviders() {
const preset = getPreset(this.settings);
if (preset === undefined) return;
@ -815,6 +789,7 @@ export default class LoomPlugin extends Plugin {
// return;
// }
// this.editor.setCursor(cursor);
this.saveAndRender(); // Add explicit view refresh
})
)
);
@ -1219,8 +1194,7 @@ export default class LoomPlugin extends Plugin {
};
plugin.update();
this.renderLoomViews();
this.renderLoomSiblingsViews();
this.refreshViews();
};
this.registerEvent(
@ -1237,8 +1211,7 @@ export default class LoomPlugin extends Plugin {
this.registerEvent(
this.app.workspace.on("resize", () => {
this.renderLoomViews();
this.renderLoomSiblingsViews();
this.refreshViews();
})
);
@ -1294,7 +1267,7 @@ export default class LoomPlugin extends Plugin {
this.state[file.path].generating = rootNode;
// show the "Generating..." indicator in the loom view
this.renderLoomViews();
this.refreshViews();
let prompt = `${this.settings.prepend}${this.fullText(file, rootNode)}`;
@ -1401,6 +1374,19 @@ export default class LoomPlugin extends Plugin {
}
async completeCohere(prompt: string) {
if (this.settings.logApiCalls) {
console.log("Cohere request:", {
model: getPreset(this.settings).model,
prompt,
max_tokens: this.settings.maxTokens,
num_generations: this.settings.n,
temperature: this.settings.temperature,
p: this.settings.topP,
frequency_penalty: this.settings.frequencyPenalty,
presence_penalty: this.settings.presencePenalty,
});
}
const tokens = (await cohere.tokenize({ text: prompt })).body.token_strings;
prompt = tokens.slice(-getPreset(this.settings).contextLength).join("");
@ -1415,6 +1401,10 @@ export default class LoomPlugin extends Plugin {
presence_penalty: this.settings.presencePenalty,
});
if (this.settings.logApiCalls) {
console.log("Cohere response:", response);
}
const result: CompletionResult =
response.statusCode === 200
? {
@ -1423,17 +1413,30 @@ export default class LoomPlugin extends Plugin {
(generation) => generation.text
),
}
:
{
: {
ok: false,
status: response.statusCode!,
// @ts-expect-error
message: response.body.message,
message: "",
};
return result;
}
async completeTextSynth(prompt: string) {
const body = {
prompt,
max_tokens: this.settings.maxTokens,
best_of: this.settings.bestOf,
n: this.settings.n,
temperature: this.settings.temperature,
top_p: this.settings.topP,
frequency_penalty: this.settings.frequencyPenalty,
presence_penalty: this.settings.presencePenalty,
};
if (this.settings.logApiCalls) {
console.log("TextSynth request:", body);
}
const response = await requestUrl({
url: `https://api.textsynth.com/v1/engines/${
getPreset(this.settings).model
@ -1444,18 +1447,13 @@ export default class LoomPlugin extends Plugin {
Authorization: `Bearer ${getPreset(this.settings).apiKey}`,
},
throw: false,
body: JSON.stringify({
prompt,
max_tokens: this.settings.maxTokens,
best_of: this.settings.bestOf,
n: this.settings.n,
temperature: this.settings.temperature,
top_p: this.settings.topP,
frequency_penalty: this.settings.frequencyPenalty,
presence_penalty: this.settings.presencePenalty,
}),
body: JSON.stringify(body),
});
if (this.settings.logApiCalls) {
console.log("TextSynth response:", response);
}
let result: CompletionResult;
if (response.status === 200) {
const completions =
@ -1465,7 +1463,7 @@ export default class LoomPlugin extends Plugin {
result = {
ok: false,
status: response.status,
message: response.json.error,
message: response.json.error || "Unknown error",
};
}
return result;
@ -1479,6 +1477,15 @@ export default class LoomPlugin extends Plugin {
"gpt-3.5-turbo",
"gpt-3.5-turbo-0301",
"gpt-4-base",
// TODO: llama 3.1 has "28k additional multilingual tokens", so cl100k is not exactly right
"meta-llama/llama-3.1-8b",
"meta-llama/llama-3.1-70b",
"meta-llama/llama-3.1-405b",
"meta-llama/llama-3.1-8b-instruct",
"meta-llama/llama-3.1-70b-instruct",
"meta-llama/llama-3.1-405b-instruct",
"meta-llama/Meta-Llama-3.1-405B",
"meta-llama/Meta-Llama-3.1-405B-FP8",
];
const p50kModels = [
"text-davinci-003",
@ -1519,6 +1526,7 @@ export default class LoomPlugin extends Plugin {
if (!url.endsWith("/")) url += "/";
url = url.replace(/v1\//, "");
url += "v1/completions";
let body: any = {
prompt,
model: getPreset(this.settings).model,
@ -1526,16 +1534,22 @@ export default class LoomPlugin extends Plugin {
n: this.settings.n,
temperature: this.settings.temperature,
top_p: this.settings.topP,
best_of:
this.settings.bestOf > this.settings.n
? this.settings.bestOf
: this.settings.n,
};
if (this.settings.bestOf > this.settings.n) {
body.best_of = this.settings.bestOf;
}
if (this.settings.frequencyPenalty !== 0)
body.frequency_penalty = this.settings.frequencyPenalty;
if (this.settings.presencePenalty !== 0)
body.presence_penalty = this.settings.presencePenalty;
if (this.settings.logApiCalls) {
console.log("OpenAI-compatible API request:", {
url,
body
});
}
const response = await requestUrl({
url,
method: "POST",
@ -1547,6 +1561,10 @@ export default class LoomPlugin extends Plugin {
body: JSON.stringify(body),
});
if (this.settings.logApiCalls) {
console.log("OpenAI-compatible API response:", response);
}
const result: CompletionResult =
response.status === 200
? {
@ -1555,7 +1573,16 @@ export default class LoomPlugin extends Plugin {
(choice: any) => choice.text
),
}
: { ok: false, status: response.status, message: "" };
: {
ok: false,
status: response.status,
message: response.json?.error?.message || "Unknown error"
};
if (!result.ok && this.settings.logApiCalls) {
console.error("OpenAI-compatible error:", response);
}
return result;
}
@ -1580,6 +1607,10 @@ export default class LoomPlugin extends Plugin {
if (this.settings.presencePenalty !== 0)
body.presence_penalty = this.settings.presencePenalty;
if (this.settings.logApiCalls) {
console.log("OpenRouter request:", body);
}
const requests = Array(this.settings.n).fill(null).map(() =>
requestUrl({
url: "https://openrouter.ai/api/v1/completions",
@ -1597,42 +1628,58 @@ export default class LoomPlugin extends Plugin {
const responses = await Promise.all(requests);
const result: CompletionResult = responses.every(response => response.status === 200)
if (this.settings.logApiCalls) {
console.log("OpenRouter responses:", responses);
}
const result: CompletionResult = responses.every(response => !response.json.hasOwnProperty('error'))
? {
ok: true,
completions: responses.map(response => response.json.choices[0].text),
}
: {
ok: false,
status: responses[0].status,
message: responses[0].text,
status: responses[0].json.error.code,
message: responses[0].json.error.message,
};
return result;
}
async completeOpenAI(prompt: string) {
prompt = this.trimOpenAIPrompt(prompt);
const body = {
model: getPreset(this.settings).model,
prompt,
max_tokens: this.settings.maxTokens,
n: this.settings.n,
temperature: this.settings.temperature,
top_p: this.settings.topP,
frequency_penalty: this.settings.frequencyPenalty,
presence_penalty: this.settings.presencePenalty,
};
if (this.settings.logApiCalls) {
console.log("OpenAI request:", body);
}
let result: CompletionResult;
try {
const response = await this.openai.createCompletion({
model: getPreset(this.settings).model,
prompt,
max_tokens: this.settings.maxTokens,
n: this.settings.n,
temperature: this.settings.temperature,
top_p: this.settings.topP,
frequency_penalty: this.settings.frequencyPenalty,
presence_penalty: this.settings.presencePenalty,
});
const response = await this.openai.createCompletion(body);
if (this.settings.logApiCalls) {
console.log("OpenAI response:", response);
}
result = {
ok: true,
completions: response.data.choices.map((choice) => choice.text || ""),
};
} catch (e) {
if (this.settings.logApiCalls) {
console.error("OpenAI error:", e);
}
result = {
ok: false,
status: e.response.status,
message: e.response.data.error.message,
message: e.response.data.error.message || "Unknown error",
};
}
return result;
@ -1640,18 +1687,27 @@ export default class LoomPlugin extends Plugin {
async completeOpenAIChat(prompt: string) {
prompt = this.trimOpenAIPrompt(prompt);
const body = {
model: getPreset(this.settings).model,
messages: [{ role: "assistant" as const, content: prompt }],
max_tokens: this.settings.maxTokens,
n: this.settings.n,
temperature: this.settings.temperature,
top_p: this.settings.topP,
frequency_penalty: this.settings.frequencyPenalty,
presence_penalty: this.settings.presencePenalty,
};
if (this.settings.logApiCalls) {
console.log("OpenAI Chat request:", body);
}
let result: CompletionResult;
try {
const response = await this.openai.createChatCompletion({
model: getPreset(this.settings).model,
messages: [{ role: "assistant", content: prompt }],
max_tokens: this.settings.maxTokens,
n: this.settings.n,
temperature: this.settings.temperature,
top_p: this.settings.topP,
frequency_penalty: this.settings.frequencyPenalty,
presence_penalty: this.settings.presencePenalty,
});
const response = await this.openai.createChatCompletion(body);
if (this.settings.logApiCalls) {
console.log("OpenAI Chat response:", response);
}
result = {
ok: true,
completions: response.data.choices.map(
@ -1659,10 +1715,13 @@ export default class LoomPlugin extends Plugin {
),
};
} catch (e) {
if (this.settings.logApiCalls) {
console.error("OpenAI Chat error:", e);
}
result = {
ok: false,
status: e.response.status,
message: e.response.data.error.message,
message: e.response.data.error.message || "Unknown error",
};
}
return result;
@ -1670,27 +1729,39 @@ export default class LoomPlugin extends Plugin {
async completeAzure(prompt: string) {
prompt = this.trimOpenAIPrompt(prompt);
const body = {
model: getPreset(this.settings).model,
prompt,
max_tokens: this.settings.maxTokens,
n: this.settings.n,
temperature: this.settings.temperature,
top_p: this.settings.topP,
frequency_penalty: this.settings.frequencyPenalty,
presence_penalty: this.settings.presencePenalty,
};
if (this.settings.logApiCalls) {
console.log("Azure request:", body);
}
let result: CompletionResult;
try {
const response = await this.azure.createCompletion({
model: getPreset(this.settings).model,
prompt,
max_tokens: this.settings.maxTokens,
n: this.settings.n,
temperature: this.settings.temperature,
top_p: this.settings.topP,
frequency_penalty: this.settings.frequencyPenalty,
presence_penalty: this.settings.presencePenalty,
});
const response = await this.azure.createCompletion(body);
if (this.settings.logApiCalls) {
console.log("Azure response:", response);
}
result = {
ok: true,
completions: response.data.choices.map((choice) => choice.text || ""),
};
} catch (e) {
if (this.settings.logApiCalls) {
console.error("Azure error:", e);
}
result = {
ok: false,
status: e.response.status,
message: e.response.data.error.message,
message: e.response.data.error.message || "Unknown error",
};
}
return result;
@ -1698,18 +1769,27 @@ export default class LoomPlugin extends Plugin {
async completeAzureChat(prompt: string) {
prompt = this.trimOpenAIPrompt(prompt);
const body = {
model: getPreset(this.settings).model,
messages: [{ role: "assistant" as const, content: prompt }],
max_tokens: this.settings.maxTokens,
n: this.settings.n,
temperature: this.settings.temperature,
top_p: this.settings.topP,
frequency_penalty: this.settings.frequencyPenalty,
presence_penalty: this.settings.presencePenalty,
};
if (this.settings.logApiCalls) {
console.log("Azure Chat request:", body);
}
let result: CompletionResult;
try {
const response = await this.azure.createChatCompletion({
model: getPreset(this.settings).model,
messages: [{ role: "assistant", content: prompt }],
max_tokens: this.settings.maxTokens,
n: this.settings.n,
temperature: this.settings.temperature,
top_p: this.settings.topP,
frequency_penalty: this.settings.frequencyPenalty,
presence_penalty: this.settings.presencePenalty,
});
const response = await this.azure.createChatCompletion(body);
if (this.settings.logApiCalls) {
console.log("Azure Chat response:", response);
}
result = {
ok: true,
completions: response.data.choices.map(
@ -1717,10 +1797,13 @@ export default class LoomPlugin extends Plugin {
),
};
} catch (e) {
if (this.settings.logApiCalls) {
console.error("Azure Chat error:", e);
}
result = {
ok: false,
status: e.response.status,
message: e.response.data.error.message,
message: e.response.data.error.message || "Unknown error",
};
}
return result;
@ -1803,6 +1886,35 @@ export default class LoomPlugin extends Plugin {
await this.saveData({ settings: this.settings, state: this.state });
this.initializeProviders();
}
// Add this new method to properly refresh all views
private refreshViews() {
// Refresh Loom views
this.app.workspace.getLeavesOfType("loom").forEach((leaf) => {
if (leaf.view instanceof LoomView) {
leaf.view.render();
}
});
// Refresh Loom siblings views
this.app.workspace.getLeavesOfType("loom-siblings").forEach((leaf) => {
if (leaf.view instanceof LoomSiblingsView) {
leaf.view.render();
}
});
}
// Update saveAndRender to use the new refresh method
async saveAndRender() {
await this.save();
if (this.rendering) return;
this.rendering = true;
this.refreshViews();
this.rendering = false;
}
}
// this relies on `LoomPlugin`, so it's here, not in `views.ts`
@ -1902,63 +2014,87 @@ class LoomSettingTab extends PluginSettingTab {
});
fillInModelDropdown.createEl("option", {
text: "davinci-002",
attr: { value: "davinci-002" },
text: "Llama 3.1 405B (Hyperbolic)",
attr: { value: "llama-3.1-405b-hyperbolic" },
});
fillInModelDropdown.createEl("option", {
text: "code-davinci-002",
attr: { value: "code-davinci-002" },
text: "Llama 3.1 405B (OpenRouter)",
attr: { value: "llama-3.1-405b-openrouter" },
});
fillInModelDropdown.createEl("option", {
text: "code-davinci-002 (Proxy)",
attr: { value: "code-davinci-002-proxy" },
text: "Claude 3 Opus",
attr: { value: "claude-3-opus" },
});
fillInModelDropdown.createEl("option", {
text: "gpt-4-base",
text: "Claude 3.5 Sonnet",
attr: { value: "claude-3.5-sonnet" },
});
fillInModelDropdown.createEl("option", {
text: "GPT-4 base",
attr: { value: "gpt-4-base" },
});
fillInModelDropdown.createEl("option", {
text: "claude-3-opus",
attr: { value: "claude-3-opus" },
text: "davinci-002",
attr: { value: "davinci-002" },
});
fillInModelDropdown.addEventListener("change", (event) => {
const value = (event.target as HTMLSelectElement).value;
switch (value) {
case "davinci-002": {
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].provider = "openai";
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].model = "davinci-002";
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].contextLength = 16384;
break;
}
case "code-davinci-002": {
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].provider = "openai";
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].model = "code-davinci-002";
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].contextLength = 8001;
break;
}
case "code-davinci-002-proxy": {
case "llama-3.1-405b-hyperbolic": {
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].provider = "openai-compat";
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].model = "code-davinci-002";
// @ts-expect-error
].url = "https://api.hyperbolic.xyz";
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].contextLength = 8001;
].model = "meta-llama/Meta-Llama-3.1-405B";
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].contextLength = 32768;
break;
}
case "llama-3.1-405b-openrouter": {
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].provider = "openrouter";
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
// @ts-expect-error
].quantization = "bf16";
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].model = "meta-llama/llama-3.1-405b";
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].contextLength = 32768;
break;
}
case "claude-3-opus": {
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].provider = "anthropic";
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].model = "claude-3-opus-20240229";
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].contextLength = 50000;
break;
}
case "claude-3.5-sonnet": {
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].provider = "anthropic";
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].model = "claude-3-5-sonnet-20240620";
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].contextLength = 50000;
break;
}
case "gpt-4-base": {
@ -1973,16 +2109,16 @@ class LoomSettingTab extends PluginSettingTab {
].contextLength = 8192;
break;
}
case "claude-3-opus": {
case "davinci-002": {
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].provider = "anthropic";
].provider = "openai";
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].model = "claude-3-opus-20240229";
].model = "davinci-002";
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].contextLength = 20000;
].contextLength = 16384;
break;
}
}

View file

@ -1,7 +1,7 @@
{
"id": "loom",
"name": "Loom",
"version": "1.22.1",
"version": "1.22.6",
"minAppVersion": "0.15.0",
"description": "Loom in Obsidian",
"author": "celeste",

View file

@ -1,6 +1,6 @@
{
"name": "obsidian-loom",
"version": "1.22.1",
"version": "1.22.6",
"description": "Loom in Obsidian",
"main": "main.js",
"scripts": {