Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions routellm/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,27 @@ async def acompletion(
kwargs["messages"], router, threshold
)
return await acompletion(api_base=self.api_base, api_key=self.api_key, **kwargs)
# --- NEW: lightweight helper -------------------------------------------
def invoke(
self,
prompt: str,
*,
router: str = "mf",
threshold: float = 0.5,
return_score: bool = False,
):
"""
Fast path that runs the router **only** and returns the routed model
name. Set `return_score=True` to also get the strong-model win-rate.

Example
-------
>>> ctrl = Controller(routers=["mf"], strong_model="gpt-4o", weak_model="llama3-8b")
>>> model = ctrl.invoke("Write a haiku about routing.")
'llama3-8b-8192'
"""
self._validate_router_threshold(router, threshold)
router_inst = self.routers[router]
win_rate = router_inst.calculate_strong_win_rate(prompt)
chosen = self.model_pair.strong if win_rate >= threshold else self.model_pair.weak
return (chosen, win_rate) if return_score else chosen
5 changes: 3 additions & 2 deletions routellm/routers/routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ def __init__(
strong_model="gpt-4-1106-preview",
weak_model="mixtral-8x7b-instruct-v0.1",
num_tiers=10,
embedding_model: str = "text-embedding-3-small"
):
self.strong_model = strong_model
self.weak_model = weak_model
Expand All @@ -154,7 +155,7 @@ def __init__(
for dataset in arena_embedding_datasets
]
self.arena_conv_embedding = np.concatenate(embeddings)
self.embedding_model = "text-embedding-3-small"
self.embedding_model = embedding_model

assert len(self.arena_df) == len(
self.arena_conv_embedding
Expand All @@ -181,7 +182,7 @@ def calculate_strong_win_rate(
prompt_emb = (
(
OPENAI_CLIENT.embeddings.create(
input=[prompt], model=self.embedding_model
input=[prompt], model=self.embedding_model , encoding_format="float"
)
)
.data[0]
Expand Down
2 changes: 1 addition & 1 deletion routellm/routers/similarity_weighted/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from sklearn.linear_model import LogisticRegression

choices = ["A", "B", "C", "D"]
OPENAI_CLIENT = OpenAI()
OPENAI_CLIENT = OpenAI(api_key="tpsg-WuXePnCvR4CG6y8wB3MXbAkqHi4yCaf" , base_url="https://api.tapsage.com/openai/v1")


def compute_tiers(model_ratings, num_tiers):
Expand Down