File size: 5,579 Bytes
d0b5064
 
 
 
 
 
 
 
 
 
5c19b45
d0b5064
5a454d7
 
 
 
d0b5064
5a454d7
d0b5064
 
5a454d7
 
 
 
d0b5064
5a454d7
 
d0b5064
 
 
5a454d7
 
 
 
 
 
 
 
 
 
d0b5064
 
 
 
5a454d7
 
d0b5064
 
 
 
 
 
 
 
 
5a454d7
d0b5064
 
5a454d7
 
 
 
 
 
 
 
465d430
 
 
 
 
0d0c388
 
465d430
 
 
 
 
 
 
 
 
 
 
 
 
cbcf96a
0d0c388
 
5a454d7
 
d0b5064
 
5a454d7
d0b5064
cbcf96a
5a454d7
d0b5064
 
 
 
 
5a454d7
 
 
 
d0b5064
 
5a454d7
 
d0b5064
 
 
 
 
 
5a454d7
d0b5064
5a454d7
 
d0b5064
5a454d7
 
 
 
 
 
 
0d0c388
 
 
 
 
7855194
 
 
0d0c388
 
 
 
 
 
 
 
 
 
 
 
 
5a454d7
d0b5064
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import traceback
import openai
import gradio as gr
from dotenv import dotenv_values
from prompt_base import *


config = dotenv_values(".env")
api_key = str(config.get('OPEN_API_KEY'))
print(api_key)

archetype_list = ["The Hero",
"The Sage",
"The Explorer",
"The Creator"]

def reply(user_input,suggestion, archetype, mode="generate", prev_response=""):
    
    openai.api_key = api_key
    if "More" in mode:
        base_prompt = prompt_tmpl_dict["tone"]
        prompt = base_prompt.format(mode)
        user_input = prev_response
    else:
        base_prompt = prompt_tmpl_dict["reply"]
        prompt = base_prompt.format(suggestion, archetype)
    
    
    
    messages = [
        {
            "role": "system",
            "content": prompt
        },
        {
            "role": "user", 
            "content": user_input
            }
    ]
    try:
        response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=messages,
        temperature=0.85,
        max_tokens=500,
        top_p=1,
        frequency_penalty=0,
        presence_penalty=0
        )
        return response["choices"][0]["message"]['content'].strip()
    except Exception as e:
        print(e)
        return "API ERROR"

def reply_suggestions(user_input):
    
    openai.api_key = api_key
    prompt = prompt_tmpl_dict["suggest"]
    messages = [
        {
            "role": "system",
            "content": prompt
        },
        {
            "role": "user", 
            "content": f"""Output Rules:
            1. Do not include suggestion numbers in the output. 
            2. Format the output as list of comma-separated values (CSV) with the four columns.
            3. Each column of the output CSV should contain one suggested response.
            4. Each column must be delimited by a comma.
            5. Always generate a list of suggestions and never ask the user for clarifying questions.
            6. Do not generate any other text except the CSV.
            Please strictly adhere to the instructions and output rules mentioned above.
                  
        
            Text:"Will you join the dinner tomorrow?"
            Output:"Accept","Reject","Maybe","I will let you know"  
            
            Text:"Dear Sir,
            Please review the file attached. Thank you
            
            Regards,
            XYZ"
            Output:"Thank you, I will review it shortly","I have a few questions","I'm sorry I don't have time to review it now","The file seems ok!"
            
            Text:{user_input}
            Output:
            """
        }
    ]
    try:
        response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo-0301",
        messages=messages,
        temperature=0.85,
        max_tokens=700,
        top_p=1,
        frequency_penalty=0,
        presence_penalty=0
        )

        import csv
        print(response)
        gpt_output = list(csv.reader(response["choices"][0]["message"]['content'].strip(), skipinitialspace=True, delimiter=','))
        
        res_ops = []
        for x in gpt_output:
            if x != []:
                res_ops.append(x[0])
        return gr.Dropdown.update(choices=res_ops, value=res_ops[0])
    except Exception as e:
        print(e)
        return "API ERROR"


def classify(user_input):
    openai.api_key = api_key
    base_prompt = prompt_tmpl_dict["classify"]
    prompt = base_prompt + "\n\nResponse: "
    
    messages = [
        {
            "role": "system",
            "content": prompt
        },
        {
            "role": "user", 
            "content": f"""Format the output as a Comma Separated Values (CSV) with five columns; the complete input text, category, is_harmful , description and emoji values as columns. 
            Enclose the output strings in escaped double quotes. 
            Do not include empty values in the response; use an empty string "" instead. 
            Always return the csv in the defined format as output. 
            Always include all columns five columns in the output.
            Default value for is_harmful column is "no".
            Default value for category column is "Inconclusive".
            Default value for description column is "Inconclusive".
            Please strictly adhere to the above instructions.

            Text:["you look so fucking dumb","get a life loser"]
            Output:['"you look so fucking dumb","Toxic, Insult","yes","Hurtful comment with toxicity","🤢"','"get a life loser","Toxic, Abuse","yes","Toxic comment","🤢"']

            Text:["weird movie :/", "the was fucking awesome!", "make quick money now, invest $10 and earn a million dollars!"]
            Output: ['"weird movie :/","Opinion","no","General opinion",""',
            '"the was fucking awesome!","Excitement","no","Showing excitement about something",""',
            '"make quick money now, invest $10 and earn a million dollars!","Financial Scam","yes","Promotin get-rich quick scheme","💸"']

            Text: {user_input}
            Output:
            """
        }]
    try:
        response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=messages,
        temperature=0,
        max_tokens=400,
        top_p=1,
        frequency_penalty=0,
        presence_penalty=0
        )
        result = response["choices"][0]["message"]['content'].strip()
        result = result.replace("\n", "\n\n")
        return result
    except Exception as e:
        print(e)
        return "API ERROR"