Files
e2b-dev--e2b/CONTRIBUTING.md
T
Tomas Valenta 2764922f53 Update docs
2023-04-16 15:26:49 +00:00

4.0 KiB

Contributing

If you want to contribute, open a PR, issue, or start a discussion on our Discord.

🤖 Adding a new model provider

If you want to add a new model provider (like OpenAI or HuggingFace) complete the following steps and create a PR.

When you add a provider you can also add a specific model (like OpenAI's GPT-4) under that provider.

Here is an example code for adding a new provider.

1. Add the provider to frontend

  • Add provider name to ModelProvider enum in state/model.ts
  • Add provider and models template to modelTemplates object in state/model.ts
    • creds and args defined in the modelTemplates are accessible on backend in get_model under their exact names in config["args"] object.
  • Add provider's PNG icon image to public/ in a resolution that is bigger than 30x30 px.
  • Add provider's icon path to iconPaths object in components/icons/ProviderIcon.tsx

2. Add provider to backend (api-service/models/base.py)

  • Add provider name to ModelProvider enum
  • Add provider integration (implementing LangChain's BaseLanguageModel) to get_model function. You can use an existing integration from LangChain or create a new integration from scratch.

The new provider integrations should be placed in api-service/models/providers/.

Provider integrations

We use LangChain under the hood, so if you are adding a new integration you have to implement the BaseLanguageModel class. That means implementing the _acall async method that calls the model with a prompt and returns the output and also calling self.callback_manager.on_llm_new_token from inside the _acall method to diggest the output.

Using LangChain integration

You can often use existing LangChain integrations to add new model providers to e2b with just a few modifications.

Here is an example of modified Replicate integration. We had to add _acall method to support async execution and override validate_environment to prevent checking if the Replicate API key env var is set up because we pass the env var via a normal parameter.

If you are modifying existing LangChain integration add it to api-service/models/providers/<provider>.py.

From scratch

You can follow the langchain's guide to implement the LLM class (it inherits from BaseLanguageModel).

Here is an example of the implementation:

from typing import List, Optional
from langchain.llms.base import LLM

class NewModelProviderWithStreaming(LLM):
    temperature: str
    new_provider_api_token: str

    # You only need to implement the `_acall` method
    async def _acall(self, prompt: str, stop: Optional[List[str]] = None) -> str:
        # Call the model and get outputs
        # You can use `temperature` and `new_provider_api_token` args
        text = ""
        for token in outputs:
            text += token
            if self.callback_manager.is_async:
                await self.callback_manager.on_llm_new_token(
                    token,
                    verbose=self.verbose,
                    # We explicitly flush the logs in log queue because the calls to this model are not actually async so they block.
                    flush=True,
                )
            else:
                self.callback_manager.on_llm_new_token(
                    token,
                    verbose=self.verbose,
                )
        return text

3. Test

Test if the provider works by starting the app, selecting the provider and model in the "Model" sidebar menu and trying to "Run" it.

Then add a screenshot of agent's steps to the PR.