-
Notifications
You must be signed in to change notification settings - Fork 2.4k
feat: boilerplate for express server localhost 1337 #803
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
Merged
linhtran174
merged 11 commits into
main
from
768-fs-adapter-calling-server-api-for-handling-fs
Dec 5, 2023
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ed5413a
Base server code (mirrored from electron + removed unnecessary handlers
linhtran174 64a58d1
WIP: fs adapter calling server API
linhtran174 e5f57b8
boilerplate for express server
linhtran174 5fa5e20
Merge branch 'main' into 768-fs-adapter-calling-server-api-for-handli…
linhtran174 1bf75cf
Merge branch 'main' of https://github.com/janhq/jan into 768-fs-adapt…
linhtran174 f5c19bc
Switch to fastify + add model CRUD impl
linhtran174 f6f5db0
Resolve comments + update licence
linhtran174 12e7651
resolve comments
linhtran174 e5885d8
Merge branch 'main' of https://github.com/janhq/jan into 768-fs-adapt…
linhtran174 dbcd357
Remove old merge mistake - auto merge adding lines to Makefile
linhtran174 d0e6571
fix console print
linhtran174 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 @@ | ||
| data |
Empty file.
Empty file.
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
This file contains hidden or 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,19 @@ | ||
| import fastify from 'fastify' | ||
| import dotenv from 'dotenv' | ||
| import v1API from './v1' | ||
| const server = fastify() | ||
|
|
||
| dotenv.config() | ||
| server.register(v1API, {prefix: "/api/v1"}) | ||
|
|
||
|
|
||
| const JAN_API_PORT = Number.parseInt(process.env.JAN_API_PORT || '1337') | ||
| const JAN_API_HOST = process.env.JAN_API_HOST || "0.0.0.0" | ||
|
|
||
| server.listen({ | ||
| port: JAN_API_PORT, | ||
| host: JAN_API_HOST | ||
| }).then(() => { | ||
| console.log(`JAN API listening at: http://${JAN_API_HOST}:${JAN_API_PORT}`); | ||
| }) | ||
|
|
||
This file contains hidden or 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,5 @@ | ||
| { | ||
| "watch": ["main.ts", "v1"], | ||
| "ext": "ts, json", | ||
| "exec": "tsc && node ./build/main.js" | ||
| } |
This file contains hidden or 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,32 @@ | ||
| { | ||
linhtran174 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "name": "jan-server", | ||
| "version": "0.1.3", | ||
| "main": "./build/main.js", | ||
| "author": "Jan <[email protected]>", | ||
| "license": "AGPL-3.0", | ||
| "homepage": "https://jan.ai", | ||
| "description": "Use offline LLMs with your own data. Run open source models like Llama2 or Falcon on your internal computers/servers.", | ||
| "build": "", | ||
| "scripts": { | ||
| "lint": "eslint . --ext \".js,.jsx,.ts,.tsx\"", | ||
| "test:e2e": "playwright test --workers=1", | ||
| "dev": "nodemon .", | ||
| "build": "tsc" | ||
| }, | ||
| "dependencies": { | ||
| }, | ||
| "devDependencies": { | ||
| "@types/body-parser": "^1.19.5", | ||
| "@types/npmcli__arborist": "^5.6.4", | ||
| "@typescript-eslint/eslint-plugin": "^6.7.3", | ||
| "@typescript-eslint/parser": "^6.7.3", | ||
| "dotenv": "^16.3.1", | ||
| "eslint-plugin-react": "^7.33.2", | ||
| "fastify": "^4.24.3", | ||
| "nodemon": "^3.0.1", | ||
| "run-script-os": "^1.1.6" | ||
| }, | ||
| "installConfig": { | ||
| "hoistingLimits": "workspaces" | ||
| } | ||
| } | ||
This file contains hidden or 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,22 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "target": "es5", | ||
| "module": "commonjs", | ||
| "noImplicitAny": true, | ||
| "sourceMap": true, | ||
| "strict": true, | ||
| "outDir": "./build", | ||
| "rootDir": "./", | ||
| "noEmitOnError": true, | ||
| "esModuleInterop": true, | ||
| "baseUrl": ".", | ||
| "allowJs": true, | ||
| "skipLibCheck": true, | ||
| "paths": { "*": ["node_modules/*"] }, | ||
| "typeRoots": ["node_modules/@types"] | ||
| }, | ||
| // "sourceMap": true, | ||
|
|
||
| "include": ["./**/*.ts"], | ||
| "exclude": ["core", "build", "dist", "tests", "node_modules"] | ||
| } |
This file contains hidden or 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,8 @@ | ||
| import { FastifyInstance, FastifyPluginAsync, FastifyPluginOptions } from 'fastify' | ||
|
|
||
| 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 hidden or 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 { FastifyInstance, FastifyPluginAsync, FastifyPluginOptions } from 'fastify' | ||
|
|
||
| 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 hidden or 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,37 @@ | ||
| import assistantsAPI from './assistants' | ||
| import chatCompletionAPI from './chat' | ||
| import modelsAPI from './models' | ||
| import threadsAPI from './threads' | ||
|
|
||
| import { FastifyInstance, FastifyPluginAsync } from 'fastify' | ||
|
|
||
| const router: FastifyPluginAsync = async (app: FastifyInstance, opts) => { | ||
| app.register( | ||
| assistantsAPI, | ||
| { | ||
| prefix: "/assistants" | ||
| } | ||
| ) | ||
|
|
||
| app.register( | ||
| chatCompletionAPI, | ||
| { | ||
| prefix: "/chat/completion" | ||
| } | ||
| ) | ||
|
|
||
| app.register( | ||
| modelsAPI, | ||
| { | ||
| prefix: "/models" | ||
| } | ||
| ) | ||
|
|
||
| app.register( | ||
| threadsAPI, | ||
| { | ||
| prefix: "/threads" | ||
| } | ||
| ) | ||
| } | ||
| export default router; |
This file contains hidden or 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,23 @@ | ||
| import { RouteHandlerMethod, FastifyRequest, FastifyReply } from 'fastify' | ||
| import { MODEL_FOLDER_PATH } from "./index" | ||
| import fs from 'fs/promises' | ||
|
|
||
| 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 hidden or 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,61 @@ | ||
|
|
||
| export const MODEL_FOLDER_PATH = "./data/models" | ||
| export const _modelMetadataFileName = 'model.json' | ||
|
|
||
| import fs from 'fs/promises' | ||
| import { Model } from '@janhq/core' | ||
| import { join } from 'path' | ||
|
|
||
| // 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 hidden or 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 hidden or 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,8 @@ | ||
| import { FastifyInstance, FastifyPluginAsync, FastifyPluginOptions } from 'fastify' | ||
|
|
||
| const router: FastifyPluginAsync = async (app: FastifyInstance, opts: FastifyPluginOptions) => { | ||
| //TODO: Add controllers declaration here | ||
|
|
||
| // app.get() | ||
| } | ||
| export default router; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.