mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-06-02 14:45:17 +02:00
fix: Fix bug for merging
This commit is contained in:
parent
db2ea905b4
commit
a2484f6420
2 changed files with 14 additions and 28 deletions
|
|
@ -3,25 +3,24 @@
|
|||
"""
|
||||
@Time : 2024/3/27 9:44
|
||||
@Author : leiwu30
|
||||
@File : flask_web_api.py
|
||||
@File : stream_output_via_api.py
|
||||
@Description : Stream log information and communicate over the network via web api.
|
||||
"""
|
||||
import os
|
||||
import json
|
||||
import socket
|
||||
import asyncio
|
||||
import threading
|
||||
|
||||
from metagpt.team import Team
|
||||
from metagpt.const import METAGPT_ROOT
|
||||
from metagpt.logs import set_llm_stream_logfunc
|
||||
from metagpt.utils.stream_pipe import StreamPipe
|
||||
from metagpt.roles.tutorial_assistant import TutorialAssistant
|
||||
|
||||
from contextvars import ContextVar
|
||||
from flask import Flask, Response
|
||||
from flask import request, jsonify, send_from_directory
|
||||
|
||||
from metagpt.logs import logger
|
||||
from metagpt.const import TUTORIAL_PATH
|
||||
from metagpt.logs import set_llm_stream_logfunc
|
||||
from metagpt.utils.stream_pipe import StreamPipe
|
||||
from metagpt.roles.tutorial_assistant import TutorialAssistant
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
|
||||
|
|
@ -35,7 +34,7 @@ def stream_pipe_log(content):
|
|||
def write_tutorial(message):
|
||||
async def main(idea, stream_pipe):
|
||||
stream_pipe_var.set(stream_pipe)
|
||||
role = TutorialAssistant(stream_pipe=stream_pipe)
|
||||
role = TutorialAssistant()
|
||||
await role.run(idea)
|
||||
|
||||
def thread_run(idea: str, stream_pipe: StreamPipe = None):
|
||||
|
|
@ -52,11 +51,6 @@ def write_tutorial(message):
|
|||
msg = stream_pipe.get_message()
|
||||
yield stream_pipe.msg2stream(msg)
|
||||
|
||||
# 文件位置
|
||||
# md_file = stream_pipe.get_k_message("file_name")
|
||||
# yield stream_pipe.msg2stream(
|
||||
# f"\n\n[{os.path.basename(md_file)}](http://{server_address}:{server_port}/download/{md_file})")
|
||||
|
||||
|
||||
@app.route('/v1/chat/completions', methods=['POST'])
|
||||
def completions():
|
||||
|
|
@ -74,14 +68,15 @@ def completions():
|
|||
"""
|
||||
|
||||
data = json.loads(request.data)
|
||||
print(json.dumps(data, indent=4, ensure_ascii=False))
|
||||
logger.info(json.dumps(data, indent=4, ensure_ascii=False))
|
||||
|
||||
# Non-streaming interfaces are not supported yet
|
||||
stream_type = True if "stream" in data.keys() and data["stream"] else False
|
||||
stream_type = True if data.get("stream") else False
|
||||
if not stream_type:
|
||||
return jsonify({"status": 200})
|
||||
return jsonify({"status": 400, "msg": "Non-streaming requests are not supported, please use `stream=True`."})
|
||||
|
||||
# Only accept the last user information
|
||||
# openai['model'] ~ MetaGPT['agent']
|
||||
last_message = data["messages"][-1]
|
||||
model = data["model"]
|
||||
|
||||
|
|
@ -89,13 +84,12 @@ def completions():
|
|||
if model == "write_tutorial":
|
||||
return Response(write_tutorial(last_message), mimetype="text/plain")
|
||||
else:
|
||||
return jsonify({"status": 200})
|
||||
# return Response(event_stream(), mimetype="text/plain")
|
||||
return jsonify({"status": 400, "msg": "No suitable agent found."})
|
||||
|
||||
|
||||
@app.route('/download/<path:filename>')
|
||||
def download_file(filename):
|
||||
return send_from_directory(METAGPT_ROOT, filename, as_attachment=True)
|
||||
return send_from_directory(TUTORIAL_PATH, filename, as_attachment=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
@ -12,8 +12,6 @@ from multiprocessing import Pipe
|
|||
|
||||
class StreamPipe:
|
||||
parent_conn, child_conn = Pipe()
|
||||
|
||||
variable: list = {}
|
||||
finish: bool = False
|
||||
|
||||
format_data = {
|
||||
|
|
@ -45,12 +43,6 @@ class StreamPipe:
|
|||
else:
|
||||
return None
|
||||
|
||||
def set_k_message(self, k, msg):
|
||||
self.variable[k] = msg
|
||||
|
||||
def get_k_message(self, k):
|
||||
return self.variable[k]
|
||||
|
||||
def msg2stream(self, msg):
|
||||
self.format_data['created'] = int(time.time())
|
||||
self.format_data['choices'][0]['delta']['content'] = msg
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue