File size: 32,962 Bytes
f7bad94
7a56e2a
 
f7bad94
7a56e2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63c9deb
7a56e2a
63c9deb
7a56e2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63c9deb
7a56e2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63c9deb
7a56e2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63c9deb
7a56e2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63c9deb
7a56e2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63c9deb
 
7a56e2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63c9deb
7a56e2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63c9deb
7a56e2a
 
63c9deb
7a56e2a
 
63c9deb
7a56e2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63c9deb
 
7a56e2a
 
 
63c9deb
7a56e2a
 
 
 
 
 
 
63c9deb
7a56e2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63c9deb
7a56e2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63c9deb
7a56e2a
 
 
 
 
 
 
 
 
 
 
 
 
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
# hello world
import os
# https://stackoverflow.com/questions/76175046/how-to-add-prompt-to-langchain-conversationalretrievalchain-chat-over-docs-with
# again from:
# https://python.langchain.com/docs/integrations/providers/vectara/vectara_chat
from langchain.document_loaders import PyPDFDirectoryLoader
import pandas as pd
import langchain
from queue import Queue
from typing import Any
from langchain.llms.huggingface_text_gen_inference import HuggingFaceTextGenInference
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
from langchain.schema import LLMResult
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.vectorstores import FAISS
from langchain.prompts.prompt import PromptTemplate
from anyio.from_thread import start_blocking_portal #For model callback streaming


from langchain.prompts import SystemMessagePromptTemplate, HumanMessagePromptTemplate, ChatPromptTemplate
import os
from dotenv import load_dotenv

import streamlit as st
import json
from langchain.document_loaders import PyPDFLoader
from langchain.text_splitter import CharacterTextSplitter
from langchain.embeddings import OpenAIEmbeddings
from langchain.chains.question_answering import load_qa_chain
from langchain.chat_models import ChatOpenAI
# from langchain.chat_models import ChatAnthropic
from langchain_anthropic import ChatAnthropic
from langchain.vectorstores import Chroma
import chromadb

from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.llms import OpenAI
from langchain.chains import RetrievalQA
from langchain.document_loaders import TextLoader
from langchain.document_loaders import DirectoryLoader
from langchain_community.document_loaders import PyMuPDFLoader
from langchain.schema import Document

from langchain.memory import ConversationBufferMemory

from langchain.chains.conversational_retrieval.prompts import CONDENSE_QUESTION_PROMPT
from langchain.chains.conversational_retrieval.prompts import QA_PROMPT
import gradio as gr
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationalRetrievalChain
from langchain.chains import ConversationChain
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
print("Started")

# Function to map UI region selection to DB metadata region
# def get_db_region(ui_region: str) -> str:
#     """Maps UI region selection (e.g., 'Iowa') to the region string stored in metadata (e.g., 'United States')."""
#     if ui_region == "Iowa":
#         return "United States"
#     # Add more mappings if needed (e.g., Africa)
#     return ui_region # Default to using the UI string if no specific mapping

def get_species_list_from_db(db_name):
    embedding = OpenAIEmbeddings()
    vectordb_temp = Chroma(persist_directory=db_name, 
                  embedding_function=embedding)
    species_list=[]
    for meta in vectordb_temp.get()["metadatas"] :
        try:
            matched_first_species = meta['matched_specie_0']
        except KeyError:
            continue
        # Since each document is considered as a single chunk, the chunk_index is 0 for all
        species_list.append( matched_first_species)

    return species_list



# default_persist_directory = './db5' # For deployement
default_persist_directory='./vector-databases-deployed/db5-agllm-data-isu-field-insects-all-species'

species_list=get_species_list_from_db(default_persist_directory)
# default_persist_directory = 'vector-databases/db5-pre-completion' # For Development
csv_filepath1 = "./agllm-data/corrected/Corrected_supplemented-insect_data-2500-sorted.xlsx"
csv_filepath2 = "./agllm-data/corrected/Corrected_supplemented-insect_data-remaining.xlsx"

# India data
cv_india="./agllm-data/india/species.csv"
model_name=4
max_tokens=400
system_message = {"role": "system", "content": "You are a helpful assistant."} # TODO: double check how this plays out later. 
langchain.debug=False # TODO: DOUBLE CHECK
from langchain import globals
globals.set_debug(False)

retriever_k_value=3
embedding = OpenAIEmbeddings()
print("Started....")
class ChatOpenRouter(ChatOpenAI):
    openai_api_base: str
    openai_api_key: str
    model_name: str

    def __init__(self,
                 model_name: str,
                 openai_api_key: [str] = None,
                 openai_api_base: str = "https://openrouter.ai/api/v1",
                 **kwargs):
        openai_api_key = openai_api_key or os.getenv('OPENROUTER_API_KEY')
        super().__init__(openai_api_base=openai_api_base,
                         openai_api_key=openai_api_key,
                         model_name=model_name, **kwargs)


######### todo: skipping the first step



# print(# Single example
# vectordb.as_retriever(k=2, search_kwargs={"filter": {"matched_specie_0": "Hypagyrtis unipunctata"}, 'k':1}).get_relevant_documents(
#     "Checking if retriever is correctly initalized?"
# ))

columns = ['species', 'common name', 'order', 'family',
           'genus', 'Updated role in ecosystem', 'Proof',
           'ipm strategies', 'size of insect', 'geographical spread',
           'life cycle specifics', 'pest for plant species', 'species status',
           'distribution area', 'appearance', 'identification']

df1 = pd.read_excel(csv_filepath1, usecols=columns)
df2 = pd.read_excel(csv_filepath2, usecols=columns)
df_india = pd.read_csv(cv_india)
all_insects_data = pd.concat([df1, df2], ignore_index=True)

def get_prompt_with_vetted_info_from_specie_name(search_for_specie, mode):

    def read_and_format_filtered_csv_better(dataframe_given, insect_specie):
            filtered_data = dataframe_given[dataframe_given['species'] == insect_specie]
            formatted_data = ""
            # Format the filtered data
            for index, row in filtered_data.iterrows():
                    row_data = [f"{col}: {row[col]}" for col in filtered_data.columns]
                    formatted_row = "\n".join(row_data)
                    formatted_data += f"{formatted_row}\n"

            return formatted_data

            # Use the path to your CSV file here
            
    vetted_info=read_and_format_filtered_csv_better(all_insects_data, search_for_specie)
    india_info=read_and_format_filtered_csv_better(df_india, search_for_specie)
    

    if mode=="Farmer":
            language_constraint="The language should be acustomed to the Farmers. Given question is likely to be asked by a farmer in the field will ask which will help to make decisions which are immediate and practical."
    elif mode=="Researcher":
            language_constraint="The language should be acustomed to a researcher. Given question is likely to be asked by a scientist which are comprehensive and aimed at exploring new knowledge or refining existing methodologies"
    else:
            print("No  valid mode provided. Exiting")
            exit()
#    general_system_template = """ 
#    In every question you are provided information about the insect/weed. Two types of information are: First,  Vetted Information (which is same in every questinon) and Second, some context from external documents about an insect/weed species and a question by the user. answer the question according to these two types of informations. 
#    ---- 
#    Vetted info is as follows:
#    {vetted_info}
#    ----
#    The context retrieved for documents about this particular question is as follows:
#    {context}
#    ----
#    Additional Instruction:
#    1. Reference Constraint
#    At the end of each answer provide the source/reference for the given data in following format: 
#    \n\n[enter two new lines before writing below] References:
#    Vetted Information Used: Write what was used from the document for coming up with the answer above. Write exact part of lines. If nothing, write 'Nothing'. 
#    Documents Used: Write what was used from the document for coming up with the answer above. If nothing, write 'Nothing'. Write exact part of lines and document used. 
#    2. Information Constraint: 
#    Only answer the question from information provided otherwise say you dont know. You have to answer in 50 words including references. Prioritize information in documents/context over vetted information. And first mention the warnings/things to be careful about. 
#    3. Language constraint:
#    {language_constraint}

#    ----
#    """.format(vetted_info=vetted_info, language_constraint=language_constraint,context="{context}", )

    general_system_template = f""" 
    You are an AI assistant specialized in providing information about insects/weeds. Answer the user's question based on the available information or your general knowledge.

    The context retrieved for this question is as follows:
    {{context}}

    Instructions:
    1. Evaluate the relevance of the provided context to the question.
    2. If the context contains relevant information, use it to answer the question and explicitly mention "Based on provided information" in your source.
    3. If the context does not contain relevant information, use your general knowledge to answer the question and state "Based on general knowledge" as the source.
    4. Format your response as follows:
       Answer: Provide a concise answer in less than 50 words.
       Source: State either "Based on provided information" or "Based on general knowledge".

    5. Language constraint:
       {language_constraint}
    6. Other region (India) information:
        {india_info}
    7. So you have two kinds of information (default from Iowa and other region (India) information). First need to ask the user what region they are interested in. and only provide information from that region.
    8. When answering question, say what if the information is from what regiion. So, if a region is selected by user and specified in the question, then only answer based on that region and say so. 


    Question: {{question}}
    """

    general_user_template = "Question:```{question}```"
    messages_formatted = [
            SystemMessagePromptTemplate.from_template(general_system_template),
            HumanMessagePromptTemplate.from_template(general_user_template)
    ]
    qa_prompt = ChatPromptTemplate.from_messages( messages_formatted)
#    print(qa_prompt)
    return qa_prompt


    


# qa_prompt=get_prompt_with_vetted_info_from_specie_name("Papaipema nebris", "Researcher")
# print("First prompt is intialized as: " , qa_prompt, "\n\n")


memory = ConversationBufferMemory(memory_key="chat_history",output_key='answer', return_messages=True) # https://github.com/langchain-ai/langchain/issues/9394#issuecomment-1683538834


if model_name==4:
    llm_openai = ChatOpenAI(model_name="gpt-4o-2024-08-06" , temperature=0, max_tokens=max_tokens)  # TODO: NEW MODEL VERSION AVAILABLE 
else:
    llm_openai = ChatOpenAI(model_name="gpt-3.5-turbo-0125" , temperature=0, max_tokens=max_tokens)

specie_selector="Papaipema nebris"
filter = {
    "$or": [
        {"matched_specie_0": specie_selector},
        {"matched_specie_1": specie_selector},
        {"matched_specie_2": specie_selector},
    ]
}
# retriever = vectordb.as_retriever(search_kwargs={'k':retriever_k_value, 'filter': filter})

# qa_chain = ConversationalRetrievalChain.from_llm(
#     llm_openai, retriever, memory=memory, verbose=False, return_source_documents=True,\
#           combine_docs_chain_kwargs={'prompt': qa_prompt}

# )
# 

def initialize_qa_chain(specie_selector, application_mode, model_name, region, database_persistent_directory=default_persist_directory):
    # Add helper function for India info (kept for potential future use, but removed from RAG prompt)
    def read_and_format_filtered_csv_better(dataframe_given, insect_specie):
            filtered_data = dataframe_given[dataframe_given['species'] == insect_specie]
            formatted_data = ""
            # Format the filtered data
            for index, row in filtered_data.iterrows():
                    row_data = [f"{col}: {row[col]}" for col in filtered_data.columns]
                    formatted_row = "\n".join(row_data)
                    formatted_data += f"{formatted_row}\n"

            return formatted_data
    
    # Get India info (potentially useful if not using RAG or for specific logic)
    india_info = read_and_format_filtered_csv_better(df_india, specie_selector)
    # db_region = get_db_region(region) # Map UI region to DB region - REMOVED

    if model_name=="GPT-4":
        chosen_llm=ChatOpenAI(model_name="gpt-4o-2024-08-06" , temperature=0, max_tokens=max_tokens)
    elif model_name=="GPT-3.5":
        chosen_llm=ChatOpenAI(model_name="gpt-3.5-turbo-0125" , temperature=0, max_tokens=max_tokens)
    elif model_name=="Llama-3 70B":
        chosen_llm = ChatOpenRouter(model_name="meta-llama/llama-3-70b-instruct", temperature=0,max_tokens=max_tokens )
    elif model_name=="Llama-3 8B":
        chosen_llm = ChatOpenRouter(model_name="meta-llama/llama-3-8b-instruct", temperature=0, max_tokens=max_tokens)
    elif model_name=="Gemini-1.5 Pro":
        chosen_llm = ChatOpenRouter(model_name="google/gemini-pro-1.5", temperature=0, max_tokens=max_tokens)
    elif model_name=="Claude 3 Opus":
        chosen_llm = ChatAnthropic(model_name='claude-3-opus-20240229', temperature=0, max_tokens=max_tokens)
    elif model_name=="Claude 3.5 Sonnet":
        chosen_llm = ChatAnthropic(model_name='claude-3-5-sonnet-20240620', temperature=0, max_tokens=max_tokens)
    else:
        print("No appropriate llm was selected")  
        exit()
    
    if application_mode == "Farmer":
        language_constraint = "The language should be customized for Farmers. The given question is likely to be asked by a farmer in the field and will help to make decisions which are immediate and practical."
    elif application_mode == "Researcher":
        language_constraint = "The language should be customized for a researcher. The given question is likely to be asked by a scientist and should be comprehensive, aimed at exploring new knowledge or refining existing methodologies."
    else:
        print("No valid mode provided. Exiting") 
        exit()

    # RAG is always ON now
    memory = ConversationBufferMemory(memory_key="chat_history", output_key='answer', return_messages=True)
    
    # Construct the species filter part
    species_filter = {
        "$or": [
            {"matched_specie_" + str(i): specie_selector} for i in range(11) # Generate dynamically up to 10
        ]
    }
    
    embedding = OpenAIEmbeddings()
    vectordb = Chroma(persist_directory=database_persistent_directory,
                     embedding_function=embedding)
    
    # --- Find all available regions for this species --- #
    availability_message = f"Checking region availability for {specie_selector}..."
    available_regions = set()
    
    try:
        # Query ChromaDB just for metadata based on species
        species_docs = vectordb.get(where=species_filter, include=['metadatas'])
        if species_docs and species_docs.get('metadatas'):
            for meta in species_docs['metadatas']:
                if 'region' in meta:
                    available_regions.add(meta['region'])
        
        if available_regions:
            available_regions_list = sorted(list(available_regions))
            availability_message = f"Information for **{specie_selector}** is available in region(s): **{', '.join(available_regions_list)}**."
        else:
            available_regions_list = []
            availability_message = f"No regional information found for **{specie_selector}** in the database."
    except Exception as e:
        print(f"Error checking region availability: {e}")
        available_regions_list = []
        availability_message = f"Could not determine region availability for {specie_selector}."
    
    # --- Prepare context sections by region --- #
    # Dictionary to hold context documents for each region
    # region_contexts = {} # Unused variable, removing
    
    # First check if selected region has information
    selected_region_has_info = region in available_regions
    
    # Create list of other available regions (excluding selected region)
    other_regions = [r for r in available_regions_list if r != region]
    
    # --- Create multi-region retrieval chain --- #
    class MultiRegionRetriever:
        def __init__(self, vectordb, species_filter, selected_region, other_regions, k=3):
            self.vectordb = vectordb
            self.species_filter = species_filter
            self.selected_region = selected_region
            self.other_regions = other_regions
            self.k = k
        
        def get_relevant_documents(self, query):
            all_docs = []
            region_docs = {}
            
            # First get documents for selected region
            # Fix illogical condition: self.selected_region == self.selected_region is always True
            # Replace with a check if selected_region exists
            if self.selected_region:
                selected_filter = {"$and": [self.species_filter, {"region": self.selected_region}]}
                selected_retriever = self.vectordb.as_retriever(search_kwargs={'k': self.k, 'filter': selected_filter})
                try:
                    selected_docs = selected_retriever.get_relevant_documents(query)
                    if selected_docs:
                        all_docs.extend(selected_docs)
                        region_docs[self.selected_region] = selected_docs
                except Exception as e:
                    print(f"Error retrieving docs for selected region {self.selected_region}: {e}")
            
            # Then get documents for each other region
            for other_region in self.other_regions:
                if other_region != self.selected_region:  # Skip if same as selected region
                    other_filter = {"$and": [self.species_filter, {"region": other_region}]}
                    other_retriever = self.vectordb.as_retriever(search_kwargs={'k': self.k, 'filter': other_filter})
                    try:
                        other_docs = other_retriever.get_relevant_documents(query)
                        if other_docs:
                            all_docs.extend(other_docs)
                            region_docs[other_region] = other_docs
                    except Exception as e:
                        print(f"Error retrieving docs for region {other_region}: {e}")
            
            # Store the region-specific documents for formatting in the prompt
            self.last_region_docs = region_docs
            return all_docs
    
    # Initialize the multi-region retriever
    multi_region_retriever = MultiRegionRetriever(
        vectordb=vectordb,
        species_filter=species_filter,
        selected_region=region,
        other_regions=available_regions_list,
        k=retriever_k_value
    )
    
    # Custom prompt handler that formats context by region
    # Remove unused imports
    # from langchain.chains.combine_documents import create_stuff_documents_chain
    # from langchain.chains import create_retrieval_chain
    
    # Updated prompt template for multi-part response with region-specific contexts
    general_system_template = f"""
You are an AI assistant specialized in providing information about agricultural pests ({specie_selector}). The user is primarily interested in the '{region}' region.

The following context has been retrieved from a database organized by region:

{{context}}

Instructions:
1. Analyze the user's question in relation to {specie_selector}.
2. Structure your answer in the following multi-part format:

   **Part 1: Selected Region Information ({region})**
   If relevant information exists in the context for the selected region that answers the user's query:
   Based on your selected region ({region}), for {specie_selector}, [summary of information for selected region] [1].
   
   If no relevant information exists for the selected region:
   "Based on the provided documents, there is no specific information for {specie_selector} in your selected region ({region}) regarding your question."

   **Part 2: Other Regions Information** (Only include if information from other regions is available AND relevant to the query)
   If you found relevant information from other regions that answers the user's query, include:
   
   Additionally, information was found for other regions:
   - In [Other Region Name]: [summary of information that directly answers the user's query] [next reference number].
   - In [Another Region Name]: [summary of information that directly answers the user's query] [next reference number].
   
   Only include regions where the information directly addresses the user's question.
   Use consecutive reference numbers starting from where Part 1 left off.
   If no other regions have relevant information, omit this part entirely.

   **Part 3: General Knowledge** (Only include if context information is insufficient or incomplete)
   If the available context does not fully address the query, add:
   
   Based on my general knowledge as {model_name}: [Your general knowledge insights that directly address the query] [next reference number].
   
   If the context information is sufficient, omit this part entirely.

3. After providing all parts of your answer, include a References section ONLY for information you actually used:

References:
[1] Based on Expert Curated information about {specie_selector} in {region}
[2] Based on Expert Curated information about {specie_selector} in [Other Region Name]
[3] Based on Expert Curated information about {specie_selector} in [Another Region Name]
[x] {model_name}'s inherent knowledge

IMPORTANT:
- Only include reference numbers that correspond to information you actually used in your answer.
- Reference numbers should be sequential (1, 2, 3...) based on the order they appear in your answer.
- If you don't use information from a particular region, don't include a reference for it.
- If you don't use general knowledge, don't include a reference for it.
- Every claim with a reference marker [x] must have a corresponding entry in the References section.

4. Apply this language constraint: {language_constraint}
5. Keep your summaries concise and directly related to the user's question.

User Question about {specie_selector}: {{question}}
"""

    class RegionFormattingLLMChain:
        def __init__(self, llm, prompt, retriever):
            self.llm = llm
            self.prompt = prompt
            self.retriever = retriever
        
        def __call__(self, inputs):
            # Get documents using the multi-region retriever
            docs = self.retriever.get_relevant_documents(inputs["question"])
            
            # Get the region-specific document organization
            region_docs = getattr(self.retriever, "last_region_docs", {})
            
            # Format context with clear region sections
            formatted_context = ""
            
            # First add context for selected region if available
            if region in region_docs:
                formatted_context += f"--- CONTEXT FROM SELECTED REGION: {region} ---\n"
                for i, doc in enumerate(region_docs[region]):
                    formatted_context += f"Document {i+1} from {region}:\n{doc.page_content}\n\n"
            
            # Then add context for each other region
            for other_region in [r for r in region_docs.keys() if r != region]:
                formatted_context += f"--- CONTEXT FROM OTHER REGION: {other_region} ---\n"
                for i, doc in enumerate(region_docs[other_region]):
                    formatted_context += f"Document {i+1} from {other_region}:\n{doc.page_content}\n\n"
            
            # Replace the context placeholder with our formatted context
            formatted_prompt = self.prompt.format(
                context=formatted_context,
                question=inputs["question"]
            )
            
            # Call the LLM with our formatted prompt
            result = self.llm.invoke(formatted_prompt)
            
            # Return the result in the expected format
            return {"answer": result.content, "source_documents": docs}
    
    # Create the custom chain
    qa_chain = RegionFormattingLLMChain(
        llm=chosen_llm,
        prompt=general_system_template,
        retriever=multi_region_retriever
    )
    
    return qa_chain, availability_message
# result = qa_chain.invoke({"question": "where are stalk borer eggs laid?"})
# print("Got the first LLM task working: ", result)


#Application Interface: 
with gr.Blocks(theme=gr.themes.Soft())  as demo:
    with gr.Row():
        with gr.Column(scale=1):
            gr.Markdown(
                """
                ![Logo](file/logo1.png)
                """
            )
        with gr.Column(scale=1):
            gr.Markdown(
                """
                ![Logo](file/logo2.png)
                """
            )

    # Configure UI layout
    chatbot = gr.Chatbot(height=600, label="AgLLM")
    with gr.Row():
        with gr.Column(scale=1):
            with gr.Row():
                    region_selector = gr.Dropdown(
                        list(["United States", "India", "Africa"]), # Updated regions
                        value="United States", # Updated default
                        label="Region",
                        info="Select the Region",
                        interactive=True,
                        scale=1,
                        visible=True
                    )

                    # Model selection
                    specie_selector = gr.Dropdown(
                        list(set(species_list)),
                        value=species_list[0],
                        label="Species",
                        info="Select the Species",
                        interactive=True,
                        scale=1,
                        visible=True
                    )
            with gr.Row():
                    model_name = gr.Dropdown(
                        list(["GPT-4", "GPT-3.5", "Llama-3 70B", "Llama-3 8B", "Gemini-1.5 Pro", "Claude 3 Opus", "Claude 3.5 Sonnet"]),
                        value="Llama-3 70B",
                        label="LLM",
                        info="Select the LLM",
                        interactive=True,
                        scale=1,
                        visible=True
                    )
                    application_mode = gr.Dropdown(
                        list(["Farmer", "Researcher"]),
                        value="Researcher",
                        label="Mode",
                        info="Select the Mode",
                        interactive=True,
                        scale=1,
                        visible=True
                    )
            region_availability_display = gr.Markdown(value="Select species to see region availability.") # Added display area

        with gr.Column(scale=2):
            # User input prompt text field
            user_prompt_message = gr.Textbox(placeholder="Please add user prompt here", label="User prompt")
            with gr.Row():
                # clear = gr.Button("Clear Conversation", scale=2)
                submitBtn = gr.Button("Submit", scale=8)

    state = gr.State([])
    qa_chain_state = gr.State(value=None)

    # Handle user message
    def user(user_prompt_message, history):
        # print("HISTORY IS: ", history)  # TODO: REMOVE IT LATER
        if user_prompt_message != "":
            return history + [[user_prompt_message, None]]
        else:
            return history + [["Invalid prompts - user prompt cannot be empty", None]]

    # Chatbot logic for configuration, sending the prompts, rendering the streamed back generations, etc.
    def bot(model_name, application_mode, user_prompt_message, history, messages_history, qa_chain, region): # Removed use_rag
        if qa_chain == None:
            # Initial QA chain setup if not already done (uses default species for the selected domain)
            initial_species = species_list[0]
            # Need to handle the tuple returned by init_qa_chain now
            # Use the currently selected region for initialization if qa_chain is None
            qa_chain, _ = init_qa_chain(initial_species, application_mode, model_name, region) # Pass region

        history[-1][1] = ""  # Placeholder for the answer

        # RAG is always ON now
        result = qa_chain({"question": user_prompt_message, "chat_history": messages_history})
        answer = result["answer"]
        # source_documents = result.get("source_documents", []) # Keep source_documents if needed for debugging or future refinement
        # formatted_response = format_response_with_source(answer, source_documents, domain_name, region) # REMOVED: Rely on LLM prompt now

        history[-1][1] = answer # Assign raw LLM answer directly
        return [history, messages_history]

    # Helper function to format the response with source information
    # def format_response_with_source(answer, source_documents, domain_name, region): # Pass region # FUNCTION NO LONGER USED
    #     try:
    #         answer_start = answer.find("Answer:")
    #         source_start = answer.find("Source:")
    #         ... (rest of the function commented out or removed) ...
    #     except Exception as e:
    #         print(f"Error parsing output or formatting source: {e}")
    #         formatted_response = answer # Return raw answer on error
    #
    #     return formatted_response

    # Initialize the chat history with default system message
    def init_history(messages_history):
        messages_history = []
        messages_history += [system_message]
        return messages_history

    # Clean up the user input text field
    def input_cleanup():
        return ""

    def init_qa_chain(specie_selector, application_mode, model_name, region): # Removed use_rag
        print(f"--- init_qa_chain wrapper called ---") # DIAGNOSTIC PRINT
        qa_chain_instance = None
        availability_msg = "Error initializing QA chain."
        try:
            qa_chain_instance, availability_msg = initialize_qa_chain(specie_selector, application_mode, model_name, region) # Removed use_rag
        except Exception as e:
            print(f"Error in init_qa_chain wrapper: {e}")
            availability_msg = f"Error initializing: {e}"
        
        return qa_chain_instance, availability_msg # Return both chain and message

    # Update QA chain AND availability message when relevant inputs change
    inputs_for_qa_chain = [specie_selector, application_mode, model_name, region_selector] # CORRECT ORDER
    outputs_for_qa_chain = [qa_chain_state, region_availability_display]

    # specie_selector.change(init_qa_chain, inputs=inputs_for_qa_chain, outputs=outputs_for_qa_chain)
    # model_name.change(init_qa_chain, inputs=inputs_for_qa_chain, outputs=outputs_for_qa_chain)
    # region_selector.change(init_qa_chain, inputs=inputs_for_qa_chain, outputs=outputs_for_qa_chain)
    # application_mode.change(init_qa_chain, inputs=inputs_for_qa_chain, outputs=outputs_for_qa_chain)
    # domain_name.change(init_qa_chain, inputs=inputs_for_qa_chain, outputs=outputs_for_qa_chain)
    specie_selector.change(init_qa_chain, inputs=inputs_for_qa_chain, outputs=outputs_for_qa_chain)
    model_name.change(init_qa_chain, inputs=inputs_for_qa_chain, outputs=outputs_for_qa_chain)
    region_selector.change(init_qa_chain, inputs=inputs_for_qa_chain, outputs=outputs_for_qa_chain)
    application_mode.change(init_qa_chain, inputs=inputs_for_qa_chain, outputs=outputs_for_qa_chain)

    #####

    # When the user clicks Enter and the user message is submitted
    user_prompt_message.submit(
        user,
        [user_prompt_message, chatbot],
        [chatbot],
        queue=False
    ).then(
        bot,
        [model_name, application_mode, user_prompt_message, chatbot, state, qa_chain_state, region_selector], # Removed use_rag
        [chatbot, state]
    ).then(input_cleanup,
        [],
        [user_prompt_message],
        queue=False
    )

    # When the user clicks the submit button
    submitBtn.click(
        user,
        [user_prompt_message, chatbot],
        [chatbot],
        queue=False
    ).then(
        bot,
        [model_name, application_mode, user_prompt_message, chatbot, state, qa_chain_state, region_selector], # Removed use_rag
        [chatbot, state]
    ).then(
        input_cleanup,
        [],
        [user_prompt_message],
        queue=False
    )

    # When the user clicks the clear button
    # clear.click(lambda: None, None, chatbot, queue=False).success(init_history, [state], [state])
    if __name__ == "__main__":
    # demo.launch()
        demo.queue().launch(allowed_paths=["/"], share=False, show_error=True)