From ab4b9f3515b992f6a96c8b4aebfe8aa65e4605b1 Mon Sep 17 00:00:00 2001 From: Wendong Date: Fri, 14 Mar 2025 02:34:25 +0800 Subject: [PATCH] fix async bug, optimize log info, tool call error handling --- README.md | 4 +-- README_zh.md | 4 +-- owl/run_azure_openai.py | 2 +- owl/utils/enhanced_role_playing.py | 56 +++++++++++++++++++++++++++++- pyproject.toml | 2 +- requirements.txt | 2 +- run_app.py | 4 ++- run_app_zh.py | 4 ++- uv.lock | 8 ++--- 9 files changed, 72 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index aa793c0..6259e05 100644 --- a/README.md +++ b/README.md @@ -179,7 +179,7 @@ source .venv/bin/activate .venv\Scripts\activate # Install from requirements.txt -pip install -r requirements.txt +pip install -r requirements.txt --use-pep517 ``` ## Option 3: Using conda @@ -201,7 +201,7 @@ conda activate owl pip install -e . # Option 2: Install from requirements.txt -pip install -r requirements.txt +pip install -r requirements.txt --use-pep517 # Exit the conda environment when done conda deactivate diff --git a/README_zh.md b/README_zh.md index d585b4e..58e45d9 100644 --- a/README_zh.md +++ b/README_zh.md @@ -176,7 +176,7 @@ source .venv/bin/activate .venv\Scripts\activate # 从 requirements.txt 安装 -pip install -r requirements.txt +pip install -r requirements.txt --use-pep517 ``` ## 选项3:使用 conda @@ -198,7 +198,7 @@ conda activate owl pip install -e . # 选项2:从 requirements.txt 安装 -pip install -r requirements.txt +pip install -r requirements.txt --use-pep517 # 完成后退出 conda 环境 conda deactivate diff --git a/owl/run_azure_openai.py b/owl/run_azure_openai.py index edbe27f..d695893 100644 --- a/owl/run_azure_openai.py +++ b/owl/run_azure_openai.py @@ -47,7 +47,7 @@ def construct_society(question: str) -> OwlRolePlaying: # Create models for different components using Azure OpenAI base_model_config = { "model_platform": ModelPlatformType.AZURE, - "model_type": os.getenv("AZURE_OPENAI_MODEL_TYPE"), + "model_type": os.getenv("AZURE_OPENAI_MODEL_TYPE"), "model_config_dict": ChatGPTConfig(temperature=0.4, max_tokens=4096).as_dict(), } diff --git a/owl/utils/enhanced_role_playing.py b/owl/utils/enhanced_role_playing.py index f8cf045..070dedb 100644 --- a/owl/utils/enhanced_role_playing.py +++ b/owl/utils/enhanced_role_playing.py @@ -437,7 +437,61 @@ class OwlGAIARolePlaying(OwlRolePlaying): ) -async def run_society( +def run_society( + society: OwlRolePlaying, + round_limit: int = 15, +) -> Tuple[str, List[dict], dict]: + overall_completion_token_count = 0 + overall_prompt_token_count = 0 + + chat_history = [] + init_prompt = """ + Now please give me instructions to solve over overall task step by step. If the task requires some specific knowledge, please instruct me to use tools to complete the task. + """ + input_msg = society.init_chat(init_prompt) + for _round in range(round_limit): + assistant_response, user_response = society.step(input_msg) + overall_completion_token_count += ( + assistant_response.info["usage"]["completion_tokens"] + + user_response.info["usage"]["completion_tokens"] + ) + + # convert tool call to dict + tool_call_records: List[dict] = [] + for tool_call in assistant_response.info["tool_calls"]: + tool_call_records.append(tool_call.as_dict()) + + _data = { + "user": user_response.msg.content, + "assistant": assistant_response.msg.content, + "tool_calls": tool_call_records, + } + + chat_history.append(_data) + logger.info(f"Round #{_round} user_response:\n {user_response.msgs[0].content}") + logger.info( + f"Round #{_round} assistant_response:\n {assistant_response.msgs[0].content}" + ) + + if ( + assistant_response.terminated + or user_response.terminated + or "TASK_DONE" in user_response.msg.content + ): + break + + input_msg = assistant_response.msg + + answer = chat_history[-1]["assistant"] + token_info = { + "completion_token_count": overall_completion_token_count, + "prompt_token_count": overall_prompt_token_count, + } + + return answer, chat_history, token_info + + +async def arun_society( society: OwlRolePlaying, round_limit: int = 15, ) -> Tuple[str, List[dict], dict]: diff --git a/pyproject.toml b/pyproject.toml index c9d77bf..60984b6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,7 +21,7 @@ keywords = [ "learning-systems" ] dependencies = [ - "camel-ai[all]==0.2.28", + "camel-ai[all]==0.2.29", "chunkr-ai>=0.0.41", "docx2markdown>=0.1.1", "gradio>=3.50.2", diff --git a/requirements.txt b/requirements.txt index ff6fc0b..4ccf956 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -camel-ai[all]==0.2.28 +camel-ai[all]==0.2.29 chunkr-ai>=0.0.41 docx2markdown>=0.1.1 gradio>=3.50.2 diff --git a/run_app.py b/run_app.py index 69af092..a346984 100644 --- a/run_app.py +++ b/run_app.py @@ -49,7 +49,9 @@ def main(): print( f"Error: Unable to import necessary modules. Please ensure all dependencies are installed: {e}" ) - print("Tip: Run 'pip install -r requirements.txt' to install all dependencies") + print( + "Tip: Run 'pip install -r requirements.txt --use-pep517' to install all dependencies" + ) sys.exit(1) except Exception as e: print(f"Error occurred while starting the application: {e}") diff --git a/run_app_zh.py b/run_app_zh.py index 4edc38b..7410df1 100644 --- a/run_app_zh.py +++ b/run_app_zh.py @@ -47,7 +47,9 @@ def main(): except ImportError as e: print(f"错误: 无法导入必要的模块。请确保已安装所有依赖项: {e}") - print("提示: 运行 'pip install -r requirements.txt' 安装所有依赖项") + print( + "提示: 运行 'pip install -r requirements.txt --use-pep517' 安装所有依赖项" + ) sys.exit(1) except Exception as e: print(f"启动应用程序时出错: {e}") diff --git a/uv.lock b/uv.lock index 81d0dac..8ecd163 100644 --- a/uv.lock +++ b/uv.lock @@ -482,7 +482,7 @@ wheels = [ [[package]] name = "camel-ai" -version = "0.2.28" +version = "0.2.29" source = { registry = "https://pypi.org/simple" } dependencies = [ { name = "colorama" }, @@ -499,9 +499,9 @@ dependencies = [ { name = "pyyaml" }, { name = "tiktoken" }, ] -sdist = { url = "https://files.pythonhosted.org/packages/6a/3b/7f350ae3c5bf42263688d3a69333e3908af4d45ce8f5f838af634a2720b3/camel_ai-0.2.28.tar.gz", hash = "sha256:f47e12bdf59df6e789db4587f0c5bd0adf43b2029d6be1bfcc31bfd41cab9d9f", size = 443082 } +sdist = { url = "https://files.pythonhosted.org/packages/00/f8/fdb2478ec3b61f78af2a8a8ab0b575e795a015e89c2c058cee61d63a3951/camel_ai-0.2.29.tar.gz", hash = "sha256:b077885ea7a1fd6b4d53dd77e83b6b4c2ded96e43ced6a2f4bd51a434a29bbdb", size = 440795 } wheels = [ - { url = "https://files.pythonhosted.org/packages/5d/27/8a6e97f660354ce03413872268c7f4a40ceefdf39b20f161cb7f672dc67c/camel_ai-0.2.28-py3-none-any.whl", hash = "sha256:079e7e905a36b64be47a6a27ad4b99d21ca0403b27027a4d777744968a22040a", size = 748237 }, + { url = "https://files.pythonhosted.org/packages/2b/c4/4c0c388464d4c8f8ec7704d39459883e0769268b566a82245f545b09f703/camel_ai-0.2.29-py3-none-any.whl", hash = "sha256:812143a204e364703be40066101c0cf34769bc589dac81373444acc6bab8fe7b", size = 746424 }, ] [package.optional-dependencies] @@ -3622,7 +3622,7 @@ dependencies = [ [package.metadata] requires-dist = [ - { name = "camel-ai", extras = ["all"], specifier = "==0.2.28" }, + { name = "camel-ai", extras = ["all"], specifier = "==0.2.29" }, { name = "chunkr-ai", specifier = ">=0.0.41" }, { name = "docx2markdown", specifier = ">=0.1.1" }, { name = "gradio", specifier = ">=3.50.2" },