1212
1313
1414HEADER = Dict [str , str ]
15- GOOGLE_API_KEY = os .getenv ('GOOGLE_API_KEY' )
1615
1716
1817logging .basicConfig (level = logging .INFO )
2120RESOURCE_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
3833def 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(
9287def 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
0 commit comments