🌟 The Multi-Agent Framework: First AI Software Company, Towards Natural Language Programming
format code
seehi549cb2d90b31
diff --git a/examples/stream_output_via_api.py b/examples/stream_output_via_api.pyindex 94709b5bf..5961f3a08 100644--- a/examples/stream_output_via_api.py+++ b/examples/stream_output_via_api.py@@ -6,20 +6,18 @@@File : stream_output_via_api.py@Description : Stream log information and communicate over the network via web api."""+import asyncioimport jsonimport socket-import asyncioimport threading-from contextvars import ContextVar-from flask import Flask, Response-from flask import request, jsonify, send_from_directory-from metagpt.logs import logger+from flask import Flask, Response, jsonify, request, send_from_directory+from metagpt.const import TUTORIAL_PATH-from metagpt.logs import set_llm_stream_logfunc-from metagpt.utils.stream_pipe import StreamPipe+from metagpt.logs import logger, set_llm_stream_logfuncfrom metagpt.roles.tutorial_assistant import TutorialAssistant+from metagpt.utils.stream_pipe import StreamPipeapp = Flask(__name__)@@ -39,12 +37,18 @@ def write_tutorial(message):def thread_run(idea: str, stream_pipe: StreamPipe = None):"""- Convert asynchronous function to thread function+ Convert asynchronous function to thread function"""asyncio.run(main(idea, stream_pipe))stream_pipe = StreamPipe()- thread = threading.Thread(target=thread_run, args=(message["content"], stream_pipe,))+ thread = threading.Thread(+ target=thread_run,+ args=(+ message["content"],+ stream_pipe,+ ),+ )thread.start()while thread.is_alive():@@ -52,7 +56,7 @@ def write_tutorial(message):yield stream_pipe.msg2stream(msg)[email protected]('/v1/chat/completions', methods=['POST'])[email protected]("/v1/chat/completions", methods=["POST"])def completions():"""data: {@@ -87,7 +91,7 @@ def completions():return jsonify({"status": 400, "msg": "No suitable agent found."})[email protected]('/download/<path:filename>')[email protected]("/download/<path:filename>")def download_file(filename):return send_from_directory(TUTORIAL_PATH, filename, as_attachment=True)diff --git a/metagpt/provider/zhipuai_api.py b/metagpt/provider/zhipuai_api.pyindex a45081fcf..acac44aaf 100644--- a/metagpt/provider/zhipuai_api.py+++ b/metagpt/provider/zhipuai_api.py@@ -45,7 +45,13 @@ class ZhiPuAILLM(BaseLLM):def _const_kwargs(self, messages: list[dict], stream: bool = False) -> dict:max_tokens = self.config.max_token if self.config.max_token > 0 else 1024temperature = self.config.temperature if self.config.temperature > 0.0 else 0.3- kwargs = {"model": self.model, "max_tokens": max_tokens, "messages": messages, "stream": stream, "temperature": temperature}+ kwargs = {+ "model": self.model,+ "max_tokens": max_tokens,+ "messages": messages,+ "stream": stream,+ "temperature": temperature,+ }return kwargsdef completion(self, messages: list[dict], timeout=USE_CONFIG_TIMEOUT) -> dict:diff --git a/metagpt/utils/stream_pipe.py b/metagpt/utils/stream_pipe.pyindex d3d3cff32..4c4485158 100644--- a/metagpt/utils/stream_pipe.py+++ b/metagpt/utils/stream_pipe.py@@ -5,8 +5,8 @@# @Version : None# @Description : None-import timeimport json+import timefrom multiprocessing import Pipe@@ -21,17 +21,8 @@ class StreamPipe:"model": "gpt-3.5-turbo-0125","system_fingerprint": "fp_3bc1b5746c","choices": [- {- "index": 0,- "delta":- {- "role": "assistant",- "content": "content"- },- "logprobs": None,- "finish_reason": None- }- ]+ {"index": 0, "delta": {"role": "assistant", "content": "content"}, "logprobs": None, "finish_reason": None}+ ],}def set_message(self, msg):@@ -44,6 +35,6 @@ class StreamPipe:return Nonedef msg2stream(self, msg):- self.format_data['created'] = int(time.time())- self.format_data['choices'][0]['delta']['content'] = msg+ self.format_data["created"] = int(time.time())+ self.format_data["choices"][0]["delta"]["content"] = msgreturn f"data: {json.dumps(self.format_data, ensure_ascii=False)}\n".encode("utf-8")