add Azure OpenAI API to GUI (#269)

This commit is contained in:
Wendong-Fan 2025-03-13 17:39:50 +08:00 committed by GitHub
commit 485fba98e2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 187 additions and 0 deletions

View File

@ -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
```

View File

@ -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
```

View File

@ -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=""

View File

@ -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密钥",

View File

@ -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.py",
}
@ -53,6 +54,7 @@ SCRIPT_DESCRIPTIONS = {
"Default": "Default OWL implementation, uses OpenAI GPT-4o model and full set of tools",
"GAIA Roleplaying": "GAIA benchmark implementation, used to evaluate model capabilities",
"OpenAI Compatible": "Uses third-party models compatible with OpenAI API, supports custom API endpoints",
"Azure OpenAI": "Uses Azure OpenAI API",
"Ollama": "Uses Ollama API",
"Terminal": "Uses local terminal to execute python files",
}
@ -74,6 +76,34 @@ ENV_GROUPS = {
"required": False,
"help": "Base URL for OpenAI API, optional. Set this if using a proxy or custom endpoint.",
},
{
"name": "AZURE_OPENAI_KEY",
"label": "Azure OpenAI API Key",
"type": "password",
"required": False,
"help": "Azure OpenAI API key for accessing Azure deployed GPT models. Get it from: https://portal.azure.com/",
},
{
"name": "AZURE_OPENAI_ENDPOINT",
"label": "Azure OpenAI Endpoint",
"type": "text",
"required": False,
"help": "Azure OpenAI service endpoint URL",
},
{
"name": "AZURE_DEPLOYMENT_NAME",
"label": "Azure OpenAI Deployment Name",
"type": "text",
"required": False,
"help": "Azure OpenAI service deployment name",
},
{
"name": "AZURE_OPENAI_VERSION",
"label": "Azure OpenAI API Version",
"type": "text",
"required": False,
"help": "Azure OpenAI API version, e.g. 2023-12-01-preview",
},
{
"name": "QWEN_API_KEY",
"label": "Alibaba Cloud Qwen API Key",

114
owl/run_azure_openai.py Normal file
View File

@ -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,
BrowserToolkit,
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 = [
*BrowserToolkit(
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()