Make openai dependency optional (#236)

* Make openai optional dependency

* Set openai as an extra dependency
This commit is contained in:
Albert Villanova del Moral 2025-01-17 16:46:18 +01:00 committed by GitHub
parent 1f8fd72acb
commit 68933e7e90
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 5 deletions

View File

@ -23,7 +23,6 @@ dependencies = [
"duckduckgo-search>=6.3.7",
"python-dotenv>=1.0.1",
"e2b-code-interpreter>=1.0.3",
"openai>=1.58.1",
]
[project.optional-dependencies]
@ -37,12 +36,13 @@ torch = [
litellm = [
"litellm>=1.55.10",
]
openai = ["openai>=1.58.1"]
quality = [
"ruff>=0.9.0",
]
test = [
"pytest>=8.1.0",
"smolagents[audio,litellm,torch]",
"smolagents[audio,litellm,openai,torch]",
]
dev = [
"smolagents[quality,test]",

View File

@ -34,8 +34,6 @@ from transformers import (
)
from transformers.utils.import_utils import _is_package_available
import openai
from .tools import Tool
logger = logging.getLogger(__name__)
@ -578,6 +576,12 @@ class OpenAIServerModel(Model):
api_key: str,
**kwargs,
):
try:
import openai
except ModuleNotFoundError:
raise ModuleNotFoundError(
"Please install 'openai' extra to use OpenAIServerModel: `pip install 'smolagents[openai]'`"
) from None
super().__init__()
self.model_id = model_id
self.client = openai.OpenAI(