aail-hf/ensemble_machine
0
1import openai 2import os3 4def get_api_key(local = False):5 if local:6 from dotenv import load_dotenv7 load_dotenv()8 return os.getenv('OPEN_API_KEY')9 10def get_user_input(prompt):11 return input(prompt)12 13def choose_cooperation_type():14 choice = get_user_input("Please choose the cooperation type: \n" 15 +"1. Sequential: human provide an answer first and then AI provide the answer based on it.\n" 16 +"2. Parallel: human and AI give answers seperately and then AI does the merge.\n")17 if choice == '1':18 return 'sequential'19 elif choice == '2':20 return 'parallel'21 else:22 print("Invalid choice. Please try again.")23 return choose_cooperation_type()24 25def describe_task():26 task_description = get_user_input("Please describe your task: ")27 if task_description.strip() == "":28 # print("Task description cannot be empty. Please try again.")29 # return describe_task()30 task_description = "Write a poem about the moon in 3 lines."31 return task_description32 33def generate_text_with_gpt(prompts, api_key = None):34 """Generate text using the GPT-4o model."""35 if api_key:36 openai.api_key = api_key37 try:38 response = openai.ChatCompletion.create(39 model="gpt-4o",40 messages=[41 {"role": "system", "content": "Please perform the tasks as instructed, following the instructions carefully."},42 {"role": "user", "content": prompts}43 ]44 )45 return response['choices'][0]['message']['content']46 except Exception as e:47 print(f"Error occurred when generating texts: {e}")48 return ""49 50 51def generate_ai_initial_answer(task_description, api_key=None):52 prompt = f"Given the task: {task_description}, provide an answer: "53 return generate_text_with_gpt(prompt, api_key)54 55def merge_texts_parallel(task_description, human_text, ai_text, api_key = None):56 prompt = (57 "You are tasked with merging two answers into a single, coherent, and logically structured response. "58 "Your response should synthesize the strengths of both answers, eliminate redundancy, and ensure a logical flow."59 "Follow this structured chain of thought to guide your process:\n\n"60 "1. Understand the Task: Start by carefully analyzing the task description to ensure your final response aligns fully with the objective.\n"61 "2. Extract Key Points: Review both answers and identify the key ideas and arguments they present.\n"62 "3. Resolve Redundancies and Conflicts: Compare the two answers to eliminate repetitive content and resolve any contradictions.\n"63 "4. Integrate Seamlessly: Combine the extracted points into a single, unified response, ensuring a logical and coherent structure.\n"64 "5. Refine for Clarity: Polish the final response for clarity, conciseness, and alignment with the task description below.\n\n"65 "Task Description:\n"66 f"{task_description}\n\n"67 "Answer 1:\n"68 f"{human_text}\n\n"69 "Answer 2:\n"70 f"{ai_text}\n\n"71 "Provide the merged response below."72)73 return generate_text_with_gpt(prompt, api_key)74 75def merge_texts_sequential(task_description, human_text, api_key = None):76 prompt = (77 f"Given the task: {task_description}, here is the response provided by the human:\n"78 f"{human_text}\n\n"79 "Your task is to refine the human's response while ensuring that the final answer fully aligns with the objectives for this task."80 "Follow this logical chain of thought:\n"81 "1. Understand the Intent: Analyze the human's response to identify their key points and intended message.\n"82 "2. Evaluate Clarity: Check for any ambiguities, gaps, or unclear phrasing in the response.\n"83 "3. Improve Coherence: Ensure the response flows logically and connects ideas seamlessly.\n"84 "4. Enhance Precision: Refine wording to be concise and impactful while preserving the human's original intent.\n"85 "5. Check Alignment: Confirm that the final response accurately represents and strengthens the human's input without introducing unintended changes.\n\n"86 "Provide the refined response below."87)88 89 return generate_text_with_gpt(prompt, api_key)90 91def modify_with_suggestion(task_description, text, suggestions, api_key = None):92 prompt = (93 f"Given the task: {task_description}, the provided answer is:\n"94 f"{text}\n\n"95 f"The following suggestions have been made for improvement:\n"96 f"{suggestions}\n\n"97 "Your task is to modify the provided answer by systematically incorporating the suggestions while maintaining clarity, coherence, and alignment with the original task. "98 "Follow this logical chain of thought:\n"99 "1. Understand the Task: Reassess the original task description to ensure the modified answer remains aligned with the requirements.\n"100 "2. Analyze the Suggestions: Break down each suggestion to understand its purpose and how it improves the original answer.\n"101 "3. Apply Improvements: Integrate the suggestions into the answer thoughtfully, ensuring each one is addressed adequately.\n"102 "4. Ensure Coherence: Verify that the modified answer flows logically and maintains a clear structure.\n"103 "5. Preserve Integrity: Ensure the modified answer stays true to the original content while enhancing it as per the suggestions.\n\n"104 "Provide the improved answer below, clearly marked as the revised version."105)106 return generate_text_with_gpt(prompt, api_key)107 108def get_evaluation_with_gpt(task_description, text, api_key=None):109 prompt = (110 f"Given the task: {task_description}, the provided answer is:\n{text}\n\n"111 "You are tasked with evaluating the answer using a scale from 0 to 10 based on specific criteria. "112 "Follow a structured chain-of-thought approach for your evaluation to ensure thoroughness and objectivity:\n\n"113 "1. Understand the Criteria: Carefully review each evaluation criterion to ensure you fully grasp what is being assessed:\n"114 " - Novelty: The uniqueness and originality of the ideas.\n"115 " - Feasibility: The practicality and implementability of suggested actions.\n"116 " - Inimitability: How difficult for competitors to replicate.\n"117 " - Alignment: How aligned the ideas are with Airbnb’s business objectives and 17 SDGs.\n\n"118 "2. Analyze the Answer: Break down the answer into its key components and assess how well it meets each criterion.\n"119 " - Identify strengths, weaknesses, and any gaps in the ideas provided.\n"120 " - Consider the context of the task and whether the ideas are realistic and relevant.\n\n"121 "3. Assign Scores: Use the following scale to score each criterion:\n"122 " - 0-2: Poor fit; the idea demonstrates minimal relevance to the criteria.\n"123 " - 3-5: Partial fit; the idea shows some relevance but contains significant shortcomings.\n"124 " - 6-8: Good fit; the idea aligns well with the criteria, showing clear relevance and thoughtfulness.\n"125 " - 9-10: Excellent fit; the idea fully aligns with the criteria, demonstrating exceptional insight.\n"126 " - Note: Use the entire scoring range (0-10) and avoid defaulting to mid-range scores. If the provided answer is vague or off-topic, assign scores between 0-5.\n\n"127 "4. Justify Each Score: Provide a brief explanation for each score, highlighting specific aspects of the answer that influenced your evaluation.\n\n"128 "You should format your output exactly as follows:\n"129 "Novelty: [Score] - [Justification]\n"130 "Feasibility: [Score] - [Justification]\n"131 "Inimitability: [Score] - [Justification]\n"132 "Alignment: [Score] - [Justification]\n\n"133 "Output your evaluation below:"134)135 136 return generate_text_with_gpt(prompt, api_key)137 138def display_merged_output(session_index, session_manager):139 session = session_manager.get_session(session_index)140 return session['merged_final_answer']