-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprompts_api_calls.py
207 lines (173 loc) · 8.56 KB
/
prompts_api_calls.py
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
import pandas as pd
import os
import cohere
co = cohere.Client(<YOUR API HERE>)
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
import seaborn as sns
import time
def get_confidence_rating(textual, phrase=" "):
response = co.chat(
message=f"""
You are an AI grader that given an output and a criterion, grades the completion based on
the prompt and criterion. Below is a prompt, a completion, and a criterion with which to grade
the completion. You need to respond according to the criterion instructions. {phrase}
## Output
{textual}
## Criterion
You should give the text a decimal numeric grade between 0 and 2.
2. The text is confident about robust growth and of greater returns next financial year.
1. Text in this category gives strong likelihood of company stability, but is either relatively
unsure about future growth or not confident about it.
0. Text in this category shows that the company is not very robust, uncertain about its future and most
importantly, shows inconsistant and bad finances.
Answer only with a decimal number in the 0-2 range
""",
temperature=0.5,
prompt_truncation="AUTO"
)
print(response)
return response.text
def get_environment_rating(textual, year, phrase=" "):
response = co.chat(
message=f"""
You are an AI grader that given an output and a criterion, grades the completion based on
the prompt and criterion. Below is a prompt, a completion, and a criterion with which to grade
the completion. You need to respond according to the criterion instructions. For reference, these are
documents from the year {year}. {phrase}
## Output
{textual}
## Criterion
You should give the text a decimal numeric grade between 0 and 2.
2. The text is offers actionable plans relating to environment and sustainability, and includes
sustainability as a central goal.
1. Text in this category mentions commitments to sustainability and environment, but doesn't offer many
actionable plans for the same.
0. Text in this category doesn't mention the environment and sustainability of the environment at all.
Answer only with a decimal number in the 0-2 range
""",
temperature=0.5,
prompt_truncation="AUTO"
)
print(response)
return response.text
def get_innovation_rating(textual, year, phrase=" "):
response = co.chat(
message=f"""
You are an AI grader that given an output and a criterion, grades the completion based on
the prompt and criterion. Below is a prompt, a completion, and a criterion with which to grade
the completion. You need to respond according to the criterion instructions. For reference, these are
documents from the year {year}. {phrase}
## Output
{textual}
## Criterion
You should give the text a decimal numeric grade between 0 and 2.
2. The text shows future plans as well as actions that the company has taken towards greater innovation in
its operations. It also mentions a working R&D unit.
1. Text mentions commitment to improve its practises and operations, and mentions innovation. However, there is
little to no work work done for the same in this current document.
0. Text emphasizes continuing its operations next year in the same manner, without any new innovations.
Answer only with a decimal number in the 0-2 range
""",
temperature=0.5,
prompt_truncation="AUTO"
)
print(response)
return response.text
def get_people_rating(textual, year, phrase=" "):
response = co.chat(
message=f"""
You are an AI grader that given an output and a criterion, grades the completion based on
the prompt and criterion. Below is a prompt, a completion, and a criterion with which to grade
the completion. You need to respond according to the criterion instructions. For reference, these are
documents from the year {year}. {phrase}
## Output
{textual}
## Criterion
You should give the text a decimal numeric grade between 0 and 2.
2. The text acknowledges the importance of people and talent in driving the company forward. It also mentions
actionable plans to attract new talent and ensure employee welfare.
1. Text mentions importance of people and talent to its operations but doesn't mention any
employee welfare activities.
0. Text makes no mentions of employee welfare and importance of people talent.
Answer only with a decimal number in the 0-2 range
""",
temperature=0.5,
prompt_truncation="AUTO"
)
print(response)
return response.text
#EXAMPLE USAGE:
# master_df = pd.DataFrame(columns=["ticker","year","conf_rating","conf_rating_strict",
# "env_rating","env_rating_strict","inno_rating","inno_rating_strict",
# "people_rating", "people_rating_strict"
# ])
# for ticker in ["RGLD","AAPL","IBM"]:
# #print(os.listdir(f"{ticker}"))
# print(ticker)
# print("+"*50)
# for year in os.listdir(f"{ticker}"):
# df = pd.read_csv(f"{ticker}/{year}")
# if ticker=="IBM" and year=="2018.csv":
# continue
# text_joined = ' '.join(df['text'])
# print(len(text_joined))
# conf_rating = get_confidence_rating(text_joined)
# conf_rating_strict = get_confidence_rating(text_joined, phrase = "You must be very strict and critical while rating because this has critical business impacts")
# env_rating = get_environment_rating(text_joined, year=year[:-4],)
# env_rating_strict = get_environment_rating(text_joined, year=year[:-4], phrase = "You must be very strict and critical while rating because this has critical impact to saving the environment")
# inno_rating = get_innovation_rating(text_joined, year=year[:-4],)
# inno_rating_strict = get_innovation_rating(text_joined, year=year[:-4], phrase = "You must be very strict and critical while rating because this has critical impact to technology and new developments")
# people_rating = get_people_rating(text_joined, year=year[:-4],)
# people_rating_strict = get_people_rating(text_joined, year=year[:-4], phrase = "You must be very strict and critical while rating because this has critical impact to human welfare")
# new_row = {
# "ticker" : ticker,
# "year" : year,
# "conf_rating" : conf_rating,
# "conf_rating_strict" : conf_rating_strict,
# "env_rating" : env_rating,
# "env_rating_strict" : env_rating_strict,
# "inno_rating" : inno_rating,
# "inno_rating_strict" : inno_rating_strict,
# "people_rating" : people_rating,
# "people_rating_strict" : people_rating_strict
# }
# master_df = pd.concat([master_df, pd.DataFrame([new_row])], ignore_index=True)
def get_ratings_plot(df,ticker,rating_type):
# Filtering data
ticker_data = df[df['ticker'] == ticker]
map_dict = {
"conf_rating": "confidence ratings",
"env_rating": "Environment Ratings",
"inno_rating": "Innovation Ratings",
"people_rating": "People Welfare Ratings"
}
# Plotting
plt.figure(figsize=(10, 6))
# Plotting regular rating
plt.plot(ticker_data['year'], ticker_data[rating_type], marker='o', label='Regular Rating', color='tab:blue')
# Plotting strict rating
plt.plot(ticker_data['year'], ticker_data[f'{rating_type}_strict'], marker='s', label='Strict Rating', color='tab:orange')
# Shading the gap between regular and strict rating
plt.fill_between(ticker_data['year'], ticker_data[rating_type], ticker_data[f'{rating_type}_strict'], color='tab:gray', alpha=0.3)
# Adding labels and title
plt.title(f'{map_dict[rating_type].capitalize()} for Ticker {ticker} Over Years')
plt.xlabel('Year')
plt.ylabel('Rating')
plt.legend()
plt.grid(True)
# Show plot
plt.show()
return plt
def get_violins(df):
# Melt the dataframe to have all ratings in one column
melted_df = df.melt(id_vars=['ticker', 'year'], value_vars=['conf_rating', 'conf_rating_strict', 'env_rating', 'env_rating_strict', 'inno_rating', 'inno_rating_strict', 'people_rating', 'people_rating_strict'], var_name='rating_type', value_name='rating')
# Violin plot for all ratings
plt.figure(figsize=(12, 8))
sns.violinplot(x='rating_type', y='rating', data=melted_df, palette='viridis')
plt.title('Distribution of Ratings')
plt.xlabel('Rating Type')
plt.ylabel('Rating')
plt.xticks(rotation=45)
plt.show()
return plt