Dmitry Beresnev commited on
Commit ·
57a7076
1
Parent(s): 23f86af
add custom LLM server test
Browse files285 unmodified lines286287288289290291149 unmodified lines44144244344444544644775 unmodified lines523524525526527528285 unmodified linesreturn sock.connect_ex(("127.0.0.1", OPENCLAW_PORT)) == 0def with_token(url: str, token: str) -> str:if not token:return url149 unmodified linespublic_ui_url = with_token(OPENCLAW_STANDARD_UI_PUBLIC_URL, OPENCLAW_GATEWAY_TOKEN)if public_ui_url:components.iframe(public_ui_url, height=560, scrolling=True)st.link_button("Open Standard UI", public_ui_url, use_container_width=True)else:st.info("Standard UI is not reachable yet. Start gateway and refresh.")75 unmodified lines))st.divider()st.subheader("Backtesting Lab")st.caption(285 unmodified lines286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337149 unmodified lines48748848949049149249375 unmodified lines569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640285 unmodified linesreturn sock.connect_ex(("127.0.0.1", OPENCLAW_PORT)) == 0def infer_llm_api_base() -> str:candidates = [os.getenv("LLM_SPACE_OPENAI_URL", "").strip(),os.getenv("LLM_SPACE_WEBCHAT_URL", "").strip(),]for url in candidates:if not url:continueif "/v1/chat/completions" in url:return url.split("/v1/chat/completions", 1)[0]if "/v1/web-chat/completions" in url:return url.split("/v1/web-chat/completions", 1)[0]return url.rstrip("/")return "https://researchengineering-agi.hf.space"def llm_list_models(api_base: str) -> dict:resp = requests.get(f"{api_base}/models", timeout=15)resp.raise_for_status()return resp.json()def llm_switch_model(api_base: str, model_name: str) -> dict:resp = requests.post(f"{api_base}/switch-model",json={"model_name": model_name},timeout=120,)resp.raise_for_status()return resp.json()def llm_chat_completion(api_base: str, prompt: str, max_tokens: int, temperature: float) -> dict:resp = requests.post(f"{api_base}/v1/chat/completions",json={"messages": [{"role": "user", "content": prompt}],"max_tokens": max_tokens,"temperature": temperature,},timeout=300,)resp.raise_for_status()return resp.json()def with_token(url: str, token: str) -> str:if not token:return url149 unmodified linespublic_ui_url = with_token(OPENCLAW_STANDARD_UI_PUBLIC_URL, OPENCLAW_GATEWAY_TOKEN)if public_ui_url:components.iframe(public_ui_url, height=560, scrolling=True)st.markdown(f"[Open Standard UI]({public_ui_url})")else:st.info("Standard UI is not reachable yet. Start gateway and refresh.")75 unmodified lines))st.divider()st.subheader("External LLM Server Test")llm_base_default = infer_llm_api_base()llm_base_url = st.text_input("LLM API Base URL", value=llm_base_default)llm_models_col, llm_switch_col = st.columns([2, 3])with llm_models_col:if st.button("Fetch Models", use_container_width=True):try:models_info = llm_list_models(llm_base_url.rstrip("/"))st.session_state["llm_models_info"] = models_infoexcept Exception as exc:st.error(f"Models request failed: {exc}")models_info = st.session_state.get("llm_models_info", {})if models_info:st.json(models_info)with llm_switch_col:available_models = models_info.get("available_models",["deepseek-chat", "mistral-7b", "openhermes-7b", "deepseek-coder", "llama-7b"],)selected_model = st.selectbox("Switch Model", options=available_models, index=0)if st.button("Switch Active Model", use_container_width=True):try:switch_resp = llm_switch_model(llm_base_url.rstrip("/"), selected_model)st.success(f"Switched: {switch_resp}")except Exception as exc:st.error(f"Model switch failed: {exc}")llm_prompt_col, llm_result_col = st.columns([2, 3])with llm_prompt_col:llm_test_prompt = st.text_area("Prompt",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.",height=140,)llm_max_tokens = st.number_input("Max Tokens", min_value=1, max_value=4096, value=256)llm_temp = st.number_input("Temperature", min_value=0.0, max_value=2.0, value=0.7, step=0.1)run_llm_test = st.button("Run Chat Completion", use_container_width=True)with llm_result_col:if run_llm_test:try:t0 = time.perf_counter()chat_resp = llm_chat_completion(llm_base_url.rstrip("/"),llm_test_prompt,int(llm_max_tokens),float(llm_temp),)dt = time.perf_counter() - t0st.caption(f"Latency: {dt:.2f}s")content = (chat_resp.get("choices", [{}])[0].get("message", {}).get("content", ""))if content:st.markdown("**Response**")st.write(content)st.markdown("**Raw JSON**")st.json(chat_resp)except Exception as exc:st.error(f"Chat request failed: {exc}")st.divider()st.subheader("Backtesting Lab")st.caption(
app.py+113-1