File size: 23,480 Bytes
5e35da7
 
 
 
 
 
023005c
 
 
 
 
b0fa2a5
023005c
 
4c49e30
 
023005c
 
 
 
 
b0fa2a5
023005c
 
 
b0fa2a5
 
 
 
 
 
 
 
 
 
 
023005c
5e35da7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
583938d
5e35da7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
023005c
 
 
5e35da7
 
 
 
 
 
 
 
 
 
d7d564a
 
 
 
 
 
 
 
 
 
 
5e35da7
d7d564a
 
 
 
fa0da9f
 
d7d564a
fa0da9f
 
 
 
 
d7d564a
 
 
fa0da9f
5e35da7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d7d564a
 
fa0da9f
5e35da7
fa0da9f
5e35da7
d7d564a
 
fa0da9f
 
 
 
 
 
c0083b8
 
 
 
 
 
 
fa0da9f
 
 
 
 
5e35da7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa0da9f
 
c0083b8
 
 
fa0da9f
c0083b8
 
 
fa0da9f
c0083b8
 
 
 
fa0da9f
c0083b8
5c8d948
c0083b8
 
 
 
 
 
 
 
 
 
fa0da9f
c0083b8
5e35da7
 
c0083b8
27d74bc
 
 
 
 
c0083b8
fa0da9f
 
c0083b8
 
 
 
 
 
 
 
 
5c8d948
c0083b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c8d948
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e35da7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
import streamlit as st
import pandas as pd
import os
import subprocess
import time

# Lazy imports - only load when actually needed (saves 5-10 seconds on startup)
def _lazy_import_llm_libs():
    """Import heavy LLM libraries only when needed"""
    global ChatOpenAI, ChatPromptTemplate, SystemMessagePromptTemplate
    global AIMessagePromptTemplate, HumanMessagePromptTemplate
    global SimpleDirectoryReader, VectorStoreIndex
    global LlamaCPP, messages_to_prompt, completion_to_prompt
    
    from langchain_community.chat_models import ChatOpenAI
    from langchain_core.prompts.chat import (
        ChatPromptTemplate,
        SystemMessagePromptTemplate,
        AIMessagePromptTemplate,
        HumanMessagePromptTemplate,
    )
    from llama_index.core import (
        SimpleDirectoryReader,
        VectorStoreIndex,
    )
    from llama_index.llms.llama_cpp import LlamaCPP
    
    # Try to import prompt utilities (may not exist in newer versions)
    try:
        from llama_index.llms.llama_cpp.llama_utils import (
            messages_to_prompt,
            completion_to_prompt,
        )
    except ImportError:
        messages_to_prompt = None
        completion_to_prompt = None

# set version
# st.session_state.demo_lite = False

# initialize model
# llm = "tbd"

print("BP 4 ")


# GPU detection and environment configuration
def detect_gpu_and_environment():
    """
    Detect if GPU is available and if running on HuggingFace Spaces
    Returns: dict with gpu_available, is_hf_space, and n_gpu_layers
    """
    config = {
        "gpu_available": False,
        "is_hf_space": False,
        "n_gpu_layers": 0,
        "model_base_path": "/Users/dheym/Library/CloudStorage/OneDrive-Personal/Documents/side_projects/GRDN/src/models"
    }
    
    # Check if running on HuggingFace Spaces
    if os.environ.get("SPACE_ID") or os.environ.get("SPACE_AUTHOR_NAME"):
        config["is_hf_space"] = True
        config["model_base_path"] = "/home/user/app/src/models"  # HF Spaces absolute path
        print("๐Ÿค— Running on HuggingFace Spaces")
    
    # Try to detect GPU using torch
    try:
        import torch
        if torch.cuda.is_available():
            config["gpu_available"] = True
            gpu_name = torch.cuda.get_device_name(0)
            gpu_memory = torch.cuda.get_device_properties(0).total_memory / 1e9
            config["n_gpu_layers"] = -1  # -1 means offload all layers to GPU
            print(f"๐Ÿš€ GPU detected: {gpu_name} with {gpu_memory:.2f} GB memory")
            print(f"๐Ÿš€ Will offload all layers to GPU (n_gpu_layers=-1)")
        else:
            print("โš ๏ธ No GPU detected via torch.cuda")
            config["n_gpu_layers"] = 0
    except ImportError:
        print("โš ๏ธ torch not available, checking alternative methods...")
        # Alternative: check nvidia-smi or environment variables
        if os.path.exists("/usr/bin/nvidia-smi") or os.environ.get("CUDA_VISIBLE_DEVICES"):
            config["gpu_available"] = True
            config["n_gpu_layers"] = -1  # Offload all layers
            print("๐Ÿš€ GPU likely available (nvidia-smi or CUDA env detected)")
        else:
            config["n_gpu_layers"] = 0
    
    # If on HF Spaces but GPU not detected via torch, still try GPU layers
    if config["is_hf_space"] and not config["gpu_available"]:
        print("๐Ÿค— On HF Spaces - attempting GPU acceleration anyway")
        config["gpu_available"] = True
        config["n_gpu_layers"] = -1
    
    return config


# initialize model- get llm depending on st.session_state.demo_lite, and model
def init_llm(model, demo_lite):
    # st.write("BP 4.1: model: ", model)
    if demo_lite == False:
        print("BP 5 : running full demo")
        
        # Load heavy LLM libraries now (lazy import)
        _lazy_import_llm_libs()
        
        # Detect GPU and environment
        env_config = detect_gpu_and_environment()
        n_gpu_layers = env_config["n_gpu_layers"]
        model_base_path = env_config["model_base_path"]
        
        if env_config["gpu_available"]:
            print(f"โœ… GPU acceleration ENABLED with {n_gpu_layers} layers")
        else:
            print("โš ๏ธ Running on CPU (no GPU detected)")
        
        # Only Llama 3.2-1B is supported (legacy models removed for simplicity)
        model_path = os.path.join(model_base_path, "Llama-3.2-1B-Instruct-Q4_K_M.gguf")
        print(f"Loading Llama 3.2-1B from: {model_path}")
        
        # Check if model exists
        if not os.path.exists(model_path):
            error_msg = f"โš ๏ธ Model not found at {model_path}"
            if env_config["is_hf_space"]:
                error_msg += ". Please ensure the model file is uploaded to your HuggingFace Space."
            st.error(error_msg)
            print(f"โŒ {error_msg}")
            return None
        
        # Initialize Llama 3.2-1B with GPU support
        llm = LlamaCPP(
            model_path=model_path,
            temperature=0.3,  # Slightly higher for more variety
            max_new_tokens=800,  # Limit to prevent infinite generation
            context_window=8192,  # Llama 3.2 supports 128K context
            generate_kwargs={
                "top_p": 0.95,
                "top_k": 40,
                "repeat_penalty": 1.2,  # Penalize repetition
            },
            model_kwargs={"n_gpu_layers": n_gpu_layers},
            verbose=True,
        )
        print(f"LLM initialized with GPU layers: {n_gpu_layers}")
        return llm


def parse_and_evaluate_text(text):
    # Find the indices of the opening and closing brackets
    opening_bracket_index = text.find("[")
    closing_bracket_index = text.find("]")

    if opening_bracket_index != -1 and closing_bracket_index != -1:
        # Extract the text within the brackets
        extracted_list = (
            "[" + text[opening_bracket_index + 1 : closing_bracket_index] + "]"
        )
        # Return the evaluated text list
        return eval(extracted_list)

    else:
        print("Error with parsing plant list")
        return None


def chat_response(template, prompt_text, model, demo_lite):
    if model == "openai-gpt35turbo":
        chat = ChatOpenAI(temperature=0.1)
        system_message_prompt = SystemMessagePromptTemplate.from_template(template)
        human_template = "{text}"
        human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
        chat_prompt = ChatPromptTemplate.from_messages(
            [system_message_prompt, human_message_prompt]
        )
        response = chat(chat_prompt.format_prompt(text=prompt_text).to_messages())

        return response
        # return response.content
    else:
        # Use Llama 3.2-1B (only supported model)
        print("Using Llama 3.2-1B for chat")
        if "llm" not in st.session_state:
            print("Initializing LLM...")
            st.session_state.llm = init_llm(model, demo_lite)
        if st.session_state.llm is None:
            return "Error: Could not initialize LLM. Please check the logs."
        
        # Add timeout and max tokens to prevent infinite generation
        full_prompt = template + "\n\n" + prompt_text
        print(f"LLM prompt length: {len(full_prompt)} chars")
        
        try:
            # Use stricter generation parameters to reduce fluff
            response = st.session_state.llm.complete(
                full_prompt, 
                max_tokens=600,  # Reduced from 800 to force conciseness
                temperature=0.1,  # Lower temperature for more focused output
                top_p=0.9,  # Slightly lower for less randomness
            )
            print(f"LLM response length: {len(response.text)} chars")
            return response.text
        except Exception as e:
            print(f"Error during LLM completion: {e}")
            return f"Error generating response: {str(e)}"


# # get the plant list from user input
# def get_plant_list(input_plant_text, model):
#     template="You are a helpful assistant that knows all about gardening and plants and python data structures."
#     text = 'which of the elements of this list can be grown in a garden, [' + input_plant_text + ']? Return JUST a python list object containing the elements that can be grown in a garden. Do not include any other text or explanation.'
#     plant_list_text = chat_response(template, text, model)
#     plant_list = parse_and_evaluate_text(plant_list_text.content)
#     print(plant_list)
#     return plant_list


# get plant care tips based on plant list
def get_plant_care_tips(plant_list, model, demo_lite):
    plant_care_tips = ""
    
    # Create a clean, comma-separated list of plants
    plant_names = ", ".join(str(p) for p in st.session_state.input_plants_raw[:6])  # Limit to first 6 plants for conciseness
    if len(st.session_state.input_plants_raw) > 6:
        plant_names += f" (and {len(st.session_state.input_plants_raw) - 6} more)"
    
    # Very strict prompt with clear example - no fluff allowed
    template = "You are a gardening expert. Follow the format exactly. No extra text."
    text = f"""Plants: {plant_names}

RULES:
- Use EXACTLY this format for each plant
- NO introductions, NO conclusions, NO "Next plant", NO "I hope"
- Just plant name, then 4 lines of info

FORMAT EXAMPLE:
Tomatoes
Sunlight: Full sun (6-8 hours daily)
Water: Deep soak twice weekly
Zones: 5-9
Tip: Support with stakes or cages

Carrots
Sunlight: Full sun (6 hours minimum)
Water: Light watering every 3 days
Zones: 3-10
Tip: Thin seedlings to 2 inches apart

YOUR TURN - provide tips for the plants above using EXACTLY this format:"""

    plant_care_tips = chat_response(template, text, model, demo_lite)
    print("Plant care tips RAW response:", plant_care_tips[:200])
    
    # Safety check for None response
    if plant_care_tips is None:
        return "Error: Could not generate plant care tips. Please try again or select a different model."
    
    # AGGRESSIVE CLEANING - remove all unwanted text
    plant_care_tips = plant_care_tips.strip()
    
    # Remove common unwanted phrases (case-insensitive)
    unwanted_phrases = [
        "Keep it concise", "Keep it BRIEF", "I hope these tips are helpful",
        "I hope this helps", "hope this is helpful", "Next plant:",
        "Lastly:", "Last but not least", "Here are", "Here's",
        "Do NOT repeat yourself", "Do NOT add extra headers",
        "Just the plant tips", "Start immediately",
        "YOUR TURN", "RULES:", "FORMAT EXAMPLE:",
        "Plants:", "provide tips for"
    ]
    
    import re
    for phrase in unwanted_phrases:
        # Remove case-insensitive
        plant_care_tips = re.sub(re.escape(phrase), "", plant_care_tips, flags=re.IGNORECASE)
    
    # Remove any lines that start with common unwanted patterns
    lines = plant_care_tips.split('\n')
    cleaned_lines = []
    for line in lines:
        line_stripped = line.strip()
        # Skip empty lines or lines with unwanted patterns
        if not line_stripped:
            continue
        if line_stripped.lower().startswith(('i hope', 'here are', 'here is', 'next plant', 'lastly', 'last but')):
            continue
        if 'helpful' in line_stripped.lower() and len(line_stripped) < 50:
            continue
        cleaned_lines.append(line)
    
    plant_care_tips = '\n'.join(cleaned_lines).strip()
    
    # Bold the plant names by detecting lines that are likely plant names
    # (lines with no colons that come before lines with colons)
    # Use HTML <strong> tags since we'll be displaying in an HTML div
    lines = plant_care_tips.split('\n')
    formatted_lines = []
    for i, line in enumerate(lines):
        line = line.strip()
        if not line:
            formatted_lines.append('<br>')
            continue
        
        # If this line has no colon and the next line has a colon, it's likely a plant name
        if ':' not in line and i + 1 < len(lines) and ':' in lines[i + 1]:
            # Bold the plant name with HTML
            formatted_lines.append(f"<strong style='color: #20B2AA; font-size: 1.1em;'>{line}</strong>")
        else:
            formatted_lines.append(line)
    
    plant_care_tips = '<br>'.join(formatted_lines)
    
    return plant_care_tips


# get compatability matrix for companion planting
def get_compatibility_matrix(plant_list, model, demo_lite):
    # Convert the compatibility matrix to a string
    with open("data/compatibilities_text.txt", "r") as file:
        # Read the contents of the file
        compatibility_text = file.read()
    plant_comp_context = compatibility_text
    template = "You are a helpful assistant that knows all about gardening, companion planting, and python data structures- specifically compatibility matrices."
    text = (
        "from this list of plants, ["
        + str(plant_list)
        + "], Return JUST a python array (with values separated by commas like this: [[0,1],[1,0]]\n\n ) for companion plant compatibility. Each row and column should represent plants, and the element of the array will contain a -1, 0, or 1 depending on if the relationship between plants is antagonists, neutral, or companions, respectively. You must refer to this knowledge base of information on plant compatibility: \n\n, "
        + plant_comp_context
        + "\n\n A plant's compatibility with itself is always 0. Do not include any other text or explanation."
    )
    compatibility_mat = chat_response(template, text, model, demo_lite)

    # Find the indices of the opening and closing brackets
    opening_bracket_index = compatibility_mat.content.find("[[")
    closing_bracket_index = compatibility_mat.content.find("]]")
    if opening_bracket_index != -1 and closing_bracket_index != -1:
        # Extract the text within the brackets
        extracted_mat = (
            "["
            + compatibility_mat.content[
                opening_bracket_index + 1 : closing_bracket_index
            ]
            + "]]"
        )
        # Return the evaluated mat
        # check to see if compatiblity matrix only contains values of -1, 0, or 1
        if eval(extracted_mat).count("0") + eval(extracted_mat).count("1") == len(
            eval(extracted_mat)
        ):
            # continue
            pass
        else:
            # try again up to 5 times
            for i in range(5):
                print(
                    "Error with parsing plant compatibility matrix. Trying for attempt #"
                    + str(i + 1)
                )
                print(extracted_mat)
                extracted_mat = chat_response(
                    template
                    + "remember, it MUST ONLY CONTAIN -1s, 0s, and 1s, like this structure: [[0,1],[1,0]]",
                    text,
                    model,
                    demo_lite,
                )
                # Extract the text within the brackets
                extracted_mat = (
                    "["
                    + compatibility_mat.content[
                        opening_bracket_index + 1 : closing_bracket_index
                    ]
                    + "]]"
                )
                print(extracted_mat)
                total_count = 0
                count_0 = extracted_mat.count("0")
                count_1 = extracted_mat.count("1")
                total_count = count_0 + count_1
                print("matrix count of -1, 0, 1: ", total_count)
                # if count euals the number of plants squared, then we have a valid matrix
                print("plant_list_len: ", len(plant_list) ** 2)
                if total_count == (len(plant_list)) ** 2:
                    # if count == eval(extracted_mat):
                    print("success")
                    return eval(extracted_mat)
                    break

    else:
        print("Error with parsing plant compatibility matrix")
        # try again up to 5 times
        for i in range(5):
            print(
                "Error with parsing plant compatibility matrix. Trying for attempt #"
                + str(i + 1)
            )
            extracted_mat = chat_response(
                template
                + "remember, it MUST ONLY CONTAIN -1s, 0s, and 1s, like this structure: [[0,1],[1,0]]",
                text,
                model,
                demo_lite,
            )
            # Extract the text within the brackets
            extracted_mat = (
                "["
                + compatibility_mat.content[
                    opening_bracket_index + 1 : closing_bracket_index
                ]
                + "]]"
            )
            print(extracted_mat)
            total_count = 0
            count_0 = extracted_mat.count("0")
            count_1 = extracted_mat.count("1")
            total_count = count_0 + count_1
            print("matrix count of -1, 0, 1: ", total_count)
            # if count euals the number of plants squared, then we have a valid matrix
            print("plant_list_len: ", len(plant_list) ** 2)
            if total_count == (len(plant_list)) ** 2:
                # if count == eval(extracted_mat):
                print("success")
                return eval(extracted_mat)
                break

        return None


# get compatability matrix for companion planting via subsetting a hardcoded matrix
# make plant_compatibility.csv into a matrix. it currently has indexes as rows and columns for plant names and then compatibility values as the values
plant_compatibility = pd.read_csv("src/data/plant_compatibility.csv", index_col=0)


def get_compatibility_matrix_2(plant_list):
    # Subset the matrix to only include the plants in the user's list
    plant_compatibility = st.session_state.raw_plant_compatibility.loc[
        plant_list, plant_list
    ]

    # full matrix
    full_mat = st.session_state.raw_plant_compatibility.to_numpy()

    # Convert the DataFrame to a NumPy array
    plant_compatibility_matrix = plant_compatibility.to_numpy()

    # Get the list of original indices (from the DataFrame)
    original_indices = plant_compatibility.index.tolist()

    # Create a dictionary to map plant names to their original indices
    plant_index_mapping = {plant: index for index, plant in enumerate(original_indices)}

    # Return the matrix and the plant-index mapping
    return plant_compatibility_matrix, full_mat, plant_index_mapping


# get plant groupings from LLM
def get_seed_groupings_from_LLM(model, demo_lite):
    plant_groupings_evaluated = "no response yet"
    if demo_lite:
        # just return "no response yet" for now
        return plant_groupings_evaluated
    template = "You are a helpful assistant that only outputs python lists of lists of lists of plants."
    # make sure output is strictly and only a list of lists for one grouping
    text = (
        """I am working on a gardening project and need to optimally group a set of plants based on their compatibility. Below is the compatibility matrix for the plants, where each value represents how well two plants grow together (positive values indicate good compatibility, negative values indicate poor compatibility). I also have specific constraints for planting: there are a certain number of plant beds (n_plant_beds), each bed can have a minimum of min_species species and a maximum of max_species species. Given these constraints, please suggest several groupings of these plants into n_plant_beds beds, optimizing for overall compatibility.

        Number of Plant Beds: """
        + str(st.session_state.n_plant_beds)
        + """
        Minimum Species per Bed: """
        + str(st.session_state.min_species)
        + """
        Maximum Species per Bed: """
        + str(st.session_state.max_species)
        + """
        Plants and Compatibility Matrix:"""
        + str(
            st.session_state.raw_plant_compatibility.loc[
                st.session_state.input_plants_raw, st.session_state.input_plants_raw
            ]
        )
        + """

        Please provide a grouping that maximize positive interactions within each bed and minimize negative interactions, adhering to the specified bed constraints. Return a list of lists  where each list represents an iteration of plant groupings. Each list within the list represents a bed, and each list within the bed represents the plants in that bed.
        sample output: [['plant1', 'plant2'] #bed1, ['plant3', 'plant4'] #bed2, ['plant1', 'plant3'] #bed3]
        another sample output: [['plant1', 'plant2', 'plant3'] #bed1, ['plant4', 'plant5', 'plant6'] #bed2, ['plant7', 'plant8', 'plant9'] #bed3]
        Note: the number of beds, the number of plants per bed, and the number of plants in the list may vary.
        Note: only output ONE python list of lists of plants. Do not include any other text or explanation.

        """
    )

    plant_groupings = chat_response(template, text, model, demo_lite)
    # check to see if we've cut off the response due to time limit. if so, return "no response yet" for now
    if plant_groupings == None:
        return "no response yet"
    print("response about LLMs choice on groupings", plant_groupings)

    # try to eval the string to a list of lists
    try:
        plant_groupings_evaluated = eval(plant_groupings)
        # check type of output
        print(type(plant_groupings_evaluated))
        # we expect a list of lists
    except:
        print("Error with parsing plant groupings")
        # try again up to 5 times
        for i in range(5):
            print(
                "Error with parsing plant groupings. Trying for attempt #" + str(i + 1)
            )
            plant_groupings = chat_response(template, text, model, demo_lite)
            print(plant_groupings)
            # try to eval the string to a list of lists
            try:
                # make sure plant1 is not in the output
                if "plant1" in plant_groupings.lower():
                    print("plant1 is in the output")
                    continue
                else:
                    plant_groupings_evaluated = eval(plant_groupings)
                    print("successful eval; output: ", plant_groupings_evaluated)
                    break
            except:
                # try to find the list of lists within the string
                opening_bracket_index = plant_groupings.find("[[")
                closing_bracket_index = plant_groupings.find("]]")
                if opening_bracket_index != -1 and closing_bracket_index != -1:
                    # Extract the text within the brackets
                    extracted_list = (
                        "["
                        + plant_groupings[
                            opening_bracket_index + 1 : closing_bracket_index
                        ]
                        + "]]"
                    )
                    # Return the evaluated text list
                    if "plant1" in extracted_list.lower():
                        print("plant1 is in the output")
                        continue
                    else:
                        plant_groupings_evaluated = eval(extracted_list)
                        print("successful eval; output: ", plant_groupings_evaluated)
                        break
                else:
                    print("Error with parsing plant groupings")
                    continue

    return plant_groupings_evaluated