File size: 10,028 Bytes
97d1422 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
import subprocess
from pathlib import Path
import os
import json
from dotenv import load_dotenv
from shutil import copyfile
from openai import OpenAI
load_dotenv()
# OpenRouter configuration
OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY")
MODEL = "qwen/qwen3-coder:free" # Qwen Distilled Coder model on OpenRouter
if not OPENROUTER_API_KEY:
raise ValueError("OPENROUTER_API_KEY must be set in .env file")
# Create OpenAI client pointed to OpenRouter
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key=OPENROUTER_API_KEY,
default_headers={"HTTP-Referer": "http://localhost:5000"} # Required by OpenRouter
)
# Define tools in OpenAI format
TOOLS = [
{
"type": "function",
"function": {
"name": "view",
"description": "View the contents of a file or directory",
"parameters": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "Path to the file or directory"
}
},
"required": ["path"]
}
}
},
{
"type": "function",
"function": {
"name": "create",
"description": "Create a new file with the given content",
"parameters": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "Path to the file to create"
},
"file_text": {
"type": "string",
"description": "Content to write to the file"
}
},
"required": ["path", "file_text"]
}
}
},
{
"type": "function",
"function": {
"name": "str_replace",
"description": "Replace a string in a file with another string",
"parameters": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "Path to the file"
},
"old_str": {
"type": "string",
"description": "String to replace"
},
"new_str": {
"type": "string",
"description": "String to replace with"
}
},
"required": ["path", "old_str", "new_str"]
}
}
},
{
"type": "function",
"function": {
"name": "bash",
"description": "Execute a bash command",
"parameters": {
"type": "object",
"properties": {
"command": {
"type": "string",
"description": "Command to execute"
}
},
"required": ["command"]
}
}
}
]
def execute_tool(tool_name: str, tool_input: dict) -> dict:
"""Execute a tool and return structured result with error handling."""
try:
# string replace tools
if tool_name == "view":
path = Path(str(tool_input.get("path")))
if path.is_file():
content = path.read_text()
return {"content": content, "is_error": False}
elif path.is_dir():
content = "\n".join(sorted([f.name for f in path.iterdir()]))
return {"content": content, "is_error": False}
else:
return {"content": f"Error: {path} does not exist", "is_error": True}
elif tool_name == "create":
path = Path(str(tool_input.get("path")))
content = str(tool_input.get("file_text"))
if not content:
return {
"content": "Error: No content provided in file_text",
"is_error": True,
}
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(content)
return {"content": f"File {path} written successfully", "is_error": False}
elif tool_name == "str_replace":
path = Path(str(tool_input.get("path")))
old_str = str(tool_input.get("old_str"))
new_str = str(tool_input.get("new_str"))
if not path.exists():
return {
"content": f"Error: File {path} does not exist",
"is_error": True,
}
content = path.read_text()
if old_str not in content:
return {
"content": f"Error: String '{old_str}' not found in {path}",
"is_error": True,
}
new_content = content.replace(old_str, new_str, 1)
path.write_text(new_content)
return {
"content": f"Replaced '{old_str}' with '{new_str}' in {path}",
"is_error": False,
}
# bash tools
elif tool_name == "bash":
command = tool_input.get("command")
print(command)
if not command:
return {
"content": "Error: No input in command",
"is_error": True,
}
result = subprocess.run(
command,
shell=True,
capture_output=True,
text=True,
timeout=30, # Add timeout for safety
)
# Return both stdout and stderr, mark as error if non-zero exit code
output = f"stdout: {result.stdout}\nstderr: {result.stderr}"
return {"content": output, "is_error": result.returncode != 0}
else:
return {
"content": f"Error: Unknown tool '{tool_name}'",
"is_error": True,
}
except Exception as e:
return {
"content": f"Error executing {tool_name}: {str(e)}",
"is_error": True,
}
if __name__ == "__main__":
prompt_content = Path("prompt.md").read_text()
system_prompt = prompt_content[
prompt_content.find("<role>") + 6 : prompt_content.find("</role>")
].strip()
instructions_content = prompt_content[
prompt_content.find("<thinking_process>") :
].strip()
# Initialize conversation history
messages = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": instructions_content}
]
while True:
user_input = input("💬 User: ")
messages.append({"role": "user", "content": user_input})
while True:
try:
# Call the OpenRouter API with Qwen model
response = client.chat.completions.create(
model=MODEL,
messages=messages,
temperature=0.2,
max_tokens=4096,
tools=TOOLS,
tool_choice="auto"
)
except Exception as e:
print(f"Error calling OpenRouter API: {str(e)}")
break
assistant_message = response.choices[0].message
assistant_content = assistant_message.content or ""
# Check if the model wants to use tools
if assistant_message.tool_calls:
tool_results = []
# Print any text content from the assistant
if assistant_content:
print(assistant_content)
print(f"Executing {len(assistant_message.tool_calls)} tool(s)...")
# Process each tool call
for tool_call in assistant_message.tool_calls:
function_name = tool_call.function.name
function_args = json.loads(tool_call.function.arguments) # Parse JSON arguments safely
print(f"Executing tool: {function_name}")
# Execute the tool
result = execute_tool(function_name, function_args)
print(result["content"])
# Add the tool result to the conversation
tool_results.append({
"tool_call_id": tool_call.id,
"role": "tool",
"name": function_name,
"content": result["content"]
})
# Add the assistant's response to the conversation
messages.append({
"role": "assistant",
"content": assistant_content,
"tool_calls": assistant_message.tool_calls
})
# Add all tool results to the conversation
messages.extend(tool_results)
continue
else:
# No tool calls, just print the response
print(assistant_content)
# Add the assistant's response to the conversation
messages.append({
"role": "assistant",
"content": assistant_content
})
break # Break out of inner loop to restart conversation
|