agentleFS
Seeed-ProjectsSeeed-Projects/Seeed-Jetson-DevelopTool/skills/codex/langchain-output-formatting/AGENTS.md

Seeed-Jetson-DevelopTool / langchain-output-formatting

AGENTS.md57 starsChanged 4 months agoMITView on GitHub

Format LLM output using Langchain on Jetson for structured smart home control signals. Deploys a local chatbot with Gradio UI that uses LlamaCpp and Langchain's StructuredOutputParser to produce JSON-formatted responses. Requires JetPack 5.0+ and a GGUF model.

The file

We split this AGENTS.md into the rules it gives an agent

7 rules. About 0 are common, 4 are distinctive, and 3 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈4

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 3

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Use Langchain's StructuredOutputParser to constrain LLM output into structured JSON for smart home control.

    line 8
  2. Distinctive:

    Run one phase at a time. After each phase:

    line 14
  3. Very specific:

    If output contains [STOP] → stop, consult the failure decision tree.

    line 16
  4. Very specific:

    If output ends with [OK] → tell the user "Phase N complete" and proceed.

    line 17
  5. Very specific:

    Create format_opt.py with the following content.

    line 69
  6. Distinctive:

    Update the model path in the script to match your actual GGUF location.

    line 149
  7. Distinctive:

    Test by typing prompts like "It's really hot today" — the LLM should respond with structured JSON containing control signals.

    line 163

≈ “Common” is an estimate: at least 1 in 5 of the 40,246 AGENTS.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
AGENTS.md

Format LLM Output with Langchain on Jetson

Use Langchain's StructuredOutputParser to constrain LLM output into structured JSON for smart home control. The chatbot runs locally on Jetson using LlamaCpp with a Gradio web interface.


Execution model

Run one phase at a time. After each phase:

  • Relay all output to the user.
  • If output contains [STOP] → stop, consult the failure decision tree.
  • If output ends with [OK] → tell the user "Phase N complete" and proceed.

Prerequisites

RequirementDetail
HardwareJetson device (e.g. reComputer J4012)
JetPack5.0+
ModelLlama 2 7B Chat GGUF (Q4_0 quantization)
Pythonpip3 available

Phase 1 — Install dependencies (~3 min)

pip3 install --no-cache-dir --verbose "langchain[llm]" openai
pip3 install --no-cache-dir --verbose gradio==3.38.0

[OK] when both packages install without error. [STOP] if pip3 fails — check Python version and pip availability.


Phase 2 — Download GGUF model (~5–15 min)

Download the Llama 2 7B Chat GGUF model (Q4_0 quantization) from Hugging Face:

mkdir -p ~/models
# Download llama-2-7b-chat.Q4_0.gguf from Hugging Face
# Example using wget or huggingface-cli:
pip3 install huggingface-hub
huggingface-cli download TheBloke/Llama-2-7B-Chat-GGUF llama-2-7b-chat.Q4_0.gguf --local-dir ~/models

Verify:

ls -lh ~/models/llama-2-7b-chat.Q4_0.gguf

[OK] when the GGUF file exists (~3.8 GB). [STOP] if download fails — check network and disk space.


Phase 3 — Create the chatbot script

Create format_opt.py with the following content. The script uses Langchain's StructuredOutputParser with ResponseSchema to define output fields (user_input, suggestion, control signal, temperature), then wraps a LlamaCpp model in a Gradio chat interface:

cat > format_opt.py << 'PYEOF'
import copy
import gradio as gr
from langchain.llms import LlamaCpp
from langchain.output_parsers import StructuredOutputParser, ResponseSchema
from langchain.prompts import PromptTemplate

class ChatBot:
    def __init__(self, llama_model_path, history_length=3):
        self.chat_history = []
        self.history_threshold = history_length
        self.llm = LlamaCpp(
            model_path=llama_model_path,
            temperature=0.75,
            max_tokens=2000,
            top_p=1
        )
        response_schemas = [
            ResponseSchema(name="user_input", description="This is the user's input"),
            ResponseSchema(name="suggestion", type="string", description="your suggestion"),
            ResponseSchema(name="control", description="This is your response"),
            ResponseSchema(name="temperature", type="int",
                           description="Degrees centigrade temperature of the air conditioner.")
        ]
        self.output_parser = StructuredOutputParser.from_response_schemas(response_schemas)
        self.format_instructions = self.output_parser.get_format_instructions()
        self.template = """
            Now you are a smart speaker, and you need to determine whether to turn on the air conditioner based on the user's input.
            In the suggestion section, please reply normal conversation.
            In the control section, if you need to turn on the air conditioner, please reply with <1>; if you need to turn off the air conditioner, please reply with <0>.
            {format_instructions}
            Please do not generate any comments.
            % USER INPUT:
            {user_input}
            YOUR RESPONSE:
        """
        self.prompt = PromptTemplate(
            input_variables=["user_input"],
            partial_variables={"format_instructions": self.format_instructions},
            template=self.template
        )

    def format_chat_prompt(self, message):
        prompt = ""
        for turn in self.chat_history:
            user_message, bot_message = turn
            prompt = f"{prompt}\nUser: {user_message}\nAssistant: {bot_message}"
        prompt = f"{prompt}\nUser: {message}\nAssistant:"
        return prompt

    def respond(self, message):
        prompt = self.prompt.format(user_input=message)
        formatted_prompt = self.format_chat_prompt(prompt)
        bot_message = self.llm(formatted_prompt)
        if len(self.chat_history) >= self.history_threshold:
            del self.chat_history[0]
        self.chat_history.append((message, bot_message))
        return "", self.chat_history

    def run_webui(self):
        with gr.Blocks() as demo:
            gr.Markdown("# Format Output of LLM Demo")
            chatbot = gr.Chatbot(height=500)
            msg = gr.Textbox(label="Prompt")
            btn = gr.Button("Submit")
            clear = gr.ClearButton(components=[msg, chatbot], value="Clear console")
            btn.click(self.respond, inputs=[msg], outputs=[msg, chatbot])
            msg.submit(self.respond, inputs=[msg], outputs=[msg, chatbot])
        gr.close_all()
        demo.launch()

if __name__ == '__main__':
    chatbot_ins = ChatBot("/home/nvidia/models/llama-2-7b-chat.Q4_0.gguf")
    chatbot_ins.run_webui()
PYEOF

Update the model path in the script to match your actual GGUF location.

[OK] when file is created.


Phase 4 — Run the chatbot (~1 min to start)

python3 format_opt.py

Open a browser and navigate to http://<jetson-ip>:7861/.

Test by typing prompts like "It's really hot today" — the LLM should respond with structured JSON containing control signals.

[OK] when the Gradio UI loads and the LLM returns formatted JSON output. [STOP] if the model fails to load or Gradio crashes.


Failure decision tree

SymptomAction
pip3 install langchain failsUpgrade pip: pip3 install --upgrade pip. Check Python ≥ 3.8.
ModuleNotFoundError: llama_cppInstall: pip3 install llama-cpp-python. May need CMAKE_ARGS="-DLLAMA_CUBLAS=on" for GPU.
Model loading OOMUse a smaller quantization (Q2_K) or free memory. Check with free -h.
Gradio won't start on port 7861Port in use. Kill existing process or set demo.launch(server_port=7862).
LLM output not valid JSONAdjust temperature lower (0.3–0.5). Ensure format_instructions are in the prompt.
Browser can't reach JetsonCheck firewall: sudo ufw allow 7861. Verify IP with hostname -I.

Reference files

  • references/source.body.md — Full Seeed Wiki tutorial with code examples, screenshots, and next steps for Riva integration (reference only)

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.