CodeEditor.svelte 2.15 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
	import CodeEditor from '$lib/components/common/CodeEditor.svelte';
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
3
4
5
	import { createEventDispatcher } from 'svelte';

	const dispatch = createEventDispatcher();
Timothy J. Baek's avatar
Timothy J. Baek committed
6
7
8

	export let value = '';

Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
9
	let codeEditor;
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
10
	let boilerplate = `from datetime import datetime
Timothy J. Baek's avatar
Timothy J. Baek committed
11
import requests
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
12
import os
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
13

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

Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
18
19
20
21
    # 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
refac  
Timothy J. Baek committed
22
23
24
25
26
27
28
29
30
31
32
33
    def get_environment_variable(self, variable_name: str) -> str:
        """
        Get the value of an environment variable.
        :param variable_name: The name of the environment variable.
        :return: The value of the environment variable or a message if it doesn't exist.
        """
        value = os.getenv(variable_name)
        if value is not None:
            return f"The value of the environment variable '{variable_name}' is '{value}'"
        else:
            return f"The environment variable '{variable_name}' does not exist."

Timothy J. Baek's avatar
Timothy J. Baek committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
    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
58

Timothy J. Baek's avatar
Timothy J. Baek committed
59
`;
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
60

Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
61
	export const formatHandler = async () => {
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
62
		if (codeEditor) {
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
63
			return await codeEditor.formatPythonCodeHandler();
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
64
		}
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
65
		return false;
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
66
	};
Timothy J. Baek's avatar
Timothy J. Baek committed
67
68
</script>

Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
69
70
71
72
73
<CodeEditor
	bind:value
	{boilerplate}
	bind:this={codeEditor}
	on:save={() => {
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
74
		dispatch('save');
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
75
76
	}}
/>