Diff of /ehragent/toolset_high.py [000000] .. [6cf5c7]

Switch to unified view

a b/ehragent/toolset_high.py
1
import sys
2
import openai
3
import autogen
4
import time
5
import os
6
from config import openai_config
7
from openai import AzureOpenAI
8
import traceback
9
10
def run_code(cell):
11
    """
12
    Returns the path to the python interpreter.
13
    """
14
    # import prompts
15
    from prompts_mimic import CodeHeader
16
    try:
17
        global_var = {"answer": 0}
18
        exec(CodeHeader+cell, global_var)
19
        cell = "\n".join([line for line in cell.split("\n") if line.strip() and not line.strip().startswith("#")])
20
        if not 'answer' in cell.split('\n')[-1]:
21
            return "Please save the answer to the question in the variable 'answer'."
22
        return str(global_var['answer'])
23
    except Exception as e:
24
        error_info = traceback.format_exc()
25
        code = CodeHeader + cell
26
        if "SyntaxError" in str(repr(e)):
27
            error_line = str(repr(e))
28
            
29
            error_type = error_line.split('(')[0]
30
            # then parse out the error message
31
            error_message = error_line.split(',')[0].split('(')[1]
32
            # then parse out the error line
33
            error_line = error_line.split('"')[1]
34
        elif "KeyError" in str(repr(e)):
35
            code = code.split('\n')
36
            key = str(repr(e)).split("'")[1]
37
            error_type = str(repr(e)).split('(')[0]
38
            for i in range(len(code)):
39
                if key in code[i]:
40
                    error_line = code[i]
41
            error_message = str(repr(e))
42
        elif "TypeError" in str(repr(e)):
43
            error_type = str(repr(e)).split('(')[0]
44
            error_message = str(e)
45
            function_mapping_dict = {"get_value": "GetValue", "data_filter": "FilterDB", "db_loader": "LoadDB", "sql_interpreter": "SQLInterpreter", "date_calculator": "Calendar"}
46
            error_key = ""
47
            for key in function_mapping_dict.keys():
48
                if key in error_message:
49
                    error_message = error_message.replace(key, function_mapping_dict[key])
50
                    error_key = function_mapping_dict[key]
51
            code = code.split('\n')
52
            error_line = ""
53
            for i in range(len(code)):
54
                if error_key in code[i]:
55
                    error_line = code[i]
56
        else:
57
            error_type = ""
58
            error_message = str(repr(e)).split("('")[-1].split("')")[0]
59
            error_line = ""
60
        # use one sentence to introduce the previous parsed error information
61
        if error_type != "" and error_line != "":
62
            error_info = f'{error_type}: {error_message}. The error messages occur in the code line "{error_line}".'
63
        else:
64
            error_info = f'Error: {error_message}.'
65
        error_info += '\nPlease make modifications accordingly and make sure the rest code works well with the modification.'
66
67
        return error_info
68
69
70
def llm_agent(config_list):
71
    llm_config = {
72
        "functions": [
73
            {
74
                "name": "python",
75
                "description": "run cell in ipython and return the execution result.",
76
                "parameters": {
77
                    "type": "object",
78
                    "properties": {
79
                        "cell": {
80
                            "type": "string",
81
                            "description": "Valid Python cell to execute.",
82
                        }
83
                    },
84
                    "required": ["cell"],
85
                },
86
            },
87
        ],
88
        "config_list": config_list,
89
        "request_timeout": 120,
90
    }
91
    chatbot = autogen.AssistantAgent(
92
        name="chatbot",
93
        system_message="For coding tasks, only use the functions you have been provided with. Reply TERMINATE when the task is done.",
94
        llm_config=llm_config,
95
    )
96
    return chatbot