Skip to content

feat(ai): llm provider #539

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 54 additions & 15 deletions crates/base/test_cases/supabase-ai/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import { assertGreater, assertLessOrEqual } from "jsr:@std/assert";
import {
assertEquals,
assertExists,
assertGreater,
assertIsError,
assertLessOrEqual,
assertStringIncludes,
assertThrows,
} from "jsr:@std/assert";

const session = new Supabase.ai.Session("gte-small");

assertThrows(() => {
const _ = new Supabase.ai.Session("gte-small_wrong_name");
}, "invalid 'Session' type");

function dotProduct(a: number[], b: number[]) {
let result = 0;
for (let i = 0; i < a.length; i++) {
Expand All @@ -15,27 +27,54 @@ export default {
async fetch() {
// Generate embedding
// @ts-ignore unkwnow type
const meow: number[] = await session.run("meow", {
mean_pool: true,
normalize: true,
});
const [meow, meowError] = await session.run("meow") as [
number[],
undefined,
];

// @ts-ignore unkwnow type
const love: number[] = await session.run("I love cats", {
const [love, loveError] = await session.run("I love cats", {
mean_pool: true,
normalize: true,
});
}) as [number[], undefined];

// "Valid input should result in ok value"
{
assertExists(meow);
assertExists(love);

assertEquals(meowError, undefined);
assertEquals(loveError, undefined);
}

// "Invalid input should result in error value"
{
const [notCat, notCatError] = await session.run({
bad_input: { "not a cat": "let fail" },
}) as [undefined, { message: string; inner: Error }];

assertEquals(notCat, undefined);

assertExists(notCatError);
assertIsError(notCatError.inner);
assertStringIncludes(
notCatError.message,
"must provide a valid prompt value",
);
}

// Ensures `mean_pool` and `normalize`
const sameScore = dotProduct(meow, meow);
const diffScore = dotProduct(meow, love);
// "Ensures `mean_pool` and `normalize`"
{
const sameScore = dotProduct(meow, meow);
const diffScore = dotProduct(meow, love);

assertGreater(sameScore, 0.9);
assertGreater(diffScore, 0.5);
assertGreater(sameScore, diffScore);
assertGreater(sameScore, 0.9);
assertGreater(diffScore, 0.5);
assertGreater(sameScore, diffScore);

assertLessOrEqual(sameScore, 1);
assertLessOrEqual(diffScore, 1);
assertLessOrEqual(sameScore, 1);
assertLessOrEqual(diffScore, 1);
}

return new Response(
null,
Expand Down
21 changes: 21 additions & 0 deletions ext/ai/js/ai.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Session } from "./ai.ts";
import { LLMSessionRunInputOptions } from "./llm/llm_session.ts";
import {
OllamaProviderInput,
OllamaProviderOptions,
} from "./llm/providers/ollama.ts";
import {
OpenAIProviderInput,
OpenAIProviderOptions,
} from "./llm/providers/openai.ts";

export namespace ai {
export { Session };
export {
LLMSessionRunInputOptions as LLMRunOptions,
OllamaProviderInput as OllamaInput,
OllamaProviderOptions as OllamaOptions,
OpenAIProviderInput as OpenAICompatibleInput,
OpenAIProviderOptions as OpenAICompatibleOptions,
};
}
263 changes: 0 additions & 263 deletions ext/ai/js/ai.js

This file was deleted.

Loading
Loading