seanpedrickcase commited on
Commit
0a726d1
·
1 Parent(s): 11881a6

Corrected prompt. Now runs Haiku correctly

Browse files
Files changed (5) hide show
  1. Dockerfile +3 -3
  2. app.py +10 -2
  3. requirements_aws.txt +16 -0
  4. tools/llm_api_call.py +32 -15
  5. tools/prompts.py +2 -2
Dockerfile CHANGED
@@ -14,14 +14,14 @@ RUN apt-get update && apt-get install -y \
14
 
15
  WORKDIR /src
16
 
17
- COPY requirements_cpu.txt .
18
 
19
  RUN pip uninstall -y typing_extensions \
20
  && pip install --no-cache-dir --target=/install typing_extensions==4.12.2 \
21
  && pip install torch==2.5.1+cpu --target=/install --index-url https://download.pytorch.org/whl/cpu \
22
- && pip install --no-cache-dir --target=/install -r requirements_cpu.txt
23
 
24
- RUN rm requirements_cpu.txt
25
 
26
  # Stage 2: Final runtime image
27
  FROM public.ecr.aws/docker/library/python:3.11.9-slim-bookworm
 
14
 
15
  WORKDIR /src
16
 
17
+ COPY requirements_aws.txt .
18
 
19
  RUN pip uninstall -y typing_extensions \
20
  && pip install --no-cache-dir --target=/install typing_extensions==4.12.2 \
21
  && pip install torch==2.5.1+cpu --target=/install --index-url https://download.pytorch.org/whl/cpu \
22
+ && pip install --no-cache-dir --target=/install -r requirements_aws.txt
23
 
24
+ RUN rm requirements_aws.txt
25
 
26
  # Stage 2: Final runtime image
27
  FROM public.ecr.aws/docker/library/python:3.11.9-slim-bookworm
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import os
2
  import socket
3
  from tools.helper_functions import ensure_output_folder_exists, add_folder_to_path, put_columns_in_df, get_connection_params, output_folder, get_or_create_env_var, reveal_feedback_buttons, wipe_logs, model_full_names, view_table, empty_output_vars_extract_topics, empty_output_vars_summarise, RUN_LOCAL_MODEL
4
- from tools.aws_functions import upload_file_to_s3
5
  from tools.llm_api_call import extract_topics, load_in_data_file, load_in_previous_data_files, sample_reference_table_summaries, summarise_output_topics, batch_size_default
6
  from tools.auth import authenticate_user
7
  from tools.prompts import initial_table_prompt, prompt2, prompt3, system_prompt, add_existing_topics_system_prompt, add_existing_topics_prompt
@@ -107,6 +107,14 @@ local_model_type = "Gemma 2b"
107
  if RUN_LOCAL_MODEL == "1":
108
  load_model(local_model_type, chatf.gpu_layers, chatf.context_length, chatf.gpu_config, chatf.cpu_config, chatf.torch_device)
109
 
 
 
 
 
 
 
 
 
110
  # Create the gradio interface
111
  app = gr.Blocks(theme = gr.themes.Base())
112
 
@@ -167,7 +175,7 @@ with app:
167
  """
168
  )
169
  with gr.Row():
170
- model_choice = gr.Dropdown(value = "gemini-1.5-flash-002", choices = model_full_names, label="LLM model to use", multiselect=False)
171
  in_api_key = gr.Textbox(value = "", label="Enter Gemini API key (only if using Google API models)", lines=1, type="password")
172
 
173
  with gr.Accordion("Upload xlsx or csv files with consultation responses", open = True):
 
1
  import os
2
  import socket
3
  from tools.helper_functions import ensure_output_folder_exists, add_folder_to_path, put_columns_in_df, get_connection_params, output_folder, get_or_create_env_var, reveal_feedback_buttons, wipe_logs, model_full_names, view_table, empty_output_vars_extract_topics, empty_output_vars_summarise, RUN_LOCAL_MODEL
4
+ from tools.aws_functions import upload_file_to_s3, RUN_AWS_FUNCTIONS
5
  from tools.llm_api_call import extract_topics, load_in_data_file, load_in_previous_data_files, sample_reference_table_summaries, summarise_output_topics, batch_size_default
6
  from tools.auth import authenticate_user
7
  from tools.prompts import initial_table_prompt, prompt2, prompt3, system_prompt, add_existing_topics_system_prompt, add_existing_topics_prompt
 
107
  if RUN_LOCAL_MODEL == "1":
108
  load_model(local_model_type, chatf.gpu_layers, chatf.context_length, chatf.gpu_config, chatf.cpu_config, chatf.torch_device)
109
 
110
+ default_model_choice = "gemma_2b_it_local"
111
+
112
+ elif RUN_AWS_FUNCTIONS == "1":
113
+ default_model_choice = "anthropic.claude-3-haiku-20240307-v1:0"
114
+
115
+ else:
116
+ default_model_choice = "gemini-1.5-flash-002"
117
+
118
  # Create the gradio interface
119
  app = gr.Blocks(theme = gr.themes.Base())
120
 
 
175
  """
176
  )
177
  with gr.Row():
178
+ model_choice = gr.Dropdown(value = default_model_choice, choices = model_full_names, label="LLM model to use", multiselect=False)
179
  in_api_key = gr.Textbox(value = "", label="Enter Gemini API key (only if using Google API models)", lines=1, type="password")
180
 
181
  with gr.Accordion("Upload xlsx or csv files with consultation responses", open = True):
requirements_aws.txt ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ pandas==2.2.3
2
+ gradio==5.8.0
3
+ boto3==1.35.71
4
+ pyarrow==18.1.0
5
+ openpyxl==3.1.3
6
+ markdown==3.7
7
+ tabulate==0.9.0
8
+ lxml==5.3.0
9
+ google-generativeai==0.8.3
10
+ html5lib==1.1
11
+ beautifulsoup4==4.12.3
12
+ rapidfuzz==3.10.1
13
+ llama-cpp-python==0.2.90 --extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cpu
14
+ transformers==4.47.0
15
+ numpy==1.26.4
16
+ typing_extensions==4.12.2
tools/llm_api_call.py CHANGED
@@ -436,7 +436,10 @@ def send_request(prompt: str, conversation_history: List[dict], model: object, c
436
  # output_str = output['choices'][0]['text']
437
 
438
  # Check if is a LLama.cpp model response
439
- if 'choices' in response:
 
 
 
440
  conversation_history.append({'role': 'assistant', 'parts': [response['choices'][0]['text']]})
441
  else:
442
  conversation_history.append({'role': 'assistant', 'parts': [response.text]})
@@ -477,19 +480,17 @@ def process_requests(prompts: List[str], system_prompt: str, conversation_histor
477
 
478
  response, conversation_history = send_request(prompt, conversation_history, model=model, config=config, model_choice=model_choice, system_prompt=system_prompt, temperature=temperature)
479
 
480
- if 'choices' in response:
481
- responses.append(response)
482
-
 
 
 
483
  # Create conversation txt object
484
  whole_conversation.append(prompt)
485
  whole_conversation.append(response['choices'][0]['text'])
486
-
487
  else:
488
  responses.append(response)
489
- #print("response.usage_metadata:", response.usage_metadata)
490
- #print("Response.text:", response.text)
491
- #print("responses:", responses)
492
- # Create conversation txt object
493
  whole_conversation.append(prompt)
494
  whole_conversation.append(response.text)
495
 
@@ -714,14 +715,22 @@ def write_llm_output_and_logs(responses: List[ResponseObject],
714
  return input_string[last_index:] # Return everything from the last match onward
715
  return input_string # Return the original string if the term is not found
716
 
717
- if "choices" in responses[-1]:
718
- print("Text response:", responses[-1]["choices"][0]['text'])
 
 
 
 
 
 
719
  start_of_table_response = remove_before_last_term(responses[-1]["choices"][0]['text'])
720
  cleaned_response = clean_markdown_table(start_of_table_response)
721
  print("cleaned_response:", cleaned_response)
722
  else:
 
723
  start_of_table_response = remove_before_last_term(responses[-1].text)
724
  cleaned_response = clean_markdown_table(start_of_table_response)
 
725
 
726
  markdown_table = markdown.markdown(cleaned_response, extensions=['tables'])
727
 
@@ -1328,15 +1337,20 @@ def extract_topics(in_data_file,
1328
  try:
1329
  final_table_output_path = output_folder + batch_file_path_details + "_full_final_response_" + model_choice_clean + "_temp_" + str(temperature) + ".txt"
1330
 
1331
- if "choices" in responses[-1]:
 
 
 
 
1332
  with open(final_table_output_path, "w", encoding='utf-8', errors='replace') as f:
1333
  f.write(responses[-1]["choices"][0]['text'])
1334
  display_table =responses[-1]["choices"][0]['text']
1335
-
1336
  else:
1337
  with open(final_table_output_path, "w", encoding='utf-8', errors='replace') as f:
1338
  f.write(responses[-1].text)
1339
  display_table = responses[-1].text
 
 
1340
 
1341
  log_files_output_paths.append(final_table_output_path)
1342
 
@@ -1539,12 +1553,15 @@ def summarise_output_topics_query(model_choice:str, in_api_key:str, temperature:
1539
 
1540
  print("Finished summary query")
1541
 
1542
- # Extract text from the `responses` list
1543
- if "choices" in responses[-1]:
 
1544
  response_texts = [resp["choices"][0]['text'] for resp in responses]
1545
  else:
1546
  response_texts = [resp.text for resp in responses]
1547
 
 
 
1548
  latest_response_text = response_texts[-1]
1549
 
1550
  #print("latest_response_text:", latest_response_text)
 
436
  # output_str = output['choices'][0]['text']
437
 
438
  # Check if is a LLama.cpp model response
439
+ # Check if the response is a ResponseObject
440
+ if isinstance(response, ResponseObject):
441
+ conversation_history.append({'role': 'assistant', 'parts': [response.text]})
442
+ elif 'choices' in response:
443
  conversation_history.append({'role': 'assistant', 'parts': [response['choices'][0]['text']]})
444
  else:
445
  conversation_history.append({'role': 'assistant', 'parts': [response.text]})
 
480
 
481
  response, conversation_history = send_request(prompt, conversation_history, model=model, config=config, model_choice=model_choice, system_prompt=system_prompt, temperature=temperature)
482
 
483
+ if isinstance(response, ResponseObject):
484
+ responses.append(response)
485
+ whole_conversation.append(prompt)
486
+ whole_conversation.append(response.text)
487
+ elif 'choices' in response:
488
+ responses.append(response)
489
  # Create conversation txt object
490
  whole_conversation.append(prompt)
491
  whole_conversation.append(response['choices'][0]['text'])
 
492
  else:
493
  responses.append(response)
 
 
 
 
494
  whole_conversation.append(prompt)
495
  whole_conversation.append(response.text)
496
 
 
715
  return input_string[last_index:] # Return everything from the last match onward
716
  return input_string # Return the original string if the term is not found
717
 
718
+ # Check if the last response is a ResponseObject
719
+ if isinstance(responses[-1], ResponseObject):
720
+ #print("Text response:", responses[-1].text)
721
+ start_of_table_response = remove_before_last_term(responses[-1].text)
722
+ cleaned_response = clean_markdown_table(start_of_table_response)
723
+ print("cleaned_response:", cleaned_response)
724
+ elif "choices" in responses[-1]:
725
+ #print("Text response:", responses[-1]["choices"][0]['text'])
726
  start_of_table_response = remove_before_last_term(responses[-1]["choices"][0]['text'])
727
  cleaned_response = clean_markdown_table(start_of_table_response)
728
  print("cleaned_response:", cleaned_response)
729
  else:
730
+ #print("Text response:", responses[-1].text)
731
  start_of_table_response = remove_before_last_term(responses[-1].text)
732
  cleaned_response = clean_markdown_table(start_of_table_response)
733
+ print("cleaned_response:", cleaned_response)
734
 
735
  markdown_table = markdown.markdown(cleaned_response, extensions=['tables'])
736
 
 
1337
  try:
1338
  final_table_output_path = output_folder + batch_file_path_details + "_full_final_response_" + model_choice_clean + "_temp_" + str(temperature) + ".txt"
1339
 
1340
+ if isinstance(responses[-1], ResponseObject):
1341
+ with open(final_table_output_path, "w", encoding='utf-8', errors='replace') as f:
1342
+ f.write(responses[-1].text)
1343
+ display_table = responses[-1].text
1344
+ elif "choices" in responses[-1]:
1345
  with open(final_table_output_path, "w", encoding='utf-8', errors='replace') as f:
1346
  f.write(responses[-1]["choices"][0]['text'])
1347
  display_table =responses[-1]["choices"][0]['text']
 
1348
  else:
1349
  with open(final_table_output_path, "w", encoding='utf-8', errors='replace') as f:
1350
  f.write(responses[-1].text)
1351
  display_table = responses[-1].text
1352
+
1353
+
1354
 
1355
  log_files_output_paths.append(final_table_output_path)
1356
 
 
1553
 
1554
  print("Finished summary query")
1555
 
1556
+ if isinstance(responses[-1], ResponseObject):
1557
+ response_texts = [resp.text for resp in responses]
1558
+ elif "choices" in responses[-1]:
1559
  response_texts = [resp["choices"][0]['text'] for resp in responses]
1560
  else:
1561
  response_texts = [resp.text for resp in responses]
1562
 
1563
+
1564
+
1565
  latest_response_text = response_texts[-1]
1566
 
1567
  #print("latest_response_text:", latest_response_text)
tools/prompts.py CHANGED
@@ -3,7 +3,7 @@ system_prompt = """You are a researcher analysing responses from an open text da
3
  initial_table_prompt = """The open text data is shown in the following table that contains two columns, Reference and Response. Response table:
4
  {response_table}
5
 
6
- Your task is to create one new markdown table with the headings 'General Topic', 'Subtopic', 'Sentiment', 'Summary', and 'Response references'.
7
  In the first column identify general topics relevant to responses. Create as many general topics as you can.
8
  In the second column list subtopics relevant to responses. Make the subtopics as specific as possible and make sure they cover every issue mentioned.
9
  In the third column write the sentiment of the subtopic: Negative, Neutral, or Positive.
@@ -28,7 +28,7 @@ Topics known to be relevant to this dataset are shown in the following Topics ta
28
  {topics}
29
 
30
  Your task is to create one new markdown table, assigning responses from the Response table to existing topics, or to create new topics if no existing topics are relevant.
31
- Create a new markdown table with the headings 'General Topic', 'Subtopic', 'Sentiment', 'Summary', and 'Response references'.
32
  In the first and second columns, assign General Topics and Subtopics to Responses. Assign topics from the Topics table above if they are very relevant to the text of the Response. Fill in the General Topic and Sentiment for the Subtopic if they do not already exist. If you find a new topic that does not exist in the Topics table, add a new row to the new table. Make the General Topic and Subtopic as specific as possible.
33
  In the third column, write the sentiment of the Subtopic: Negative, Neutral, or Positive.
34
  In the fourth column list each specific Response reference number that is relevant to the Subtopic, separated by commas. Do no write any other text in this column.
 
3
  initial_table_prompt = """The open text data is shown in the following table that contains two columns, Reference and Response. Response table:
4
  {response_table}
5
 
6
+ Your task is to create one new markdown table with the headings 'General Topic', 'Subtopic', 'Sentiment', 'Response references', and 'Summary'.
7
  In the first column identify general topics relevant to responses. Create as many general topics as you can.
8
  In the second column list subtopics relevant to responses. Make the subtopics as specific as possible and make sure they cover every issue mentioned.
9
  In the third column write the sentiment of the subtopic: Negative, Neutral, or Positive.
 
28
  {topics}
29
 
30
  Your task is to create one new markdown table, assigning responses from the Response table to existing topics, or to create new topics if no existing topics are relevant.
31
+ Create a new markdown table with the headings 'General Topic', 'Subtopic', 'Sentiment', 'Response references', and 'Summary'.
32
  In the first and second columns, assign General Topics and Subtopics to Responses. Assign topics from the Topics table above if they are very relevant to the text of the Response. Fill in the General Topic and Sentiment for the Subtopic if they do not already exist. If you find a new topic that does not exist in the Topics table, add a new row to the new table. Make the General Topic and Subtopic as specific as possible.
33
  In the third column, write the sentiment of the Subtopic: Negative, Neutral, or Positive.
34
  In the fourth column list each specific Response reference number that is relevant to the Subtopic, separated by commas. Do no write any other text in this column.