Skip to content

Commit c7be2c0

Browse files
authored
Merge pull request #11 from jpodivin/ld_auth
Authentication improvement
2 parents b739037 + 60f5d4a commit c7be2c0

File tree

3 files changed

+47
-19
lines changed

3 files changed

+47
-19
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Ignore potential API keys in file form
2+
.*API*

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,14 @@ Evaluation of Log Detective performance can be performed automatically using
5151
the `validation.py`script. Dependencies for the tool are defined in the
5252
`requirements.txt` file and should be installed in a virtual environment.
5353

54-
Before running the script, the API key for the LLM judge must be set
55-
in an environment variable `OPENAI_API_KEY`.
54+
Keys for OpenAI compatible LLM inference provider and Log Detective itself,
55+
must be stored in files. These will be used at runtime.
56+
This prevents logging of secrets in history.
5657

5758
Example:
5859

5960
```
60-
./validation.py <DATA_PATH> <LOG_DETECTIVE_URL> <LLM_URL> <LLM_NAME>
61+
./validation.py <PATH_TO_OPEN_AI_API_KEY> <DATA_PATH> <LOG_DETECTIVE_URL> <LLM_URL> <LLM_NAME>
6162
```
6263
Script sends each of the the stored log files for evaluation by Log Detective,
6364
then submits both results of final analysis from Log Detective and actual issue

validation.py

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,20 @@
1010
import argparse
1111
from pydantic import BaseModel, Field, ValidationError
1212

13-
# --- Configuration ---
14-
# Set your OpenAI API key as an environment variable named OPENAI_API_KEY
15-
# You can get a key from https://beta.openai.com/account/api-keys
16-
API_KEY = os.getenv("OPENAI_API_KEY")
1713
LOG_REPO_BASE_URL = (
1814
"https://raw.githubusercontent.com/fedora-copr/logdetective-sample/main/data/"
1915
)
2016

2117

18+
def get_api_key_from_file(path: str):
19+
"""Attempt to read API key from a file.
20+
This is safer than typing it in CLI."""
21+
22+
with open(path) as key_file:
23+
24+
return key_file.read().strip()
25+
26+
2227
class SimilarityScore(BaseModel):
2328
"""Defines the structure for the similarity score response from the LLM."""
2429

@@ -94,6 +99,7 @@ def evaluate_samples(
9499
llm_model: str,
95100
llm_token: str,
96101
log_detective_api_timeout: int,
102+
log_detective_api_key: str = "",
97103
) -> None:
98104
"""
99105
Traverses a directory to find and evaluate log analysis samples.
@@ -106,6 +112,10 @@ def evaluate_samples(
106112

107113
full_api_url = f"{server_address}{api_endpoint}"
108114

115+
log_detective_request_headers = {}
116+
if log_detective_api_key:
117+
log_detective_request_headers["Authorization"] = f"Bearer {log_detective_api_key}"
118+
109119
client = openai.OpenAI(base_url=llm_url, api_key=llm_token)
110120
scores = []
111121
elapsed_times = []
@@ -146,8 +156,8 @@ def evaluate_samples(
146156
)
147157
start_time = time.time()
148158
api_response = requests.post(
149-
full_api_url, json=payload, timeout=log_detective_api_timeout
150-
)
159+
full_api_url, json=payload, timeout=log_detective_api_timeout,
160+
headers=log_detective_request_headers)
151161
api_response.raise_for_status()
152162
actual_response_data = api_response.json()
153163
time_elapsed = time.time() - start_time
@@ -206,6 +216,12 @@ def main():
206216
"""
207217
parser = argparse.ArgumentParser(
208218
description="Evaluate AI system performance by comparing expected and actual responses.",
219+
formatter_class=argparse.ArgumentDefaultsHelpFormatter
220+
)
221+
parser.add_argument(
222+
"open_ai_api_key",
223+
help="Path to file with API key to OpenAI compatible inference provider",
224+
type=str,
209225
)
210226
parser.add_argument(
211227
"data_directory", help="Path to the directory containing the sample data."
@@ -217,28 +233,37 @@ def main():
217233
parser.add_argument("llm_url", help="URL of LLM API to use as judge")
218234
parser.add_argument("llm_model", help="Name of LLM model to use a judge")
219235
parser.add_argument(
220-
"log_detective_api_timeout",
236+
"--log-detective-api-timeout",
221237
help="Request timeout for Log Detective API",
222238
type=int,
223239
default=60,
224240
)
241+
parser.add_argument(
242+
"--log-detective-api-key",
243+
help="Path to file with Log Detective API key, if one is necessary",
244+
type=str,
245+
default=""
246+
)
225247
args = parser.parse_args()
226248

227-
if not API_KEY:
228-
print("Error: OPENAI_API_KEY environment variable not set.", file=sys.stderr)
229-
sys.exit(1)
249+
open_ai_api_key = get_api_key_from_file(args.open_ai_api_key)
230250

231251
if not os.path.isdir(args.data_directory):
232252
print(f"Error: Directory not found at '{args.data_directory}'", file=sys.stderr)
233253
sys.exit(1)
234254

255+
log_detective_api_key = ""
256+
if args.log_detective_api_key:
257+
log_detective_api_key = get_api_key_from_file(args.log_detective_api_key)
258+
235259
evaluate_samples(
236-
args.data_directory,
237-
args.logdetective_url,
238-
args.llm_url,
239-
args.llm_model,
240-
API_KEY,
241-
args.log_detective_api_timeout,
260+
directory=args.data_directory,
261+
server_address=args.logdetective_url,
262+
llm_url=args.llm_url,
263+
llm_model=args.llm_model,
264+
llm_token=open_ai_api_key,
265+
log_detective_api_timeout=args.log_detective_api_timeout,
266+
log_detective_api_key=log_detective_api_key
242267
)
243268

244269

0 commit comments

Comments
 (0)