MCPcopy Create free account
hub / github.com/Roy3838/Observer / handle_request

Method handle_request

api/gemini_pro_handler.py:65–223  ·  view source on GitHub ↗

Process a /v1/chat/completions request asynchronously for Gemini Pro models. Returns either dict (non-streaming) or StreamingResponse (streaming).

(self, request_data: dict)

Source from the content-addressed store, hash-verified

63 logger.info("GeminiProAPIHandler registered models: %s", [m["name"] for m in self.models])
64
65 async def handle_request(self, request_data: dict):
66 """
67 Process a /v1/chat/completions request asynchronously for Gemini Pro models.
68 Returns either dict (non-streaming) or StreamingResponse (streaming).
69 """
70 if not self.api_key:
71 raise ConfigError("GEMINI_PRO_API_KEY is not configured on the server.")
72
73 # --- Request Data Validation and Processing ---
74 model_name = request_data.get("model")
75 if not model_name:
76 raise ValueError("Request data must include a 'model' field.")
77
78 # Map display name to actual Gemini model ID
79 if model_name in self.model_map:
80 target_model = self.model_map[model_name]["model_id"]
81 else:
82 target_model = model_name # Fallback to direct name
83
84 messages = request_data.get("messages", [])
85 if not messages:
86 raise ValueError("Request body must contain a 'messages' array")
87
88 # Check for streaming
89 if request_data.get("stream", False):
90 return StreamingResponse(
91 self._stream_gemini_response(request_data, target_model),
92 media_type="text/event-stream"
93 )
94
95 # --- Convert OpenAI format messages to Gemini format ---
96 system_instruction, contents = self._convert_messages_to_gemini_format(messages)
97
98 if not contents:
99 raise ValueError("No valid content found to send to Gemini.")
100
101 # --- Prepare Gemini API Call ---
102 gemini_url = f"https://generativelanguage.googleapis.com/v1beta/models/{target_model}:generateContent"
103
104 # Build payload with converted contents
105 payload = {"contents": contents}
106
107 # Add system_instruction if present
108 if system_instruction:
109 payload["system_instruction"] = system_instruction
110
111 # Add generationConfig if needed from request_data (temperature, max_tokens etc.)
112 generation_config = {}
113 if "temperature" in request_data: generation_config["temperature"] = request_data["temperature"]
114 if "max_tokens" in request_data: generation_config["maxOutputTokens"] = request_data["max_tokens"]
115 if generation_config: payload["generationConfig"] = generation_config
116
117 headers = {
118 "Content-Type": "application/json",
119 "x-goog-api-key": self.api_key,
120 "User-Agent": "ObserverAI-FastAPI-Client/1.0"
121 }
122

Callers

nothing calls this directly

Calls 8

ConfigErrorClass · 0.90
get_http_clientFunction · 0.90
BackendAPIErrorClass · 0.90
HandlerErrorClass · 0.90
infoMethod · 0.80
errorMethod · 0.80

Tested by

no test coverage detected