77
88class GeminiLLMBackend (LLMBackend ):
99 """Backend for Google Gemini LLM API.
10-
10+
1111 Gemini uses a different API format than OpenAI-compatible APIs:
1212 - Endpoint: /models/{model}:generateContent
1313 - Auth: x-goog-api-key header
@@ -22,36 +22,33 @@ def build_summary_payload(
2222 ) -> dict [str , Any ]:
2323 """Build payload for Gemini generateContent API."""
2424 contents : list [dict [str , Any ]] = []
25-
25+
2626 # Add user message
2727 contents .append ({
2828 "role" : "user" ,
2929 "parts" : [{"text" : text }]
3030 })
31-
31+
3232 payload : dict [str , Any ] = {
3333 "contents" : contents ,
3434 }
35-
35+
3636 # Add system instruction if provided
37+ # Note: When system_prompt is None, we don't set a default to allow the user prompt
38+ # to fully control the output format (e.g., for JSON responses)
3739 if system_prompt :
3840 payload ["system_instruction" ] = {
3941 "parts" : [{"text" : system_prompt }]
4042 }
41- else :
42- # Default system prompt for summarization
43- payload ["system_instruction" ] = {
44- "parts" : [{"text" : "Summarize the text in one short paragraph." }]
45- }
46-
43+
4744 # Add generation config
4845 generation_config : dict [str , Any ] = {
4946 "temperature" : 1.0 , # Gemini recommends keeping at 1.0
5047 }
5148 if max_tokens is not None :
5249 generation_config ["maxOutputTokens" ] = max_tokens
5350 payload ["generationConfig" ] = generation_config
54-
51+
5552 return payload
5653
5754 def parse_summary_response (self , data : dict [str , Any ]) -> str :
@@ -83,30 +80,30 @@ def build_vision_payload(
8380 }
8481 },
8582 ]
86-
83+
8784 contents : list [dict [str , Any ]] = [
8885 {
8986 "role" : "user" ,
9087 "parts" : user_parts ,
9188 }
9289 ]
93-
90+
9491 payload : dict [str , Any ] = {
9592 "contents" : contents ,
9693 }
97-
94+
9895 # Add system instruction if provided
9996 if system_prompt :
10097 payload ["system_instruction" ] = {
10198 "parts" : [{"text" : system_prompt }]
10299 }
103-
100+
104101 # Add generation config
105102 generation_config : dict [str , Any ] = {
106103 "temperature" : 1.0 ,
107104 }
108105 if max_tokens is not None :
109106 generation_config ["maxOutputTokens" ] = max_tokens
110107 payload ["generationConfig" ] = generation_config
111-
108+
112109 return payload
0 commit comments