Skip to content

Commit

Permalink
update the temperature progress bar
Browse files Browse the repository at this point in the history
Signed-off-by: James <[email protected]>
  • Loading branch information
James committed Oct 20, 2023
1 parent 87891df commit 5a6d884
Show file tree
Hide file tree
Showing 12 changed files with 356 additions and 73 deletions.
200 changes: 174 additions & 26 deletions plugins/data-plugin/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { core, store, RegisterExtensionPoint, StoreService, DataService, PluginService } from "@janhq/core";
import {
core,
store,
RegisterExtensionPoint,
StoreService,
DataService,
PluginService,
} from "@janhq/core";

/**
* Create a collection on data store
Expand All @@ -8,7 +15,13 @@ import { core, store, RegisterExtensionPoint, StoreService, DataService, PluginS
* @returns Promise<void>
*
*/
function createCollection({ name, schema }: { name: string; schema?: { [key: string]: any } }): Promise<void> {
function createCollection({
name,
schema,
}: {
name: string;
schema?: { [key: string]: any };
}): Promise<void> {
return core.invokePluginFunc(MODULE_PATH, "createCollection", name, schema);
}

Expand All @@ -31,7 +44,13 @@ function deleteCollection(name: string): Promise<void> {
* @returns Promise<any>
*
*/
function insertOne({ collectionName, value }: { collectionName: string; value: any }): Promise<any> {
function insertOne({
collectionName,
value,
}: {
collectionName: string;
value: any;
}): Promise<any> {
return core.invokePluginFunc(MODULE_PATH, "insertOne", collectionName, value);
}

Expand All @@ -44,8 +63,22 @@ function insertOne({ collectionName, value }: { collectionName: string; value: a
* @returns Promise<void>
*
*/
function updateOne({ collectionName, key, value }: { collectionName: string; key: string; value: any }): Promise<void> {
return core.invokePluginFunc(MODULE_PATH, "updateOne", collectionName, key, value);
function updateOne({
collectionName,
key,
value,
}: {
collectionName: string;
key: string;
value: any;
}): Promise<void> {
return core.invokePluginFunc(
MODULE_PATH,
"updateOne",
collectionName,
key,
value
);
}

/**
Expand All @@ -64,7 +97,13 @@ function updateMany({
value: any;
selector?: { [key: string]: any };
}): Promise<void> {
return core.invokePluginFunc(MODULE_PATH, "updateMany", collectionName, value, selector);
return core.invokePluginFunc(
MODULE_PATH,
"updateMany",
collectionName,
value,
selector
);
}

/**
Expand All @@ -75,7 +114,13 @@ function updateMany({
* @returns Promise<void>
*
*/
function deleteOne({ collectionName, key }: { collectionName: string; key: string }): Promise<void> {
function deleteOne({
collectionName,
key,
}: {
collectionName: string;
key: string;
}): Promise<void> {
return core.invokePluginFunc(MODULE_PATH, "deleteOne", collectionName, key);
}

Expand All @@ -94,7 +139,12 @@ function deleteMany({
collectionName: string;
selector?: { [key: string]: any };
}): Promise<void> {
return core.invokePluginFunc(MODULE_PATH, "deleteMany", collectionName, selector);
return core.invokePluginFunc(
MODULE_PATH,
"deleteMany",
collectionName,
selector
);
}

/**
Expand All @@ -103,7 +153,13 @@ function deleteMany({
* @param {string} key - The key of the record to retrieve.
* @returns {Promise<any>} A promise that resolves when the record is retrieved.
*/
function findOne({ collectionName, key }: { collectionName: string; key: string }): Promise<any> {
function findOne({
collectionName,
key,
}: {
collectionName: string;
key: string;
}): Promise<any> {
return core.invokePluginFunc(MODULE_PATH, "findOne", collectionName, key);
}

Expand All @@ -123,7 +179,13 @@ function findMany({
selector: { [key: string]: any };
sort?: [{ [key: string]: any }];
}): Promise<any> {
return core.invokePluginFunc(MODULE_PATH, "findMany", collectionName, selector, sort);
return core.invokePluginFunc(
MODULE_PATH,
"findMany",
collectionName,
selector,
sort
);
}

function onStart() {
Expand All @@ -135,8 +197,16 @@ function onStart() {
// Register all the above functions and objects with the relevant extension points
export function init({ register }: { register: RegisterExtensionPoint }) {
register(PluginService.OnStart, PLUGIN_NAME, onStart);
register(StoreService.CreateCollection, createCollection.name, createCollection);
register(StoreService.DeleteCollection, deleteCollection.name, deleteCollection);
register(
StoreService.CreateCollection,
createCollection.name,
createCollection
);
register(
StoreService.DeleteCollection,
deleteCollection.name,
deleteCollection
);
register(StoreService.InsertOne, insertOne.name, insertOne);
register(StoreService.UpdateOne, updateOne.name, updateOne);
register(StoreService.UpdateMany, updateMany.name, updateMany);
Expand All @@ -145,15 +215,39 @@ export function init({ register }: { register: RegisterExtensionPoint }) {
register(StoreService.FindOne, findOne.name, findOne);
register(StoreService.FindMany, findMany.name, findMany);

register(DataService.GetConversations, getConversations.name, getConversations);
register(DataService.CreateConversation, createConversation.name, createConversation);
register(DataService.UpdateConversation, updateConversation.name, updateConversation);
register(
DataService.GetConversations,
getConversations.name,
getConversations
);
register(
DataService.CreateConversation,
createConversation.name,
createConversation
);
register(
DataService.UpdateConversation,
updateConversation.name,
updateConversation
);
register(DataService.UpdateMessage, updateMessage.name, updateMessage);
register(DataService.DeleteConversation, deleteConversation.name, deleteConversation);
register(
DataService.DeleteConversation,
deleteConversation.name,
deleteConversation
);
register(DataService.CreateMessage, createMessage.name, createMessage);
register(DataService.GetConversationMessages, getConversationMessages.name, getConversationMessages);
register(
DataService.GetConversationMessages,
getConversationMessages.name,
getConversationMessages
);

register("getConversationById", getConversationById.name, getConversationById);
register(
"getConversationById",
getConversationById.name,
getConversationById
);
register("createBot", createBot.name, createBot);
register("getBots", getBots.name, getBots);
register("getBotById", getBotById.name, getBotById);
Expand Down Expand Up @@ -186,29 +280,83 @@ function updateMessage(message: any): Promise<void> {
}

function deleteConversation(id: any) {
return store.deleteOne("conversations", id).then(() => store.deleteMany("messages", { conversationId: id }));
return store
.deleteOne("conversations", id)
.then(() => store.deleteMany("messages", { conversationId: id }));
}

function getConversationMessages(conversationId: any) {
return store.findMany("messages", { conversationId }, [{ createdAt: "desc" }]);
return store.findMany("messages", { conversationId }, [
{ createdAt: "desc" },
]);
}

function createBot(bot: any): Promise<void> {
return store.insertOne("bots", bot);
console.debug("Creating bot", JSON.stringify(bot, null, 2));
return store
.insertOne("bots", bot)
.then(() => {
console.debug("Bot created", JSON.stringify(bot, null, 2));
return Promise.resolve();
})
.catch((err) => {
console.error("Error creating bot", err);
return Promise.reject(err);
});
}

function getBots(): Promise<any> {
return store.findMany("bots", {});
console.debug("Getting bots");
return store
.findMany("bots", {name: { $gt: null }})
.then((bots) => {
console.debug("Bots retrieved", JSON.stringify(bots, null, 2));
return Promise.resolve(bots);
})
.catch((err) => {
console.error("Error getting bots", err);
return Promise.reject(err);
});
}

function deleteBot(id: any): Promise<any> {
return store.deleteOne("bots", id);
function deleteBot(id: string): Promise<any> {
console.debug("Deleting bot", id);
return store
.deleteOne("bots", id)
.then(() => {
console.debug("Bot deleted", id);
return Promise.resolve();
})
.catch((err) => {
console.error("Error deleting bot", err);
return Promise.reject(err);
});
}

function updateBot(bot: any): Promise<void> {
return store.updateOne("bots", bot._id, bot);
console.debug("Updating bot", JSON.stringify(bot, null, 2));
return store
.updateOne("bots", bot._id, bot)
.then(() => {
console.debug("Bot updated");
return Promise.resolve();
})
.catch((err) => {
console.error("Error updating bot", err);
return Promise.reject(err);
});
}

function getBotById(botId: string): Promise<any> {
return store.findOne("bots", botId);
console.debug("Getting bot", botId);
return store
.findOne("bots", botId)
.then((bot) => {
console.debug("Bot retrieved", JSON.stringify(bot, null, 2));
return Promise.resolve(bot);
})
.catch((err) => {
console.error("Error getting bot", err);
return Promise.reject(err);
});
}
2 changes: 1 addition & 1 deletion plugins/data-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"node_modules"
],
"dependencies": {
"@janhq/core": "file:../../core", // revert back this line to the original one
"@janhq/core": "file:../../core",
"pouchdb-find": "^8.0.1",
"pouchdb-node": "^8.0.1"
},
Expand Down
39 changes: 22 additions & 17 deletions plugins/inference-plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,26 @@ const stopModel = () => {
invokePluginFunc(MODULE_PATH, "killSubprocess");
};

function requestInference(recentMessages: any[]): Observable<string> {
function requestInference(recentMessages: any[], bot?: any): Observable<string> {
return new Observable((subscriber) => {
const requestBody = JSON.stringify({
messages: recentMessages,
stream: true,
model: "gpt-3.5-turbo",
max_tokens: bot?.maxTokens ?? 2048,
frequency_penalty: bot?.frequencyPenalty ?? 0,
presence_penalty: bot?.presencePenalty ?? 0,
temperature: bot?.customTemperature ?? 0,
});
console.debug(`Request body: ${requestBody}`);
fetch(INFERENCE_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "text/event-stream",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
messages: recentMessages,
stream: true,
model: "gpt-3.5-turbo",
max_tokens: 500,
}),
body: requestBody,
})
.then(async (response) => {
const stream = response.body;
Expand Down Expand Up @@ -62,14 +67,8 @@ function requestInference(recentMessages: any[]): Observable<string> {
});
}

async function retrieveLastTenMessages(conversationId: string) {
async function retrieveLastTenMessages(conversationId: string, bot?: any) {
// TODO: Common collections should be able to access via core functions instead of store
const conversation = await store.findOne("conversations", conversationId);
let bot = undefined;
if (conversation.botId != null) {
bot = await store.findOne("bots", conversation.botId);
}

const messageHistory = (await store.findMany("messages", { conversationId }, [{ createdAt: "asc" }])) ?? [];

let recentMessages = messageHistory
Expand All @@ -88,13 +87,19 @@ async function retrieveLastTenMessages(conversationId: string) {
},...recentMessages];
}

console.debug(`Sending: ${JSON.stringify(recentMessages)}`);
console.debug(`Last 10 messages: ${JSON.stringify(recentMessages, null, 2)}`);

return recentMessages;
}

async function handleMessageRequest(data: NewMessageRequest) {
const recentMessages = await retrieveLastTenMessages(data.conversationId);
const conversation = await store.findOne("conversations", data.conversationId);
let bot = undefined;
if (conversation.botId != null) {
bot = await store.findOne("bots", conversation.botId);
}

const recentMessages = await retrieveLastTenMessages(data.conversationId, bot);
const message = {
...data,
message: "",
Expand All @@ -108,7 +113,7 @@ async function handleMessageRequest(data: NewMessageRequest) {
message._id = id;
events.emit(EventName.OnNewMessageResponse, message);

requestInference(recentMessages).subscribe({
requestInference(recentMessages, bot).subscribe({
next: (content) => {
message.message = content;
events.emit(EventName.OnMessageResponseUpdate, message);
Expand Down
Loading

0 comments on commit 5a6d884

Please sign in to comment.