this is what is being passed: [{‘role’: ‘user’, ‘content’: ‘You are to follow these instructions: Your role is to run both c++, an python code given to you\n Question: cpp\n#include <iostream>\n\n// Function to calculate the sum of two numbers\nint calculateSum(int a, int b) {\n return a + b;\n}\n\nvoid test_calculateSum_positiveNumbers() {\n int result = calculateSum(2, 3);\n std::cout << "Test calculateSum with positive numbers: " << (result == 5 ? "Passed" : "Failed") << std::endl;\n}\n\nvoid test_calculateSum_negativeNumbers() {\n int result = calculateSum(-2, -3);\n std::cout << "Test calculateSum with negative numbers: " << (result == -5 ? "Passed" : "Failed") << std::endl;\n}\n\nvoid test_calculateSum_mixedNumbers() {\n int result = calculateSum(-2, 3);\n std::cout << "Test calculateSum with mixed numbers: " << (result == 1 ? "Passed" : "Failed") << std::endl;\n}\n\nvoid test_calculateSum_zero() {\n int result = calculateSum(0, 0);\n std::cout << "Test calculateSum with zeros: " << (result == 0 ? "Passed" : "Failed") << std::endl;\n}\n\nvoid test_calculateSum_largeNumbers() {\n int result = calculateSum(2000, 3000);\n std::cout << "Test calculateSum with large numbers: " << (result == 5000 ? "Passed" : "Failed") << std::endl;\n}\n\nint main() {\n test_calculateSum_positiveNumbers();\n test_calculateSum_negativeNumbers();\n test_calculateSum_mixedNumbers();\n test_calculateSum_zero();\n test_calculateSum_largeNumbers();\n\n return 0;\n}\n
’}]
<Response [200]>
{‘error’: {‘code’: None, ‘message’: ‘unexpected_error’, ‘param’: None, ‘type’: ‘unexpected_error’}}
Using Sambanova for function calling but keep receiving an unknown error.
my code:
def generate(self, question):
final_instruction = self.instruct + f"\n Question: {question}"
print(“Final Instruction:”, final_instruction)
if self.functionCaller:
response = self.llm.invoke(final_instruction)
message =“”
print(f"Tool outputs{response.tool_calls}“)
for tool_call in response.tool_calls:
selected_tool = self.tools[tool_call[“name”]]
tool_output = selected_tool.invoke(tool_call[“args”])
print(f"Tool output: {tool_output}”)
message = message + tool_output
return message
else:
print(final_instruction)
response = self.llm.invoke(final_instruction)
return response
Tools:
from langchain_core.tools import StructuredTool, Tool, ToolException, tool
from pydantic import BaseModel, Field
import os
import subprocess
import tempfile
import json
import re
class CodeInput(BaseModel):
code : str = Field(…, description=“This is code either c code or python code”)
C_INCLUDE_TO_PACKAGE = {
# Add mappings if needed for non-standard headers
‘boost/’: ‘libboost-all-dev’,
}
def find_c_dependencies(code: str):
includes = re.findall(r’#include\s*<"[">]', code)
needed_packages = set()
for inc in includes:
for key, pkg in C_INCLUDE_TO_PACKAGE.items():
if key in inc:
needed_packages.add(pkg)
return needed_packages
def install_dependencies(packages):
if not packages:
return
pass
def uninstall_dependencies(packages):
pass
@tool
def run_c(c_code: CodeInput):
“”"
Runs given C++ code. Detects which libraries the code uses,
installs them before compilation, and then cleans up after.
“”"
packages = find_c_dependencies(c_code.code)
install_dependencies(packages)
# Use .cpp extension to ensure code is compiled as C++
filep = tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.cpp', prefix='test')
executable = filep.name.replace('.cpp', '')
try:
with open(filep.name, 'w') as file:
file.write(c_code.code)
compile_process = subprocess.run(['g++', filep.name, '-o', executable, '-std=c++17'],
capture_output=True, text=True)
if compile_process.returncode != 0:
raise Exception(f"Compilation failed:\n{compile_process.stderr}")
run_process = subprocess.run([executable], capture_output=True, text=True)
if run_process.returncode != 0:
raise Exception(f"Execution failed:\n{run_process.stderr}")
return run_process.stdout
finally:
if os.path.exists(filep.name):
os.unlink(filep.name)
if os.path.exists(executable):
os.unlink(executable)
uninstall_dependencies(packages)
@tool
def run_py(py_code: CodeInput):
“”"
Runs given Python code
“”"
filep = tempfile.NamedTemporaryFile(delete=False, mode=‘w’, suffix=‘.py’, prefix=‘test’)
try:
with open(filep.name, ‘w’) as file:
file.write(py_code.code)
executable = filep.name
run_process = subprocess.run([“python3”, executable], capture_output=True, text=True)
if run_process.returncode != 0:
raise Exception(f"Execution failed:\n{run_process.stderr}")
return run_process.stdout
finally:
os.unlink(executable)