Spaces:
Sleeping
Sleeping
File size: 13,332 Bytes
af31cad d80cfe4 cdba494 af31cad d80cfe4 af31cad cdba494 d80cfe4 af31cad cdba494 af31cad cdba494 b55e829 cdba494 b55e829 d80cfe4 af31cad cdba494 b55e829 cdba494 b55e829 cdba494 b55e829 cdba494 b55e829 cdba494 b55e829 cdba494 b55e829 cdba494 b55e829 cdba494 b55e829 cdba494 b55e829 cdba494 b55e829 cdba494 b55e829 cdba494 b55e829 cdba494 b55e829 cdba494 af31cad d80cfe4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
import gradio as gr
import asyncio
import os
from typing import Dict, Any
from dotenv import load_dotenv
from minion import config
from minion.main import LocalPythonEnv
from minion.main.rpyc_python_env import RpycPythonEnv
from minion.main.brain import Brain
from minion.providers import create_llm_provider
# Load .env file
load_dotenv()
class LLMConfig:
def __init__(self, api_type: str, api_key: str, base_url: str, api_version: str,
model: str, temperature: float = 0.7, max_tokens: int = 4000,
vision_enabled: bool = False):
self.api_type = api_type
self.api_key = api_key
self.base_url = base_url
self.api_version = api_version
self.model = model
self.temperature = temperature
self.max_tokens = max_tokens
self.vision_enabled = vision_enabled
def get_preset_configs():
"""Get preset configurations"""
presets = {
"gpt-4o": LLMConfig(
api_type=os.getenv("GPT_4O_API_TYPE", "azure"),
api_key=os.getenv("GPT_4O_API_KEY", ""),
base_url=os.getenv("GPT_4O_BASE_URL", ""),
api_version=os.getenv("GPT_4O_API_VERSION", "2024-06-01"),
model=os.getenv("GPT_4O_MODEL", "gpt-4o"),
temperature=float(os.getenv("GPT_4O_TEMPERATURE", "0")),
max_tokens=int(os.getenv("GPT_4O_MAX_TOKENS", "4000"))
),
"gpt-4o-mini": LLMConfig(
api_type=os.getenv("GPT_4O_MINI_API_TYPE", "azure"),
api_key=os.getenv("GPT_4O_MINI_API_KEY", ""),
base_url=os.getenv("GPT_4O_MINI_BASE_URL", ""),
api_version=os.getenv("GPT_4O_MINI_API_VERSION", "2024-06-01"),
model=os.getenv("GPT_4O_MINI_MODEL", "gpt-4o-mini"),
temperature=float(os.getenv("GPT_4O_MINI_TEMPERATURE", "0.1")),
max_tokens=int(os.getenv("GPT_4O_MINI_MAX_TOKENS", "4000"))
),
"gpt-4.1": LLMConfig(
api_type=os.getenv("GPT_41_API_TYPE", "azure"),
api_key=os.getenv("GPT_41_API_KEY", ""),
base_url=os.getenv("GPT_41_BASE_URL", ""),
api_version=os.getenv("GPT_41_API_VERSION", "2025-03-01-preview"),
model=os.getenv("GPT_41_MODEL", "gpt-4.1"),
temperature=float(os.getenv("GPT_41_TEMPERATURE", "0.7")),
max_tokens=int(os.getenv("GPT_41_MAX_TOKENS", "4000"))
),
"o4-mini": LLMConfig(
api_type=os.getenv("O4_MINI_API_TYPE", "azure"),
api_key=os.getenv("O4_MINI_API_KEY", ""),
base_url=os.getenv("O4_MINI_BASE_URL", ""),
api_version=os.getenv("O4_MINI_API_VERSION", "2025-03-01-preview"),
model=os.getenv("O4_MINI_MODEL", "o4-mini"),
temperature=float(os.getenv("O4_MINI_TEMPERATURE", "0.7")),
max_tokens=int(os.getenv("O4_MINI_MAX_TOKENS", "4000"))
)
}
return presets
def get_default_config():
"""Get default configuration"""
return LLMConfig(
api_type=os.getenv("DEFAULT_API_TYPE", "azure"),
api_key=os.getenv("DEFAULT_API_KEY", ""),
base_url=os.getenv("DEFAULT_BASE_URL", ""),
api_version=os.getenv("DEFAULT_API_VERSION", "2024-06-01"),
model=os.getenv("DEFAULT_MODEL", "gpt-4o"),
temperature=float(os.getenv("DEFAULT_TEMPERATURE", "0.7")),
max_tokens=int(os.getenv("DEFAULT_MAX_TOKENS", "4000"))
)
def get_available_routes():
"""Get available route options for current minion system"""
return [
"", # Auto route selection (empty for automatic)
"raw", # Raw LLM output without processing
"native", # Native minion processing
"cot", # Chain of Thought reasoning
"dcot", # Dynamic Chain of Thought
"plan", # Planning-based approach
"python" # Python code execution
]
def create_custom_llm_config(api_type: str, api_key: str, base_url: str,
api_version: str, model: str, temperature: float,
max_tokens: int) -> Dict[str, Any]:
"""Create custom LLM configuration"""
return {
'api_type': api_type,
'api_key': api_key,
'base_url': base_url,
'api_version': api_version,
'model': model,
'temperature': temperature,
'max_tokens': max_tokens,
'vision_enabled': False
}
def build_brain_with_config(llm_config_dict: Dict[str, Any]):
"""Build brain with specified configuration"""
# Create a config object similar to LLMConfig
class Config:
def __init__(self, config_dict):
for key, value in config_dict.items():
setattr(self, key, value)
config_obj = Config(llm_config_dict)
llm = create_llm_provider(config_obj)
python_env = LocalPythonEnv(verbose=False)
brain = Brain(
python_env=python_env,
llm=llm,
)
return brain
# Get preset configurations and default configuration
preset_configs = get_preset_configs()
default_config = get_default_config()
available_routes = get_available_routes()
async def minion_respond_async(query: str, preset_model: str, api_type: str,
api_key: str, base_url: str, api_version: str,
model: str, temperature: float, max_tokens: int,
route: str, query_type: str, check_enabled: bool):
"""Respond to query using specified configuration"""
# If a preset model is selected, use preset configuration
if preset_model != "Custom":
config_obj = preset_configs.get(preset_model, default_config)
llm_config_dict = {
'api_type': config_obj.api_type,
'api_key': config_obj.api_key,
'base_url': config_obj.base_url,
'api_version': config_obj.api_version,
'model': config_obj.model,
'temperature': config_obj.temperature,
'max_tokens': config_obj.max_tokens,
'vision_enabled': config_obj.vision_enabled
}
else:
# Use custom configuration
llm_config_dict = create_custom_llm_config(
api_type, api_key, base_url, api_version, model, temperature, max_tokens
)
brain = build_brain_with_config(llm_config_dict)
# Handle empty route selection for auto route
route_param = route if route else None
# Add query_type to kwargs if route is python
kwargs = {'query': query, 'route': route_param, 'check': check_enabled}
if route == "python" and query_type:
kwargs['query_type'] = query_type
obs, score, *_ = await brain.step(**kwargs)
return obs
def minion_respond(query: str, preset_model: str, api_type: str, api_key: str,
base_url: str, api_version: str, model: str, temperature: float,
max_tokens: int, route: str, query_type: str, check_enabled: bool):
"""Gradio sync interface, automatically schedules async"""
return asyncio.run(minion_respond_async(
query, preset_model, api_type, api_key, base_url,
api_version, model, temperature, max_tokens, route, query_type, check_enabled
))
def update_fields(preset_model: str):
"""Update other fields when preset model is selected"""
if preset_model == "Custom":
# Return default values, let user configure themselves
return (
default_config.api_type,
"", # Don't display API key
default_config.base_url,
default_config.api_version,
default_config.model,
default_config.temperature,
default_config.max_tokens
)
else:
config_obj = preset_configs.get(preset_model, default_config)
# Ensure API type is from valid choices
api_type = config_obj.api_type if config_obj.api_type in ["azure", "openai", "groq", "ollama", "anthropic", "gemini"] else "azure"
return (
api_type,
"***hidden***", # Hide API key display
config_obj.base_url,
config_obj.api_version,
config_obj.model,
config_obj.temperature,
config_obj.max_tokens
)
def update_query_type_visibility(route: str):
"""Show query_type dropdown only when route is python"""
return gr.update(visible=(route == "python"))
# Create Gradio interface
with gr.Blocks(title="Minion Brain Chat") as demo:
gr.Markdown("# Minion Brain Chat\nIntelligent Q&A powered by Minion1 Brain")
with gr.Row():
with gr.Column(scale=2):
query_input = gr.Textbox(
label="Enter your question",
placeholder="Please enter your question...",
lines=3
)
submit_btn = gr.Button("Submit", variant="primary")
# Move Answer section to left column, closer to question input
output = gr.Textbox(
label="Answer",
lines=10,
show_copy_button=True
)
with gr.Column(scale=1):
# Move route selection to the front
route_dropdown = gr.Dropdown(
label="Route",
choices=available_routes,
value="",
info="empty: auto select, raw: direct LLM, native: standard, cot: chain of thought, dcot: dynamic cot, plan: planning, python: code execution"
)
# Add query_type option, visible only when route="python"
query_type_dropdown = gr.Dropdown(
label="Query Type",
choices=["calculate", "code_solution", "generate"],
value="calculate",
visible=False,
info="Type of query for python route"
)
# Add check option
check_checkbox = gr.Checkbox(
label="Enable Check",
value=False,
info="Enable output verification and validation"
)
preset_dropdown = gr.Dropdown(
label="Preset Model",
choices=["Custom"] + list(preset_configs.keys()),
value="gpt-4o",
info="Select preset configuration or custom"
)
api_type_input = gr.Dropdown(
label="API Type",
choices=["azure", "openai", "groq", "ollama", "anthropic", "gemini"],
value=default_config.api_type,
info="Select API provider type"
)
api_key_input = gr.Textbox(
label="API Key",
value="***hidden***",
type="password",
info="Your API key"
)
base_url_input = gr.Textbox(
label="Base URL",
value=default_config.base_url,
info="API base URL"
)
api_version_input = gr.Textbox(
label="API Version",
value=default_config.api_version,
info="API version (required for Azure)"
)
model_input = gr.Textbox(
label="Model",
value=default_config.model,
info="Model name"
)
temperature_input = gr.Slider(
label="Temperature",
minimum=0.0,
maximum=2.0,
value=default_config.temperature,
step=0.1,
info="Control output randomness"
)
max_tokens_input = gr.Slider(
label="Max Tokens",
minimum=100,
maximum=8000,
value=default_config.max_tokens,
step=100,
info="Maximum number of tokens to generate"
)
# Update other fields when preset model changes
preset_dropdown.change(
fn=update_fields,
inputs=[preset_dropdown],
outputs=[api_type_input, api_key_input, base_url_input,
api_version_input, model_input, temperature_input, max_tokens_input]
)
# Update query_type visibility when route changes
route_dropdown.change(
fn=update_query_type_visibility,
inputs=[route_dropdown],
outputs=[query_type_dropdown]
)
# Submit button event
submit_btn.click(
fn=minion_respond,
inputs=[query_input, preset_dropdown, api_type_input, api_key_input,
base_url_input, api_version_input, model_input, temperature_input,
max_tokens_input, route_dropdown, query_type_dropdown, check_checkbox],
outputs=[output]
)
# Enter key submit
query_input.submit(
fn=minion_respond,
inputs=[query_input, preset_dropdown, api_type_input, api_key_input,
base_url_input, api_version_input, model_input, temperature_input,
max_tokens_input, route_dropdown, query_type_dropdown, check_checkbox],
outputs=[output]
)
if __name__ == "__main__":
demo.launch(mcp_server=True)
|