-
Notifications
You must be signed in to change notification settings - Fork 149
feat(cli): allow model passed by OpenAI client to be used in the served Mellea program #1512
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
Open
markstur
wants to merge
5
commits into
generative-computing:main
Choose a base branch
from
markstur:m_model_id
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f24c5b0
feat(cli): allow model and other client options to be used in m serve…
markstur 3b30fd0
tests(cli): add tests for new m serve client_options
markstur 265b356
docs(cli): improve docstring
markstur 6db4dbc
fix: remove unused
markstur 689cbcb
test(cli): add test coverage
markstur File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
docs/examples/m_serve/model-routing/client_model_routing.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # pytest: skip_always | ||
|
|
||
| """Client demonstrating how to interact with the model-routing m serve example. | ||
|
|
||
| Pattern A: use the standard `model` field to select the backend via client_options. | ||
| Pattern B: unknown model falls back to the default backend. | ||
|
|
||
| The allowlist and default behavior is an implementation decision in the example | ||
| Mellea program being served. It could easily be changed to ignore the requested | ||
| model or to throw an error instead of having a default model. | ||
|
|
||
| Run the server first: | ||
| uv run m serve docs/examples/m_serve/model-routing/m_serve_example_model_routing.py | ||
| """ | ||
|
|
||
| import openai | ||
|
|
||
| PORT = 8080 | ||
| client = openai.OpenAI(api_key="na", base_url=f"http://0.0.0.0:{PORT}/v1") | ||
|
|
||
| print("=== Pattern A: standard model field routes via client_options ===") | ||
| # The standard `model` field is read by the server via | ||
| # client_options and used to select the backend. | ||
| response_a = client.chat.completions.create( | ||
| model="granite4.1:8b", messages=[{"role": "user", "content": "What is 2 + 2?"}] | ||
| ) | ||
| print(f"model echoed back : {response_a.model}") | ||
| print(f"response : {response_a.choices[0].message.content}\n") | ||
|
|
||
| print("=== Pattern B: unknown model falls back to default ===") | ||
| response_b = client.chat.completions.create( | ||
| model="some-unknown-model", messages=[{"role": "user", "content": "What is 2 + 2?"}] | ||
| ) | ||
| print(f"model echoed back : {response_b.model}") | ||
| print(f"response : {response_b.choices[0].message.content}") |
78 changes: 78 additions & 0 deletions
78
docs/examples/m_serve/model-routing/m_serve_example_model_routing.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| # pytest: ollama, e2e | ||
|
|
||
| """Example showing how to use client_options to route on the client model ID. | ||
|
|
||
| In an OpenAI-compatible request the client sends a `model` string, e.g.: | ||
|
|
||
| client.chat.completions.create(model="granite4.1:8b", messages=[...]) | ||
|
|
||
| That string is **routing / metadata** from the server's perspective: `m serve` | ||
| echoes it back in the response but does NOT include it in `model_options` (which | ||
| is filtered for backend consumption). | ||
|
|
||
| Declare `client_options` in `serve()` and `m serve` passes the full raw client | ||
| request as a dict, giving access to `model` and every other field the client | ||
| sent — without any of those values leaking into `model_options`. | ||
|
|
||
| To ignore the client model ID entirely and always use a fixed backend, simply | ||
| omit the `client_options` parameter (see the simple/ examples). | ||
|
|
||
| Run the server: | ||
| m serve docs/examples/m_serve/model-routing/m_serve_example_model_routing.py | ||
|
|
||
| Test with the client: | ||
| python docs/examples/m_serve/model-routing/client_model_routing.py | ||
| """ | ||
|
|
||
| import os | ||
| from typing import Any | ||
|
|
||
| import mellea | ||
| from mellea.backends.model_ids import IBM_GRANITE_4_1_3B, IBM_GRANITE_4_1_8B | ||
| from mellea.core import ModelOutputThunk | ||
| from mellea.serve import ChatMessage | ||
|
|
||
| _ollama_host = os.environ.get("OLLAMA_HOST", "localhost:11434") | ||
|
planetf1 marked this conversation as resolved.
Outdated
|
||
| if not _ollama_host.startswith(("http://", "https://")): | ||
| _ollama_host = f"http://{_ollama_host}" | ||
|
|
||
| _DEFAULT_MODEL = IBM_GRANITE_4_1_3B | ||
|
|
||
| _ALLOWED_MODELS: dict[str, Any] = { | ||
| IBM_GRANITE_4_1_3B.ollama_name: IBM_GRANITE_4_1_3B, # type: ignore[dict-item] | ||
| IBM_GRANITE_4_1_8B.ollama_name: IBM_GRANITE_4_1_8B, # type: ignore[dict-item] | ||
| } | ||
|
|
||
|
|
||
| def serve( | ||
| input: list[ChatMessage], | ||
| requirements: list[str] | None = None, | ||
| model_options: dict[str, Any] | None = None, | ||
| client_options: dict[str, Any] | None = None, | ||
| ) -> ModelOutputThunk: | ||
| """Serve with backend selected from the standard client `model` field. | ||
|
|
||
| Reads `client_options["model"]` (the standard OpenAI `model` field) and | ||
| routes to an allowlisted Ollama backend. Falls back to `granite4.1:3b` | ||
| when the value is unrecognised. `model_options` is clean — it contains | ||
| only backend generation parameters, never routing metadata. | ||
|
|
||
| Args: | ||
| input: Chat messages from the client. | ||
| requirements: Optional requirement strings forwarded from the client. | ||
| model_options: Generation parameters filtered for backend consumption. | ||
| client_options: Full raw client request fields, including `model`. | ||
|
|
||
| Returns: | ||
| ModelOutputThunk with the generated response. | ||
| """ | ||
| model_name = (client_options or {}).get("model") | ||
| chosen_model = _ALLOWED_MODELS.get(model_name, _DEFAULT_MODEL) # type: ignore[arg-type] | ||
|
|
||
| message = input[-1].get_text_content() or "No message provided" | ||
| session = mellea.start_session(model_id=chosen_model) | ||
| return session.instruct( | ||
| description=message, | ||
| requirements=requirements, # type: ignore[arg-type] | ||
| model_options=model_options, | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.