justyns / silverbullet-ai

Plug for SilverBullet to integrate LLM functionality
https://ai.silverbullet.md/
GNU Affero General Public License v3.0
26 stars 1 forks source link

Custom message context enrichment functions #15

Closed justyns closed 6 months ago

justyns commented 6 months ago

Similar to #14 but also separate. It'd be nice to allow users to provide their own hook for changing the chat messages that get sent to the llm apis.

For example, a user could create something like: "regex to detect github issue url -> download github issue and summarize it -> add as context to chat message"

Another use case would be to implement #14 itself

zefhemel commented 6 months ago

Not this is the best way, but there is a system.invokeSpaceFunction syscall now that you could leverage to allow people to write custom JS for this.

justyns commented 6 months ago

One method I was experimenting with was sending an event:

silverbullet.registerEventListener({name: "ai:enrichMessage"}, async (event) => {
  console.log("task event:", event, event.data);
  let enrichedContent = event.data.enrichedContent; 
  enrichedContent = `${enrichedContent} BLOOP`;
  return {enrichedContent};
});

This actually works pretty well, but only for a single listener.

justyns commented 6 months ago

Another attempt, this one works with multiple functions:

silverbullet.registerFunction('addBloop', async (message) => {
  return `${message} BLOOP`;
});

silverbullet.registerFunction('addBlarp', async (message) => {
  return `${message} BLARP`;
});

silverbullet.registerEventListener({name: "ai:enrichMessage"}, async (event) => {
  return 'addBloop';
});

silverbullet.registerEventListener({name: "ai:enrichMessage"}, async (event) => {
  return 'addBlarp';
});

I'm not sure if I like having to define both an event listener and a function, but it does work. Maybe an alternative would be a new silverbullet.registerPlugHook type of syntax?

justyns commented 6 months ago

I went with the syntax in my last comment, and also added a way to configure them in SETTINGS too to avoid having an eventlistener https://github.com/justyns/silverbullet-ai/pull/26