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