CodeEditor.svelte 1.58 KB
Newer Older
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
1
<script lang="ts">
Timothy J. Baek's avatar
Timothy J. Baek committed
2
3
	import CodeEditor from '$lib/components/common/CodeEditor.svelte';

Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
4
	export let saveHandler: Function;
Timothy J. Baek's avatar
Timothy J. Baek committed
5
6
	export let value = '';

Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
7
8
	let codeEditor;

Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
9
	let boilerplate = `# Tip: Use Ctrl/Cmd + S to format the code
Timothy J. Baek's avatar
Timothy J. Baek committed
10

Timothy J. Baek's avatar
Timothy J. Baek committed
11
12
13
from datetime import datetime
import requests

Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
14

Timothy J. Baek's avatar
Timothy J. Baek committed
15
16
17
18
class Tools:
    def __init__(self):
        pass

Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
19
20
21
22
    # Add your custom tools using pure Python code here, make sure to add type hints
    # Use Sphinx-style docstrings to document your tools, they will be used for generating tools specifications
    # Please refer to function_calling_filter_pipeline.py file from pipelines project for an example

Timothy J. Baek's avatar
Timothy J. Baek committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
    def get_current_time(self) -> str:
        """
        Get the current time.
        :return: The current time.
        """

        now = datetime.now()
        current_time = now.strftime("%H:%M:%S")
        return f"Current Time = {current_time}"

    def calculator(self, equation: str) -> str:
        """
        Calculate the result of an equation.
        :param equation: The equation to calculate.
        """

        # Avoid using eval in production code
        # https://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html
        try:
            result = eval(equation)
            return f"{equation} = {result}"
        except Exception as e:
            print(e)
            return "Invalid equation"
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
47

Timothy J. Baek's avatar
Timothy J. Baek committed
48
`;
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
49

Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
50
	export const formatHandler = async () => {
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
51
		if (codeEditor) {
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
52
			return await codeEditor.formatPythonCodeHandler();
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
53
		}
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
54
		return false;
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
55
	};
Timothy J. Baek's avatar
Timothy J. Baek committed
56
57
</script>

Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
58
59
60
61
62
63
64
65
<CodeEditor
	bind:value
	{boilerplate}
	bind:this={codeEditor}
	on:save={() => {
		saveHandler();
	}}
/>