Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added default llm name to the config #48

Merged
merged 3 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions llmclient/llms.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ class LiteLLMModel(LLMModel):

model_config = ConfigDict(extra="forbid")

name: str = "gpt-4o-mini"
name: str = CommonLLMNames.GPT_4O.value
config: dict = Field(
default_factory=dict,
description=(
Expand All @@ -468,10 +468,17 @@ class LiteLLMModel(LLMModel):
@model_validator(mode="before")
@classmethod
def maybe_set_config_attribute(cls, data: dict[str, Any]) -> dict[str, Any]:
"""If a user only gives a name, make a sensible config dict for them."""
"""
Set the config attribute if it is not provided.

If name is not provided, uses the default name.
If a user only gives a name, make a sensible config dict for them.
"""
if "config" not in data:
data["config"] = {}
if "name" in data and "model_list" not in data["config"]:
if "name" not in data:
data["name"] = data["config"].get("name", cls.model_fields["name"].default)
if "model_list" not in data["config"]:
data["config"] = {
"model_list": [
{
Expand Down
45 changes: 41 additions & 4 deletions tests/test_llms.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,50 @@


class TestLiteLLMModel:
def test_instantiation_methods(self) -> None:
# Test default instantiation
model1 = LiteLLMModel()
assert model1.name == CommonLLMNames.GPT_4O.value
assert isinstance(model1.config, dict)
assert "model_list" in model1.config

# Test name-only instantiation
name = CommonLLMNames.ANTHROPIC_TEST.value
model2 = LiteLLMModel(name=name)
assert model2.name == name
assert model2.config["model_list"][0]["model_name"] == name

# Test config-only instantiation
config = {
"name": CommonLLMNames.OPENAI_TEST.value,
"temperature": 0,
"max_tokens": 56,
}
model3 = LiteLLMModel(config=config)
assert model3.name == CommonLLMNames.OPENAI_TEST.value
assert (
model3.config["model_list"][0]["model_name"]
== CommonLLMNames.OPENAI_TEST.value
)
assert model3.config["model_list"][0]["litellm_params"]["temperature"] == 0
assert model3.config["model_list"][0]["litellm_params"]["max_tokens"] == 56

# Test name and config instantiation
name = CommonLLMNames.OPENAI_TEST.value
config = {"temperature": 0.5, "max_tokens": 100}
model4 = LiteLLMModel(name=name, config=config)
assert model4.name == name
assert model4.config["model_list"][0]["model_name"] == name
assert model4.config["model_list"][0]["litellm_params"]["temperature"] == 0.5
assert model4.config["model_list"][0]["litellm_params"]["max_tokens"] == 100

@pytest.mark.vcr(match_on=[*VCR_DEFAULT_MATCH_ON, "body"])
@pytest.mark.parametrize(
"config",
[
pytest.param(
{
"model_name": CommonLLMNames.OPENAI_TEST.value,
"name": CommonLLMNames.OPENAI_TEST.value,
"model_list": [
{
"model_name": CommonLLMNames.OPENAI_TEST.value,
Expand All @@ -44,7 +81,7 @@ class TestLiteLLMModel:
),
pytest.param(
{
"model_name": CommonLLMNames.ANTHROPIC_TEST.value,
"name": CommonLLMNames.ANTHROPIC_TEST.value,
"model_list": [
{
"model_name": CommonLLMNames.ANTHROPIC_TEST.value,
Expand All @@ -62,7 +99,7 @@ class TestLiteLLMModel:
)
@pytest.mark.asyncio
async def test_call(self, config: dict[str, Any]) -> None:
llm = LiteLLMModel(name=config["model_name"], config=config)
llm = LiteLLMModel(config=config)
messages = [
Message(role="system", content="Respond with single words."),
Message(role="user", content="What is the meaning of the universe?"),
Expand Down Expand Up @@ -140,7 +177,7 @@ async def ac(x) -> None:
[
pytest.param(
{
"model_name": CommonLLMNames.OPENAI_TEST.value,
"name": CommonLLMNames.OPENAI_TEST.value,
"model_list": [
{
"model_name": CommonLLMNames.OPENAI_TEST.value,
Expand Down