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

Method handle_request

api/fireworks_handler.py:78–186  ·  view source on GitHub ↗

Process a /v1/chat/completions request asynchronously via Fireworks AI. Translates display model name to actual Fireworks model ID. Returns either dict (non-streaming) or StreamingResponse (streaming).

(self, request_data: dict)

Source from the content-addressed store, hash-verified

76 }
77
78 async def handle_request(self, request_data: dict):
79 """
80 Process a /v1/chat/completions request asynchronously via Fireworks AI.
81 Translates display model name to actual Fireworks model ID.
82 Returns either dict (non-streaming) or StreamingResponse (streaming).
83 """
84 if not self.api_key:
85 raise ConfigError("FIREWORKS_API_KEY is not configured on the server.")
86
87 # --- Get Display Name and Translate to Actual Model ID ---
88 display_model_name = request_data.get("model")
89 if not display_model_name:
90 raise ValueError("Request data must include a 'model' field (using the display name).")
91
92 # Look up the display name in our map
93 model_info = self.model_map.get(display_model_name)
94 if not model_info:
95 # If the display name isn't found, the model is unsupported by this mapping
96 logger.warning(f"Received request for unmapped Fireworks model display name: {display_model_name}")
97 raise ValueError(f"Model display name '{display_model_name}' is not recognized or supported.")
98
99 actual_model_id = model_info.get("model_id")
100 if not actual_model_id:
101 # Should not happen if map is defined correctly, but good practice to check
102 logger.error(f"Internal configuration error: Missing 'model_id' for display name '{display_model_name}' in model_map.")
103 raise ConfigError(f"Internal mapping error for model '{display_model_name}'.")
104 # --- End Translation ---
105
106 # --- Prepare API Call ---
107 # Create a copy of the request data to modify
108 payload = request_data.copy()
109 # Set the 'model' in the payload to the ACTUAL Fireworks ID
110 payload["model"] = actual_model_id
111
112 # Default values from the curl example
113 if "max_tokens" not in payload:
114 payload["max_tokens"] = 16384
115 if "top_p" not in payload:
116 payload["top_p"] = 1
117 if "top_k" not in payload:
118 payload["top_k"] = 40
119 if "presence_penalty" not in payload:
120 payload["presence_penalty"] = 0
121 if "frequency_penalty" not in payload:
122 payload["frequency_penalty"] = 0
123 if "temperature" not in payload:
124 payload["temperature"] = 0.6
125
126 # Update headers (in case API key was missing during init)
127 headers = self.base_headers.copy()
128 headers["Authorization"] = f"Bearer {self.api_key}"
129
130 logger.info(f"Calling Fireworks API: display_model='{display_model_name}', actual_model='{actual_model_id}', streaming={payload.get('stream', False)}")
131
132 # --- Check for streaming ---
133 if payload.get("stream", False):
134 return StreamingResponse(
135 self._stream_fireworks_response(headers, payload, display_model_name, actual_model_id),

Callers

nothing calls this directly

Calls 7

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

Tested by

no test coverage detected