Last active
April 14, 2026 17:09
-
-
Save Shivakishore14/ad46b46ef352f7100b61907f3546f060 to your computer and use it in GitHub Desktop.
rootcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from langchain_moonshot import ChatMoonshot | |
| from langchain_openai import ChatOpenAI | |
| from langchain.tools import tool | |
| from langchain_core.messages import HumanMessage, SystemMessage, ToolMessage | |
| import subprocess | |
| import os | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| # model definition | |
| model = ChatOpenAI( | |
| model="gpt-5", | |
| api_key=os.getenv("OPENAI_API_KEY") | |
| ) | |
| # create tool and bind it to agent | |
| @tool | |
| def execute_bash(command: str) -> str: | |
| """Execute a bash command and return the output.""" | |
| user_input = input(f"Agent wants to execute: {command}\nDo you want to proceed? (y/n): ") | |
| if user_input.lower() != 'y': | |
| return "Command execution cancelled by user." | |
| result = subprocess.run(command, shell=True, capture_output=True, text=True) | |
| return result.stdout.strip() | |
| model = model.bind_tools([execute_bash]) | |
| # create system prompt | |
| messages = [SystemMessage(content="You are RootCode, A coding agent which helps user code locally on the machine. Use tools to work on the current project")] | |
| # loop to get user input, tool execution and agent response | |
| while True: | |
| user_input = input("You: ") | |
| messages.append(HumanMessage(content=user_input)) | |
| response = model.invoke(messages) | |
| messages.append(response) | |
| while response.tool_calls: | |
| for tool_call in response.tool_calls: | |
| tool_name = tool_call["name"] | |
| tool_args = tool_call["args"] | |
| if tool_name == "execute_bash": | |
| tool_response = execute_bash.invoke(tool_args) | |
| else: | |
| tool_response = f"Unknown tool: {tool_name}" | |
| messages.append(ToolMessage( | |
| content=tool_response, | |
| name=tool_name, | |
| tool_call_id=tool_call["id"] | |
| )) | |
| response = model.invoke(messages) | |
| messages.append(response) | |
| print(f"RootCode: {response.content}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment