very simple langchain agent that is able to build 3d objects (warning not, currently multimodal and is not self correcting and uses cadquery to generate the step file )
from langchain_community.chat_models import ChatLiteLLM
from langchain.agents import create_agent
import os
import cadquery as cq
from langchain_core.tools import tool
import sys
@tool
def generate_step_box(length: float, width: float, height: float, filename: str = "agent_part.step") -> str:
"""
Generates a 3D CAD box with the specified dimensions and exports it as a STEP file.
Args:
length (float): The length of the box along the X axis.
width (float): The width of the box along the Y axis.
height (float): The height of the box along the Z axis.
filename (str): The name of the output STEP file. Defaults to 'agent_part.step'.
Returns:
str: A message indicating success or failure of the file generation.
"""
try:
# Generate the geometry using CadQuery
model = cq.Workplane("XY").box(length, width, height)
# Export the model to a working STEP file
cq.exporters.export(model, filename)
return f"Successfully generated CAD model and saved to {filename} with dimensions L:{length}, W:{width}, H:{height}."
except Exception as e:
return f"Failed to generate CAD model. Error: {str(e)}"
# 1. Put the tool in a list
tools = [generate_step_box]
def build_agent():
"""Build and return the agent graph."""
# llm = ChatOpenAI(model=MODEL, temperature=TEMPERATURE)
llm = ChatLiteLLM(model="anthropic/claude-3-haiku-20240307", temperature=0.1)
return create_agent(llm,tools=tools, system_prompt="build stuff please")
# return create_react_agent(llm, tools=TOOLS, prompt=SYSTEM_PROMPT)
def run_agent(question: str) -> str:
"""Run the agent on a single question and return the response text."""
return run_agent_with_tools(question)["response"]
def run_agent_with_tools(question: str) -> dict:
"""Run the agent and return response + tool usage info for evaluation."""
agent = build_agent()
result = agent.invoke({"messages": [{"role": "user", "content": question}]})
messages = result["messages"]
tools_used = []
for msg in messages:
if hasattr(msg, "tool_calls") and msg.tool_calls:
for tc in msg.tool_calls:
tools_used.append(tc["name"])
return {
"response": messages[-1].content,
"tools_used": tools_used,
}
# ---------------------------------------------------------------------------
# CLI
# ---------------------------------------------------------------------------
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python agent.py <question>")
sys.exit(1)
question = " ".join(sys.argv[1:])
print(run_agent(question))
very simple langchain agent that is able to build 3d objects (warning not, currently multimodal and is not self correcting and uses cadquery to generate the step file )
sample output
References