File size: 2,525 Bytes
55b5946
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import openai 
from configs import OPEN_API_KEY
def get_user_input(prompt):
    return input(prompt)

def choose_cooperation_type():
    choice = get_user_input("Please choose the cooperation type: \n" 
                            +"1. Sequential: human provide an answer first and then AI provide the answer based on it.\n" 
                            +"2. Parallel: human and AI give answers seperately and then AI does the merge.\n")
    if choice == '1':
        return 'sequential'
    elif choice == '2':
        return 'parallel'
    else:
        print("Invalid choice. Please try again.")
        return choose_cooperation_type()

def describe_task():
    task_description = get_user_input("Please describe your task: ")
    if task_description.strip() == "":
        # print("Task description cannot be empty. Please try again.")
        # return describe_task()
        task_description = "Write a poem about the moon in 3 lines."
    return task_description

# def get_api_key():
#     return configs.api_key  # get the API key from the configs file

def generate_text_with_gpt(promts):
    client = openai.OpenAI(api_key=OPEN_API_KEY)
    
    try:
        response = client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=[
            {"role": "system", "content": promts},
            ]
        )
        return response.choices[0].message.content
    except Exception as e:
        print(f"Error occured when generating texts: {e}")
        return ""

def merge_texts_parallel(task_description, human_text, ai_text):
    prompt = f"Given the task as :{task_description}, there are two answers provided:\n" + \
             f"The first answer:  {human_text}\nThe second answer: {ai_text}\n" + \
             f"Merge the two answers into one in a coherent way: "
    return generate_text_with_gpt(prompt)

def merge_texts_sequential(task_description, human_text):
    prompt = f"Given the task as :{task_description}, the human answer is: {human_text}\n" + \
             f"Provide an answer of your own but make sure it is coherent and should be based on the human answer: "
    return generate_text_with_gpt(prompt)

def evaluation(task_description, text):
    prompt = f"Given the task as :{task_description}, the answer provided is: {text}\n" + \
             f"Evaluate the answer and provide scores between 0 and 10,\n" + \
             f"where criteria for evaluation are correctness, relevance, novelty, fluency, and aesthetic Value:"
    return generate_text_with_gpt(prompt)