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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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'
|
| 11 |
+
|
| 12 |
+
app.config['SESSION_TYPE'] = 'filesystem'
|
| 13 |
+
Session(app)
|
| 14 |
+
|
| 15 |
+
prompt_dict = {}
|
| 16 |
+
|
| 17 |
+
@app.route('/')
|
| 18 |
+
def index():
|
| 19 |
+
return render_template('index.html', prompts=prompt_dict)
|
| 20 |
+
|
| 21 |
+
@app.route('/add', methods=['POST'])
|
| 22 |
+
def add_prompt():
|
| 23 |
+
prompt = request.form['prompt'].strip()
|
| 24 |
+
response = request.form['response'].strip()
|
| 25 |
+
if prompt and response:
|
| 26 |
+
prompt_dict[prompt] = response
|
| 27 |
+
flash('Prompt added successfully.')
|
| 28 |
+
else:
|
| 29 |
+
flash('Prompt or response cannot be empty.')
|
| 30 |
+
return redirect(url_for('index'))
|
| 31 |
+
|
| 32 |
+
@app.route('/gpt3', methods=['POST'])
|
| 33 |
+
def gpt3():
|
| 34 |
+
prompt = request.form['prompt']
|
| 35 |
+
|
| 36 |
+
# Initialize message_history in session if it doesn't exist
|
| 37 |
+
if 'message_history' not in session:
|
| 38 |
+
session['message_history'] = []
|
| 39 |
+
|
| 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
|
| 61 |
+
return jsonify({"text": response_text.strip()})
|
| 62 |
+
except Exception as e:
|
| 63 |
+
return jsonify({"error": str(e)})
|
| 64 |
+
|
| 65 |
+
@app.route('/super_coder', methods=['POST'])
|
| 66 |
+
def super_coder():
|
| 67 |
+
super_coder_choice = request.form['super_coder_choice']
|
| 68 |
+
|
| 69 |
+
if super_coder_choice == "1":
|
| 70 |
+
app_name = request.form['app_name']
|
| 71 |
+
app_prompt = request.form['app_prompt']
|
| 72 |
+
|
| 73 |
+
# Initialize super_coder_history in session if it doesn't exist
|
| 74 |
+
if 'super_coder_history' not in session:
|
| 75 |
+
session['super_coder_history'] = []
|
| 76 |
+
|
| 77 |
+
session['super_coder_history'].append(f"New application {app_name} setup initiated.")
|
| 78 |
+
|
| 79 |
+
while True:
|
| 80 |
+
choice = request.form['choice']
|
| 81 |
+
|
| 82 |
+
if choice == "1":
|
| 83 |
+
session['super_coder_history'].append("Continuing development...")
|
| 84 |
+
response = get_litellm_response(f"Continue developing the {app_name} application based on the prompt: {app_prompt}", session['super_coder_history'])
|
| 85 |
+
session['super_coder_history'].append(response)
|
| 86 |
+
elif choice == "2":
|
| 87 |
+
guidance = request.form['guidance']
|
| 88 |
+
response = get_litellm_response(f"Provide guidance for the current development of {app_name}: {guidance}", session['super_coder_history'])
|
| 89 |
+
session['super_coder_history'].append(response)
|
| 90 |
+
elif choice == "3":
|
| 91 |
+
break
|
| 92 |
+
else:
|
| 93 |
+
session['super_coder_history'].append("Invalid choice. Please try again.")
|
| 94 |
+
|
| 95 |
+
session.modified = True
|
| 96 |
+
return jsonify({"history": session['super_coder_history']})
|
| 97 |
+
|
| 98 |
+
elif super_coder_choice == "2":
|
| 99 |
+
template_choice = request.form['template_choice']
|
| 100 |
+
|
| 101 |
+
template_instructions = {
|
| 102 |
+
"1": "Create a PyTorch application template with basic structure and dependencies.",
|
| 103 |
+
"2": "Create a machine learning pipeline template with data preprocessing, model training, and evaluation steps.",
|
| 104 |
+
"3": "Create a template that demonstrates the integration of Mergekit library for advanced functionality."
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
if template_choice in template_instructions:
|
| 108 |
+
instruction = template_instructions[template_choice]
|
| 109 |
+
response = get_litellm_response(instruction, [])
|
| 110 |
+
return jsonify({"text": response})
|
| 111 |
+
else:
|
| 112 |
+
return jsonify({"error": "Invalid choice. Please try again."})
|
| 113 |
+
|
| 114 |
+
elif super_coder_choice == "3":
|
| 115 |
+
prompt = request.form['prompt']
|
| 116 |
+
auto_steps = int(request.form['auto_steps'])
|
| 117 |
+
additional_steps = int(request.form['additional_steps'])
|
| 118 |
+
|
| 119 |
+
def autonomous_coding(prompt, steps, additional_steps):
|
| 120 |
+
history = []
|
| 121 |
+
|
| 122 |
+
if steps == 0:
|
| 123 |
+
while True:
|
| 124 |
+
choice = request.form['choice']
|
| 125 |
+
|
| 126 |
+
if choice == "1":
|
| 127 |
+
history.append("Continuing development...")
|
| 128 |
+
response = get_litellm_response(f"Continue developing the code based on the previous prompt: {prompt}", history)
|
| 129 |
+
history.append(response)
|
| 130 |
+
elif choice == "2":
|
| 131 |
+
guidance = request.form['guidance']
|
| 132 |
+
response = get_litellm_response(f"Provide guidance for the current development: {guidance}", history)
|
| 133 |
+
history.append(response)
|
| 134 |
+
elif choice == "3":
|
| 135 |
+
break
|
| 136 |
+
else:
|
| 137 |
+
history.append("Invalid choice. Please try again.")
|
| 138 |
+
else:
|
| 139 |
+
for i in range(steps):
|
| 140 |
+
history.append(f"Autonomous Coding Step {i+1}/{steps}")
|
| 141 |
+
response = get_litellm_response(f"Continue developing the code autonomously based on the prompt: {prompt}", history)
|
| 142 |
+
history.append(response)
|
| 143 |
+
|
| 144 |
+
if additional_steps > 0:
|
| 145 |
+
for i in range(additional_steps):
|
| 146 |
+
history.append(f"Additional Autonomous Coding Step {i+1}/{additional_steps}")
|
| 147 |
+
response = get_litellm_response(f"Continue developing the code autonomously based on the prompt: {prompt}", history)
|
| 148 |
+
history.append(response)
|
| 149 |
+
|
| 150 |
+
autonomous_coding(prompt, 0, 0)
|
| 151 |
+
|
| 152 |
+
return history
|
| 153 |
+
|
| 154 |
+
history = autonomous_coding(prompt, auto_steps, additional_steps)
|
| 155 |
+
return jsonify({"history": history})
|
| 156 |
+
|
| 157 |
+
elif super_coder_choice == "4":
|
| 158 |
+
# Placeholder for advanced settings
|
| 159 |
+
return jsonify({"text": "Advanced Settings functionality to be implemented."})
|
| 160 |
+
|
| 161 |
+
elif super_coder_choice == "5":
|
| 162 |
+
# Placeholder for managing prompt folder
|
| 163 |
+
return jsonify({"text": "Manage Prompt Folder functionality to be implemented."})
|
| 164 |
+
|
| 165 |
+
elif super_coder_choice == "6":
|
| 166 |
+
return jsonify({"text": "Returning to the main menu..."})
|
| 167 |
+
|
| 168 |
+
else:
|
| 169 |
+
return jsonify({"error": "Invalid choice. Please try again."})
|
| 170 |
+
|
| 171 |
+
import json
|
| 172 |
+
|
| 173 |
+
@app.route('/clear_session', methods=['GET'])
|
| 174 |
+
def clear_session():
|
| 175 |
+
session.clear()
|
| 176 |
+
return jsonify({"result": "Session cleared"})
|
| 177 |
+
|
| 178 |
+
@app.route('/history')
|
| 179 |
+
def history():
|
| 180 |
+
if 'message_history' in session:
|
| 181 |
+
message_history = session['message_history']
|
| 182 |
+
# Convert the message history array to a JSON string
|
| 183 |
+
history_str = json.dumps(message_history)
|
| 184 |
+
return jsonify({"history": history_str})
|
| 185 |
+
else:
|
| 186 |
+
return jsonify({"history": "No message history found in the current session."})
|
| 187 |
+
|
| 188 |
+
if __name__ == '__main__':
|
| 189 |
+
app.run(host='0.0.0.0', port=8080)
|