-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to fastify + add model CRUD impl
- Loading branch information
1 parent
1bf75cf
commit f5c19bc
Showing
11 changed files
with
154 additions
and
63 deletions.
There are no files selected for viewing
Empty file.
Empty file.
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 |
---|---|---|
@@ -1,28 +1,17 @@ | ||
import express from 'express' | ||
import bodyParser from 'body-parser' | ||
import fs from 'fs' | ||
import v1API from './v1' | ||
import fastify from 'fastify' | ||
|
||
import v1API from './v1' | ||
const JAN_API_PORT = 1337; | ||
|
||
const server = express() | ||
server.use(bodyParser.urlencoded()) | ||
server.use(bodyParser.json()) | ||
const server = fastify() | ||
|
||
const USER_ROOT_DIR = '.data' | ||
server.use("/v1", v1API) | ||
server.register(v1API, {prefix: "/api/v1"}) | ||
|
||
// server.post("fs", (req, res) => { | ||
// let op = req.body.op; | ||
// switch(op){ | ||
// case 'readFile': | ||
// fs.readFile(req.body.path, ()=>{}) | ||
// case 'writeFile': | ||
// fs.writeFile(req.body.path, Buffer.from(req.body.data, "base64"), ()=>{}) | ||
// } | ||
// }) | ||
|
||
server.listen(JAN_API_PORT, () => { | ||
console.log(`JAN API listening at: http://localhost:${JAN_API_PORT}`); | ||
server.listen({ | ||
port: JAN_API_PORT, | ||
host: "0.0.0.0" | ||
}).then(()=>{ | ||
console.log(`JAN API listening at: http://0.0.0.0:${JAN_API_PORT}`); | ||
}) | ||
|
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 |
---|---|---|
@@ -1,5 +1,8 @@ | ||
import { Request, Response } from 'express' | ||
import { FastifyInstance, FastifyPluginAsync, FastifyPluginOptions } from 'fastify' | ||
|
||
export default function route(req: Request, res: Response){ | ||
|
||
} | ||
const router: FastifyPluginAsync = async (app: FastifyInstance, opts: FastifyPluginOptions) => { | ||
//TODO: Add controllers for assistants here | ||
// app.get("/", controller) | ||
// app.post("/", controller) | ||
} | ||
export default router; |
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 |
---|---|---|
@@ -1,5 +1,11 @@ | ||
import { Request, Response } from 'express' | ||
import { FastifyInstance, FastifyPluginAsync, FastifyPluginOptions } from 'fastify' | ||
|
||
export default function route(req: Request, res: Response){ | ||
|
||
} | ||
const router: FastifyPluginAsync = async (app: FastifyInstance, opts: FastifyPluginOptions) => { | ||
//TODO: Add controllers for here | ||
// app.get("/", controller) | ||
|
||
app.post("/", (req, res) => { | ||
req.body | ||
}) | ||
} | ||
export default router; |
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 |
---|---|---|
@@ -1,20 +1,37 @@ | ||
import { Request, Response } from 'express' | ||
|
||
import assistantsAPI from './assistants' | ||
import chatCompletionAPI from './chat' | ||
import modelsAPI from './models' | ||
import threadsAPI from './threads' | ||
|
||
export default function route(req: Request, res: Response){ | ||
console.log(req.path.split("/")[1]) | ||
switch (req.path.split("/")[1]){ | ||
case 'assistants': | ||
assistantsAPI(req, res) | ||
case 'chat': | ||
chatCompletionAPI(req, res) | ||
case 'models': | ||
modelsAPI(req, res) | ||
case 'threads': | ||
threadsAPI(req, res) | ||
} | ||
} | ||
import { FastifyInstance, FastifyPluginAsync } from 'fastify' | ||
|
||
const router: FastifyPluginAsync = async (app: FastifyInstance, opts) => { | ||
app.register( | ||
assistantsAPI, | ||
{ | ||
prefix: "/assisstants" | ||
} | ||
) | ||
|
||
app.register( | ||
chatCompletionAPI, | ||
{ | ||
prefix: "/chat/completion" | ||
} | ||
) | ||
|
||
app.register( | ||
modelsAPI, | ||
{ | ||
prefix: "/models" | ||
} | ||
) | ||
|
||
app.register( | ||
threadsAPI, | ||
{ | ||
prefix: "/threads" | ||
} | ||
) | ||
} | ||
export default router; |
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 |
---|---|---|
@@ -1,5 +1,23 @@ | ||
import { Request, Response } from 'express' | ||
import { RouteHandlerMethod, FastifyRequest, FastifyReply } from 'fastify' | ||
import { MODEL_FOLDER_PATH } from "./index" | ||
import fs from 'fs/promises' | ||
|
||
export default function controller(req: Request, res: Response){ | ||
const controller: RouteHandlerMethod = async (req: FastifyRequest, res: FastifyReply) => { | ||
//TODO: download models impl | ||
//Mirror logic from JanModelExtension.downloadModel? | ||
let model = req.body.model; | ||
|
||
} | ||
// Fetching logic | ||
// const directoryPath = join(MODEL_FOLDER_PATH, model.id) | ||
// await fs.mkdir(directoryPath) | ||
|
||
// const path = join(directoryPath, model.id) | ||
// downloadFile(model.source_url, path) | ||
// TODO: Different model downloader from different model vendor | ||
|
||
res.status(200).send({ | ||
status: "Ok" | ||
}) | ||
} | ||
|
||
export default controller; |
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 |
---|---|---|
@@ -1,18 +1,61 @@ | ||
import { Request, Response } from 'express' | ||
|
||
import downloadModelController from './downloadModel' | ||
export const MODEL_FOLDER_PATH = "./data/models" | ||
export const _modelMetadataFileName = 'model.json' | ||
|
||
function getModelController(req: Request, res: Response){ | ||
} | ||
import fs from 'fs/promises' | ||
import { Model } from '@janhq/core' | ||
import { join } from 'path' | ||
|
||
export default function route(req: Request, res: Response){ | ||
switch(req.method){ | ||
case 'get': | ||
getModelController(req, res) | ||
break; | ||
case 'post': | ||
downloadModelController(req, res) | ||
break; | ||
// map string => model object | ||
let modelIndex = new Map<String, Model>(); | ||
async function buildModelIndex(){ | ||
let modelIds = await fs.readdir(MODEL_FOLDER_PATH); | ||
// TODO: read modelFolders to get model info, mirror JanModelExtension? | ||
try{ | ||
for(let modelId in modelIds){ | ||
let path = join(MODEL_FOLDER_PATH, modelId) | ||
let fileData = await fs.readFile(join(path, _modelMetadataFileName)) | ||
modelIndex.set(modelId, JSON.parse(fileData.toString("utf-8")) as Model) | ||
} | ||
} | ||
} | ||
catch(err){ | ||
console.error("build model index failed. ", err); | ||
} | ||
} | ||
buildModelIndex() | ||
|
||
import { FastifyInstance, FastifyPluginAsync, FastifyPluginOptions } from 'fastify' | ||
import downloadModelController from './downloadModel' | ||
import { startModel, stopModel } from './modelOp' | ||
|
||
const router: FastifyPluginAsync = async (app: FastifyInstance, opts: FastifyPluginOptions) => { | ||
//TODO: Add controllers declaration here | ||
|
||
///////////// CRUD //////////////// | ||
// Model listing | ||
app.get("/", async (req, res) => { | ||
res.status(200).send( | ||
modelIndex.values() | ||
) | ||
}) | ||
|
||
// Retrieve model info | ||
app.get("/:id", (req, res) => { | ||
res.status(200).send( | ||
modelIndex.get(req.params.id) | ||
) | ||
}) | ||
|
||
// Delete model | ||
app.delete("/:id", (req, res) => { | ||
modelIndex.delete(req.params) | ||
|
||
// TODO: delete on disk | ||
}) | ||
|
||
///////////// Other ops //////////////// | ||
app.post("/", downloadModelController) | ||
app.put("/start", startModel) | ||
app.put("/stop", stopModel) | ||
} | ||
export default router; |
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,11 @@ | ||
import {FastifyRequest, FastifyReply} from 'fastify' | ||
|
||
export async function startModel(req: FastifyRequest, res: FastifyReply): Promise<void> { | ||
|
||
|
||
} | ||
|
||
export async function stopModel(req: FastifyRequest, res: FastifyReply): Promise<void> { | ||
|
||
|
||
} |
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 |
---|---|---|
@@ -1,5 +1,8 @@ | ||
import { Request, Response } from 'express' | ||
import { FastifyInstance, FastifyPluginAsync, FastifyPluginOptions } from 'fastify' | ||
|
||
export default function route(req: Request, res: Response){ | ||
const router: FastifyPluginAsync = async (app: FastifyInstance, opts: FastifyPluginOptions) => { | ||
//TODO: Add controllers declaration here | ||
|
||
} | ||
// app.get() | ||
} | ||
export default router; |