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

feat: Featherless integration #3542

Open
wants to merge 2 commits into
base: dev
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
1 change: 1 addition & 0 deletions core/src/types/model/modelEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export enum InferenceEngine {
cortex_llamacpp = 'cortex.llamacpp',
cortex_onnx = 'cortex.onnx',
cortex_tensorrtllm = 'cortex.tensorrt-llm',
featherless = 'featherless',
}

export type ModelArtifact = {
Expand Down
79 changes: 79 additions & 0 deletions extensions/inference-featherless-extension/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Featherless Engine Extension

Created using Jan extension example

# Create a Jan Extension using Typescript

Use this template to bootstrap the creation of a TypeScript Jan extension. 🚀

## Create Your Own Extension

To create your own extension, you can use this repository as a template! Just follow the below instructions:

1. Click the Use this template button at the top of the repository
2. Select Create a new repository
3. Select an owner and name for your new repository
4. Click Create repository
5. Clone your new repository

## Initial Setup

After you've cloned the repository to your local machine or codespace, you'll need to perform some initial setup steps before you can develop your extension.

> [!NOTE]
>
> You'll need to have a reasonably modern version of
> [Node.js](https://nodejs.org) handy. If you are using a version manager like
> [`nodenv`](https://github.com/nodenv/nodenv) or
> [`nvm`](https://github.com/nvm-sh/nvm), you can run `nodenv install` in the
> root of your repository to install the version specified in
> [`package.json`](./package.json). Otherwise, 20.x or later should work!

1. :hammer_and_wrench: Install the dependencies

```bash
npm install
```

1. :building_construction: Package the TypeScript for distribution

```bash
npm run bundle
```

1. :white_check_mark: Check your artifact

There will be a tgz file in your extension directory now

## Update the Extension Metadata

The [`package.json`](package.json) file defines metadata about your extension, such as
extension name, main entry, description and version.

When you copy this repository, update `package.json` with the name, description for your extension.

## Update the Extension Code

The [`src/`](./src/) directory is the heart of your extension! This contains the
source code that will be run when your extension functions are invoked. You can replace the
contents of this directory with your own code.

There are a few things to keep in mind when writing your extension code:

- Most Jan Extension functions are processed asynchronously.
In `index.ts`, you will see that the extension function will return a `Promise<any>`.

```typescript
import { events, MessageEvent, MessageRequest } from '@janhq/core'

function onStart(): Promise<any> {
return events.on(MessageEvent.OnMessageSent, (data: MessageRequest) =>
this.inference(data)
)
}
```

For more information about the Jan Extension Core module, see the
[documentation](https://github.com/janhq/jan/blob/main/core/README.md).

So, what are you waiting for? Go ahead and start customizing your extension!
43 changes: 43 additions & 0 deletions extensions/inference-featherless-extension/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "@janhq/inference-featherless-extension",
"productName": "Featherless Inference Engine",
"version": "1.0.0",
"description": "This extension enables Featherless.ai chat completion API calls",
"main": "dist/index.js",
"module": "dist/module.js",
"engine": "Featherless",
"author": "Jan <[email protected]>",
"license": "AGPL-3.0",
"scripts": {
"build": "tsc -b . && webpack --config webpack.config.js",
"build:publish": "rimraf *.tgz --glob && yarn build && npm pack && cpx *.tgz ../../pre-install",
"sync:core": "cd ../.. && yarn build:core && cd extensions && rm yarn.lock && cd inference-featherless-extension && yarn && yarn build:publish"
},
"exports": {
".": "./dist/index.js",
"./main": "./dist/module.js"
},
"devDependencies": {
"cpx": "^1.5.0",
"rimraf": "^3.0.2",
"webpack": "^5.88.2",
"webpack-cli": "^5.1.4",
"ts-loader": "^9.5.0"
},
"dependencies": {
"@janhq/core": "file:../../core",
"fetch-retry": "^5.0.6",
"ulidx": "^2.3.0"
},
"engines": {
"node": ">=18.0.0"
},
"files": [
"dist/*",
"package.json",
"README.md"
],
"bundleDependencies": [
"fetch-retry"
]
}
28 changes: 28 additions & 0 deletions extensions/inference-featherless-extension/resources/models.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[
{
"sources": [
{
"url": "https://featherless.ai"
}
],
"id": "featherless-auto",
"object": "model",
"name": "Featherless",
"version": "1.0",
"description": "Featherless lets you access all of huggingfaces models through their api",
"format": "api",
"settings": {},
"parameters": {
"max_tokens": 1024,
"temperature": 0.7,
"top_p": 0.95,
"frequency_penalty": 0,
"presence_penalty": 0
},
"metadata": {
"author": "Featherless",
"tags": ["General", "Big Context Length"]
},
"engine": "Featherless"
}
]
34 changes: 34 additions & 0 deletions extensions/inference-featherless-extension/resources/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[
{
"key": "featherless-api-key",
"title": "API Key",
"description": "The Featherless API uses API keys for authentication. Visit your [API Keys](https://featherless.ai/account/api-keys) page to retrieve the API key you'll use in your requests.",
"controllerType": "input",
"controllerProps": {
"placeholder": "Insert API Key",
"value": "",
"type": "password",
"inputActions": ["unobscure", "copy"]
}
},
{
"key": "chat-completions-endpoint",
"title": "Chat Completions Endpoint",
"description": "The endpoint to use for chat completions. See the [documentation](https://featherless.ai/docs) for more information.",
"controllerType": "input",
"controllerProps": {
"placeholder": "https://api.featherless.ai/v1/chat/completions",
"value": "https://api.featherless.ai/v1/chat/completions"
}
},
{
"key": "featherless-model",
"title": "Model",
"description": "Choose a model at [Featherless](https://featherless.ai/models)",
"controllerType": "input",
"controllerProps": {
"placeholder": "choose a model at https://featherless.ai/models",
"value": "Sao10K/L3-8B-Stheno-v3.2"
}
}
]
88 changes: 88 additions & 0 deletions extensions/inference-featherless-extension/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* @file This file exports a class that implements the InferenceExtension interface from the @janhq/core package.
* The class provides methods for initializing and stopping a model, and for making inference requests.
* It also subscribes to events emitted by the @janhq/core package and handles new message requests.
* @version 1.0.0
* @module inference-openai-extension/src/index
*/

import { RemoteOAIEngine } from '@janhq/core'
import { PayloadType } from '@janhq/core'

declare const SETTINGS: Array<any>
declare const MODELS: Array<any>

enum Settings {
apiKey = 'featherless-api-key',
model = 'featherless-model',
chatCompletionsEndPoint = 'chat-completions-endpoint',
}

/**
* A class that implements the InferenceExtension interface from the @janhq/core package.
* The class provides methods for initializing and stopping a model, and for making inference requests.
* It also subscribes to events emitted by the @janhq/core package and handles new message requests.
*/
export default class JanInferenceFeatherlessExtension extends RemoteOAIEngine {
inferenceUrl: string = ''
provider: string = 'Featherless'
model?: string | undefined

override async onLoad(): Promise<void> {
super.onLoad()

// Register Settings
this.registerSettings(SETTINGS)
this.registerModels(MODELS)

this.apiKey = await this.getSetting<string>(Settings.apiKey, '')
this.inferenceUrl = await this.getSetting<string>(
Settings.chatCompletionsEndPoint,
''
)
this.model = await this.getSetting<string>(Settings.model, '')

if (!this.model?.length) this.model = undefined
if (this.inferenceUrl.length === 0) {
SETTINGS.forEach((setting) => {
if (setting.key === Settings.chatCompletionsEndPoint) {
this.inferenceUrl = setting.controllerProps.value as string
}
})
}
}

override async headers(): Promise<HeadersInit> {
return {
'Content-Type': 'application/json',
'HTTP-Referer': 'https://jan.ai',
'Authorization': `Bearer ${this.apiKey}`,
}
}

onSettingUpdate<T>(key: string, value: T): void {
if (key === Settings.apiKey) {
this.apiKey = value as string
} else if (key === Settings.chatCompletionsEndPoint) {
if (typeof value !== 'string') return

if (value.trim().length === 0) {
SETTINGS.forEach((setting) => {
if (setting.key === Settings.chatCompletionsEndPoint) {
this.inferenceUrl = setting.controllerProps.value as string
}
})
} else {
this.inferenceUrl = value
}
} else if (key === Settings.model) {
this.model =
typeof value === 'string' && value.length > 0 ? value : undefined
}
}

transformPayload = (payload: PayloadType) => ({
...payload,
model: this.model,
})
}
14 changes: 14 additions & 0 deletions extensions/inference-featherless-extension/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"target": "es2016",
"module": "ES6",
"moduleResolution": "node",
"outDir": "./dist",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": false,
"skipLibCheck": true,
"rootDir": "./src"
},
"include": ["./src"]
}
37 changes: 37 additions & 0 deletions extensions/inference-featherless-extension/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const webpack = require('webpack')
const packageJson = require('./package.json')
const settingJson = require('./resources/settings.json')
const modelsJson = require('./resources/models.json')

module.exports = {
experiments: { outputModule: true },
entry: './src/index.ts', // Adjust the entry point to match your project's main file
mode: 'production',
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
],
},
plugins: [
new webpack.DefinePlugin({
MODELS: JSON.stringify(modelsJson),
SETTINGS: JSON.stringify(settingJson),
ENGINE: JSON.stringify(packageJson.engine),
}),
],
output: {
filename: 'index.js', // Adjust the output file name as needed
library: { type: 'module' }, // Specify ESM output format
},
resolve: {
extensions: ['.ts', '.js'],
},
optimization: {
minimize: false,
},
// Add loaders and other configuration as needed for your project
}
11 changes: 11 additions & 0 deletions web/public/images/ModelProvider/featherless.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions web/utils/modelEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export const getLogoEngine = (engine: InferenceEngine) => {
return 'images/ModelProvider/cohere.svg'
case InferenceEngine.nvidia:
return 'images/ModelProvider/nvidia.svg'
case InferenceEngine.featherless:
return 'images/ModelProvider/featherless.svg'
default:
return undefined
}
Expand Down Expand Up @@ -56,6 +58,8 @@ export const getTitleByEngine = (engine: InferenceEngine) => {
return 'OpenAI'
case InferenceEngine.openrouter:
return 'OpenRouter'
case InferenceEngine.featherless:
return 'Featherless'
default:
return engine.charAt(0).toUpperCase() + engine.slice(1)
}
Expand Down