generated from silverbulletmd/silverbullet-plug-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from justyns/fix-undefined-error
Fix bug causing generateEmbeddings to run when it shouldn't
- Loading branch information
Showing
6 changed files
with
301 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import { assertEquals } from "https://deno.land/std@0.224.0/assert/mod.ts"; | ||
import "./mocks/syscalls.ts"; | ||
import { aiSettings, initializeOpenAI } from "./init.ts"; | ||
import { | ||
canIndexPage, | ||
indexEmbeddings, | ||
shouldIndexEmbeddings, | ||
shouldIndexSummaries, | ||
} from "./embeddings.ts"; | ||
|
||
const settingsPageSample = ` | ||
\`\`\`yaml | ||
ai: | ||
indexEmbeddings: true | ||
indexEmbeddingsExcludePages: | ||
- passwords | ||
indexEmbeddingsExcludeStrings: | ||
- foo | ||
chat: | ||
bakeMessages: false | ||
customEnrichFunctions: | ||
- enrichWithURL | ||
textModels: | ||
- name: mock-t1 | ||
provider: mock | ||
modelName: mock-t1 | ||
imageModels: | ||
- name: mock-i1 | ||
provider: mock | ||
modelName: mock-i1 | ||
embeddingModels: | ||
- name: mock-e1 | ||
modelName: mock-e1 | ||
provider: mock | ||
baseUrl: http://localhost:11434 | ||
requireAuth: false | ||
\`\`\` | ||
`; | ||
|
||
const settingsPageSampleNoEmbeddings = ` | ||
\`\`\`yaml | ||
ai: | ||
indexEmbeddings: true | ||
indexSummaries: true | ||
\`\`\` | ||
`; | ||
|
||
const secretsPageSample = ` | ||
\`\`\`yaml | ||
OPENAI_API_KEY: bar | ||
\`\`\` | ||
`; | ||
|
||
Deno.test("canIndexPage respects aiSettings.indexEmbeddingsExcludePages", async () => { | ||
await syscall("mock.setPage", "SETTINGS", settingsPageSample); | ||
await syscall("mock.setPage", "SECRETS", secretsPageSample); | ||
await initializeOpenAI(); | ||
aiSettings.indexEmbeddingsExcludePages = ["ExcludedPage"]; | ||
assertEquals(canIndexPage("RegularPage"), true); | ||
assertEquals(canIndexPage("ExcludedPage"), false); | ||
assertEquals(canIndexPage("_HiddenPage"), false); | ||
assertEquals(canIndexPage("Library/SomePage"), false); | ||
}); | ||
|
||
Deno.test("shouldIndexEmbeddings returns true when conditions are met", async () => { | ||
await syscall("mock.setPage", "SETTINGS", settingsPageSample); | ||
await syscall("mock.setPage", "SECRETS", secretsPageSample); | ||
await initializeOpenAI(); | ||
await syscall("mock.setEnv", "server"); | ||
|
||
const result = await shouldIndexEmbeddings(); | ||
assertEquals(result, true); | ||
}); | ||
|
||
Deno.test("shouldIndexEmbeddings returns false when not on server", async () => { | ||
await syscall("mock.setPage", "SETTINGS", settingsPageSample); | ||
await syscall("mock.setPage", "SECRETS", secretsPageSample); | ||
await initializeOpenAI(); | ||
await syscall("mock.setEnv", "client"); | ||
|
||
const result = await shouldIndexEmbeddings(); | ||
assertEquals(result, false); | ||
}); | ||
|
||
Deno.test("shouldIndexEmbeddings returns false when indexEmbeddings is disabled", async () => { | ||
const modifiedSettings = settingsPageSample.replace( | ||
"indexEmbeddings: true", | ||
"indexEmbeddings: false", | ||
); | ||
await syscall("mock.setPage", "SETTINGS", modifiedSettings); | ||
await syscall("mock.setPage", "SECRETS", secretsPageSample); | ||
await initializeOpenAI(); | ||
await syscall("mock.setEnv", "server"); | ||
|
||
const result = await shouldIndexEmbeddings(); | ||
assertEquals(result, false); | ||
}); | ||
|
||
Deno.test("shouldIndexSummaries returns true when conditions are met", async () => { | ||
const modifiedSettings = settingsPageSample.replace( | ||
"indexEmbeddings: true", | ||
"indexEmbeddings: true\n indexSummary: true", | ||
); | ||
await syscall("mock.setPage", "SETTINGS", modifiedSettings); | ||
await syscall("mock.setPage", "SECRETS", secretsPageSample); | ||
await initializeOpenAI(); | ||
await syscall("mock.setEnv", "server"); | ||
|
||
const result = await shouldIndexSummaries(); | ||
assertEquals(result, true); | ||
}); | ||
|
||
Deno.test("shouldIndexSummaries returns false when indexSummary is disabled", async () => { | ||
await syscall("mock.setPage", "SETTINGS", settingsPageSample); | ||
await syscall("mock.setPage", "SECRETS", secretsPageSample); | ||
await initializeOpenAI(); | ||
await syscall("mock.setEnv", "server"); | ||
|
||
const result = await shouldIndexSummaries(); | ||
assertEquals(result, false); | ||
}); | ||
|
||
Deno.test("shouldIndexEmbeddings returns false when no embedding models are configured", async () => { | ||
await syscall("mock.setPage", "SETTINGS", settingsPageSampleNoEmbeddings); | ||
await syscall("mock.setPage", "SECRETS", secretsPageSample); | ||
await initializeOpenAI(); | ||
await syscall("mock.setEnv", "server"); | ||
|
||
const result = await shouldIndexEmbeddings(); | ||
assertEquals(result, false); | ||
}); | ||
|
||
Deno.test("shouldIndexSummaries returns false when no embedding models are configured", async () => { | ||
await syscall("mock.setPage", "SETTINGS", settingsPageSampleNoEmbeddings); | ||
await syscall("mock.setPage", "SECRETS", secretsPageSample); | ||
await initializeOpenAI(); | ||
await syscall("mock.setEnv", "server"); | ||
|
||
const result = await shouldIndexSummaries(); | ||
assertEquals(result, false); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { AbstractProvider } from "../interfaces/Provider.ts"; | ||
import { AbstractImageProvider } from "../interfaces/ImageProvider.ts"; | ||
import { AbstractEmbeddingProvider } from "../interfaces/EmbeddingProvider.ts"; | ||
import { | ||
EmbeddingGenerationOptions, | ||
ImageGenerationOptions, | ||
StreamChatOptions, | ||
} from "../types.ts"; | ||
|
||
export class MockProvider extends AbstractProvider { | ||
constructor( | ||
apiKey: string, | ||
modelName: string, | ||
baseUrl: string = "http://localhost", | ||
) { | ||
super(apiKey, baseUrl, "mock", modelName); | ||
} | ||
|
||
async chatWithAI(options: StreamChatOptions): Promise<any> { | ||
const mockResponse = "This is a mock response from the AI."; | ||
if (options.onDataReceived) { | ||
for (const char of mockResponse) { | ||
await new Promise((resolve) => setTimeout(resolve, 50)); | ||
options.onDataReceived(char); | ||
} | ||
} | ||
return mockResponse; | ||
} | ||
} | ||
|
||
export class MockImageProvider extends AbstractImageProvider { | ||
constructor( | ||
apiKey: string, | ||
modelName: string, | ||
baseUrl: string = "http://localhost", | ||
) { | ||
super(apiKey, baseUrl, "mock", modelName); | ||
} | ||
|
||
generateImage(options: ImageGenerationOptions): Promise<string> { | ||
return new Promise<string>((resolve) => { | ||
setTimeout(() => { | ||
resolve("https://example.com/mock-image.jpg"); | ||
}, 5); | ||
}); | ||
} | ||
} | ||
|
||
export class MockEmbeddingProvider extends AbstractEmbeddingProvider { | ||
constructor( | ||
apiKey: string, | ||
modelName: string, | ||
baseUrl: string = "http://localhost", | ||
) { | ||
super(apiKey, baseUrl, "mock", modelName); | ||
} | ||
|
||
_generateEmbeddings( | ||
options: EmbeddingGenerationOptions, | ||
): Promise<Array<number>> { | ||
return new Promise<Array<number>>((resolve) => { | ||
setTimeout(() => { | ||
const mockEmbedding = Array(1536).fill(0).map(() => Math.random()); | ||
resolve(mockEmbedding); | ||
}, 5); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.