Skip to content

Commit 9617436

Browse files
authored
feat: allow to introduce any model for supported agents (#54)
1 parent 44e0bde commit 9617436

16 files changed

Lines changed: 151 additions & 36 deletions

File tree

eval/cli.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
),
2727
)
2828
@click.option("--prompt", type=str, help=("Which prompt to use."))
29+
@click.option("--model", type=str, default=None, help=("Which prompt to use."))
2930
@click.option(
3031
"--prompt-file",
3132
type=str,
@@ -55,6 +56,7 @@ def run(
5556
prompt_file: str,
5657
env_file: str,
5758
tag: str | None = None,
59+
model: str | None = None,
5860
) -> None:
5961
load_dotenv(env_file)
6062
config_from_file = EvalFileConfig.get_config_from_file(config_section=config, path=config_file)
@@ -65,6 +67,7 @@ def run(
6567
"prompt": prompt or config_from_file.prompt,
6668
"score_threshold": score or config_from_file.score_threshold,
6769
"samples": samples or config_from_file.samples,
70+
"model": model or config_from_file.model,
6871
}
6972
)
7073

@@ -76,6 +79,7 @@ def run(
7679
tag=tag,
7780
agent=eval_config.agent,
7881
prompt=configured_prompts.get_prompt(eval_config.prompt),
82+
model=eval_config.model,
7983
)
8084

8185

eval/evaluator.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22

3-
from lightman_ai.ai.utils import get_agent_instance_from_agent_name
3+
from lightman_ai.ai.utils import get_agent_class_from_agent_name
44
from lightman_ai.article.models import Article
55

66
from eval.classifier import Classifier
@@ -11,16 +11,19 @@
1111

1212

1313
def eval(
14+
*,
1415
prompt: str,
1516
score_threshold: int,
1617
relevant_articles: set[Article],
1718
non_relevant_articles: set[Article],
1819
samples: int,
1920
tag: str | None,
2021
agent: str,
22+
model: str | None,
2123
) -> None:
24+
agent_class = get_agent_class_from_agent_name(agent)
2225
classified_articles = Classifier(
23-
agent=get_agent_instance_from_agent_name(agent)(prompt),
26+
agent=agent_class(prompt, model=model),
2427
score=score_threshold,
2528
relevant_articles=relevant_articles,
2629
non_relevant_articles=non_relevant_articles,
@@ -36,6 +39,7 @@ def eval(
3639
prompt=prompt,
3740
score=score_threshold,
3841
logger=logger,
42+
model=model or agent_class._default_model_name,
3943
)
4044

4145
results_template.save()

eval/templates.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def __init__(
1717
samples: int,
1818
prompt: str,
1919
score: int,
20+
model: str,
2021
logger: logging.Logger | None = None,
2122
) -> None:
2223
self.results_metrics = results_metrics
@@ -26,6 +27,7 @@ def __init__(
2627
self.prompt = prompt
2728
self.score = score
2829
self.results_dir = Path(RESULTS_DIR)
30+
self.model = model
2931
self.logger = logger or logging.getLogger("eval")
3032

3133
@property
@@ -55,6 +57,7 @@ def _get_summary(self) -> str:
5557
# Summary
5658
- Tag: {self.tag or "-"}
5759
- Agent: {self.agent}
60+
- Model: {self.model}
5861
- Samples: {self.samples}
5962
- Score threshold: {self.score}
6063
- Prompt: \n {self.prompt}

justfile

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# VARIABLE DEFINITIONS
22
venv := ".venv"
33
python_version :="3.13"
4+
run := "poetry run"
5+
eval_path := "eval/cli.py"
46

57
venv-exists := path_exists(venv)
68

79

8-
target_dirs := "src tests"
10+
target_dirs := "src tests eval"
911

1012
# ALIASES
1113
alias t := test
@@ -19,6 +21,9 @@ venv:
1921
uv sync --frozen --all-extras; \
2022
fi
2123

24+
# Runs the evaluation script
25+
eval *args: venv
26+
PYTHONPATH=. {{ run }} python {{ eval_path }} {{ args }}
2227

2328
# Cleans all artifacts generated while running this project, including the virtualenv.
2429
clean:
@@ -27,17 +32,17 @@ clean:
2732

2833
# Runs the tests with the specified arguments (any path or pytest argument).
2934
test *test-args='': venv
30-
uv run pytest {{ test-args }} --no-cov
35+
{{ run }} pytest {{ test-args }} --no-cov
3136

3237
# Runs all tests including coverage report.
3338
test-all: venv
34-
uv run pytest
39+
{{ run }} pytest
3540

3641
# Format all code in the project.
3742
format: venv
38-
uv run ruff check {{ target_dirs }} --fix
43+
{{ run }} ruff check {{ target_dirs }}
3944

4045
# Lint all code in the project.
4146
lint: venv
42-
uv run ruff check {{ target_dirs }}
43-
uv run mypy {{ target_dirs }}
47+
{{ run }} ruff check {{ target_dirs }}
48+
{{ run }} mypy {{ target_dirs }}

lightman.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
agent = 'openai'
33
score_threshold = 8
44
prompt = 'classify'
5-
65
service_desk_project_key=''
76
service_desk_request_id_type=''
87

@@ -11,6 +10,7 @@ agent = 'openai'
1110
score_threshold = 8
1211
prompt = 'classify'
1312
samples = 3
13+
model = 'gpt-4o'
1414

1515
[prompts]
1616
classify = ""

src/lightman_ai/ai/base/agent.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99

1010

1111
class BaseAgent(ABC):
12-
_model: type[OpenAIModel] | type[GoogleModel]
13-
_model_name: str
12+
_class: type[OpenAIModel] | type[GoogleModel]
13+
_default_model_name: str
1414

15-
def __init__(self, system_prompt: str, logger: logging.Logger | None = None) -> None:
16-
model = self._model(self._model_name)
15+
def __init__(self, system_prompt: str, model: str | None = None, logger: logging.Logger | None = None) -> None:
16+
agent_model = self._class(model or self._default_model_name)
1717
self.agent: Agent[Never, SelectedArticlesList] = Agent(
18-
model=model, output_type=SelectedArticlesList, system_prompt=system_prompt
18+
model=agent_model, output_type=SelectedArticlesList, system_prompt=system_prompt
1919
)
2020
self.logger = logger or logging.getLogger("lightman")
2121

src/lightman_ai/ai/gemini/agent.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
class GeminiAgent(BaseAgent):
1010
"""Class that provides an interface to operate with the Gemini model."""
1111

12-
_model = GoogleModel
13-
_model_name = "gemini-2.5-pro-preview-05-06"
12+
_class = GoogleModel
13+
_default_model_name = "gemini-2.5-pro-preview-05-06"
1414

1515
@override
1616
def _run_prompt(self, prompt: str) -> SelectedArticlesList:

src/lightman_ai/ai/openai/agent.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
class OpenAIAgent(BaseAgent):
1212
"""Class that provides an interface to operate with the OpenAI model."""
1313

14-
_model = OpenAIModel
15-
_model_name = "gpt-4.1"
14+
_class = OpenAIModel
15+
_default_model_name = "gpt-4.1"
1616

1717
def _execute_agent(self, prompt: str) -> AgentRunResult[SelectedArticlesList]:
1818
with map_openai_exceptions():

src/lightman_ai/ai/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
AGENT_MAPPING = {"openai": OpenAIAgent, "gemini": GeminiAgent}
66

77

8-
def get_agent_instance_from_agent_name(agent: str) -> type[BaseAgent]:
8+
def get_agent_class_from_agent_name(agent: str) -> type[BaseAgent]:
99
if agent not in AGENT_MAPPING:
1010
raise ValueError(f"Agent '{agent}' is not recognized. Available agents: {list(AGENT_MAPPING.keys())}")
1111
return AGENT_MAPPING[agent]

src/lightman_ai/cli.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def entry_point() -> None:
2525
help=(f"Location of the config file containing the prompts. Defaults to `{DEFAULT_CONFIG_FILE}`."),
2626
)
2727
@click.option("--prompt", type=str, help=("Which prompt to use"))
28+
@click.option("--model", type=str, default=None, help=("Which model to use. Must be set in conjunction with --agent."))
2829
@click.option(
2930
"--score",
3031
type=int,
@@ -60,6 +61,7 @@ def run(
6061
agent: str,
6162
prompt: str,
6263
prompt_file: str,
64+
model: str | None,
6365
score: int | None,
6466
config_file: str,
6567
config: str,
@@ -80,6 +82,7 @@ def run(
8082
"agent": agent or config_from_file.agent,
8183
"prompt": prompt or config_from_file.prompt,
8284
"score_threshold": score or config_from_file.score_threshold,
85+
"model": model or config_from_file.model,
8386
}
8487
)
8588

@@ -94,6 +97,7 @@ def run(
9497
dry_run=dry_run,
9598
project_key=config_from_file.service_desk_project_key,
9699
request_id_type=config_from_file.service_desk_request_id_type,
100+
model=final_config.model,
97101
)
98102
relevant_articles_metadata = [f"{article.title} ({article.link})" for article in relevant_articles]
99103
logger.warning("Found these articles: \n- %s", "\n- ".join(relevant_articles_metadata))

0 commit comments

Comments
 (0)