Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
-
import
|
| 2 |
import os
|
| 3 |
from flask import Flask, render_template, request, jsonify, session
|
| 4 |
from flask_session import Session
|
| 5 |
from datetime import datetime, timedelta
|
| 6 |
|
| 7 |
-
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
| 10 |
app.secret_key = 'your_secret_key'
|
|
@@ -40,22 +40,21 @@ def gpt3():
|
|
| 40 |
# Append the user input to message_history
|
| 41 |
session['message_history'].append("User: " + prompt)
|
| 42 |
|
| 43 |
-
def
|
| 44 |
-
# Convert message history to the format required by
|
| 45 |
-
|
| 46 |
|
| 47 |
-
response =
|
| 48 |
model="gpt-3.5-turbo",
|
| 49 |
-
messages=
|
| 50 |
max_tokens=1200,
|
| 51 |
temperature=0.85,
|
| 52 |
-
n=1
|
| 53 |
-
stream=False
|
| 54 |
)
|
| 55 |
-
return response
|
| 56 |
|
| 57 |
try:
|
| 58 |
-
response_text =
|
| 59 |
# Append the assistant's response to message_history
|
| 60 |
session['message_history'].append("Assistant: " + response_text.strip())
|
| 61 |
session.modified = True # Ensure the session is saved after modification
|
|
@@ -79,7 +78,6 @@ def history():
|
|
| 79 |
return jsonify({"history": history_str})
|
| 80 |
else:
|
| 81 |
return jsonify({"history": "No message history found in the current session."})
|
| 82 |
-
|
| 83 |
-
|
| 84 |
if __name__ == '__main__':
|
| 85 |
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 1 |
+
from litellm import completion
|
| 2 |
import os
|
| 3 |
from flask import Flask, render_template, request, jsonify, session
|
| 4 |
from flask_session import Session
|
| 5 |
from datetime import datetime, timedelta
|
| 6 |
|
| 7 |
+
# os.environ["OPENAI_API_KEY"] = os.environ.get("OPENAI_API_KEY")
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
| 10 |
app.secret_key = 'your_secret_key'
|
|
|
|
| 40 |
# Append the user input to message_history
|
| 41 |
session['message_history'].append("User: " + prompt)
|
| 42 |
|
| 43 |
+
def get_litellm_response(user_input, message_history):
|
| 44 |
+
# Convert message history to the format required by LiteLLM
|
| 45 |
+
messages = [{"role": msg.split(': ')[0].lower(), "content": msg.split(': ')[1]} for msg in message_history]
|
| 46 |
|
| 47 |
+
response = completion(
|
| 48 |
model="gpt-3.5-turbo",
|
| 49 |
+
messages=messages,
|
| 50 |
max_tokens=1200,
|
| 51 |
temperature=0.85,
|
| 52 |
+
n=1
|
|
|
|
| 53 |
)
|
| 54 |
+
return response['choices'][0]['message']['content']
|
| 55 |
|
| 56 |
try:
|
| 57 |
+
response_text = get_litellm_response(prompt, session['message_history'])
|
| 58 |
# Append the assistant's response to message_history
|
| 59 |
session['message_history'].append("Assistant: " + response_text.strip())
|
| 60 |
session.modified = True # Ensure the session is saved after modification
|
|
|
|
| 78 |
return jsonify({"history": history_str})
|
| 79 |
else:
|
| 80 |
return jsonify({"history": "No message history found in the current session."})
|
| 81 |
+
|
|
|
|
| 82 |
if __name__ == '__main__':
|
| 83 |
app.run(host='0.0.0.0', port=7860)
|