Skip to content

Commit ad8292e

Browse files
committed
add api-key as an input argument
replace os.getenv() with os.environ[] (need all of these) fix api-key type : str set INPUT_API-KEY in test
1 parent ac06739 commit ad8292e

8 files changed

Lines changed: 25 additions & 32 deletions

File tree

.github/workflows/build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ jobs:
3636
with:
3737
report-files: tests/sample_report.json,tests/json_dict_div_zero_try_except.json
3838
student-files: tests/sample_code.py
39+
api-key: ${{ secrets.GOOGLE_API_KEY }}
3940
readme-path: tests/sample_readme.md
40-
env:
41-
GOOGLE_API_KEY: ${{ secrets.GOOGLE_API_KEY }}
41+
timeout-minutes: 5
4242

4343
- name: Output the outputs of the integration test of the action
4444
run: |

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ Provide AI-powered feedback on Python code assignments using Google's Gemini lan
2020
with:
2121
# JSON files by pytest-json-report plugin
2222
report-files: report0.json, report1.json
23+
# API key for AI
24+
api-key: ${{ secrets.GOOGLE_API_KEY }}
2325
# python file including the student's code
2426
student-files: exercies.py
2527
# assignment instruction file
2628
readme-path: README.md
27-
env:
28-
GOOGLE_API_KEY: ${{ secrets.GOOGLE_API_KEY }}
2929
timeout-minutes: 5
3030
```

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ inputs:
1111
required: true
1212
default: 'report.json'
1313
type: list
14+
api-key:
15+
description: 'API token for AI'
16+
required: true
17+
type: string
1418
student-files:
1519
description: "Comma-separated list of student's Python file paths or a glob pattern"
1620
required: false

ai_tutor.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313

1414
HEADER = Dict[str, str]
15-
GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')
1615

1716

1817
logging.basicConfig(level=logging.INFO)
@@ -21,13 +20,9 @@
2120
RESOURCE_EXHAUSTED = 429
2221

2322

24-
def test_API_key():
25-
assert GOOGLE_API_KEY, 'API KEY NOT Available'
26-
27-
2823
@functools.lru_cache
29-
def url() -> str:
30-
return f'https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent?key={GOOGLE_API_KEY}'
24+
def url(api_key:str) -> str:
25+
return f'https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent?key={api_key}'
3126

3227

3328
@functools.lru_cache
@@ -37,7 +32,7 @@ def header() -> HEADER:
3732

3833
def ask_gemini(
3934
question: str,
40-
url:str=url(),
35+
api_key:str,
4136
header:HEADER=header(),
4237
retry_delay_sec: float = 5.0,
4338
max_retry_attempt: int = 3,
@@ -67,7 +62,7 @@ def ask_gemini(
6762
logging.error(f"Timeout exceeded for question: {question}")
6863
break # Exit the loop on timeout
6964

70-
response = requests.post(url, headers=header, json=data)
65+
response = requests.post(url(api_key), headers=header, json=data)
7166

7267
if response.status_code == 200:
7368
result = response.json()
@@ -92,7 +87,8 @@ def ask_gemini(
9287
def gemini_qna(
9388
report_paths:List[pathlib.Path],
9489
student_files:List[pathlib.Path],
95-
readme_file:pathlib.Path
90+
readme_file:pathlib.Path,
91+
api_key:str,
9692
) -> str:
9793
'''
9894
Queries the Gemini API to provide explanations for failed pytest test cases.
@@ -133,7 +129,7 @@ def gemini_qna(
133129
"\n\n".join(questions)
134130
+ get_code_instruction(student_files, readme_file)
135131
) # Add code & instruction only once
136-
answers = ask_gemini(consolidated_question)
132+
answers = ask_gemini(consolidated_question, api_key)
137133

138134
return answers
139135

entrypoint.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@
1919

2020

2121
def main() -> None:
22-
# Check if the API key is available
23-
ai_tutor.test_API_key()
24-
25-
report_files_str = os.getenv('INPUT_REPORT-FILES')
22+
report_files_str = os.environ['INPUT_REPORT-FILES']
2623
report_files = get_path_tuple(report_files_str)
2724

28-
student_files_str = os.getenv('INPUT_STUDENT-FILES')
25+
student_files_str = os.environ['INPUT_STUDENT-FILES']
2926
student_files = get_path_tuple(student_files_str)
3027

31-
readme_file_str = os.getenv('INPUT_README-PATH')
28+
readme_file_str = os.environ['INPUT_README-PATH']
3229
readme_file = pathlib.Path(readme_file_str)
3330
assert readme_file.exists(), 'No README file'
3431

35-
feedback = ai_tutor.gemini_qna(report_files, student_files, readme_file)
32+
assert 'INPUT_API-KEY' in os.environ, os.environ.keys()
33+
api_key = os.environ['INPUT_API-KEY']
34+
35+
feedback = ai_tutor.gemini_qna(report_files, student_files, readme_file, api_key)
3636

3737
print(feedback)
3838

tests/test_ai_tutor.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818
)
1919

2020

21-
# fake API key
22-
os.environ['GOOGLE_API_KEY'] = 'test_key'
23-
2421
import ai_tutor
2522

2623

tests/test_entrypoint.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
)
1616

1717

18-
# fake API key
19-
os.environ['GOOGLE_API_KEY'] = 'test_key'
20-
2118
import entrypoint
2219

2320

tests/test_integration.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,14 @@
1111
str(pathlib.Path(__file__).parent.parent.resolve())
1212
)
1313

14-
# fake API key
15-
os.environ['GOOGLE_API_KEY'] = 'test_key'
1614

1715
import entrypoint
1816

1917

2018
@unittest.mock.patch('ai_tutor.gemini_qna')
2119
def test_main_argument_passing__all_exists(mock_gemini_qna, caplog, tmp_path) -> None:
2220
# Setup
23-
os.environ['GOOGLE_API_KEY'] = 'test_key'
21+
os.environ['INPUT_API-KEY'] = 'test_key'
2422

2523
os.environ['GITHUB_OUTPUT'] = str(tmp_path / 'output.txt')
2624

@@ -49,7 +47,8 @@ def test_main_argument_passing__all_exists(mock_gemini_qna, caplog, tmp_path) ->
4947
mock_gemini_qna.assert_called_once_with(
5048
(tmp_path / 'file1.txt', tmp_path / 'file2.txt', tmp_path / 'file3.txt'),
5149
(tmp_path / 'file4.txt', tmp_path / 'file5.txt', tmp_path / 'file6.txt'),
52-
tmp_path / 'readme.txt'
50+
tmp_path / 'readme.txt',
51+
'test_key',
5352
)
5453

5554
assert 'does not exist' not in caplog.text

0 commit comments

Comments
 (0)