Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 1 addition & 23 deletions core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
export type CoreService =
| StoreService
| DataService
| InferenceService
| ModelManagementService
| SystemMonitoringService
| PreferenceService
Expand Down Expand Up @@ -93,27 +92,6 @@ export enum DataService {
UpdateBot = "updateBot",
}

/**
* InferenceService exports.
* @enum {string}
*/
export enum InferenceService {
/**
* Initializes a model for inference.
*/
InitModel = "initModel",

/**
* Stops a running inference model.
*/
StopModel = "stopModel",

/**
* Single inference response.
*/
InferenceRequest = "inferenceRequest",
}

/**
* ModelManagementService exports.
* @enum {string}
Expand Down Expand Up @@ -261,4 +239,4 @@ export {
export { preferences } from "./preferences";
export { fs } from "./fs";

export { JanPlugin, PluginType } from "./plugin";
export { JanPlugin, PluginType } from "./plugin";
2 changes: 2 additions & 0 deletions core/src/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export { ConversationalPlugin } from './conversational'
export { InferencePlugin } from './inference'
export { ModelPlugin } from './model'
8 changes: 8 additions & 0 deletions core/src/plugins/inference.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { NewMessageRequest } from "../events";
import { JanPlugin } from "../plugin";

export abstract class InferencePlugin extends JanPlugin {
abstract initModel(modelFileName: string): Promise<void>;
abstract stopModel(): Promise<void>;
abstract inferenceRequest(data: NewMessageRequest): Promise<any>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: Give response type here

}
3 changes: 0 additions & 3 deletions plugins/inference-plugin/@types/global.d.ts

This file was deleted.

166 changes: 0 additions & 166 deletions plugins/inference-plugin/index.ts

This file was deleted.

1 change: 0 additions & 1 deletion plugins/inference-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"name": "@janhq/inference-plugin",
"version": "1.0.20",
"description": "Inference Plugin, powered by @janhq/nitro, bring a high-performance Llama model inference in pure C++.",
"icon": "https://raw.githubusercontent.com/tailwindlabs/heroicons/88e98b0c2b458553fbadccddc2d2f878edc0387b/src/20/solid/command-line.svg",
"main": "dist/index.js",
"module": "dist/module.js",
"author": "Jan <[email protected]>",
Expand Down
2 changes: 2 additions & 0 deletions plugins/inference-plugin/src/@types/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
declare const MODULE: string;
declare const INFERENCE_URL: string;
52 changes: 52 additions & 0 deletions plugins/inference-plugin/src/helpers/sse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Observable } from "rxjs";
/**
* Sends a request to the inference server to generate a response based on the recent messages.
* @param recentMessages - An array of recent messages to use as context for the inference.
* @returns An Observable that emits the generated response as a string.
*/
export function requestInference(recentMessages: any[]): Observable<string> {
return new Observable((subscriber) => {
const requestBody = JSON.stringify({
messages: recentMessages,
stream: true,
model: "gpt-3.5-turbo",
max_tokens: 2048,
});
fetch(INFERENCE_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "text/event-stream",
"Access-Control-Allow-Origin": "*",
},
body: requestBody,
})
.then(async (response) => {
const stream = response.body;
const decoder = new TextDecoder("utf-8");
const reader = stream?.getReader();
let content = "";

while (true && reader) {
const { done, value } = await reader.read();
if (done) {
break;
}
const text = decoder.decode(value);
const lines = text.trim().split("\n");
for (const line of lines) {
if (line.startsWith("data: ") && !line.includes("data: [DONE]")) {
const data = JSON.parse(line.replace("data: ", ""));
content += data.choices[0]?.delta?.content ?? "";
if (content.startsWith("assistant: ")) {
content = content.replace("assistant: ", "");
}
subscriber.next(content);
}
}
}
subscriber.complete();
})
.catch((err) => subscriber.error(err));
});
}
Loading