123 lines
6.1 KiB
Python
123 lines
6.1 KiB
Python
import sys
|
||
import os
|
||
current_directory = os.path.dirname(os.path.abspath(__file__))
|
||
sys.path.append(current_directory)
|
||
from flask_socketio import SocketIO, join_room, emit, Namespace
|
||
import agentscope
|
||
from agentscope.message import Msg
|
||
import os
|
||
import asyncio
|
||
import threading
|
||
import json
|
||
|
||
|
||
import configparser
|
||
from ...function_tools.function_manager import FunctionManager
|
||
|
||
|
||
import uuid
|
||
from ...utils import *
|
||
EMPTY_CHAPTER_CHAIN = [ Chapter(1, CHAPTER_FOCUS, "本章是未打开某个具体章节时的默认章节。", "处于本章节时不会有任何章节跳转。请作为一名经验丰富的算法教师,回答用户的问题。") ]
|
||
|
||
|
||
|
||
class ChatManager:
|
||
def __init__(self,user_uuid,ase_client,raw_markdown,raw_markdown_prompts,raw_score_prompts,max_iter = 5, app=None, socketio=None):
|
||
self.chapter_chain_now = -1
|
||
self.ase_client = ase_client
|
||
self.app = app
|
||
self.socketio = socketio
|
||
self.user_uuid = user_uuid
|
||
self.raw_markdown = raw_markdown
|
||
self.raw_markdown_prompts = raw_markdown_prompts
|
||
self.raw_score_prompts = raw_score_prompts
|
||
self.chapter_chain = load_chapters(raw_markdown, raw_markdown_prompts, raw_score_prompts)
|
||
self.function_manager = FunctionManager()
|
||
self.bb = None
|
||
|
||
def next_chapter(self):
|
||
assert self.chapter_chain_now + 1 < len(self.chapter_chain), "chapter_chain_now out of range"
|
||
self.chapter_chain_now += 1
|
||
self.focus_chapter(self.chapter_chain_now)
|
||
|
||
def focus_chapter(self, chapter_index):
|
||
assert chapter_index < len(self.chapter_chain), "chapter_index out of range"
|
||
self.chapter_chain_now = chapter_index
|
||
self.chapter_chain[chapter_index].Focus()
|
||
self.ase_client.loadmarkdown(self.chapter_chain[chapter_index].chapter, self.chapter_chain[chapter_index].chapter_prompt, self.chapter_chain[chapter_index].score_prompt)
|
||
|
||
def upload_bb(self):
|
||
self.ase_client.send_text('backboard-in', self.bb.get_info_prompt())
|
||
|
||
def message_pass(self, messagetype, message):
|
||
print("message_pass", self.user_uuid, messagetype, message)
|
||
with self.app.app_context():
|
||
try:
|
||
self.socketio.emit(messagetype, message, room=self.user_uuid, namespace='/agent')
|
||
except Exception as e:
|
||
print("message_pass",e)
|
||
|
||
def system_message(self, message):
|
||
with self.app.app_context():
|
||
try:
|
||
self.socketio.emit('system_message', message, room=self.user_uuid, namespace='/agent')
|
||
except Exception as e:
|
||
print("ERROR: system_message",e)
|
||
|
||
|
||
def save_chapter_memory(self, agent,course_id, lesson_id, subchapter_title, mem_list, score, is_rebuttal):
|
||
|
||
self.app.my_function.save_chapter_memory(self.user_uuid, course_id, lesson_id, subchapter_title, mem_list, score, is_rebuttal)
|
||
|
||
|
||
|
||
def function_call(self, function_name, arg_function):
|
||
return self.function_manager.call_function(function_name, arg_function)
|
||
|
||
def change_language(self, id, language):
|
||
self.ase_client.send_text('language', language)
|
||
|
||
def sample_judge(self, id, bb):
|
||
self.function_manager.call_function('sample_judge', {'bb': bb})
|
||
|
||
with self.app.app_context():
|
||
try:
|
||
print(bb.get_active_file_reletive_path())
|
||
assert bb is not None and bb.active_file_path != "" , "no path specified"
|
||
if bb.get_active_file_reletive_path().endswith(".py"):
|
||
self.socketio.emit('terminal', {'data': f"python {bb.get_active_file_reletive_path()}"}, room=id, namespace='/vscode')
|
||
self.socketio.emit('system_message', "代码试运行尚在建设,推荐直接通过命令行执行!", room=id, namespace='/agent')
|
||
else:
|
||
self.socketio.emit('system_message', "代码试运行尚仅支持python文件", room=id, namespace='/agent')
|
||
except Exception as e:
|
||
print(e)
|
||
if (str(e) == "no path specified"):
|
||
self.socketio.emit("system_message", "未加载IDE文件同步,请尝试重新打开IDE文件或刷新页面,并确保插件已连接", room=id, namespace='/agent')
|
||
|
||
def judge(self, id, bb):
|
||
agent = self.agents[id]
|
||
with self.app.app_context():
|
||
try:
|
||
if (agent.chapter_chain[agent.chapter_chain_now].append_tools is not None and len(agent.chapter_chain[agent.chapter_chain_now].append_tools)):
|
||
for tool, tool_kwargs in agent.chapter_chain[agent.chapter_chain_now].append_tools:
|
||
if tool.__name__ == "judge":
|
||
print(bb.get_active_file_reletive_path())
|
||
assert bb is not None and bb.active_file_path != "" , "no path specified"
|
||
if bb.get_active_file_reletive_path().endswith(".py"):
|
||
# self.socketio.emit('terminal', {'data': f"python {bb.get_active_file_reletive_path()}"}, room=id, namespace='/vscode')
|
||
self.socketio.emit('system_message', "代码评测中,请稍后", room=id, namespace='/agent')
|
||
agent.function_call({'name':'judge','arguments':{'filepath': bb.get_active_file_reletive_path()}})
|
||
else:
|
||
self.socketio.emit('system_message', "代码评测尚仅支持python文件", room=id, namespace='/agent')
|
||
|
||
else:
|
||
with self.app.app_context():
|
||
self.socketio.emit("system_message", "本章学习无需评测", room=id, namespace='/agent')
|
||
else:
|
||
with self.app.app_context():
|
||
self.socketio.emit("system_message", "本章学习无需评测", room=id, namespace='/agent')
|
||
except Exception as e:
|
||
if (str(e) == "no path specified"):
|
||
self.socketio.emit("system_message", "未加载IDE文件同步,请尝试重新打开IDE文件或刷新页面,并确保插件已连接", room=id, namespace='/agent')
|
||
|