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

Make api_key param optional in LLMEvaluator #8340

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
11 changes: 7 additions & 4 deletions haystack/components/evaluators/llm_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def __init__(
*,
raise_on_failure: bool = True,
api: str = "openai",
api_key: Secret = Secret.from_env_var("OPENAI_API_KEY"),
api_key: Optional[Secret] = None,
api_params: Optional[Dict[str, Any]] = None,
):
"""
Expand All @@ -84,7 +84,7 @@ def __init__(
The API to use for calling an LLM through a Generator.
Supported APIs: "openai".
:param api_key:
The API key.
The API key. Optional to support if using a locally hosted model.
davidsbatista marked this conversation as resolved.
Show resolved Hide resolved
:param api_params:
Parameters for an OpenAI API compatible completions call.

Expand All @@ -106,7 +106,10 @@ def __init__(
self.api_params["generation_kwargs"] = merged_generation_kwargs

if api == "openai":
self.generator = OpenAIGenerator(api_key=api_key, **self.api_params)
generator_kwargs = {**self.api_params}
if api_key:
generator_kwargs["api_key"] = api_key
self.generator = OpenAIGenerator(**generator_kwargs)
else:
raise ValueError(f"Unsupported API: {api}")

Expand Down Expand Up @@ -288,7 +291,7 @@ def to_dict(self) -> Dict[str, Any]:
outputs=self.outputs,
examples=self.examples,
api=self.api,
api_key=self.api_key.to_dict(),
api_key=self.api_key.to_dict() if self.api_key is not None else None,
srini047 marked this conversation as resolved.
Show resolved Hide resolved
api_params=self.api_params,
progress_bar=self.progress_bar,
)
Expand Down
1 change: 1 addition & 0 deletions test/components/evaluators/test_llm_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ def test_to_dict_default(self, monkeypatch):
component = LLMEvaluator(
instructions="test-instruction",
inputs=[("predicted_answers", List[str])],
api_key=Secret.from_env_var("OPENAI_API_KEY"),
outputs=["score"],
examples=[
{"inputs": {"predicted_answers": "Football is the most popular sport."}, "outputs": {"score": 0}}
Expand Down