call_rust_from_python.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. from evaluator import *
  2. DESCRIPTION = "Test if the model can write rust code that can be imported from python and knows how to build it."
  3. TAGS = ['rust', 'c', 'python', 'code']
  4. question = """I want to call some rust code from python on a linux machine. Specifically, I want to be able to run
  5. python3 -c 'import magic; print(magic.do_add(3, 4))'
  6. and have it give me the sum of these numbers. Here, do_add should be a rust function that adds two integers.
  7. Please give me a complete set of instructions for how to make this run, so that when I run this code as-is it will work.
  8. """
  9. question2 = """
  10. Now please conver this answer into a JSON array that packages it all up so I don't need to think and can just copy files to specific locations and then run things. Format your answer
  11. {"path/to/file": "contents of rust file",
  12. "path/to/python_file": "contents of any necessary python file that exposes do_add",
  13. "build.sh": "contents of a bash file to build the project",
  14. ... # any other files necessary
  15. }
  16. The only thing I will do is put files in the right place and run `bash build.sh`. Do not give any explanation of the code. ONLY ANSWER WITH A SINGLE JSON OBJECT. Your answer should start ```json and end with ```.
  17. """
  18. def is_ok():
  19. import os
  20. os.popen("bash build.sh").read()
  21. return os.popen("python3 -c 'import magic; print(magic.do_add(31, 11))'").read() == "42\n"
  22. TestCallRustFromPy = question >> LLMConversation() >> LLMConversation(question2) >> ExtractJSON() >> MakeFilesFromJSON() >> PyEvaluator(is_ok)
  23. TestCallCFromPy = question.replace("rust", "C") >> LLMConversation() >> LLMConversation(question2.replace("rust", "C")) >> ExtractJSON() >> MakeFilesFromJSON() >> PyEvaluator(is_ok)
  24. if __name__ == "__main__":
  25. print(run_test(TestCallRustFromPy))