File size: 17,701 Bytes
adecf03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
"""

This script interacts with various LLMs (e.g., GPT-3.5, GPT-4, Claude, and LLaMA) to generate and execute Python code for machine learning tasks. It uses the user's selected LLM model and prompts the user for necessary API keys based on the model. The results of generated code execution are stored in an Excel file.



Requirements:

- openai for GPT models

- anthropic for Claude models

- replicate for LLaMA models

- pandas, subprocess, tempfile, re for general file handling and code execution

- rdkit for chemical descriptor generation (used in the ML task)



The main function `generate_and_execute_code` interacts with the LLM and stores results in a pandas DataFrame.

"""

import re
import tempfile
import os
import glob
import shutil
import pandas as pd
import time
import traceback
import glob
import numpy as np
from IPython.utils.io import capture_output
import subprocess
import pandas as pd
import replicate
import anthropic
import openai
import json



def get_model_and_api_keys():
    """

    Prompts the user to select an LLM model and then prompts for the corresponding API keys based on the chosen model.



    Returns:

        tuple: A tuple containing the selected model and a dictionary of API keys.

    """
    print("Please enter the LLM model to be used (e.g., 'gpt-3.5-turbo', 'claude-3', 'llama-2'):")
    model = input().strip().lower()

    api_keys = {}
    if 'gpt' in model:
        api_keys['openai_key'] = input("Please provide your OpenAI API key: ").strip()
    if 'o1' in model:
        api_keys['openai_key'] = input("Please provide your OpenAI API key: ").strip()    
    if 'claude' in model:
        api_keys['anthropic_key'] = input("Please provide your Anthropic API key: ").strip()
    if 'llama' in model:
        api_keys['replicate_key'] = input("Please provide your Replicate API key: ").strip()

    return model, api_keys


def generate_and_execute_code(user_prompts, model='gpt-3.5-turbo', num_calls=2, max_reflection=0):
    """

    Generate code using the specified model, execute it, and store the results in an Excel file.



    Args:

        user_prompts (list): A list of prompts for generating the Python code.

        model (str): The model to use for code generation. Examples:

            - 'llama-3.1'

            - 'llma-3'

            - 'claude-3-opus-20240229'

            - 'claude-3-sonnet-20240229'

            - 'claude-3-5-sonnet-20240620'

            - 'gpt-4-turbo-2024-04-09'

            - 'gpt-4-0613'

            - 'gpt-4o'

            - 'gpt-4o-mini'

            - 'gpt-3.5-turbo' (default)

        num_calls (int): The number of times to generate and execute code.

        max_reflection (int): The maximum number of reflection attempts when code execution fails.



    Returns:

        pandas.DataFrame: A DataFrame containing the generated code, execution results, response times, number of reflections, and conversation history.

    """
    # Prompt for API keys based on the selected model
    openai_key = anthropic_key = replicate_key = None

    # Load keys from environment or prompt user
    if 'openai_key' in api_keys:
        openai.api_key = api_keys['openai_key']
    if 'anthropic_key' in api_keys:
        anthropic_key = api_keys['anthropic_key']
    if 'replicate_key' in api_keys:
        os.environ["REPLICATE_API_TOKEN"] = api_keys['replicate_key']

    results = []

    for _ in range(num_calls):
        
        # Delete all previous CSV and pkl files
        for file_path in glob.glob('*.csv') + glob.glob('*.pkl'):
            os.remove(file_path)
        
        conversation_history = []
        row_data = {'Model': model}
        execution_result, num_reflections = 0, 0

        for i in range(len(user_prompts)):
            if i > 0 and execution_result != 1:
                row_data[f'Generated Code {i+1}'] = 'N/A'
                row_data[f'Execution Result {i+1}'] = 'N/A'
                row_data[f'Response Time {i+1} (s)'] = 'N/A'
                row_data[f'Number of Reflections {i+1}'] = 'N/A'
                continue
            
            prompt_conversation_history = conversation_history.copy()
            response_content, response_time = chat(model, user_prompts[i], prompt_conversation_history)
            prompt_conversation_history.append({"role": "user", "content": user_prompts[i]})
            prompt_conversation_history.append({"role": "assistant", "content": response_content})

            code_block = re.search(r'```python\n(.*?)\n```', response_content, re.DOTALL)
            if code_block:
                code = code_block.group(1)
            else:
                code = response_content
                print(f"Warning: No code block found in the response for prompt {i+1}. Attempting to execute the entire response.")

            for j in range(max_reflection + 1):
                execution_result, error_message = run_code(code)
                if execution_result == 1:
                    break
                if j < max_reflection:
                    reflection_prompt = f"Please reflect on the code you previously wrote. There is an error and I cannot run it on my Jupyter Notebook. The error message is:\n{error_message}\nPlease try to catch any bugs or failures to follow the user instruction. In your answer, give me the full revised code."
                    prompt_conversation_history.append({"role": "user", "content": reflection_prompt})
                    revised_response_content, revised_response_time = chat(model, reflection_prompt, prompt_conversation_history)
                    prompt_conversation_history.append({"role": "assistant", "content": revised_response_content})

                    revised_code_block = re.search(r'```python\n(.*?)\n```', revised_response_content, re.DOTALL)

                    if revised_code_block:
                        code = revised_code_block.group(1)

                    response_time += revised_response_time
                    num_reflections += 1

            row_data[f'Generated Code {i+1}'] = code
            row_data[f'Execution Result {i+1}'] = execution_result
            row_data[f'Response Time {i+1} (s)'] = response_time
            row_data[f'Number of Reflections {i+1}'] = num_reflections
            conversation_history = prompt_conversation_history.copy()

        row_data['Conversation History'] = str(conversation_history)
        results.append(row_data)

    df = pd.DataFrame(results)
    df.to_excel(f'Results_{model}.xlsx', index=False)

    return df


def run_code(code):
    """

    Run the provided code in a separate Python process and return the execution result and error message (if any).



    Args:

        code (str): The code to be executed.



    Returns:

        tuple: A tuple containing the execution result (0 for failure, 1 for success) and the error message (if any).

    """
    try:
        with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.py') as temp_file:
            temp_file.write(code)
            temp_file_path = temp_file.name
        
        result = subprocess.run(['python', temp_file_path], capture_output=True, text=True)
        os.unlink(temp_file_path)

        if result.returncode == 0:
            return 1, None
        else:
            return 0, result.stderr
    except Exception as e:
        error_message = traceback.format_exc()
        os.unlink(temp_file_path)
        return 0, error_message


def chat(model, user_prompt, conversation_history, max_retries=3):
    """

    Helper function to chat with the specified model and handle retries.



    Args:

        model (str): The model to use for code generation.

        user_prompt (str): The prompt for generating the Python code.

        conversation_history (list): The history of the conversation.

        max_retries (int): The maximum number of retries if an error occurs.



    Returns:

        tuple: A tuple containing the response content and response time.

    """
    retry_count = 0
    pre_prompt = "You are a helpful coding assistant who always writes detailed and executable code without human implementation."

    while retry_count < max_retries:
        try:
            start_time = time.time()
            if model.startswith('claude'):
                client = anthropic.Anthropic(api_key=anthropic_key)
                response = client.completions.create(
                    model=model,
                    prompt=conversation_history + [{"role": "user", "content": user_prompt}],
                    max_tokens=4096
                )
                response_content = response["completion"]
            elif model.startswith('gpt'):
                response = openai.ChatCompletion.create(
                    model=model,
                    messages=[{"role": "system", "content": pre_prompt}] + conversation_history + [{"role": "user", "content": user_prompt}]
                )
                response_content = response.choices[0].message["content"]
            elif model.startswith('o'):
                response = openai.ChatCompletion.create(
                    model=model,
                    messages= [
                        *conversation_history,
                        {"role": "user", "content": user_prompt}
                    ]
                )
                response_content = response.choices[0].message["content"]
            elif model.startswith('llama'):
                formatted_history = "\n".join([f"{msg['role']}: {msg['content']}" for msg in conversation_history])
                model_name = "meta/llama-2-70b-chat" if model == "llama-2" else "meta/codellama-34b-instruct:eeb928567781f4e90d2aba57a51baef235de53f907c214a4ab42adabf5bb9736"
                response = replicate.run(model_name, input={"prompt": f"{formatted_history}User: {user_prompt}\nAssistant:"})
                response_content = ''.join(response)

            response_time = time.time() - start_time
            return response_content, response_time
        except Exception as e:
            retry_count += 1
            if retry_count == max_retries:
                return f"Error: {str(e)}", 0

def process_excel_files(file_names):
    summary_data = []
    
    for file_name in file_names:
        # Check if the "acc_" file already exists
        output_file_name = "acc_" + file_name
        if os.path.isfile(output_file_name):
            # If the file exists, read it instead of the original file
            df = pd.read_excel(output_file_name)
        else:
            # If the file doesn't exist, read the original file
            df = pd.read_excel(file_name)
            
            # Create a new column "Performance" and initialize it with an empty string
            df["Performance"] = ""
            
            # Filter rows where "Execution Result 1" is 1
            filtered_df = df[df["Execution Result 1"] == 1]
            
            total_rows = len(df)
            print(f"Processing {file_name} with {total_rows} rows")
            
            # Iterate over the filtered rows
            for index, row in filtered_df.iterrows():
                print(f"Processing row {index + 1}/{total_rows}")
                code = row["Generated Code 1"]
                
                # Create a temporary file to write the code
                with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.py') as temp_file:
                    temp_file.write(code)
                    temp_file_path = temp_file.name
                
                try:
                    # Execute the code using subprocess
                    result = subprocess.run(['python', temp_file_path], capture_output=True, text=True, timeout=100)
                    os.unlink(temp_file_path)
                    
                    if result.returncode == 0:
                        # Code executed successfully
                        output = result.stdout.split('\n')
                        
                        # Find the accuracy value from the captured output
                        accuracy_values = []
                        for line in output:
                            match = re.search(r"accuracy:\s*(\d+(\.\d+)?)", line, re.IGNORECASE)
                            if match:
                                accuracy_values.append(float(match.group(1)))
                        
                        # Update the "Performance" column with the highest accuracy value
                        if accuracy_values:
                            df.at[index, "Performance"] = max(accuracy_values)
                    else:
                        # Code execution encountered an error
                        print(f"Error executing code at index {index} in file {file_name}: {result.stderr}")
                except subprocess.TimeoutExpired:
                    print(f"Error: Code execution at index {index} in file {file_name} exceeded time limit and was terminated.")
                    os.unlink(temp_file_path)

    
                except Exception as e:
                    # Error occurred while executing the code
                    error_message = traceback.format_exc()
                    os.unlink(temp_file_path)
                    print(f"Error executing code at index {index} in file {file_name}: {error_message}")
                    
            
            # Save the updated DataFrame back to the Excel file
            df.to_excel(output_file_name, index=False)
        
        # Generate summary data for the current file
        model_name = file_name.split("_")[1]
        total_rows = len(df)
        executed_rows = len(df[df["Execution Result 1"] == 1])
        
        # Count rows where "Number of Reflections 1" is 0 within the filtered DataFrame
        one_conversation_rows = len(df[(df["Execution Result 1"] == 1) & (df["Number of Reflections 1"] == 0)])
        
        # Handle empty or non-numeric values in the "Performance" column
        avg_accuracy = pd.to_numeric(df["Performance"], errors='coerce').mean()
        
        # Calculate the average time considering the "Number of Reflections 1"
        df["Adjusted Time"] = df["Response Time 1 (s)"] / (df["Number of Reflections 1"] + 1)
        avg_time = df["Adjusted Time"].mean()
        
        correctness = len(df[pd.to_numeric(df["Performance"], errors='coerce') > 0.85]) /  total_rows #executed_rows
        
        # Calculate the average code length
        code_length = df["Generated Code 1"].apply(lambda x: len(str(x).split())).mean()
        
        summary_row = {
            "Model Name": model_name,
            "Avg Time": avg_time,
            "Code Length": code_length,
            "Code Executability (one conversation)": one_conversation_rows / total_rows,
            "Code Executability (with reflection)": executed_rows / total_rows,
            "Correctness": correctness,
            "Avg Accuracy of ML Models": avg_accuracy
        }
        
        summary_data.append(summary_row)
    
    # Create a summary DataFrame from the summary data
    summary_df = pd.DataFrame(summary_data)
    
    # Reorder the columns
    column_order = ["Model Name", "Avg Time", "Code Length", "Code Executability (one conversation)",
                    "Code Executability (with reflection)", "Correctness", "Avg Accuracy of ML Models"]
    summary_df = summary_df[column_order]
    
    # Save the summary DataFrame to an Excel file
    summary_file_name = "summary.xlsx"
    summary_df.to_excel(summary_file_name, index=False)
    
    return summary_file_name

def process_and_summarize_results():
    """

    This function processes all generated Excel files (starting with "Results_") in the current folder, 

    calculates performance metrics for each file, and summarizes the results in a new Excel file.

    

    The function evaluates the accuracy of machine learning models by reading the generated code from the 

    "Generated Code 1" column, executing it, and extracting the accuracy from the output. The performance 

    and other statistics are summarized and saved to an "acc_" prefixed Excel file for each individual 

    result and a "summary.xlsx" file for the overall summary.

    """

    # Automatically collect all "Results_xxxxxxx.xlsx" files in the current directory
    file_names = glob.glob("Results_*.xlsx")

    # Ensure there are files to process
    if not file_names:
        print("No 'Results_.xlsx' files found for processing.")
        return
    
    # Call the existing process_excel_files function to process and evaluate each file
    summary_file = process_excel_files(file_names)
    
    print(f"Processing complete. Summary file generated: {summary_file}")
    
# Main Execution
if __name__ == "__main__":
    # Ask for model and API keys
    model, api_keys = get_model_and_api_keys()

    # Example prompts for ML code generation
    # Load the prompt from combined JSON file
    with open("prompts.json", "r") as json_file:
        prompt_data = json.load(json_file)

    # Extract user prompts for file1
    user_prompts = prompt_data["ml_prompts"]

    
    # Run the code generation and execution process
    generate_and_execute_code(user_prompts, model=model, num_calls=100, max_reflection=2)
    
    #Process and summarize the generated results
    process_and_summarize_results()