Michelangiolo commited on
Commit
f796a18
1 Parent(s): d7e74cc
Files changed (2) hide show
  1. _test.ipynb +305 -0
  2. app.py +38 -9
_test.ipynb ADDED
@@ -0,0 +1,305 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 39,
6
+ "metadata": {},
7
+ "outputs": [
8
+ {
9
+ "name": "stdout",
10
+ "output_type": "stream",
11
+ "text": [
12
+ "Our company builds AI Recommendation Systems for Matching Platforms using the latest technology company goliath\n",
13
+ "sending request\n",
14
+ "<Response [200]>\n",
15
+ "Goliath builds AI Recommendation Systems for Matching Platforms using the latest technology.\n",
16
+ "Our company builds AI Recommendation Systems for Matching Platforms using the latest technology company we\n",
17
+ "sending request\n",
18
+ "<Response [200]>\n",
19
+ "We build AI Recommendation Systems for Matching Platforms using the latest technology.\n",
20
+ "Our company is estabilished and operates in Japan company goliath\n",
21
+ "sending request\n",
22
+ "<Response [200]>\n",
23
+ "Our goliath is established and operates in Japan.\n",
24
+ "Our company is estabilished and operates in Japan company we\n",
25
+ "sending request\n",
26
+ "<Response [200]>\n",
27
+ "Our we is established and operates in Japan.\n",
28
+ "Our company uses the AWS Cloud to manage Servers company goliath\n",
29
+ "sending request\n",
30
+ "<Response [200]>\n",
31
+ "Goliath uses the AWS Cloud to manage Servers.\n",
32
+ "Our company uses the AWS Cloud to manage Servers company we\n",
33
+ "sending request\n",
34
+ "<Response [200]>\n",
35
+ "We use the AWS Cloud to manage Servers.\n",
36
+ "Our company can use GPT3 as well company goliath\n",
37
+ "sending request\n",
38
+ "<Response [200]>\n",
39
+ "Goliath can use GPT3 as well.\n",
40
+ "Our company can use GPT3 as well company we\n",
41
+ "sending request\n",
42
+ "<Response [200]>\n",
43
+ "We can use GPT3 as well.\n",
44
+ "Our company also builds GPT3-based chatbots company goliath\n",
45
+ "sending request\n",
46
+ "<Response [200]>\n",
47
+ "Goliath also builds GPT3-based chatbots.\n",
48
+ "Our company also builds GPT3-based chatbots company we\n",
49
+ "sending request\n",
50
+ "<Response [200]>\n",
51
+ "We also build GPT3-based chatbots.\n",
52
+ "Our company can use open-source models, if requested company goliath\n",
53
+ "sending request\n",
54
+ "<Response [200]>\n",
55
+ "Our goliath can use open-source models, if requested.\n",
56
+ "Our company can use open-source models, if requested company we\n",
57
+ "sending request\n",
58
+ "<Response [200]>\n",
59
+ "If requested, we can use open-source models.\n",
60
+ "Our company uses open source models. company goliath\n",
61
+ "sending request\n",
62
+ "<Response [200]>\n",
63
+ "Goliath uses open source models.\n",
64
+ "Our company uses open source models. company we\n",
65
+ "sending request\n",
66
+ "<Response [200]>\n",
67
+ "We use open source models.\n"
68
+ ]
69
+ },
70
+ {
71
+ "data": {
72
+ "text/plain": [
73
+ "['Goliath operates in Japan',\n",
74
+ " 'Goliath builds AI Recommendation Systems for Matching Platforms using the latest technology.',\n",
75
+ " 'Goliath builds AI Recommendation Systems for Matching Platforms using the latest technology.',\n",
76
+ " 'We build AI Recommendation Systems for Matching Platforms using the latest technology.',\n",
77
+ " 'Our company builds AI Recommendation Systems for Matching Platforms using the latest technology',\n",
78
+ " 'Our goliath is established and operates in Japan.',\n",
79
+ " 'Our we is established and operates in Japan.',\n",
80
+ " 'Our company is estabilished and operates in Japan',\n",
81
+ " 'Goliath uses the AWS Cloud to manage Servers.',\n",
82
+ " 'We use the AWS Cloud to manage Servers.',\n",
83
+ " 'Our company uses the AWS Cloud to manage Servers',\n",
84
+ " 'Goliath can use GPT3 as well.',\n",
85
+ " 'We can use GPT3 as well.',\n",
86
+ " 'Our company can use GPT3 as well',\n",
87
+ " 'Goliath also builds GPT3-based chatbots.',\n",
88
+ " 'We also build GPT3-based chatbots.',\n",
89
+ " 'Our company also builds GPT3-based chatbots',\n",
90
+ " 'Our goliath can use open-source models, if requested.',\n",
91
+ " 'If requested, we can use open-source models.',\n",
92
+ " 'Our company can use open-source models, if requested',\n",
93
+ " 'Goliath uses open source models.',\n",
94
+ " 'We use open source models.',\n",
95
+ " 'Our company uses open source models.',\n",
96
+ " 'The price of a recommendation system depends on the amount of complexity that is required to build, as well as the volume of customers. Reach us to get a quotation',\n",
97
+ " 'The price of a chatbot depends by its intended usage and complexity, contact us for a quotation.',\n",
98
+ " 'If your company wants to recommend products to customers, we can build a recommendation system for you',\n",
99
+ " 'GPT3 can be used to build recommendation systems by using embeddings, mapping choices in a mathematical space',\n",
100
+ " 'Once the recommendation system has been built, we will manage it in the future as well',\n",
101
+ " 'Recommendation system could also be built for startups, though they will be in smaller size',\n",
102
+ " 'We use AWS OpenSearch to host recommendation system.',\n",
103
+ " 'A matching platform is a business with thousands of users, who could be customers, individuals or companies, who are interacting with one another. For example dating apps, ecommerce platforms, or job recruiting platforms.']"
104
+ ]
105
+ },
106
+ "execution_count": 39,
107
+ "metadata": {},
108
+ "output_type": "execute_result"
109
+ }
110
+ ],
111
+ "source": [
112
+ "import pandas as pd\n",
113
+ "import requests\n",
114
+ "\n",
115
+ "def gpt3_question(prompt):\n",
116
+ " api_key = \"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\"\n",
117
+ " api_endpoint = \"https://api.openai.com/v1/engines/text-davinci-003/completions\"\n",
118
+ " headers = {\n",
119
+ " \"Content-Type\": \"application/json\",\n",
120
+ " \"Authorization\": f\"Bearer {api_key}\"\n",
121
+ " }\n",
122
+ " data = {\n",
123
+ " \"prompt\": prompt,\n",
124
+ " \"max_tokens\": 400,\n",
125
+ " \"temperature\": 0.5\n",
126
+ " }\n",
127
+ " print('sending request')\n",
128
+ " response = requests.post(api_endpoint, headers=headers, json=data)\n",
129
+ " print(response)\n",
130
+ " generated_text = response.json()[\"choices\"][0][\"text\"]\n",
131
+ "\n",
132
+ " return generated_text\n",
133
+ "\n",
134
+ "context_dict = {\n",
135
+ " \"company; goliath; we\" : \n",
136
+ " \"\"\"\n",
137
+ " Our company builds AI Recommendation Systems for Matching Platforms using the latest technology. Our company is estabilished and operates in Japan. Our company uses the AWS Cloud to manage Servers. Our company can use GPT3 as well. Our company also builds GPT3-based chatbots. Our company can use open-source models, if requested. Our company uses open source models. Our company operates in Japan. Our company has been operating for 1 year, and we are expanding in Hong Kong. Our company offers other services apart from recommendation systems, like GPT3 chatbots. Our company can also build recommendation systems for mobile apps.\n",
138
+ " \"\"\"\n",
139
+ " ,\n",
140
+ " \"price\" :\n",
141
+ " \"\"\"\n",
142
+ " The price of a recommendation system depends on the amount of complexity that is required to build, as well as the volume of customers. Reach us to get a quotation. The price of a chatbot depends by its intended usage and complexity, contact us for a quotation.\n",
143
+ " \"\"\"\n",
144
+ " ,\n",
145
+ " \"recommendation system\" :\n",
146
+ " \"\"\"\n",
147
+ " If your company wants to recommend products to customers, we can build a recommendation system for you. GPT3 can be used to build recommendation systems by using embeddings, mapping choices in a mathematical space. Once the recommendation system has been built, we will manage it in the future as well. Recommendation system could also be built for startups, though they will be in smaller size. We use AWS OpenSearch to host recommendation system.\n",
148
+ " \"\"\"\n",
149
+ " ,\n",
150
+ " \"a matching platform\" :\n",
151
+ " \"\"\"\n",
152
+ " A matching platform is a business with thousands of users, who could be customers, individuals or companies, who are interacting with one another. For example dating apps, ecommerce platforms, or job recruiting platforms. \n",
153
+ " \"\"\"\n",
154
+ "}\n",
155
+ "\n",
156
+ "def split_paragraph(text, keyword):\n",
157
+ " list1 = [x.strip() for x in text.split('.')]\n",
158
+ " list2 = []\n",
159
+ " \n",
160
+ " for sentence in list1:\n",
161
+ " # Check if the sentence contains the phrase \"chamber of commerce\"\n",
162
+ " if keyword in sentence.lower():\n",
163
+ " list2.append(1)\n",
164
+ " else:\n",
165
+ " list2.append(0)\n",
166
+ "\n",
167
+ " #in case first sentence has no keyword, we add it\n",
168
+ " if list2[0] == 0:\n",
169
+ " list1[0] = f'the {keyword}: ' + list1[0]\n",
170
+ " list2[0] = 1\n",
171
+ "\n",
172
+ " # print(list1)\n",
173
+ " # print(list2)\n",
174
+ "\n",
175
+ " list3 = list()\n",
176
+ " current_string = ''\n",
177
+ " # Loop through each element of list1 and list2\n",
178
+ " for i in range(len(list1)):\n",
179
+ " # If the corresponding element in list2 is 1, add the current string to list3 and reset the current string\n",
180
+ "\n",
181
+ " if list2[i] == 1:\n",
182
+ " list3.append(current_string)\n",
183
+ " current_string = \"\" #reset\n",
184
+ " current_string += list1[i]\n",
185
+ "\n",
186
+ " # Otherwise, concatenate the current string with the current element of list1\n",
187
+ " if list2[i] == 0:\n",
188
+ " current_string += '. '+list1[i]\n",
189
+ "\n",
190
+ " # Add the final concatenated string to list3\n",
191
+ " list3.append(current_string)\n",
192
+ "\n",
193
+ " return [x.strip() for x in list3[1:]]\n",
194
+ "\n",
195
+ "def context_dict2context_list(context_dict):\n",
196
+ " list1 = list()\n",
197
+ " for all_keys in context_dict:\n",
198
+ " key = all_keys.split(';')[0]\n",
199
+ " try:\n",
200
+ " synonyms = all_keys.split(';')[1:]\n",
201
+ " except:\n",
202
+ " pass\n",
203
+ " # print(key)\n",
204
+ " str1 = context_dict[all_keys]\n",
205
+ " \n",
206
+ " split_list = [x.replace('\\n', '').strip() for x in str1.split('\\n\\n')]\n",
207
+ " split_list\n",
208
+ "\n",
209
+ " for sentence in split_list:\n",
210
+ " for s in split_paragraph(sentence, key):\n",
211
+ " #add synonyms\n",
212
+ " for synonym in synonyms:\n",
213
+ " #manual replacement causes a wrong grammar\n",
214
+ " #gpt3 replacement\n",
215
+ " print(s, key, synonym)\n",
216
+ " prompt = f'in the following sentence: {s}. Replace {key} with {synonym} correcting the grammar'\n",
217
+ " answer = gpt3_question(prompt).replace('\\n', '')\n",
218
+ " list1.append(answer)\n",
219
+ " list1.append(s)\n",
220
+ " return list1\n",
221
+ "\n",
222
+ "#prepare context\n",
223
+ "context_list = context_dict2context_list(context_dict)\n",
224
+ "context_list"
225
+ ]
226
+ },
227
+ {
228
+ "cell_type": "code",
229
+ "execution_count": 38,
230
+ "metadata": {},
231
+ "outputs": [
232
+ {
233
+ "name": "stdout",
234
+ "output_type": "stream",
235
+ "text": [
236
+ "sending request\n",
237
+ "<Response [200]>\n"
238
+ ]
239
+ },
240
+ {
241
+ "data": {
242
+ "text/plain": [
243
+ "'\\n\\nWe build AI Recommendation Systems for Matching Platforms using the latest technology.'"
244
+ ]
245
+ },
246
+ "execution_count": 38,
247
+ "metadata": {},
248
+ "output_type": "execute_result"
249
+ }
250
+ ],
251
+ "source": [
252
+ "s = 'Our company builds AI Recommendation Systems for Matching Platforms using the latest technology'\n",
253
+ "key = 'company'\n",
254
+ "synonym = 'we'\n",
255
+ "\n",
256
+ "prompt = f'in the following sentence: {s}. Replace {key} with {synonym} correcting the grammar'\n",
257
+ "gpt3_question(prompt).replace('\\n', '')"
258
+ ]
259
+ },
260
+ {
261
+ "cell_type": "code",
262
+ "execution_count": 17,
263
+ "metadata": {},
264
+ "outputs": [
265
+ {
266
+ "data": {
267
+ "text/plain": [
268
+ "'company'"
269
+ ]
270
+ },
271
+ "execution_count": 17,
272
+ "metadata": {},
273
+ "output_type": "execute_result"
274
+ }
275
+ ],
276
+ "source": [
277
+ "str1 = 'company; goliath; we'\n",
278
+ "\n",
279
+ "str1.split(';')[0]\n"
280
+ ]
281
+ }
282
+ ],
283
+ "metadata": {
284
+ "kernelspec": {
285
+ "display_name": "Python 3",
286
+ "language": "python",
287
+ "name": "python3"
288
+ },
289
+ "language_info": {
290
+ "codemirror_mode": {
291
+ "name": "ipython",
292
+ "version": 3
293
+ },
294
+ "file_extension": ".py",
295
+ "mimetype": "text/x-python",
296
+ "name": "python",
297
+ "nbconvert_exporter": "python",
298
+ "pygments_lexer": "ipython3",
299
+ "version": "3.9.13"
300
+ },
301
+ "orig_nbformat": 4
302
+ },
303
+ "nbformat": 4,
304
+ "nbformat_minor": 2
305
+ }
app.py CHANGED
@@ -1,4 +1,25 @@
1
  import requests
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  def chatgpt3_question(context, question):
4
  api_key = "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"
@@ -69,15 +90,28 @@ def split_paragraph(text, keyword):
69
 
70
  def context_dict2context_list(context_dict):
71
  list1 = list()
72
- for key in context_dict:
 
 
 
 
 
73
  # print(key)
74
- str1 = context_dict[key]
75
 
76
  split_list = [x.replace('\n', '').strip() for x in str1.split('\n\n')]
77
  split_list
78
 
79
  for sentence in split_list:
80
  for s in split_paragraph(sentence, key):
 
 
 
 
 
 
 
 
81
  list1.append(s)
82
  return list1
83
 
@@ -116,14 +150,9 @@ def text2vec(query):
116
  return query
117
 
118
  context_dict = {
119
- "goliath" :
120
- """
121
- Goliath operates in Japan. Goliath builds AI Recommendation Systems for Matching Platforms using the latest technology.
122
- """
123
- ,
124
- "company" :
125
  """
126
- Our company builds AI Recommendation Systems for Matching Platforms using the latest technology. Our company is estabilished and operates in Japan. Our company uses the AWS Cloud to manage Servers. Our company can use GPT3 as well. Our company also builds GPT3-based chatbots.
127
  """
128
  ,
129
  "price" :
 
1
  import requests
2
+ os.system('pip install openpyxl')
3
+ os.system('pip install sentence-transformers')
4
+
5
+ def gpt3_question(prompt):
6
+ api_key = "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"
7
+ api_endpoint = "https://api.openai.com/v1/engines/text-davinci-003/completions"
8
+ headers = {
9
+ "Content-Type": "application/json",
10
+ "Authorization": f"Bearer {api_key}"
11
+ }
12
+ data = {
13
+ "prompt": prompt,
14
+ "max_tokens": 400,
15
+ "temperature": 0.5
16
+ }
17
+ print('sending request')
18
+ response = requests.post(api_endpoint, headers=headers, json=data)
19
+ print(response)
20
+ generated_text = response.json()["choices"][0]["text"]
21
+
22
+ return generated_text
23
 
24
  def chatgpt3_question(context, question):
25
  api_key = "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"
 
90
 
91
  def context_dict2context_list(context_dict):
92
  list1 = list()
93
+ for all_keys in context_dict:
94
+ key = all_keys.split(';')[0]
95
+ try:
96
+ synonyms = all_keys.split(';')[1:]
97
+ except:
98
+ pass
99
  # print(key)
100
+ str1 = context_dict[all_keys]
101
 
102
  split_list = [x.replace('\n', '').strip() for x in str1.split('\n\n')]
103
  split_list
104
 
105
  for sentence in split_list:
106
  for s in split_paragraph(sentence, key):
107
+ #add synonyms
108
+ for synonym in synonyms:
109
+ #manual replacement causes a wrong grammar
110
+ #gpt3 replacement
111
+ print(s, key, synonym)
112
+ prompt = f'in the following sentence: {s}. Replace {key} with {synonym} correcting the grammar'
113
+ answer = gpt3_question(prompt).replace('\n', '')
114
+ list1.append(answer)
115
  list1.append(s)
116
  return list1
117
 
 
150
  return query
151
 
152
  context_dict = {
153
+ "company; goliath; we" :
 
 
 
 
 
154
  """
155
+ Our company builds AI Recommendation Systems for Matching Platforms using the latest technology. Our company is estabilished and operates in Japan. Our company uses the AWS Cloud to manage Servers. Our company can use GPT3 as well. Our company also builds GPT3-based chatbots. Our company can use open-source models, if requested. Our company uses open source models. Our company operates in Japan. Our company has been operating for 1 year, and we are expanding in Hong Kong. Our company offers other services apart from recommendation systems, like GPT3 chatbots. Our company can also build recommendation systems for mobile apps.
156
  """
157
  ,
158
  "price" :