Skip to content

Commit f32de4d

Browse files
committed
[Feature] Configurable slections for AI Managed mode
1 parent 18a850e commit f32de4d

3 files changed

Lines changed: 68 additions & 47 deletions

File tree

config.toml.sample

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@ fallback_char = "?"
1515
temperature = 1
1616
system_role = "assistant"
1717
model = "gemini-pro-exp"
18-
assistant = "o4-mini"
19-
assistant_role = "<instructions>\nInstructions for AI Assistant:\n\n- Select the optimal AI model to answer user questions based on their complexity and nature.\n- Provide your response only in JSON format, strictly following the example below.\n- The \"system\" message should contain clear, guiding instructions for the selected AI model, without sharing your reasoning or including your own answer or opinion.\n\n<example>\n{\n \"model\": \"{{selected_model}}\",\n \"messages\": [\n {\"role\": \"system\", \"content\": \"{{system_instruction_to_ai_model}}\"}\n ]\n}\n</example>\n\nEvaluation Criteria:\n\n1. Analyze the user's question based on topic, required depth of knowledge, and length.\n2. Select the most suitable model from the options, considering capabilities and cost-effectiveness.\n3. Use the tables below to guide your model selection.\n\n<table1>\n| Model Name | Input Cost ($/1M tokens) | Output Cost ($/1M tokens) | Remarks |\n|----------------|--------------------------|---------------------------|-----------------------------------------------------------|\n| gpt-41 | 2.00 | 8.00 | High-intelligence model for complex, multi-step tasks. |\n| gpt-41-mini | 0.40 | 1.60 | Affordable, intelligent model for fast, lightweight tasks. |\n| o3 | 2.00 | 8.00 | Advanced reasoning model for solving hard problems across domains. |\n| o4-mini | 1.10 | 4.40 | Efficient reasoning in coding, math, and science. |\n</table1>\n\nModels are ranked from highest to lowest capability. Only suggest the best-fitting model.\n\n<table2>\n| Category | Model to Consider |\n|----------------------------------------|--------------------|\n| Math, Science, Coding | o4-mini |\n| One-shot (deep reasoning with context) | o3 |\n| Multi-turn, complex conversations with reasoning | gpt-41 |\n| Complex tasks, problem solving across domains. | gpt-41 |\n| Common Tasks, General Topics | gpt-41-mini |\n</table2>\n\nNotes:\n\n- Do not include explanations or clarifications outside the JSON response.\n- Optimize query handling, model capability, and cost in your decision.\n</instructions>"
18+
19+
[chat.managed]
20+
assistant = "gpt-4o-mini"
21+
assistant_role = "<instructions>\nInstructions for AI Assistant:\n\n- Select the optimal AI model to answer user questions based on their complexity and nature.\n- Provide your response only in JSON format, strictly following the example below.\n- The \"system\" message should contain clear, guiding instructions for the selected AI model, without sharing your reasoning or including your own answer or opinion.\n\n<example>\n{\n \"model\": \"{{selected_model}}\",\n \"messages\": [\n {\"role\": \"system\", \"content\": \"{{system_instruction_to_ai_model}}\"}\n ]\n}\n</example>\n\nEvaluation Criteria:\n\n1. Analyze the user's question based on topic, required depth of knowledge, and length.\n2. Select the most suitable model from the options, considering capabilities and cost-effectiveness.\n3. Use the tables below to guide your model selection.\n\n<table1>\n| Model Name | Cost | Remarks |\n|-|-|-|-|\n| {{assistant_generalist}} | Highest | High-intelligence model for complex, multi-step tasks. |\n| {{assistant_fast}} | Cheapest | Affordable, intelligent model for fast, lightweight tasks. |\n| {{assistant_thinker}} | Highest | Advanced reasoning model for solving hard problems across domains. |\n| {{assistant_coder}} | Medium | Efficient reasoning in coding, math, and science. |\n</table1>\n\nModels are ranked from highest to lowest capability. Only suggest the best-fitting model.\n\n<table2>\n| Category | Model to Consider |\n|-|-|\n| Math, Science, Coding | {{assistant_coder}} |\n| One-shot (deep reasoning with context) | {{assistant_thinker}} |\n| Multi-turn, complex conversations with reasoning | {{assistant_generalist}} |\n| Complex tasks, problem solving across domains. | {{assistant_generalist}} |\n| Common Tasks, General Topics | {{assistant_fast}} |\n</table2>\n\nNotes:\n\n- Do not include explanations or clarifications outside the JSON response.\n- Optimize query handling, model capability, and cost in your decision.\n</instructions>"
22+
assistant_generalist = "gpt-41"
23+
assistant_fast = "gpt-41-mini"
24+
assistant_thinker = "o3"
25+
assistant_coder = "o4-mini"
2026

2127
[chat.features]
2228
model_selector = true

console_gpt/menus/ai_managed.py

Lines changed: 59 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import json
2+
from pyexpat import model
23
from typing import Tuple
34

5+
from httpx import get
46
from rich.console import Console
57
from unichat import UnifiedChatApi
68

@@ -14,45 +16,13 @@
1416
from console_gpt.prompts.temperature_prompt import temperature_prompt
1517
from console_gpt.prompts.user_prompt import chat_user_prompt
1618

17-
tools = [
18-
{
19-
"name": "managed_prompt",
20-
"description": "Selects optimal AI model and generates appropriate system instructions based on user query analysis",
21-
"inputSchema": {
22-
"type": "object",
23-
"properties": {
24-
"model": {
25-
"type": "string",
26-
"enum": ["gpt-41", "gpt-41-mini", "o3", "o4-mini"],
27-
"description": "The selected AI model based on query analysis",
28-
},
29-
"messages": {
30-
"type": "array",
31-
"items": {
32-
"type": "object",
33-
"properties": {
34-
"role": {
35-
"type": "string",
36-
"enum": ["system"],
37-
"description": "The role of the message, only system messages are allowed",
38-
},
39-
"content": {
40-
"type": "string",
41-
"description": "The system instruction for the selected AI model",
42-
},
43-
},
44-
"required": ["role", "content"],
45-
},
46-
"minItems": 1,
47-
"maxItems": 1,
48-
},
49-
},
50-
"required": ["model", "messages"],
51-
},
52-
}
19+
MODEL_KEYS = [
20+
"{{assistant_generalist}}",
21+
"{{assistant_fast}}",
22+
"{{assistant_thinker}}",
23+
"{{assistant_coder}}",
5324
]
5425

55-
5626
def managed_prompt() -> Tuple[ChatObject, str]:
5727
"""
5828
Use assistant help to determine the best model and fromat for the query
@@ -69,13 +39,14 @@ def managed_prompt() -> Tuple[ChatObject, str]:
6939

7040

7141
def configure_assistant():
72-
assistant_model = fetch_variable("defaults", "assistant")
73-
assistant_role = fetch_variable("defaults", "assistant_role")
42+
assistant_model = fetch_variable("managed", "assistant")
43+
assistant_role = fetch_variable("managed", "assistant_role")
44+
replacements = {key: fetch_variable("managed", key.strip("{}")) for key in MODEL_KEYS}
45+
for key, value in replacements.items():
46+
assistant_role = assistant_role.replace(key, value)
7447
model_data = fetch_variable("models", assistant_model)
75-
model_data.update(dict(model_title=assistant_model))
76-
model_data.update(dict(role=assistant_role))
77-
model_data = update_api_key_if_placeholder(model_data)
78-
return model_data
48+
model_data.update(model_title=assistant_model, role=assistant_role)
49+
return update_api_key_if_placeholder(model_data)
7950

8051

8152
def update_api_key_if_placeholder(model_data):
@@ -95,6 +66,50 @@ def get_client(assistant):
9566
return UnifiedChatApi(**assistant_params)
9667

9768

69+
def get_tools_schema():
70+
"""
71+
Dynamically generate the tools schema with the current model names from config.
72+
"""
73+
model_names = [fetch_variable("managed", key.strip("{}")) for key in MODEL_KEYS]
74+
return [
75+
{
76+
"name": "managed_prompt",
77+
"description": "Selects optimal AI model and generates appropriate system instructions based on user query analysis",
78+
"inputSchema": {
79+
"type": "object",
80+
"properties": {
81+
"model": {
82+
"type": "string",
83+
"enum": model_names,
84+
"description": "The selected AI model based on query analysis",
85+
},
86+
"messages": {
87+
"type": "array",
88+
"items": {
89+
"type": "object",
90+
"properties": {
91+
"role": {
92+
"type": "string",
93+
"enum": ["system"],
94+
"description": "The role of the message, only system messages are allowed",
95+
},
96+
"content": {
97+
"type": "string",
98+
"description": "The system instruction for the selected AI model",
99+
},
100+
},
101+
"required": ["role", "content"],
102+
},
103+
"minItems": 1,
104+
"maxItems": 1,
105+
},
106+
},
107+
"required": ["model", "messages"],
108+
},
109+
}
110+
]
111+
112+
98113
@sigint_wrapper
99114
def send_request(client, assistant, conversation):
100115
if "reasoning_effort" in assistant:
@@ -107,7 +122,7 @@ def send_request(client, assistant, conversation):
107122
model=assistant["model_name"],
108123
messages=conversation,
109124
stream=False,
110-
tools=tools,
125+
tools=get_tools_schema(),
111126
reasoning_effort=reasoning_effort,
112127
)
113128
return response.choices[0].message.tool_calls

console_gpt/menus/model_menu.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def model_menu() -> Dict[str, Union[int, str, float]]:
2626

2727
# Fetches the default model
2828
default_model = fetch_variable("defaults", "model")
29-
default_assistant = fetch_variable("defaults", "assistant")
29+
default_assistant = fetch_variable("managed", "assistant")
3030
all_models = fetch_variable("models")
3131

3232
# Add new options for model management

0 commit comments

Comments
 (0)