File size: 1,480 Bytes
42472b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from langchain.tools import ShellTool
from langchain.agents import tool
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate

from models import llm


generate_python_code = """
Please write Shell script to fulfill the following requirement:

---
{input}
---

Only output the code section with code block.
"""

generate_python_code_promopt = PromptTemplate(input_variables=["input"], template=generate_python_code,)

generate_code_chain = LLMChain(llm = llm(temperature=0.1), prompt=generate_python_code_promopt, output_key="code")

shell_tool = ShellTool()

@tool("Generate and Excute Shell Code ", return_direct=True)
def generate_and_excute_shell_code(input: str) -> str:
    '''useful for when you need to generate python code and excute it'''
    command = generate_code_chain.run(input)
    print(command)
    code_content = command
    if('```python' in command): 
        start = command.find('```python') + len('```python')
        end = command.rfind('```')
        code_content = command[start:end].strip()
    elif("```" in command): 
        start = command.find('```') + len('```')
        end = command.rfind('```')
        code_content = command[start:end].strip()
    result = shell_tool.run(code_content)
    return f"""
code:
```
{code_content}
```

execute result:
---
{result}
---
    """


shell_tool = ShellTool()
shell_tool.description = shell_tool.description + f"args {shell_tool.args}".replace("{", "{{").replace("}", "}}")