-
Notifications
You must be signed in to change notification settings - Fork 81
Feat: OpenAI TITIO support by middleware #414
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @guapisolo, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the Miles router by adding native support for OpenAI API-compatible chat completion requests. It introduces a new middleware layer and associated utility functions to seamlessly translate between OpenAI's request/response formats and the router's internal processing, alongside an example script to validate this new capability. The changes aim to broaden the router's interoperability and ease of use for developers accustomed to the OpenAI API standard. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request adds support for an OpenAI-compatible API endpoint to the miles router. The changes include a new OpenAICompatMiddleware to handle /v1/chat/completions requests, utility functions for converting between OpenAI and internal formats, and an example usage script. My review has identified a few critical issues. There's a bug in openai_generate.py that duplicates response content, and another in OpenAICompatMiddleware that could lead to an UnboundLocalError. I've also pointed out several high and medium severity issues related to unsafe data access, potential bugs in logic, and maintainability improvements such as avoiding broad exception catches and removing redundant code. Please address the critical issues before merging.
| try: | ||
| response = await self.router.client.post(url, json=generate_payload) | ||
| response.raise_for_status() | ||
| output = response.json() | ||
| finally: | ||
| self.router._finish_url(worker_url) | ||
|
|
||
| model = chat_request.model or getattr(self.router.args, "hf_checkpoint", "default") | ||
| prompt_tokens = len(generate_payload.get("input_ids") or []) | ||
| return JSONResponse(content=build_chat_response(model, output, prompt_tokens)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a potential UnboundLocalError here. If an exception is raised within the try block (e.g., from client.post or response.raise_for_status), the output variable will not be assigned. The finally block will execute, and then the code will attempt to use output on line 65, which will crash. The logic that depends on output should be moved inside the try block.
| try: | |
| response = await self.router.client.post(url, json=generate_payload) | |
| response.raise_for_status() | |
| output = response.json() | |
| finally: | |
| self.router._finish_url(worker_url) | |
| model = chat_request.model or getattr(self.router.args, "hf_checkpoint", "default") | |
| prompt_tokens = len(generate_payload.get("input_ids") or []) | |
| return JSONResponse(content=build_chat_response(model, output, prompt_tokens)) | |
| try: | |
| response = await self.router.client.post(url, json=generate_payload) | |
| response.raise_for_status() | |
| output = response.json() | |
| model = chat_request.model or getattr(self.router.args, "hf_checkpoint", "default") | |
| prompt_tokens = len(generate_payload.get("input_ids") or []) | |
| return JSONResponse(content=build_chat_response(model, output, prompt_tokens)) | |
| finally: | |
| self.router._finish_url(worker_url) |
| choice = data["choices"][0] | ||
| content = choice["message"]["content"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| sleep 3 | ||
| pkill -9 ray || true | ||
| pkill -9 python || true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
miles/utils/openai_utils.py
Outdated
| ) -> dict[str, Any]: | ||
| try: | ||
| chat_request = ChatCompletionRequest.model_validate(payload) | ||
| except Exception as exc: # noqa: BLE001 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Catching a broad Exception can hide bugs and make debugging difficult. It's better to catch more specific exceptions. Since ChatCompletionRequest is a Pydantic model, ChatCompletionRequest.model_validate(payload) will raise pydantic.ValidationError on failure. Catching this specific exception would be more robust.
de122b1 to
ac4483c
Compare
ac4483c to
24a8995
Compare
24a8995 to
37b4b5f
Compare
Details are to be checked with miles router code owner.