Dmitry Beresnev commited on
Commit
57a7076
·
1 Parent(s): 23f86af

add custom LLM server test

Browse files
Files changed (1) hide show
  1. app.py +113 -1
app.py CHANGED
@@ -286,6 +286,52 @@ def is_gateway_port_open(timeout: float = 0.5) -> bool:
286
  return sock.connect_ex(("127.0.0.1", OPENCLAW_PORT)) == 0
287
 
288
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
289
  def with_token(url: str, token: str) -> str:
290
  if not token:
291
  return url
@@ -441,7 +487,7 @@ with std_ui_embed_col:
441
  public_ui_url = with_token(OPENCLAW_STANDARD_UI_PUBLIC_URL, OPENCLAW_GATEWAY_TOKEN)
442
  if public_ui_url:
443
  components.iframe(public_ui_url, height=560, scrolling=True)
444
- st.link_button("Open Standard UI", public_ui_url, use_container_width=True)
445
  else:
446
  st.info("Standard UI is not reachable yet. Start gateway and refresh.")
447
 
@@ -523,6 +569,72 @@ st.expander("Service Diagnostics (Supervisor/Caddy/Streamlit/OpenClaw)").code(
523
  )
524
  )
525
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
526
  st.divider()
527
  st.subheader("Backtesting Lab")
528
  st.caption(
 
286
  return sock.connect_ex(("127.0.0.1", OPENCLAW_PORT)) == 0
287
 
288
 
289
+ def infer_llm_api_base() -> str:
290
+ candidates = [
291
+ os.getenv("LLM_SPACE_OPENAI_URL", "").strip(),
292
+ os.getenv("LLM_SPACE_WEBCHAT_URL", "").strip(),
293
+ ]
294
+ for url in candidates:
295
+ if not url:
296
+ continue
297
+ if "/v1/chat/completions" in url:
298
+ return url.split("/v1/chat/completions", 1)[0]
299
+ if "/v1/web-chat/completions" in url:
300
+ return url.split("/v1/web-chat/completions", 1)[0]
301
+ return url.rstrip("/")
302
+ return "https://researchengineering-agi.hf.space"
303
+
304
+
305
+ def llm_list_models(api_base: str) -> dict:
306
+ resp = requests.get(f"{api_base}/models", timeout=15)
307
+ resp.raise_for_status()
308
+ return resp.json()
309
+
310
+
311
+ def llm_switch_model(api_base: str, model_name: str) -> dict:
312
+ resp = requests.post(
313
+ f"{api_base}/switch-model",
314
+ json={"model_name": model_name},
315
+ timeout=120,
316
+ )
317
+ resp.raise_for_status()
318
+ return resp.json()
319
+
320
+
321
+ def llm_chat_completion(api_base: str, prompt: str, max_tokens: int, temperature: float) -> dict:
322
+ resp = requests.post(
323
+ f"{api_base}/v1/chat/completions",
324
+ json={
325
+ "messages": [{"role": "user", "content": prompt}],
326
+ "max_tokens": max_tokens,
327
+ "temperature": temperature,
328
+ },
329
+ timeout=300,
330
+ )
331
+ resp.raise_for_status()
332
+ return resp.json()
333
+
334
+
335
  def with_token(url: str, token: str) -> str:
336
  if not token:
337
  return url
 
487
  public_ui_url = with_token(OPENCLAW_STANDARD_UI_PUBLIC_URL, OPENCLAW_GATEWAY_TOKEN)
488
  if public_ui_url:
489
  components.iframe(public_ui_url, height=560, scrolling=True)
490
+ st.markdown(f"[Open Standard UI]({public_ui_url})")
491
  else:
492
  st.info("Standard UI is not reachable yet. Start gateway and refresh.")
493
 
 
569
  )
570
  )
571
 
572
+ st.divider()
573
+ st.subheader("External LLM Server Test")
574
+ llm_base_default = infer_llm_api_base()
575
+ llm_base_url = st.text_input("LLM API Base URL", value=llm_base_default)
576
+ llm_models_col, llm_switch_col = st.columns([2, 3])
577
+
578
+ with llm_models_col:
579
+ if st.button("Fetch Models", use_container_width=True):
580
+ try:
581
+ models_info = llm_list_models(llm_base_url.rstrip("/"))
582
+ st.session_state["llm_models_info"] = models_info
583
+ except Exception as exc:
584
+ st.error(f"Models request failed: {exc}")
585
+ models_info = st.session_state.get("llm_models_info", {})
586
+ if models_info:
587
+ st.json(models_info)
588
+
589
+ with llm_switch_col:
590
+ available_models = models_info.get(
591
+ "available_models",
592
+ ["deepseek-chat", "mistral-7b", "openhermes-7b", "deepseek-coder", "llama-7b"],
593
+ )
594
+ selected_model = st.selectbox("Switch Model", options=available_models, index=0)
595
+ if st.button("Switch Active Model", use_container_width=True):
596
+ try:
597
+ switch_resp = llm_switch_model(llm_base_url.rstrip("/"), selected_model)
598
+ st.success(f"Switched: {switch_resp}")
599
+ except Exception as exc:
600
+ st.error(f"Model switch failed: {exc}")
601
+
602
+ llm_prompt_col, llm_result_col = st.columns([2, 3])
603
+ with llm_prompt_col:
604
+ llm_test_prompt = st.text_area(
605
+ "Prompt",
606
+ value="Summarize the key financial metrics from this report: Revenue increased 15% YoY to $10M, with EBITDA margins improving from 20% to 25%. Cash flow remained strong at $3M.",
607
+ height=140,
608
+ )
609
+ llm_max_tokens = st.number_input("Max Tokens", min_value=1, max_value=4096, value=256)
610
+ llm_temp = st.number_input("Temperature", min_value=0.0, max_value=2.0, value=0.7, step=0.1)
611
+ run_llm_test = st.button("Run Chat Completion", use_container_width=True)
612
+
613
+ with llm_result_col:
614
+ if run_llm_test:
615
+ try:
616
+ t0 = time.perf_counter()
617
+ chat_resp = llm_chat_completion(
618
+ llm_base_url.rstrip("/"),
619
+ llm_test_prompt,
620
+ int(llm_max_tokens),
621
+ float(llm_temp),
622
+ )
623
+ dt = time.perf_counter() - t0
624
+ st.caption(f"Latency: {dt:.2f}s")
625
+ content = (
626
+ chat_resp.get("choices", [{}])[0]
627
+ .get("message", {})
628
+ .get("content", "")
629
+ )
630
+ if content:
631
+ st.markdown("**Response**")
632
+ st.write(content)
633
+ st.markdown("**Raw JSON**")
634
+ st.json(chat_resp)
635
+ except Exception as exc:
636
+ st.error(f"Chat request failed: {exc}")
637
+
638
  st.divider()
639
  st.subheader("Backtesting Lab")
640
  st.caption(