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
ModelProviderenum in state/model.ts - Add provider and models template to
modelTemplatesobject in state/model.tscredsandargsdefined in themodelTemplatesare accessible on backend inget_modelunder their exact names inconfig["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
iconPathsobject in components/icons/ProviderIcon.tsx
2. Add provider to backend (api-service/models/base.py)
- Add provider name to
ModelProviderenum - Add provider integration (implementing LangChain's
BaseLanguageModel) toget_modelfunction. 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.
