The CodePromptForge ToolKit provides a set of LangChain-compatible tools for developing AI agents that can read, analyze, modify, and write code. These tools enable agents to explore a codebase, extract insights, generate new content, and modify files dynamically.
- File and Directory Inspection: Retrieve file contents, list directory structures, and analyze project organization.
- Automated Code Processing: Identify and manipulate specific code files based on extensions.
- Code Modification: Write and update files programmatically.
- Cleanup and Maintenance: Remove unnecessary files from results folders.
- Seamless Integration: Designed to work with LangChain's agent framework.
- Optional Assistant Module: Allows users to create and register AI assistants.
To install the core functionality of CodePromptForge:
pip install codepromptforgeTo use the AI Assistant framework, install the package with the assistant extra:
pip install codepromptforge[assistant]This includes dependencies such as:
langchain_ollamalanggraph
Retrieve the structure of a directory
Returns a list of all files in a specified directory, ignoring files in.gitignoreor explicitly excluded.
tool = GetDirectoryTreeTool()
tool.run(folder_path="src")["src/main.py", "src/utils/helpers.py", "src/config/settings.json"]Read the content of a specific file
Retrieves the contents of a file for analysis or processing.
tool = GetFileContentTool()
tool.run(file_path="src/main.py")"def main():\n print('Hello, world!')"List all files in a specific folder with their contents
Provides a dictionary where keys are file names and values are their contents.
tool = GetFilesInFolderTool()
tool.run(folder_path="src"){
"main.py": "def main():\n print('Hello, world!')",
"config.json": "{'debug': true, 'version': '1.0'}"
}Retrieve all files in a folder and its subdirectories
Useful for analyzing an entire project structure.
tool = GetFilesRecursivelyTool()
tool.run(folder_path="src"){
"src/main.py": "def main():\n print('Hello, world!')",
"src/utils/helpers.py": "def helper():\n return 'Helper function'"
}Find all files matching specific extensions in the base directory
Helpful for searching for specific types of files, such as Python scripts or Markdown documentation.
tool = FindFilesTool()
tool.run(extensions=["py", "md"])["src/main.py", "docs/readme.md"]Write or modify a file
Creates or modifies a file inside the.resultfolder.
tool = WriteFileTool()
tool.run(file_path="output.txt", content="New content for the file")"File written successfully: .result/output.txt"Remove unnecessary files from the
.resultfolder
Ensures that old files donβt clutter the workspace.
tool = CleanResultFolderTool()
tool.run(excluded_files=["output.txt"])"Cleaned .result folder. Removed files: ['output.txt']"Merge multiple files into a single prompt
Key feature for creating context-rich inputs for LLMs.
tool = ForgePromptTool()
tool.run(extensions=["py", "md"])"Merged files: ['src/main.py', 'docs/readme.md']"Runs the forge process on specified file extensions
Automates the process of finding, merging, and generating code prompts.
tool = RunTool()
tool.run(extensions=["py", "txt"])"Generated combined prompt in output.txt"The CodePromptForge ToolKit is designed to be integrated into LangChain agents for intelligent code analysis. Hereβs how you can create a React agent that uses these tools:
from langchain.agents import initialize_agent
from langchain.chat_models import ChatOpenAI
from codepromptforge.main import CodePromptForge
# Initialize the CodePromptForge toolkit
forge = CodePromptForge(base_dir=".")
# Create an agent with the tools
agent = initialize_agent(
tools=forge.get_tools(),
llm=ChatOpenAI(temperature=0),
agent="zero-shot-react-description",
verbose=True
)
# Example: Ask the agent to analyze the project directory
agent.run("List all Python files in the project and summarize their content.")- The agent will call
get_directory_tree()to explore the project structure. - It will filter files using
find_files(["py"])to locate Python scripts. - It will use
get_file_content()to analyze each file. - Based on the retrieved content, it will generate a summary.
If installed with [assistant], you can leverage AI assistants to process and generate code.
from codepromptforge.assistant import AssistantRegistry
# List available assistants
print(AssistantRegistry.list_assistants())
# Expected output: ['react_assistant']
from langchain_ollama import ChatOllama
# Initialize an LLM
llm = ChatOllama(
model="qwen2.5:14b",
temperature=0,
num_ctx=80000,
num_gpu=1,
)
# Retrieve and use the assistant
agent = AssistantRegistry.get_assistant('react_assistant', llm)
def print_stream(stream):
for s in stream:
message = s["messages"][-1]
if isinstance(message, tuple):
print(message)
else:
message.pretty_print()
# Provide a query for the assistant
inputs = {
"messages": [("user", "This is my package named CodePromptForge. Your job is to describe what needs to be modified to improve the current code")]
}
print_stream(agent.stream(inputs, stream_mode="values"))The assistant will analyze the package and suggest improvements based on best practices.
The CodePromptForge ToolKit provides a ready-to-use suite of tools that can be seamlessly integrated into AI agents for code reading, modification, and generation. Whether you're building LLM-based code assistants, automated reviewers, or code refactoring agents, these tools simplify and automate complex workflows.
π Start developing with CodePromptForge today! π