Update README.md
Browse files11 unmodified lines1213141511 unmodified linesTo use the model with optillm you can just prepend `router` to the model name. E.g. if we set `router-gpt-4o-mini` as the model, it will use the `gpt-4o-mini` as the base model.Otherwise, refer to the code in [router-plugin](https://github.com/codelion/optillm/blob/main/optillm/plugins/router_plugin.py) to see how to use this model for classification.11 unmodified lines12131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979811 unmodified linesTo use the model with optillm you can just prepend `router` to the model name. E.g. if we set `router-gpt-4o-mini` as the model, it will use the `gpt-4o-mini` as the base model.Otherwise, refer to the code in [router-plugin](https://github.com/codelion/optillm/blob/main/optillm/plugins/router_plugin.py) to see how to use this model for classification.# UsageTo use the model directly you will need to use our `OptILMClassifier` class as we added additional layers to the base model. Also, notethe mapping of the returned index to the `APPROACHES` list as shown below.```pythonimport torchimport torch.nn as nnimport torch.nn.functional as Ffrom transformers import AutoModel, AutoTokenizer, AutoConfigfrom huggingface_hub import hf_hub_downloadfrom safetensors import safe_openfrom safetensors.torch import load_modelfrom transformers import AutoTokenizer, AutoModel# ConstantsMAX_LENGTH = 512APPROACHES = ["none", "mcts", "bon", "moa", "rto", "z3", "self_consistency", "pvg", "rstar", "cot_reflection", "plansearch", "leap", "re2"]MODEL_NAME = "codelion/optillm-bert-uncased"class OptILMClassifier(nn.Module):def __init__(self, base_model, num_labels):super().__init__()self.base_model = base_modelself.effort_encoder = nn.Sequential(nn.Linear(1, 64),nn.ReLU(),nn.Linear(64, 64),nn.ReLU())self.classifier = nn.Linear(base_model.config.hidden_size + 64, num_labels)def forward(self, input_ids, attention_mask, effort):outputs = self.base_model(input_ids=input_ids, attention_mask=attention_mask)pooled_output = outputs.last_hidden_state[:, 0] # Shape: (batch_size, hidden_size)effort_encoded = self.effort_encoder(effort.unsqueeze(1)) # Shape: (batch_size, 64)combined_input = torch.cat((pooled_output, effort_encoded), dim=1)logits = self.classifier(combined_input)return logitsdef preprocess_input(tokenizer, system_prompt, initial_query):combined_input = f"{system_prompt}\n\nUser: {initial_query}"encoding = tokenizer.encode_plus(combined_input,add_special_tokens=True,max_length=MAX_LENGTH,padding='max_length',truncation=True,return_attention_mask=True,return_tensors='pt')return encoding['input_ids'], encoding['attention_mask']def predict_approach(model, input_ids, attention_mask, device, effort=0.7):model.eval()with torch.no_grad():input_ids = input_ids.to(device)attention_mask = attention_mask.to(device)effort_tensor = torch.tensor([effort], dtype=torch.float).to(device)logits = model(input_ids, attention_mask=attention_mask, effort=effort_tensor)probabilities = F.softmax(logits, dim=1)predicted_approach_index = torch.argmax(probabilities, dim=1).item()confidence = probabilities[0][predicted_approach_index].item()return APPROACHES[predicted_approach_index], confidence```You can now use the `predict_approach` method to get the predicted approach as follows:```python# Load the trained modelrouter_model, tokenizer, device = load_optillm_model()# Preprocess the inputinput_ids, attention_mask = preprocess_input(tokenizer, system_prompt, initial_query)# Predict the best approachpredicted_approach, _ = predict_approach(router_model, input_ids, attention_mask, device)print(f"Router predicted approach: {predicted_approach}")```
README.md+84-1