-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeLLM.py
More file actions
145 lines (121 loc) · 5.66 KB
/
Copy pathCodeLLM.py
File metadata and controls
145 lines (121 loc) · 5.66 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
from langchain.prompts import ChatPromptTemplate
from langchain_core.output_parsers import BaseOutputParser
from langchain_openai import ChatOpenAI
TEMPLATE = """\
You are given a process description and the process model depicting the control flow.
problem description:
=============
{input}
=============
process model:
======
{model}
======
Your goal is to generate a valid Python code that correctly implements the process description, using the following tools:
=============
{tools}
=============
Each tool is represented by a JSON string having the following structure:
{{
"name": <class_name>,
"description": <description>,
"input_parameters": <input_parameters>,
"output_values": <output_values>,
"actor": <actor_name>
}}
where:
- <class_name> is the class implementing the tool.
- <description> is a string describing what the tool is able to do.
- <input_parameters> is the list of input parameters of the tool, separated by a comma. Each input parameter has the following structure <name>:<type> where <name> is the name of the input parameter and <type> is the type of the input parameter.
- <output_values> is the list of output values of the tool, separated by a comma. Each output value has the following structure <name>:<type> where <name> is the name of the output value and <type> is the type of the output value.
- <actor_name> is the actor that can perform the task executed by the tool.
Guidelines:
- Use the tools to execute the process tasks whenever possible. To use the tools you need to use the .call() static method with the proper input (e.g., the tool with name BookAppointment which does not take inputs should be called with BookAppointment.call()).
- Consider the tools already imported. You don't need to implement the classes of the tools. You only need to use them with the .call() method.
- Variables names you use should be meaningful.
- Double-check the generated code. It should generalize to any valid input, and not just the provided examples.
- Make sure to address the control flow provided by the process model. Use conditional statements (if-else) for exclusive gateways and parallel execution (threads) for parallel gateways.
- The code needs to be self-contained, and executable as-is.
- Do not add any other information after the ``` markdown end delimiters.
The generated code must follow this structure:
```python
def process(...):
...
return ...
if __name__ == "__main__":
...
```
Answer:
```python
"""
TEMPLATE_NO_MODEL = """\
You are given a process description.
problem description:
=============
{input}
=============
Your goal is to generate a valid Python code that correctly implements the process description, using the following tools:
=============
{tools}
=============
Each tool is represented by a JSON string having the following structure:
{{
"name": <class_name>,
"description": <description>,
"input_parameters": <input_parameters>,
"output_values": <output_values>,
"actor": <actor_name>
}}
where:
- <class_name> is the class implementing the tool.
- <description> is a string describing what the tool is able to do.
- <input_parameters> is the list of input parameters of the tool, separated by a comma. Each input parameter has the following structure <name>:<type> where <name> is the name of the input parameter and <type> is the type of the input parameter.
- <output_values> is the list of output values of the tool, separated by a comma. Each output value has the following structure <name>:<type> where <name> is the name of the output value and <type> is the type of the output value.
- <actor_name> is the actor that can perform the task executed by the tool.
Guidelines:
- Use the tools to execute the process tasks whenever possible. To use the tools you need to use the .call() static method with the proper input (e.g., the tool with name BookAppointment which does not take inputs should be called with BookAppointment.call()).
- Consider the tools already imported. You don't need to implement the classes of the tools. You only need to use them with the .call() method.
- Variables names you use should be meaningful.
- Double-check the generated code. It should generalize to any valid input, and not just the provided examples.
- Make sure to address the control flow of the process.
- The code needs to be self-contained, and executable as-is.
- Do not add any other information after the ``` markdown end delimiters.
The generated code must follow this structure:
```python
def process(...):
...
return ...
if __name__ == "__main__":
...
```
Answer:
```python
"""
class CustomOutputParser(BaseOutputParser):
"""The output parser for the LLM."""
def parse(self, text: str) -> str:
text = text.strip("\n")
text = text.strip()
# count how many ``` are in the text
back_count = text.count("```")
if back_count != 2:
print(text)
raise ValueError("The string should contain exactly two triple backticks")
code = text.split("```")[1]
code = code.strip().strip("python").strip()
#print(code)
return code
class CodeLLM():
def __init__(self, model, openai_key, temperature=0.0):
self.model = ChatOpenAI(model=model, openai_api_key=openai_key, temperature=temperature)
self.prompt = ChatPromptTemplate.from_template(TEMPLATE)
'''
prompt takes as input:
- tools: a list of tools
- model: model of the process
- input: a process description
'''
self.output_parser = CustomOutputParser()
def get_chain(self):
chain = self.prompt | self.model | self.output_parser
return chain