Skip to content
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

Start http service earlier and add /ready http route #5

Merged
merged 4 commits into from
Oct 15, 2024
Merged
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
29 changes: 16 additions & 13 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
services:
subql-ai:
image: subquerynetwork/subql-ai-app
# build:
# context: .
# dockerfile: ./Dockerfile
# image: subquerynetwork/subql-ai-app
build:
context: .
dockerfile: ./Dockerfile
ports:
- 7827:7827
restart: unless-stopped
Expand All @@ -14,24 +14,27 @@ services:
# - -p=/app/index.ts # TODO this doesn't work because dependencies are not copied
- -p=ipfs://QmNaNBhXJoFpRJeNQcnTH8Yh6Rf4pzJy6VSnfnQSZHysdZ
- -h=http://host.docker.internal:11434
# healthcheck:
# test: ["CMD", "curl", "-f", "http://subql-ai:7827/health"]
# interval: 3s
# timeout: 5s
# retries: 10
healthcheck:
test: ["CMD", "curl", "-f", "http://subql-ai:7827/ready"]
interval: 3s
timeout: 5s
retries: 10

# A simple chat UI
ui:
image: ghcr.io/open-webui/open-webui:main
ports:
- 8080:8080
restart: always
depends_on:
"subql-ai":
condition: service_healthy
environment:
- 'OPENAI_API_BASE_URLS=http://subql-ai:7827/v1'
- 'OPENAI_API_KEYS=foobar'
- 'WEBUI_AUTH=false'
- "OPENAI_API_BASE_URLS=http://subql-ai:7827/v1"
- "OPENAI_API_KEYS=foobar"
- "WEBUI_AUTH=false"
volumes:
- open-webui:/app/backend/data
- open-webui:/app/backend/data

volumes:
open-webui:
13 changes: 7 additions & 6 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ export async function runApp(config: {
forceReload?: boolean;
}): Promise<void> {
const model = new Ollama({ host: config.host });
const projectPath = await loadProject(
const [projectPath, source] = await loadProject(
config.projectPath,
config.ipfs,
undefined,
config.forceReload,
);
const sandbox = await getDefaultSandbox(resolve(projectPath));
const sandbox = await getDefaultSandbox(resolve(projectPath), source);

const ctx = await makeContext(
const pendingCtx = makeContext(
sandbox,
model,
(dbPath) =>
Expand All @@ -44,7 +44,7 @@ export async function runApp(config: {
),
);

const runnerHost = new RunnerHost(() => {
const runnerHost = new RunnerHost(async () => {
const chatStorage = new MemoryChatStorage();

chatStorage.append([{ role: "system", content: sandbox.systemPrompt }]);
Expand All @@ -53,20 +53,21 @@ export async function runApp(config: {
sandbox,
chatStorage,
model,
ctx,
await pendingCtx,
);
});

switch (config.interface) {
case "cli":
await pendingCtx;
if (sandbox.userMessage) {
console.log(sandbox.userMessage);
}
await cli(runnerHost);
break;
case "http":
default:
http(runnerHost, config.port);
http(runnerHost, config.port, pendingCtx);
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ export async function publishProject(
sandboxFactory = getDefaultSandbox,
): Promise<string> {
projectPath = await Deno.realPath(projectPath);
const projectJson = await getProjectJson(projectPath, sandboxFactory);
const projectJson = await getProjectJson(
projectPath,
"local",
sandboxFactory,
);
let code = await generateBundle(projectPath);
const vectorDbPath = projectJson.vectorStorage?.path;
if (vectorDbPath) {
Expand Down
9 changes: 9 additions & 0 deletions src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,22 @@ export type ChatChunkResponse = Static<typeof ChatChunkResponse>;
export function http(
runnerHost: RunnerHost,
port: number,
onReady?: Promise<unknown>,
): Deno.HttpServer<Deno.NetAddr> {
const app = new Hono();

// The ready status should change once the project is fully loaded, including the vector DB
let ready = false;
onReady?.then(() => ready = true);

app.get("/health", (c) => {
return c.text("ok");
});

app.get("/ready", (c) => {
return c.text(ready.toString());
});

app.get("/v1/models", (c) => {
return c.json({
object: "list",
Expand Down
8 changes: 5 additions & 3 deletions src/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import type { IProject } from "./project/project.ts";
import type { TSchema } from "@sinclair/typebox";
import type { IPFSClient } from "./ipfs.ts";
import { loadProject } from "./loader.ts";
import type { ProjectSource } from "./util.ts";

export async function getProjectJson(
projectPath: string,
source: ProjectSource,
sandboxFactory = getDefaultSandbox,
): Promise<Omit<IProject, "tools"> & { tools: string[]; config?: TSchema }> {
const sandbox = await sandboxFactory(resolve(projectPath));
const sandbox = await sandboxFactory(resolve(projectPath), source);

return {
model: sandbox.model,
Expand All @@ -26,8 +28,8 @@ export async function projectInfo(
ipfs: IPFSClient,
json = false,
): Promise<void> {
const loadedPath = await loadProject(projectPath, ipfs);
const projectJson = await getProjectJson(loadedPath);
const [loadedPath, source] = await loadProject(projectPath, ipfs);
const projectJson = await getProjectJson(loadedPath, source);

if (json) {
console.log(JSON.stringify(
Expand Down
10 changes: 5 additions & 5 deletions src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CIDReg, type IPFSClient } from "./ipfs.ts";
import { resolve } from "@std/path/resolve";
import { UntarStream } from "@std/tar";
import { ensureDir, exists } from "@std/fs";
import { getSpinner } from "./util.ts";
import { getSpinner, type ProjectSource } from "./util.ts";

export const getOSTempDir = () =>
Deno.env.get("TMPDIR") || Deno.env.get("TMP") || Deno.env.get("TEMP") ||
Expand All @@ -14,7 +14,7 @@ export async function loadProject(
ipfs: IPFSClient,
tmpDir?: string,
forceReload?: boolean,
): Promise<string> {
): Promise<[string, ProjectSource]> {
if (CIDReg.test(projectPath)) {
const spinner = getSpinner().start("Loading project from IPFS");
try {
Expand All @@ -25,7 +25,7 @@ export async function loadProject(
// Early exit if the file has already been fetched
if (!forceReload && (await exists(filePath))) {
spinner.succeed("Loaded project from IPFS");
return filePath;
return [filePath, "ipfs"];
}
await ensureDir(tmp);

Expand All @@ -36,14 +36,14 @@ export async function loadProject(

spinner.succeed("Loaded project from IPFS");

return filePath;
return [filePath, "ipfs"];
} catch (e) {
spinner.fail("Failed to load project");
throw e;
}
}

return resolve(projectPath);
return [resolve(projectPath), "local"];
}

export async function loadVectorStoragePath(
Expand Down
19 changes: 12 additions & 7 deletions src/project/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ export const VectorConfig = Type.Object({
});

export const Project = Type.Object({
// Note: If a new spec version is introduced Type.TemplateLiteral could be used here
specVersion: Type.Literal(
"0.0.1",
{ description: "The specification version of the project structure." },
),
model: Type.String({
description: "The llm model to use",
}),
Expand Down Expand Up @@ -71,8 +76,9 @@ export type IProjectEntrypoint<T extends TObject = TObject> = Static<
IProjectEntry<T>
>;

export function validateProject(project: unknown): void {
return Value.Assert(Project, project);
export function validateProject(project: unknown): project is IProject {
Value.Assert(Project, project);
return true;
}

function validateProjectEntry(entry: unknown): entry is IProjectEntry {
Expand Down Expand Up @@ -109,10 +115,9 @@ export async function getProjectFromEntrypoint(
// Check that the constructed project is valid
const project = await entrypoint.projectFactory(config);

validateProject(project);

return project;
} else {
throw new Error("Unable to validate project");
if (validateProject(project)) {
return project;
}
}
throw new Error("Unable to validate project");
}
37 changes: 34 additions & 3 deletions src/sandbox/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,44 @@
import type { ProjectSource } from "../util.ts";
import type { ISandbox } from "./sandbox.ts";
import { WebWorkerSandbox } from "./webWorker/webWorkerSandbox.ts";
import {
type Permissions,
WebWorkerSandbox,
} from "./webWorker/webWorkerSandbox.ts";

export * from "./sandbox.ts";
export * from "./mockSandbox.ts";
export * from "./unsafeSandbox.ts";

export function getDefaultSandbox(path: string): Promise<ISandbox> {
const IPFS_PERMISSIONS: Permissions = {
allowRead: false,
allowFFI: false,
};

const LOCAL_PERMISSIONS: Permissions = {
allowRead: true,
allowFFI: true,
};

function getPermisionsForSource(source: ProjectSource): Permissions {
switch (source) {
case "local":
return LOCAL_PERMISSIONS;
case "ipfs":
return IPFS_PERMISSIONS;
default:
throw new Error(
`Unable to set permissions for unknown source: ${source}`,
);
}
}

export function getDefaultSandbox(
path: string,
source: ProjectSource,
): Promise<ISandbox> {
// return UnsafeSandbox.create(path);
return WebWorkerSandbox.create(path);
const permissions = getPermisionsForSource(source);
return WebWorkerSandbox.create(path, permissions);
}

export { WebWorkerSandbox };
12 changes: 10 additions & 2 deletions src/sandbox/webWorker/webWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
type IProjectEntrypoint,
} from "../../project/project.ts";
import type { IContext } from "../../context/context.ts";
import { PrettyTypeboxError } from "../../util.ts";

const conn = rpc.createMessageConnection(
new BrowserMessageReader(self),
Expand Down Expand Up @@ -52,9 +53,16 @@ conn.onRequest(Init, async (config) => {
throw new Error("Please call `load` first");
}

project ??= await getProjectFromEntrypoint(entrypoint, config);
try {
project ??= await getProjectFromEntrypoint(entrypoint, config);

return toJsonProject();
return toJsonProject();
} catch (e: unknown) {
if (e instanceof Error) {
throw PrettyTypeboxError(e, "Project validation failed");
}
throw e;
}
});

conn.onRequest(GetConfig, () => {
Expand Down
22 changes: 17 additions & 5 deletions src/sandbox/webWorker/webWorkerSandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,36 @@ import { loadConfigFromEnv } from "../../util.ts";
import { FromSchema } from "../../fromSchema.ts";
import type { IContext } from "../../context/context.ts";
import type { IVectorConfig } from "../../project/project.ts";
import { dirname } from "@std/path/dirname";

export type Permissions = {
/**
* For local projects allow reading all locations for imports to work.
* TODO: This could be limited to the project dir + DENO_DIR cache but DENO_DIR doesn't provide the default currently
*/
allowRead?: boolean;
allowFFI?: boolean;
};

export class WebWorkerSandbox implements ISandbox {
#connection: rpc.MessageConnection;
#config: TSchema | undefined;
#tools: Tool[];

public static async create(path: string): Promise<WebWorkerSandbox> {
public static async create(
path: string,
permissions?: Permissions,
): Promise<WebWorkerSandbox> {
const w = new Worker(
import.meta.resolve("./webWorker.ts" /*path*/),
import.meta.resolve("./webWorker.ts"),
{
type: "module",
deno: {
permissions: {
env: false, // Should be passed through in loadConfigFromEnv below
// hrtime: false,
net: "inherit", // TODO remove localhost
ffi: true, // Needed for node js ffi
read: true, // Needed for imports to node modules
ffi: permissions?.allowFFI ?? false, // Needed for node js ffi, TODO this could be the same as read permissions
read: permissions?.allowRead ? true : [dirname(path)],
run: false,
write: false,
},
Expand Down
24 changes: 23 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Static, TSchema } from "@sinclair/typebox";
import { Value } from "@sinclair/typebox/value";
import { AssertError, Value } from "@sinclair/typebox/value";
import ora, { type Ora } from "ora";
import { brightBlue } from "@std/fmt/colors";

Expand Down Expand Up @@ -32,3 +32,25 @@ export function getPrompt(): string | null {

return response;
}

// Possible sources where projects can be loaded from
export type ProjectSource = "local" | "ipfs";

export function PrettyTypeboxError(
error: Error,
prefix = "Type Assertion Failed",
): Error {
if (
error instanceof AssertError || error.constructor.name === "AssertError"
) {
const errs = [...(error as AssertError).Errors()];

let msg = `${prefix}:\n`;
for (const e of errs) {
msg += `\t${e.path}: ${e.message}\n`;
}
return new Error(msg, { cause: error });
}

return error;
}
Loading
Loading