main.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. import logging
  2. import os
  3. import json
  4. import re
  5. import openai
  6. import requests
  7. from openai import OpenAI
  8. from flask import Flask, request, jsonify, send_from_directory, url_for
  9. from convert import alpaca_to_chatgpt, csv_to_jsonl
  10. app = Flask(__name__)
  11. ssl = None
  12. # ssl =('/etc/ssl/sample.crt', '/etc/ssl/sample.pem')
  13. app.openai_key = os.environ.get("OPENAI_KEY", "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")
  14. app.openai_client = OpenAI(api_key=app.openai_key)
  15. #logging.basicConfig(level=logging.DEBUG, filename='/jkt-disk-01/app/mms/chatgpt-apache/chatgpt.log', format='%(asctime)s %(message)s')
  16. app.chat_messages = [
  17. {"role": "system",
  18. "content": "Please respond professionally and in a friendly manner, using the same language as the original request."}
  19. ]
  20. app.translate_messages = [
  21. {"role": "system",
  22. "content": "Please translate using the requested language."}
  23. ]
  24. app.suggest_messages = [
  25. {"role": "system",
  26. "content": "Please suggest reply messages based on the previous conversations and the user's request."}
  27. ]
  28. app.recommend_messages = [
  29. {"role": "system",
  30. "content": "Give normalized total weight of each category in json based on headlines"
  31. }
  32. ]
  33. app.summary_messages = [
  34. {"role": "system",
  35. "content": "Please summarize an article."
  36. }
  37. ]
  38. UPLOAD_FOLDER = 'files'
  39. app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
  40. @app.route('/files/<name>')
  41. def download_file(name):
  42. return send_from_directory(app.config["UPLOAD_FOLDER"], name)
  43. @app.route('/', methods=['GET', 'POST'])
  44. def test():
  45. return jsonify({"status": "0"})
  46. def recommend(headlines, category):
  47. chat_messages = app.recommend_messages.copy()
  48. try:
  49. json_payload = {
  50. "role": "user",
  51. "content": f"""{headlines}
  52. Berikan nilai berat masing-masing kategori, jumlahkan dan normalisasikan:
  53. {category}
  54. Berikan dalam bentuk json
  55. """
  56. }
  57. chat_messages.append(json_payload)
  58. json_response = app.openai_client.chat.completions.create(model="gpt-3.5-turbo-1106",
  59. messages=chat_messages,
  60. response_format={"type": "json_object"}
  61. )
  62. return json.loads(json_response.choices[0].message.content)
  63. except Exception as error_print:
  64. app.logger.exception("error")
  65. result = {}, 405
  66. def vision(message, image_url=None, image_b64=None):
  67. chat_messages = app.chat_messages.copy()
  68. url = ""
  69. if image_url:
  70. url = f"{image_url}"
  71. elif image_b64:
  72. url = f"data:image/jpeg;base64,{image_b64}"
  73. try:
  74. json_payload = {
  75. "role": "user",
  76. "content": [
  77. {"type": "text", "text": message},
  78. {
  79. "type": "image_url",
  80. "image_url": {
  81. "url": url,
  82. },
  83. },
  84. ],
  85. }
  86. chat_messages.append(json_payload)
  87. json_response = app.openai_client.chat.completions.create(
  88. model="gpt-4o",
  89. messages=chat_messages,
  90. max_tokens=500
  91. )
  92. return {"role": "assistant", "content": json_response.choices[0].message.content}
  93. except Exception as error_print:
  94. app.logger.exception("error")
  95. result = {}, 405
  96. @app.route('/gpt', methods=['POST'])
  97. def gpt():
  98. assistant_id = ""
  99. chat_messages = []
  100. chat_model = "gpt-3.5-turbo"
  101. use_video = False
  102. suggest = False
  103. summarize = False
  104. predict_q = 0
  105. max_char_msg = 500
  106. max_resp_token = 600
  107. category = []
  108. headlines = []
  109. image_url = ""
  110. num_choices = 1
  111. json_payload = request.get_json()
  112. if not json_payload:
  113. json_payload = []
  114. has_named_params = False
  115. if isinstance(json_payload, dict):
  116. has_named_params = 'payload' in json_payload
  117. if 'payload' in json_payload:
  118. if 'predict_q' in json_payload:
  119. predict_q = 5 if json_payload['predict_q'] > 4 else 0 if json_payload['predict_q'] < 1 else \
  120. json_payload['predict_q']
  121. if 'num_choices' in json_payload:
  122. num_choices = 5 if json_payload['num_choices'] > 4 else 1 if json_payload['num_choices'] < 2 else \
  123. json_payload['num_choices']
  124. if 'use_video' in json_payload:
  125. use_video = json_payload['use_video'] == "1"
  126. if 'chat_model' in json_payload and 'assistant_id' not in json_payload:
  127. chat_model = json_payload['chat_model']
  128. max_resp_token = 2048
  129. if 'translate' in json_payload:
  130. chat_messages = app.translate_messages.copy()
  131. json_payload['payload'][-1]['content'] = json_payload['payload'][-1][
  132. 'content'] + f" (Translate to {json_payload['translate']})"
  133. elif 'suggest' in json_payload:
  134. suggest = json_payload['suggest'] == "1"
  135. if suggest:
  136. chat_messages = app.suggest_messages.copy()
  137. else:
  138. chat_messages = app.chat_messages.copy()
  139. json_payload['payload'][-1]['content'] = json_payload['payload'][-1][
  140. 'content'] + f" What can I say to him/her?"
  141. elif 'summarize' in json_payload:
  142. summarize = json_payload['summarize'] == "1"
  143. if summarize:
  144. chat_messages = app.summary_messages.copy()
  145. max_char_msg = 2000
  146. max_resp_token = 1000
  147. else:
  148. chat_messages = app.chat_messages.copy()
  149. json_payload['payload'][-1]['content'] = f"Please summarize this article:\n" + \
  150. json_payload['payload'][-1]['content']
  151. elif 'assistant_id' in json_payload:
  152. assistant_id = json_payload['assistant_id']
  153. else:
  154. chat_messages = app.chat_messages.copy()
  155. json_payload = json_payload['payload']
  156. if isinstance(json_payload, dict):
  157. json_payload = [json_payload]
  158. elif 'greeting' in json_payload:
  159. chat_messages = app.chat_messages.copy()
  160. company_name = json_payload['greeting']['company_name']
  161. timestamp = json_payload['greeting']['timestamp']
  162. islamic_message = f"Apakah Nama '{company_name}' terdapat unsur islami? Jawab dengan 'Ya' atau 'Tidak'"
  163. islam_messages = app.chat_messages.copy()
  164. islam_messages.append({
  165. "role": "user",
  166. "content": islamic_message
  167. })
  168. islamic_response = app.openai_client.chat.completions.create(model="gpt-3.5-turbo", # GPT-3.5 Turbo engine
  169. messages=islam_messages,
  170. max_tokens=2, temperature=0.5)
  171. if 'Ya' in islamic_response.choices[0].message.content:
  172. greeting_message = f"Buatkan respons chatbot berupa greeting dari chat perusahaan bernama {company_name} pada jam {timestamp}, tidak perlu mention waktu, dan jawab dengan 'Assalamu'alaikum...' terlebih dahulu"
  173. else:
  174. greeting_message = f"Buatkan respons chatbot berupa greeting dari chat perusahaan bernama {company_name} pada jam {timestamp}, tidak perlu mention waktu"
  175. json_payload = [
  176. {
  177. "role": "user",
  178. "content": greeting_message
  179. }
  180. ]
  181. elif 'recommend' in json_payload:
  182. headlines = json_payload['recommend']['headlines']
  183. category = json_payload['recommend']['category']
  184. return recommend(headlines, category)
  185. elif 'image_url' in json_payload:
  186. image = json_payload['image_url']
  187. message = json_payload["message"] if 'message' in json_payload else "Ini gambar apa?"
  188. return vision(message, image_url=image)
  189. elif 'image_b64' in json_payload:
  190. image = json_payload['image_b64']
  191. message = json_payload["message"] if 'message' in json_payload else "Ini gambar apa?"
  192. return vision(message, image_b64=image_url)
  193. else:
  194. chat_messages = app.chat_messages.copy()
  195. json_payload = [json_payload]
  196. json_payload = json_payload[-5:]
  197. for message in json_payload:
  198. if message['role'] == 'user':
  199. content = message['content'].lower()
  200. else:
  201. content = message['content']
  202. content_arr = content.split(" ")
  203. new_content_arr = content[:max_char_msg].split(" ")
  204. new_content_len = len(new_content_arr)
  205. arr = []
  206. for i in range(new_content_len):
  207. arr.append(content_arr[i])
  208. message['content'] = " ".join(arr)
  209. chat_messages.append(message)
  210. app.logger.info(chat_messages)
  211. result = {}
  212. try:
  213. n = num_choices
  214. if assistant_id and not suggest:
  215. runs = app.openai_client.beta.threads.create_and_run_poll(
  216. assistant_id=assistant_id,
  217. thread={
  218. "messages": chat_messages
  219. }
  220. )
  221. messages = list(app.openai_client.beta.threads.messages.list(thread_id=runs.thread_id, run_id=runs.id))
  222. message_content = messages[0].content[0].text
  223. app.logger.info(message_content.value)
  224. pattern = re.compile(r"【\d+:\d+†\(?source\)?】")
  225. filtered_message = pattern.sub("", message_content.value)
  226. result = {"role": "assistant", "content": filtered_message}
  227. else:
  228. json_response = app.openai_client.chat.completions.create(model=chat_model,
  229. messages=chat_messages,
  230. max_tokens=max_resp_token, temperature=0.7, n=n)
  231. app.logger.info(json_response.choices[0].message)
  232. if has_named_params:
  233. if suggest:
  234. choices = json_response.choices
  235. messages = [i.message for i in choices]
  236. json_formatted = []
  237. for message in messages:
  238. json_formatted.append({"role": "assistant", "content": message.content})
  239. result = {"url": "", "message": json_formatted}
  240. else:
  241. if use_video:
  242. # TODO: to be implemented
  243. result = {"url": url_for('download_file', name="test.mp4", _external=True),
  244. "message": {"role": "assistant", "content": json_response.choices[0].message.content}}
  245. else:
  246. result = {"role": "assistant", "content": json_response.choices[0].message.content}
  247. else:
  248. result = {"role": "assistant", "content": json_response.choices[0].message.content}
  249. if predict_q:
  250. query_q = {
  251. "role": "user",
  252. "content": f"Berikan {predict_q} pertanyaan lain yang akan saya ajukan berdasarkan percakapan kali ini dalam bentuk json array"
  253. }
  254. chat_messages.append(result)
  255. chat_messages.append(query_q)
  256. json_response_q = app.openai_client.chat.completions.create(model=chat_model,
  257. messages=chat_messages,
  258. max_tokens=max_resp_token,
  259. temperature=0.2,
  260. response_format={"type": "json_object"})
  261. json_response_dict = json.loads(json_response_q.choices[0].message.content)
  262. if json_response_dict is not None:
  263. if isinstance(json_response_dict, dict):
  264. if len(json_response_dict) > 1:
  265. qs = []
  266. for q in json_response_dict.values():
  267. qs.append(q)
  268. json_response_dict = qs
  269. else:
  270. try:
  271. first_key = next(iter(json_response_dict))
  272. json_response_dict = json_response_dict[first_key]
  273. except StopIteration:
  274. json_response_dict = []
  275. elif isinstance(json_response_dict, str):
  276. json_response_dict = [json_response_dict]
  277. result["predict_q"] = json_response_dict
  278. except openai.APITimeoutError as error_print:
  279. app.logger.exception("error")
  280. result = {"status": "error", "message": error_print.message}, 408
  281. except openai.NotFoundError as error_print:
  282. app.logger.exception("error")
  283. result = {"status": "error", "message": error_print.message}, error_print.status_code
  284. except Exception:
  285. app.logger.exception("error")
  286. result = {}, 405
  287. return result
  288. @app.route('/train', methods=['POST'])
  289. def train():
  290. prev_model = "gpt-3.5-turbo"
  291. if 'job_id' in request.form:
  292. return train_with_id(job_id=request.form['job_id'])
  293. elif 'train_file' in request.files:
  294. train_file = request.files['train_file']
  295. app.logger.info({"filename": train_file.filename})
  296. openai_file = None
  297. if train_file.filename.split('.')[1] == 'jsonl':
  298. openai_file = train_file.stream.read()
  299. elif train_file.filename.split('.')[1] == 'csv':
  300. openai_file = csv_to_jsonl(train_file.stream.read())
  301. elif train_file.filename.split('.')[1] == 'json':
  302. openai_file = alpaca_to_chatgpt(train_file)
  303. if 'prev_model' in request.form:
  304. prev_model = request.form['prev_model']
  305. app.logger.info(f"Previous model: {prev_model}")
  306. if 'mock' not in request.form:
  307. f = app.openai_client.files.create(
  308. file=openai_file,
  309. purpose="fine-tune"
  310. )
  311. job = app.openai_client.fine_tuning.jobs.create(
  312. training_file=f.id,
  313. model=prev_model,
  314. hyperparameters={
  315. "n_epochs": 5
  316. }
  317. )
  318. app.logger.info({"mock": "no", "status": job.status, "job_id": job.id})
  319. retval = {"status": job.status, "job_id": job.id}
  320. return retval
  321. else:
  322. app.logger.info({"mock": "yes", "status": "ok"})
  323. return {"status": "ok"}
  324. else:
  325. app.logger.error({"status": "error", "message": "Training file not found"})
  326. return {"status": "error", "message": "Training file not found"}
  327. def train_with_id(job_id):
  328. try:
  329. job = app.openai_client.fine_tuning.jobs.retrieve(job_id)
  330. if job.fine_tuned_model is None:
  331. app.logger.info({"job_id": job_id, "status": job.status})
  332. return {"status": job.status}
  333. else:
  334. app.logger.info({"job_id": job_id, "status": job.status, "model_name": job.fine_tuned_model})
  335. return {"status": job.status, "model_name": job.fine_tuned_model}
  336. except Exception as error_print:
  337. app.logger.exception("error")
  338. return {"status": "Could not find job from id"}
  339. @app.route('/assistant/create', methods=['POST'])
  340. def assistant_create():
  341. model_name = "gpt-3.5-turbo"
  342. assistant_name = "Assistant"
  343. assistant_ins = "Please respond professionally and in a friendly manner, using the same language as the original request."
  344. vector_store_id = ""
  345. file_batch_id = ""
  346. if request.is_json:
  347. request_form = request.json
  348. else:
  349. request_form = request.form.copy()
  350. assistant_name = request_form.pop('name', assistant_name)
  351. assistant_ins = request_form.pop('instructions', assistant_ins)
  352. model_name = request_form.pop('model_name', model_name)
  353. try:
  354. assistant = app.openai_client.beta.assistants.create(
  355. name=assistant_name,
  356. instructions=assistant_ins,
  357. model=model_name,
  358. tools=[{"type": "file_search"}],
  359. **request_form
  360. )
  361. if 'attachment1' in request.files:
  362. resp_att = assistant_att()
  363. retval = {}
  364. if resp_att['status'] == 'completed':
  365. resp_upd = assistant_update(assistant.id, resp_att['vector_store_id'])
  366. assistant_updated = "1" if resp_upd['status'] == 'ok' else "0"
  367. else:
  368. assistant_updated = "0"
  369. if 'vector_store_id' in resp_att:
  370. retval['vector_store_id'] = resp_att['vector_store_id']
  371. if 'file_batch_id' in resp_att:
  372. retval['file_batch_id'] = resp_att['file_batch_id']
  373. retval['status'] = "ok"
  374. retval['assistant_id'] = assistant.id
  375. retval['assistant_updated'] = assistant_updated
  376. return retval
  377. else:
  378. return {"status": "ok", "assistant_id": assistant.id, "assistant_updated": "0"}
  379. except ValueError as e:
  380. return {"status": "error",
  381. "message": "Failed to create assistant, please check whether your parameters are correct"}
  382. except Exception:
  383. return {"status": "error", "message": "Failed to create assistant, please try again"}
  384. @app.route('/assistant/attachment', methods=['POST'])
  385. def assistant_att():
  386. vector_store_id = request.form.get('vector_store_id', '')
  387. file_batch_id = request.form.get('file_batch_id', '')
  388. attachments: list[str] = []
  389. try:
  390. if not file_batch_id:
  391. if 'attachment1' not in request.files:
  392. return {"status": "error", "message": "No file for attachments"}
  393. else:
  394. has_attachments = True
  395. n = 1
  396. while has_attachments:
  397. if f'attachment{n}' in request.files:
  398. retf = app.openai_client.files.create(
  399. file=(request.files[f'attachment{n}'].filename,
  400. request.files[f'attachment{n}'].read()),
  401. purpose="assistants"
  402. )
  403. retf.filename = request.files[f'attachment{n}'].filename
  404. attachments.append(retf.id)
  405. n = n + 1
  406. else:
  407. has_attachments = False
  408. if vector_store_id:
  409. vector_store = app.openai_client.beta.vector_stores.retrieve(vector_store_id=vector_store_id)
  410. else:
  411. vector_store = app.openai_client.beta.vector_stores.create(
  412. expires_after={
  413. "anchor": "last_active_at",
  414. "days": 365
  415. }
  416. )
  417. file_batch = app.openai_client.beta.vector_stores.file_batches.create_and_poll(
  418. vector_store_id=vector_store.id,
  419. file_ids=attachments
  420. )
  421. return {"status": file_batch.status, "vector_store_id": vector_store.id, "file_batch_id": file_batch.id}
  422. else:
  423. file_batch = app.openai_client.beta.vector_stores.file_batches.retrieve(file_batch_id, vector_store_id=vector_store_id)
  424. return {"status": file_batch.status}
  425. except Exception as e:
  426. app.logger.exception("error")
  427. return {"status": "error", "message": "Upload attachment failed, please try again"}
  428. @app.route('/assistant/update', methods=['POST'])
  429. def assistant_update(aid=None, vid=None):
  430. try:
  431. request_form = request.form.copy()
  432. if aid is not None and vid is not None:
  433. assistant_id = aid
  434. vector_store_id = vid
  435. else:
  436. assistant_id = request_form.pop('assistant_id')
  437. vector_store_id = request_form.pop('vector_store_id', None)
  438. kwargs = {"assistant_id": assistant_id}
  439. if vector_store_id is not None:
  440. kwargs['tool_resources'] = {"file_search": {"vector_store_ids": [vector_store_id]}}
  441. if 'name' in request_form:
  442. kwargs['name'] = request_form.pop('name')
  443. if 'instructions' in request_form:
  444. kwargs['instructions'] = request_form.pop('instructions')
  445. app.openai_client.beta.assistants.update(**kwargs)
  446. return {"status": "ok"}
  447. except Exception as e:
  448. app.logger.exception("error")
  449. return {"status": "error", "message": "Update assistant failed, please try again"}
  450. @app.route('/llama', methods=['POST'])
  451. def llama():
  452. max_char_msg = 500
  453. max_resp_token = 600
  454. json_payload = request.get_json()
  455. if not json_payload:
  456. json_payload = []
  457. has_named_params = False
  458. if isinstance(json_payload, dict):
  459. has_named_params = 'payload' in json_payload
  460. if 'payload' in json_payload:
  461. json_payload = json_payload['payload']
  462. if isinstance(json_payload, dict):
  463. json_payload = [json_payload]
  464. else:
  465. json_payload = [json_payload]
  466. message = json_payload[-1]
  467. content = message['content']
  468. content_arr = content.split(" ")
  469. new_content_arr = content[:max_char_msg].split(" ")
  470. new_content_len = len(new_content_arr)
  471. arr = []
  472. for i in range(new_content_len):
  473. arr.append(content_arr[i])
  474. content = " ".join(arr)
  475. content = content + " Jawab dengan Bahasa Indonesia"
  476. try:
  477. json_request = {
  478. "model": "llama3",
  479. "prompt": content,
  480. "stream": False
  481. }
  482. r = requests.post("http://localhost:11434/api/generate", json=json_request)
  483. if r.status_code == 200:
  484. result = {
  485. "role": "assistant",
  486. "content": r.json()["response"]
  487. }
  488. else:
  489. result = {}, r.status_code
  490. except Exception as error_print:
  491. app.logger.exception("error")
  492. result = {}, 405
  493. return result
  494. # Press the green button in the gutter to run the script.
  495. if __name__ == '__main__':
  496. app.run(host='0.0.0.0', port=8348, debug=True, ssl_context=ssl)
  497. # See PyCharm help at https://www.jetbrains.com/help/pycharm/