main.py 21 KB

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