init
This commit is contained in:
7
Html/runs/run_20241123-164512_nagqkd/.config
Normal file
7
Html/runs/run_20241123-164512_nagqkd/.config
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"project": "vMjIPa",
|
||||
"name": "nagqkd",
|
||||
"run_id": "run_20241123-164512_nagqkd",
|
||||
"timestamp": "2024-11-23 16:45:12",
|
||||
"pid": 7301
|
||||
}
|
||||
BIN
Html/runs/run_20241123-164512_nagqkd/agentscope.db
Normal file
BIN
Html/runs/run_20241123-164512_nagqkd/agentscope.db
Normal file
Binary file not shown.
185
Html/runs/run_20241123-164512_nagqkd/code/app.py
Normal file
185
Html/runs/run_20241123-164512_nagqkd/code/app.py
Normal file
@@ -0,0 +1,185 @@
|
||||
from flask import Flask, session, request, jsonify, render_template, send_from_directory
|
||||
from flask_cors import CORS
|
||||
from flask_socketio import SocketIO, join_room, emit
|
||||
import markdown
|
||||
import os
|
||||
import uuid
|
||||
import shutil
|
||||
import sys
|
||||
import json
|
||||
parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
|
||||
sys.path.insert(0, parent_dir)
|
||||
current_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '.'))
|
||||
sys.path.insert(0, current_dir)
|
||||
from AlgoriAgent.projects.algoriAgent.agent_manager import AgentManager
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
app.secret_key = 'cakebaker'
|
||||
# 配置 CORS
|
||||
CORS(app, resources={r"/*": {"origins": "http://localhost:9888"}}, supports_credentials=True)
|
||||
|
||||
userid_recorder = {} # user_id&path -> session['user_id']
|
||||
|
||||
'''
|
||||
Backboard
|
||||
'''
|
||||
from backboardManager import BackBoardManager
|
||||
|
||||
backboard_manager = BackBoardManager()
|
||||
|
||||
|
||||
@app.route('/backboard')
|
||||
def backboard():
|
||||
|
||||
return jsonify({'status': 'success'})
|
||||
|
||||
'''
|
||||
Agent and Chat
|
||||
'''
|
||||
|
||||
socketio = SocketIO(app, cors_allowed_origins="*")
|
||||
|
||||
agent_manager = AgentManager()
|
||||
|
||||
def realtime_response(config, realtime_action):
|
||||
print("config"+str(config))
|
||||
print("realtime_action"+str(realtime_action))
|
||||
pass
|
||||
|
||||
@socketio.on('connect')
|
||||
def handle_connect():
|
||||
user = request.args.get('username')
|
||||
folder_name = request.args.get('folder')
|
||||
user_id = userid_recorder[user+'&'+folder_name]
|
||||
session['user_id'] = user_id
|
||||
print(f'User connected with session user_id: {user_id}')
|
||||
# 从markdown、markdown_prompts、score_prompts中各读取数据folder.md并进行new_agent
|
||||
with open(f'books/markdown/{folder_name}.md','r') as fmd,\
|
||||
open(f'books/markdown_prompts/{folder_name}.md','r')as fmdp,\
|
||||
open(f'books/score_prompts/{folder_name}.md','r') as fsp:
|
||||
markdown = fmd.read()
|
||||
markdown_prompts = fmdp.read()
|
||||
score_prompts = fsp.read()
|
||||
print('-----------------------')
|
||||
print(user_id)
|
||||
user_id, agent = agent_manager.new_agent(markdown, markdown_prompts, score_prompts, id=user_id)
|
||||
join_room(user_id) # 将该用户加入以user_id为名的room
|
||||
|
||||
|
||||
# 当接收到前端的消息时,触发此事件
|
||||
@socketio.on('send_message')
|
||||
def handle_message(data):
|
||||
print(f"Message from client: {data}")
|
||||
id = session.get('user_id')
|
||||
# 构造回复消息
|
||||
res = agent_manager.invoke(id, data)
|
||||
reply = f"AI: {res.content['speak']}"
|
||||
# 将回复发送回前端
|
||||
emit('receive_message', reply)
|
||||
|
||||
|
||||
'''
|
||||
Markdown to HTML
|
||||
'''
|
||||
# 配置文件路径
|
||||
MARKDOWN_DIR = 'books/markdown' # 存放 markdown 文件的文件夹
|
||||
STATIC_DIR = 'static' # 存放生成的 HTML 和图片资源的文件夹
|
||||
IMAGE_DIR = 'image' # 图片资源相对于 markdown 的位置
|
||||
|
||||
|
||||
|
||||
@app.route('/<filename>-markdown', methods=['GET'])
|
||||
def convert_md(filename):
|
||||
md_file_path = os.path.join(MARKDOWN_DIR, f'{filename}.md')
|
||||
|
||||
if not os.path.exists(md_file_path):
|
||||
return jsonify({'error': 'Markdown file not found'}), 404
|
||||
|
||||
# 读取 markdown 文件内容
|
||||
with open(md_file_path, 'r', encoding='utf-8') as file:
|
||||
md_content = file.read()
|
||||
|
||||
# 将 markdown 转换为 HTML
|
||||
html_content = markdown.markdown(md_content)
|
||||
# 插入 CSS 样式,限制图片最大宽度为 100%,高度自动调整
|
||||
html_with_styles = f"""
|
||||
<html>
|
||||
<head>
|
||||
<style>
|
||||
img {{
|
||||
max-width: 100%;
|
||||
height: auto;
|
||||
}}
|
||||
</style>
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.2.0/github-markdown.min.css">
|
||||
</head>
|
||||
<body>
|
||||
{html_content}
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
# 保存 HTML 文件到 static 文件夹
|
||||
html_output_path = os.path.join(STATIC_DIR, f'{filename}.html')
|
||||
with open(html_output_path, 'w', encoding='utf-8') as html_file:
|
||||
html_file.write(html_with_styles)
|
||||
|
||||
# 处理图片资源,将 image/xxx 文件夹拷贝到 static/image/xxx
|
||||
image_source_dir = os.path.join(MARKDOWN_DIR, IMAGE_DIR, filename)
|
||||
image_target_dir = os.path.join(STATIC_DIR, IMAGE_DIR, filename)
|
||||
print(f"Copying image resources from {image_source_dir} to {image_target_dir}")
|
||||
if os.path.exists(image_source_dir):
|
||||
# 确保 static/image/xxx 目录存在
|
||||
if not os.path.exists(image_target_dir):
|
||||
os.makedirs(image_target_dir)
|
||||
|
||||
# 拷贝图片资源
|
||||
for image_file in os.listdir(image_source_dir):
|
||||
full_image_file_path = os.path.join(image_source_dir, image_file)
|
||||
if os.path.isfile(full_image_file_path):
|
||||
shutil.copy(full_image_file_path, image_target_dir)
|
||||
|
||||
return jsonify({'html_url': f'/static/{filename}.html'})
|
||||
|
||||
# 静态文件(HTML 和图片资源)的访问
|
||||
@app.route('/static/<path:filename>')
|
||||
def serve_static(filename):
|
||||
return send_from_directory(STATIC_DIR, filename)
|
||||
|
||||
|
||||
'''
|
||||
Vscode
|
||||
'''
|
||||
@app.route('/desktop/<user_id>/<path>')
|
||||
def hello_world(user_id, path):
|
||||
if 'user_id' not in session:
|
||||
session['user_id'] = 'user_' + str(uuid.uuid4())
|
||||
userid_recorder[user_id+'&'+path] = session['user_id']
|
||||
|
||||
# 在上层 目录下创建一个名为 user_id_path 的文件夹
|
||||
path_dir = os.path.join(parent_dir, user_id, path)
|
||||
os.makedirs(path_dir, exist_ok=True)
|
||||
# 在此文件夹内部创建一个.config文件,并写入 user_id=user_id\n path=path
|
||||
config_path = os.path.join(path_dir, '.config')
|
||||
with open(config_path, 'w') as f:
|
||||
f.write(f"user_id={user_id}\npath={path}")
|
||||
return render_template('index.html', user_id=user_id, path=path)
|
||||
|
||||
|
||||
@app.route('/vscode_data', methods=['POST'])
|
||||
def vscode_data():
|
||||
data = request.json
|
||||
config = data['config']
|
||||
realtime_response(config,data)
|
||||
print(f"Received data from VSCode: {data}")
|
||||
return jsonify({"status": "success", "received": data})
|
||||
|
||||
@app.route('/get_session')
|
||||
def get_session():
|
||||
user_session = session.get('user_id', 'default_session')
|
||||
return jsonify({"session": user_session})
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
socketio.run(app, host='0.0.0.0', port=5500)
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
|
||||
|
||||
|
||||
import time
|
||||
|
||||
|
||||
class BackBoardManager:
|
||||
def __init__(self):
|
||||
self.backboards = {} # user_id: Backboard
|
||||
|
||||
def add_backboard(self, user_id, folder):
|
||||
self.backboards[user_id] = Backboard(user_id, folder)
|
||||
|
||||
def get_backboard(self, user_id):
|
||||
if user_id not in self.backboards:
|
||||
return None
|
||||
return self.backboards[user_id]
|
||||
|
||||
|
||||
|
||||
|
||||
class Backboard:
|
||||
def __init__(self, user_id, folder):
|
||||
self.user_id = user_id
|
||||
self.folder = folder
|
||||
self.create_time = time.time()
|
||||
self.enter_chapter_time = time.time()
|
||||
|
||||
def next_chapter(self):
|
||||
self.enter_chapter_time = time.time()
|
||||
|
||||
def get_info_prompt(self):
|
||||
return f"###Global Info:###\n\
|
||||
User is {self.user_id}\n\
|
||||
User's total study time is {self.get_deltatime_mmss(self.create_time, time.time())}\n\
|
||||
User's current chapter study time is {self.get_deltatime_mmss(self.enter_chapter_time, time.time())}\n\
|
||||
\n\n\
|
||||
"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def get_deltatime_mmss(self, start_time, end_time):
|
||||
elapsed_time = end_time - start_time
|
||||
hours = int(elapsed_time // 3600)
|
||||
minutes = int((elapsed_time % 3600) // 60)
|
||||
seconds = int(elapsed_time % 60)
|
||||
return f"{hours:02d}:{minutes:02d}:{seconds:02d}"
|
||||
|
||||
|
||||
9
Html/runs/run_20241123-164512_nagqkd/logging.chat
Normal file
9
Html/runs/run_20241123-164512_nagqkd/logging.chat
Normal file
@@ -0,0 +1,9 @@
|
||||
{"id": "54fb4c74af28475195a746d632f662c7", "timestamp": "2024-11-23 16:45:43", "name": "assistant", "content": "##################### ITER 1, STEP 1: REASONING ######################", "role": "assistant", "url": null, "metadata": null}
|
||||
{"id": "872e281f8f644e09bd60a163be9e74d7", "timestamp": "2024-11-23 16:45:43", "name": "system", "content": "", "role": "system", "url": null, "metadata": null}
|
||||
{"id": "e29ff932b2f645e08e193734ddc5aff9", "timestamp": "2024-11-23 16:45:43", "name": "system", "content": "Respond a JSON dictionary in a markdown's fenced code block as follows:\n```json\n{\"thought\": \"what you thought\", \"speak\": \"what you speak\", \"function\": \"[{\\\"name\\\": \\\"{function name}\\\", \\\"arguments\\\": {\\\"{argument1 name}\\\": xxx, \\\"{argument2 name}\\\": xxx}}]\"}\n```\n ####", "role": "system", "url": null, "metadata": null}
|
||||
{"id": "f8f81ff2a4ad4fb4bfb9c3ebfc33e0a0", "timestamp": "2024-11-23 16:45:43", "name": "assistant", "content": "################################Prompt################################", "role": "assistant", "url": null, "metadata": null}
|
||||
{"id": "3917d85412cc433f8b6d8aba4a6fe401", "timestamp": "2024-11-23 16:45:43", "name": "assistant", "content": "hi", "role": "assistant", "url": null, "metadata": null}
|
||||
{"id": "fe6498280e4b40ddb08efca72890a8a6", "timestamp": "2024-11-23 16:45:46", "name": "assistant", "content": "############################Result Parsed#############################", "role": "assistant", "url": null, "metadata": null}
|
||||
{"id": "92a498ab37e043a9b871fa5040782f0e", "timestamp": "2024-11-23 16:45:46", "name": "assistant", "content": "{'thought': \"The student seems to be greeting me but hasn't yet asked a question related to the current chapter on binary search. I should encourage them to engage with the material.\", 'speak': 'Hello! How can I assist you with your understanding of binary search today?', 'function': '[]'}", "role": "assistant", "url": null, "metadata": null}
|
||||
{"id": "721f5622fcdf44988cc72609546d9b83", "timestamp": "2024-11-23 16:45:46", "name": "assistant", "content": "################################Speak#################################", "role": "assistant", "url": null, "metadata": null}
|
||||
{"id": "ad1c1410f93f4b8b823df56f32c5db12", "timestamp": "2024-11-23 16:45:46", "name": "assistant", "content": {"thought": "The student seems to be greeting me but hasn't yet asked a question related to the current chapter on binary search. I should encourage them to engage with the material.", "speak": "Hello! How can I assist you with your understanding of binary search today?", "function": "[]"}, "role": "assistant", "url": null, "metadata": null}
|
||||
22
Html/runs/run_20241123-164512_nagqkd/logging.log
Normal file
22
Html/runs/run_20241123-164512_nagqkd/logging.log
Normal file
@@ -0,0 +1,22 @@
|
||||
2024-11-23 16:45:22.355 | INFO | agentscope.models:read_model_configs:186 - Load configs for model wrapper: openai_cfg
|
||||
2024-11-23 16:45:31.351 | INFO | agentscope.models.model:__init__:201 - Initialize model by configuration [openai_cfg]
|
||||
2024-11-23 16:45:31.371 | WARNING | agentscope.models.openai_model:__init__:83 - fail to get max_length for gpt-4o-mini: 'Model [gpt-4o-mini] not found in OPENAI_MAX_LENGTH. The last updated date is 20231212'
|
||||
2024-11-23 16:45:31.372 | INFO | agentscope.utils.monitor:register_budget:609 - set budget None to gpt-4o-mini
|
||||
2024-11-23 16:45:31.372 | WARNING | agentscope.utils.monitor:register_budget:639 - Calculate budgets for model [gpt-4o-mini] is not supported
|
||||
2024-11-23 16:45:31.386 | INFO | agentscope.utils.monitor:register:417 - Register metric [gpt-4o-mini.call_counter] to SqliteMonitor with unit [times] and quota [None]
|
||||
2024-11-23 16:45:31.408 | INFO | agentscope.utils.monitor:register:417 - Register metric [gpt-4o-mini.prompt_tokens] to SqliteMonitor with unit [token] and quota [None]
|
||||
2024-11-23 16:45:31.431 | INFO | agentscope.utils.monitor:register:417 - Register metric [gpt-4o-mini.completion_tokens] to SqliteMonitor with unit [token] and quota [None]
|
||||
2024-11-23 16:45:31.452 | INFO | agentscope.utils.monitor:register:417 - Register metric [gpt-4o-mini.total_tokens] to SqliteMonitor with unit [token] and quota [None]
|
||||
assistant: ##################### ITER 1, STEP 1: REASONING ######################
|
||||
system:
|
||||
system: Respond a JSON dictionary in a markdown's fenced code block as follows:
|
||||
```json
|
||||
{"thought": "what you thought", "speak": "what you speak", "function": "[{\"name\": \"{function name}\", \"arguments\": {\"{argument1 name}\": xxx, \"{argument2 name}\": xxx}}]"}
|
||||
```
|
||||
####
|
||||
assistant: ################################Prompt################################
|
||||
assistant: hi
|
||||
assistant: ############################Result Parsed#############################
|
||||
assistant: {'thought': "The student seems to be greeting me but hasn't yet asked a question related to the current chapter on binary search. I should encourage them to engage with the material.", 'speak': 'Hello! How can I assist you with your understanding of binary search today?', 'function': '[]'}
|
||||
assistant: ################################Speak#################################
|
||||
assistant: {'thought': "The student seems to be greeting me but hasn't yet asked a question related to the current chapter on binary search. I should encourage them to engage with the material.", 'speak': 'Hello! How can I assist you with your understanding of binary search today?', 'function': '[]'}
|
||||
Reference in New Issue
Block a user