CodeEditor.svelte 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <script>
  2. import CodeEditor from '$lib/components/common/CodeEditor.svelte';
  3. export let value = '';
  4. let codeEditor;
  5. let boilerplate = `# Tip: Use Ctrl/Cmd + S to format the code
  6. from datetime import datetime
  7. import requests
  8. class Tools:
  9. def __init__(self):
  10. pass
  11. # Add your custom tools using pure Python code here, make sure to add type hints
  12. # Use Sphinx-style docstrings to document your tools, they will be used for generating tools specifications
  13. # Please refer to function_calling_filter_pipeline.py file from pipelines project for an example
  14. def get_current_time(self) -> str:
  15. """
  16. Get the current time.
  17. :return: The current time.
  18. """
  19. now = datetime.now()
  20. current_time = now.strftime("%H:%M:%S")
  21. return f"Current Time = {current_time}"
  22. def calculator(self, equation: str) -> str:
  23. """
  24. Calculate the result of an equation.
  25. :param equation: The equation to calculate.
  26. """
  27. # Avoid using eval in production code
  28. # https://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html
  29. try:
  30. result = eval(equation)
  31. return f"{equation} = {result}"
  32. except Exception as e:
  33. print(e)
  34. return "Invalid equation"
  35. `;
  36. export const submitHandler = async () => {
  37. if (codeEditor) {
  38. codeEditor.formatPythonCodeHandler();
  39. }
  40. };
  41. </script>
  42. <CodeEditor bind:value {boilerplate} bind:this={codeEditor} />