优化:Streamlit fragment性能 + code_run灵活性 + LLM context_win配置

This commit is contained in:
Liang Jiaqing
2026-02-03 18:13:12 +08:00
parent d1eb67845f
commit fa98abcea5
3 changed files with 11 additions and 9 deletions

View File

@@ -74,12 +74,14 @@ for msg in st.session_state.messages:
with st.chat_message(msg["role"]): with st.chat_message(msg["role"]):
st.markdown(msg["content"]) st.markdown(msg["content"])
with st.sidebar: @st.fragment
def render_llm_switcher():
current_idx = st.session_state.get("llm_no", 0) current_idx = st.session_state.get("llm_no", 0)
st.caption(f"LLM Core: {current_idx}") st.caption(f"LLM Core: {current_idx}")
if st.button("切换备用链路"): if st.button("切换备用链路"):
st.session_state.llm_no = (st.session_state.get("llm_no", 0) + 1) % len(llmclient.raw_apis) st.session_state.llm_no = (st.session_state.get("llm_no", 0) + 1) % len(llmclient.raw_apis)
st.rerun() st.rerun(scope="fragment")
with st.sidebar: render_llm_switcher()
if prompt := st.chat_input("请输入指令"): if prompt := st.chat_input("请输入指令"):
st.session_state.messages.append({"role": "user", "content": prompt}) st.session_state.messages.append({"role": "user", "content": prompt})

12
ga.py
View File

@@ -7,18 +7,17 @@ sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from agent_loop import BaseHandler, StepOutcome, try_call_generator from agent_loop import BaseHandler, StepOutcome, try_call_generator
def code_run(code: str, code_type: str = "python", timeout: int = 60, cwd: str = None): def code_run(code, code_type="python", timeout=60, cwd=None, code_cwd=None):
""" """代码执行器
针对 Windows 优化的双模态执行器
python: 运行复杂的 .py 脚本(文件模式) python: 运行复杂的 .py 脚本(文件模式)
powershell: 运行单行指令(命令模式) powershell/bash: 运行单行指令(命令模式)
优先使用python仅在必要系统操作时使用powershell。 优先使用python仅在必要系统操作时使用powershell。
""" """
preview = (code[:60].replace('\n', ' ') + '...') if len(code) > 60 else code.strip() preview = (code[:60].replace('\n', ' ') + '...') if len(code) > 60 else code.strip()
yield f"[Action] Running {code_type} in {os.path.basename(cwd)}: {preview}\n" yield f"[Action] Running {code_type} in {os.path.basename(cwd)}: {preview}\n"
cwd = cwd or os.path.join(os.getcwd(), 'temp'); tmp_path = None cwd = cwd or os.path.join(os.getcwd(), 'temp'); tmp_path = None
if code_type == "python": if code_type == "python":
tmp_file = tempfile.NamedTemporaryFile(suffix=".py", delete=False, mode='w', encoding='utf-8') tmp_file = tempfile.NamedTemporaryFile(suffix=".ai.py", delete=False, mode='w', encoding='utf-8', dir=code_cwd)
tmp_file.write(code) tmp_file.write(code)
tmp_path = tmp_file.name tmp_path = tmp_file.name
tmp_file.close() tmp_file.close()
@@ -274,7 +273,8 @@ class GenericAgentHandler(BaseHandler):
timeout = args.get("timeout", 60) timeout = args.get("timeout", 60)
raw_path = os.path.join(self.cwd, args.get("cwd", './')) raw_path = os.path.join(self.cwd, args.get("cwd", './'))
cwd = os.path.normpath(os.path.abspath(raw_path)) cwd = os.path.normpath(os.path.abspath(raw_path))
result = yield from code_run(code, code_type, timeout, cwd) code_cwd = os.path.normpath(self.cwd)
result = yield from code_run(code, code_type, timeout, cwd, code_cwd=code_cwd)
next_prompt = self._get_anchor_prompt() + warning next_prompt = self._get_anchor_prompt() + warning
return StepOutcome(result, next_prompt=next_prompt) return StepOutcome(result, next_prompt=next_prompt)

View File

@@ -18,7 +18,7 @@ class SiderLLMSession:
return ''.join(self._core.chat(prompt, model)) return ''.join(self._core.chat(prompt, model))
class LLMSession: class LLMSession:
def __init__(self, api_key=oai_apikey, api_base=oai_apibase, model=oai_model, context_win=32000): def __init__(self, api_key=oai_apikey, api_base=oai_apibase, model=oai_model, context_win=16000):
self.api_key = api_key self.api_key = api_key
self.api_base = api_base self.api_base = api_base
self.raw_msgs = [] self.raw_msgs = []