tinycodinglm / agentic /aiagent-0000.jsonl
winapiadmin's picture
Upload folder using huggingface_hub
1f3a244 verified
|
Raw History Blame Contribute Delete
3.27 kB
opencode trace
System You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing c...

You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing code, identify bugs, security issues, and performance problems. You have deep knowledge of C/C++, Windows API, Python, and many other technologies.

User

How is a ReAct-style LLM agent structured (LLM brain, planner, executor, memory, tools)? Show a minimal OpenAI-compatible tool loop with correct tool_call_id handling. Include code examples.

Assistant

AI Agent Architecture (ReAct loop: Thought -> Action -> Observation).

markdown
User goal -> LLM Brain -> Planner -> Executor -> Tool/Env -> Observation -> Memory

Core components:

  1. LLM Brain: reasoning and decision-making
  2. Planner: decomposes goals into steps
  3. Executor: invokes tools, handles errors
  4. Memory: short-term (context window) + long-term (vector DB)
  5. Tools: search, code exec (sandboxed), file I/O, APIs

ReAct (Yao et al., 2022) interleaves reasoning traces with tool actions. Plan-and-Execute plans first, then executes. Reflexion adds self-critique.

python
from openai import OpenAI

client = OpenAI(base_url="http://localhost:1234/v1", api_key="lm-studio")
MODEL = "model-identifier-from-lm-studio"

def tool_schema(name: str, desc: str, props: dict, required: list[str]) -> dict:
    return {
        "type": "function",
        "function": {
            "name": name, "description": desc,
            "parameters": {"type": "object", "properties": props, "required": required},
        },
    }

def run_agent(goal: str, tools: dict, execute_fn, max_steps: int = 5) -> str:
    history: list[dict] = [{"role": "user", "content": goal}]
    schemas = [tool_schema(*t) for t in tools.values()]
    for _ in range(max_steps):
        resp = client.chat.completions.create(
            model=MODEL, messages=history, tools=schemas, tool_choice="auto",
        )
        msg = resp.choices[0].message
        history.append({
            "role": "assistant",
            "content": msg.content,
            "tool_calls": [
                {"id": tc.id, "type": "function",
                 "function": {"name": tc.function.name,
                              "arguments": tc.function.arguments or "{}"}}
                for tc in (msg.tool_calls or [])
            ] if msg.tool_calls else None,
        })
        if not msg.tool_calls:
            return msg.content or ""
        for tc in msg.tool_calls:
            try:
                result = execute_fn(tc.function.name, tc.function.arguments or "{}")
            except Exception as e:
                result = f"ERROR: {type(e).__name__}: {e}"
            history.append({"role": "tool", "tool_call_id": tc.id, "content": str(result)})
    return "Max steps reached"