From 7407c73951e7d5f6c9d836768e26d7eeb2f261a0 Mon Sep 17 00:00:00 2001 From: hzm <> Date: Thu, 13 Mar 2025 16:36:14 +0800 Subject: [PATCH] add Azure OpenAI API to GUI --- README.md | 3 ++ README_zh.md | 3 ++ owl/.env_template | 7 +++ owl/app.py | 30 +++++++++++ owl/run_azure_openai.py | 114 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 157 insertions(+) create mode 100644 owl/run_azure_openai.py diff --git a/README.md b/README.md index 13640ab..261a1ae 100644 --- a/README.md +++ b/README.md @@ -307,6 +307,9 @@ python owl/run_deepseek_zh.py # Run with other OpenAI-compatible models python owl/run_openai_compatiable_model.py +# Run with Azure OpenAI +python owl/run_azure_openai.py + # Run with Ollama python owl/run_ollama.py ``` diff --git a/README_zh.md b/README_zh.md index b7fd579..4e9d98c 100644 --- a/README_zh.md +++ b/README_zh.md @@ -311,6 +311,9 @@ python owl/run_deepseek_zh.py # 使用其他 OpenAI 兼容模型运行 python owl/run_openai_compatiable_model.py +# 使用 Azure OpenAI模型运行 +python owl/run_azure_openai.py + # 使用 Ollama 运行 python owl/run_ollama.py ``` diff --git a/owl/.env_template b/owl/.env_template index cbf77f4..e0e20c5 100644 --- a/owl/.env_template +++ b/owl/.env_template @@ -4,6 +4,13 @@ # OPENAI_API_KEY= "" # OPENAI_API_BASE_URL="" +# Azure OpenAI API +AZURE_OPENAI_BASE_URL="" +AZURE_API_VERSION="" +AZURE_OPENAI_API_KEY="" +AZURE_DEPLOYMENT_NAME="" + + # Qwen API (https://help.aliyun.com/zh/model-studio/developer-reference/get-api-key) # QWEN_API_KEY="" diff --git a/owl/app.py b/owl/app.py index 15b967b..5ea44e2 100644 --- a/owl/app.py +++ b/owl/app.py @@ -40,6 +40,7 @@ SCRIPTS = { "Default": "run.py", "GAIA Roleplaying": "run_gaia_roleplaying.py", "OpenAI Compatible": "run_openai_compatiable_model.py", + "Azure OpenAI": "run_azure_openai.py", "Ollama": "run_ollama.py", "Terminal": "run_terminal_zh.py", } @@ -53,6 +54,7 @@ SCRIPT_DESCRIPTIONS = { "Default": "默认OWL实现,使用OpenAI GPT-4o模型和全套工具", "GAIA Roleplaying": "GAIA基准测试实现,用于评估模型能力", "OpenAI Compatible": "使用兼容OpenAI API的第三方模型,支持自定义API端点", + "Azure OpenAI": "使用Azure OpenAI API", "Ollama": "使用Ollama API", "Terminal": "使用本地终端执行python文件", } @@ -74,6 +76,34 @@ ENV_GROUPS = { "required": False, "help": "OpenAI API的基础URL,可选。如果使用代理或自定义端点,请设置此项。", }, + { + "name": "AZURE_OPENAI_KEY", + "label": "Azure OpenAI API密钥", + "type": "password", + "required": False, + "help": "Azure OpenAI API密钥,用于访问Azure部署的GPT模型", + }, + { + "name": "AZURE_OPENAI_ENDPOINT", + "label": "Azure OpenAI端点", + "type": "text", + "required": False, + "help": "Azure OpenAI服务的端点URL", + }, + { + "name": "AZURE_DEPLOYMENT_NAME", + "label": "Azure OpenAI部署名称", + "type": "text", + "required": False, + "help": "Azure OpenAI服务的部署名称", + }, + { + "name": "AZURE_OPENAI_VERSION", + "label": "Azure OpenAI API版本", + "type": "text", + "required": False, + "help": "Azure OpenAI API版本,例如:2023-12-01-preview", + }, { "name": "QWEN_API_KEY", "label": "阿里云Qwen API密钥", diff --git a/owl/run_azure_openai.py b/owl/run_azure_openai.py new file mode 100644 index 0000000..4442a20 --- /dev/null +++ b/owl/run_azure_openai.py @@ -0,0 +1,114 @@ +# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. ========= +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. ========= +import os +from dotenv import load_dotenv +from camel.configs import ChatGPTConfig +from camel.models import ModelFactory +from camel.toolkits import ( + CodeExecutionToolkit, + ExcelToolkit, + ImageAnalysisToolkit, + SearchToolkit, + WebToolkit, + FileWriteToolkit, +) +from camel.types import ModelPlatformType + +from utils import OwlRolePlaying, run_society + +from camel.logger import set_log_level + +set_log_level(level="DEBUG") + +load_dotenv() + + +def construct_society(question: str) -> OwlRolePlaying: + r"""Construct a society of agents based on the given question. + + Args: + question (str): The task or question to be addressed by the society. + + Returns: + OwlRolePlaying: A configured society of agents ready to address the question. + """ + + # Create models for different components using Azure OpenAI + base_model_config = { + "model_platform": ModelPlatformType.AZURE, + "model_type": os.getenv("AZURE_OPENAI_MODEL_TYPE"), + "model_config_dict": ChatGPTConfig(temperature=0.4, max_tokens=4096).as_dict(), + } + + models = { + "user": ModelFactory.create(**base_model_config), + "assistant": ModelFactory.create(**base_model_config), + "web": ModelFactory.create(**base_model_config), + "planning": ModelFactory.create(**base_model_config), + "image": ModelFactory.create(**base_model_config), + } + + # Configure toolkits + tools = [ + *WebToolkit( + headless=False, # Set to True for headless mode (e.g., on remote servers) + web_agent_model=models["web"], + planning_agent_model=models["planning"], + ).get_tools(), + *CodeExecutionToolkit(sandbox="subprocess", verbose=True).get_tools(), + *ImageAnalysisToolkit(model=models["image"]).get_tools(), + SearchToolkit().search_duckduckgo, + SearchToolkit().search_google, # Comment this out if you don't have google search + SearchToolkit().search_wiki, + *ExcelToolkit().get_tools(), + *FileWriteToolkit(output_dir="./").get_tools(), + ] + + # Configure agent roles and parameters + user_agent_kwargs = {"model": models["user"]} + assistant_agent_kwargs = {"model": models["assistant"], "tools": tools} + + # Configure task parameters + task_kwargs = { + "task_prompt": question, + "with_task_specify": False, + } + + # Create and return the society + society = OwlRolePlaying( + **task_kwargs, + user_role_name="user", + user_agent_kwargs=user_agent_kwargs, + assistant_role_name="assistant", + assistant_agent_kwargs=assistant_agent_kwargs, + ) + + return society + + +def main(): + r"""Main function to run the OWL system with Azure OpenAI.""" + # Example question + question = "Navigate to Amazon.com and identify one product that is attractive to coders. Please provide me with the product name and price. No need to verify your answer." + + # Construct and run the society + society = construct_society(question) + answer, chat_history, token_count = run_society(society) + + # Output the result + print(f"\033[94mAnswer: {answer}\033[0m") + + +if __name__ == "__main__": + main()