Use multiple providers

Warning

This documentation covers an older API of Moly Kit. The documentation will be reworked. For reference, commit 1eb9630 is the last commit where this documentation was valid.

Prerequisites

This guide assumes you already read the Quickstart.

Mixing clients

As seen before, a BotContext is created from one (and only one) BotClient.

If we want out app to be able to use multiple clients configured in different ways at the same time, we will need to compose them into one.

Fortunately, Moly Kit comes with a built-in client called MultiClient, which does exactly that. MultiClient can take several "sub-clients" but acts as a single one to BotContext, routing requests to them accordengly.

Going back to our configuration from the Quickstart, we can update it to work with several clients at the same time:

use moly_kit::*;

impl LiveHook for YourAmazingWidget {
    fn after_new_from_doc(&mut self, _cx: &mut Cx) {
        let client = {
          let mut client = MultiClient::new();

          let mut openai = OpenAiClient::new("https://api.openai.com/v1".into());
          openai.set_key("<YOUR_KEY>".into());
          client.add_client(openai);

          let mut openrouter = OpenAiClient::new("https://openrouter.ai/api/v1".into());
          openrouter.set_key("<YOUR_KEY>".into());
          client.add_client(openrouter);

          let ollama = OpenAiClient::new("http://localhost:11434/v1".into());
          client.add_client(ollama);

          client
        };

        let context = BotContext::from(client);

        let mut chat = self.chat(id!(chat));
        chat.write().set_bot_context(context);
    }
}