mirror of
https://gitee.com/wanwujie/deer-flow
synced 2026-04-03 06:12:14 +08:00
feat: add model modules
This commit is contained in:
3
backend/src/models/__init__.py
Normal file
3
backend/src/models/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from .factory import create_chat_model
|
||||
|
||||
__all__ = ["create_chat_model"]
|
||||
43
backend/src/models/factory.py
Normal file
43
backend/src/models/factory.py
Normal file
@@ -0,0 +1,43 @@
|
||||
from langchain.chat_models import BaseChatModel
|
||||
|
||||
from src.config import get_app_config
|
||||
from src.reflection import resolve_class
|
||||
|
||||
|
||||
def create_chat_model(
|
||||
name: str | None = None, thinking_enabled: bool = False, **kwargs
|
||||
) -> BaseChatModel:
|
||||
"""Create a chat model instance from the config.
|
||||
|
||||
Args:
|
||||
name: The name of the model to create. If None, the first model in the config will be used.
|
||||
|
||||
Returns:
|
||||
A chat model instance.
|
||||
"""
|
||||
config = get_app_config()
|
||||
if name is None:
|
||||
name = config.models[0].name
|
||||
model_config = config.get_model_config(name)
|
||||
if model_config is None:
|
||||
raise ValueError(f"Model {name} not found in config") from None
|
||||
model_class = resolve_class(model_config.use, BaseChatModel)
|
||||
model_settings_from_config = model_config.model_dump(
|
||||
exclude_none=True,
|
||||
exclude={
|
||||
"use",
|
||||
"name",
|
||||
"display_name",
|
||||
"description",
|
||||
"supports_thinking",
|
||||
"when_thinking_enabled",
|
||||
},
|
||||
)
|
||||
if thinking_enabled and model_config.when_thinking_enabled is not None:
|
||||
if not model_config.supports_thinking:
|
||||
raise ValueError(
|
||||
f"Model {name} does not support thinking. Set `supports_thinking` to true in the `config.yaml` to enable thinking."
|
||||
) from None
|
||||
model_settings_from_config.update(model_config.when_thinking_enabled)
|
||||
model_instance = model_class(**kwargs, **model_settings_from_config)
|
||||
return model_instance
|
||||
Reference in New Issue
Block a user