|
@@ -8,12 +8,15 @@ from openai import ChatCompletion
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
ssl = None
|
|
|
-openai_key = os.environ.get("OPENAI_KEY","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")
|
|
|
+# ssl =('/etc/ssl/sample.crt', '/etc/ssl/sample.pem')
|
|
|
+openai_key = os.environ.get("OPENAI_KEY", "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")
|
|
|
openai.api_key = openai_key
|
|
|
app.chat_messages = [
|
|
|
- {"role": "system", "content": "Kamu adalah asisten yang membantu dan selalu menjawab dengan singkat"}
|
|
|
+ {"role": "system",
|
|
|
+ "content": "Jawab hanya dengan bahasa Indonesia"}
|
|
|
]
|
|
|
|
|
|
+
|
|
|
@app.route('/', methods=['GET', 'POST'])
|
|
|
def test():
|
|
|
return jsonify({"status": "0"})
|
|
@@ -24,12 +27,32 @@ def gpt():
|
|
|
json_payload = request.get_json()
|
|
|
if not json_payload:
|
|
|
json_payload = []
|
|
|
+ if isinstance(json_payload, dict):
|
|
|
+ json_payload = [json_payload]
|
|
|
+ json_payload = json_payload[-5:]
|
|
|
for message in json_payload:
|
|
|
+ content = message['content']
|
|
|
+ content_arr = content.split(" ")
|
|
|
+ new_content_arr = content[:250].split(" ")
|
|
|
+ new_content_len = len(new_content_arr)
|
|
|
+ arr = []
|
|
|
+ for i in range(new_content_len):
|
|
|
+ arr.append(content_arr[i])
|
|
|
+ message['content'] = " ".join(arr)
|
|
|
chat_messages.append(message)
|
|
|
- response = openai.ChatCompletion.create(model="gpt-3.5-turbo", # GPT-3.5 Turbo engine
|
|
|
- messages=json_payload,
|
|
|
- max_tokens=400,)
|
|
|
- return response.choices[0].message
|
|
|
+ print(chat_messages)
|
|
|
+ result = {}
|
|
|
+ try:
|
|
|
+ json_response = openai.ChatCompletion.create(model="gpt-3.5-turbo", # GPT-3.5 Turbo engine
|
|
|
+ messages=chat_messages,
|
|
|
+ max_tokens=300, temperature=0.6)
|
|
|
+ print(json_response.choices[0].message)
|
|
|
+ result = json_response.choices[0].message
|
|
|
+ except Exception as error_print:
|
|
|
+ print(error_print)
|
|
|
+ result = {}, 405
|
|
|
+ return result
|
|
|
+
|
|
|
|
|
|
# Press the green button in the gutter to run the script.
|
|
|
if __name__ == '__main__':
|