From aa54608e8002a0cc71f13185345cc3e5aea3f406 Mon Sep 17 00:00:00 2001 From: JetSquirrel Date: Sat, 7 Oct 2023 16:01:59 +0000 Subject: [PATCH 1/3] feat: add azure openai --- README.md | 17 +++++++++++------ README_zh.md | 5 +++++ agentverse/llms/openai.py | 35 +++++++++++++++++++++++------------ 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index eba2cfe39..2f915af94 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ **AgentVerse** offers a versatile framework that streamlines the process of creating custom multi-agent environments for large language models (LLMs). Designed to facilitate swift development and customization with minimal effort, our framework empowers researchers to concentrate on their research, rather than being bogged down by implementation details. -⚠️⚠️⚠️ We're presently in the process of refactoring our code to offer you the flexibility to construct both simulation environments (without a predefined goal) and task-solving environments (with a specific goal). Please note that our README is currently outdated, we will update it very soon. If you require a stable version of our code that exclusively supports simulation environments, you can find it in our [`release-1.0`](https://github.com/OpenBMB/AgentVerse/tree/release-1.0) branch. +⚠️⚠️⚠️ We're refactoring the code in process, and goal is provid a flexibility to construct simulation(without a predefined goal) and task-solving(with a specific goal) environments. Please note that README is outdated, we will update it soon. If you require a stable version code that exclusively supports simulation environments, you can using [`release-1.0`](https://github.com/OpenBMB/AgentVerse/tree/release-1.0) branch. --- @@ -79,7 +79,7 @@ In the NLP class, the professor and students engage in interactive communication Use the following command to launch the NLP Classroom example: ```bash -python main_demo.py --task nlp_classroom_9players +python main_simulation_gui.py --task nlp_classroom_9players ``` https://github.com/OpenBMB/AgentVerse/assets/11704492/6ea07850-595e-4a28-a82e-f863011353c2 @@ -90,7 +90,7 @@ A prisoner's Dilemma is a thought experiment that challenges two completely rati Use the following command to launch the Prisoner Dilemma example: ```bash -python main_demo.py --task prisoner_dilemma +python main_simulation_cli.py --task prisoner_dilemma ``` https://github.com/OpenBMB/AgentVerse/assets/11704492/017c46e5-c738-4fca-9352-b008e2d518bd @@ -112,7 +112,7 @@ https://github.com/OpenBMB/AgentVerse/assets/11704492/5058066a-abee-490d-8659-b4 In the database diagnosis scenario, the Chief DBA monitors the system anomalies (e.g., slow queries, locks, crash down). If detected, the domain experts are alerted to analyze root causes, share insights, and suggest optimization solutions together. The Chief DBA then provides a summarized report to the user. ```bash -python main_demo.py --task db_diag +python main_simulation_gui.py --task db_diag ``` https://github.com/OpenBMB/AgentVerse/assets/11704492/c633419d-afbb-47d4-bb12-6bb512e7af3a @@ -195,11 +195,16 @@ pip install -r requirements.txt ``` Some users have reported problems installing the `orjson` required by `gradio`. One simple workaround is to install it with Anaconda `conda install -c conda-forge orjson`. -You also need to export your OpenAI API key as follows +You also need to export your OpenAI API key as follows: ```bash # Export your OpenAI API key export OPENAI_API_KEY="your_api_key_here" ``` +If you want use Azure OpenAI services, pleas export your Azure OpenAI key and OpenAI API base as follows: +```bash +export AZURE_OPENAI_API_KEY="your_api_key_here" +export AZURE_OPENAI_API_BASE="your_api_base_here" +``` If you want to use the tools provided by BMTools, you need to install BMTools as follows: ```bash @@ -231,7 +236,7 @@ python3 main.py --task nlp_classroom_9players We also provide a local website demo for this environment. You can launch it with ```shell -python3 main_demo.py --task nlp_classroom_9players +python3 main_simulation_gui.py --task nlp_classroom_9players ``` After successfully launching the local server, you can visit [http://127.0.0.1:7860/](http://127.0.0.1:7860/) to view the classroom environment. diff --git a/README_zh.md b/README_zh.md index 4de51148c..988dacaf5 100644 --- a/README_zh.md +++ b/README_zh.md @@ -198,6 +198,11 @@ pip install -r requirements.txt # 导出你的OpenAI API密钥 export OPENAI_API_KEY="your_api_key_here" ``` +或者您想使用 Azure OpenAI 服务,请按照以下方式配置 OpenAI API 密钥和 API base: +```bash +export AZURE_OPENAI_API_KEY="your_api_key_here" +export AZURE_OPENAI_API_BASE="your_api_base_here" +``` 如果您想使用BMTools提供的工具,您需要按如下方式安装BMTools: ```bash diff --git a/agentverse/llms/openai.py b/agentverse/llms/openai.py index d7ea57a78..cdf72d99b 100644 --- a/agentverse/llms/openai.py +++ b/agentverse/llms/openai.py @@ -24,21 +24,27 @@ is_openai_available = False logging.warning("openai package is not installed") else: - openai.api_key = os.environ.get("OPENAI_API_KEY") # openai.proxy = os.environ.get("http_proxy") # if openai.proxy is None: # openai.proxy = os.environ.get("HTTP_PROXY") - if openai.api_key is None: + if os.environ.get("OPENAI_API_KEY")!=None: + openai.api_key = os.environ.get("OPENAI_API_KEY") + is_openai_available = True + elif os.environ.get("AZURE_OPENAI_API_KEY")!=None: + openai.api_type = "azure" + openai.api_key = os.environ.get("AZURE_OPENAI_API_KEY") + openai.api_base = os.environ.get("AZURE_OPENAI_API_BASE") + openai.api_version = "2023-05-15" + is_openai_available = True + else: logging.warning( "OpenAI API key is not set. Please set the environment variable OPENAI_API_KEY" ) - is_openai_available = False - else: - is_openai_available = True - + is_openai_available = False class OpenAIChatArgs(BaseModelArgs): model: str = Field(default="gpt-3.5-turbo") + deployment_id: str = Field(default="gpt-35-turbo") max_tokens: int = Field(default=2048) temperature: float = Field(default=1.0) top_p: int = Field(default=1) @@ -86,7 +92,7 @@ class OpenAIChatArgs(BaseModelArgs): # ) -@llm_registry.register("gpt-3.5-turbo") +@llm_registry.register("gpt-35-turbo") @llm_registry.register("gpt-4") class OpenAIChat(BaseChatModel): args: OpenAIChatArgs = Field(default_factory=OpenAIChatArgs) @@ -94,7 +100,7 @@ class OpenAIChat(BaseChatModel): def __init__(self, max_retry: int = 3, **kwargs): args = OpenAIChatArgs() args = args.dict() - + for k, v in args.items(): args[k] = kwargs.pop(k, v) if len(kwargs) > 0: @@ -121,7 +127,7 @@ def generate_response( try: # Execute function call - if functions != []: + if functions != []: response = openai.ChatCompletion.create( messages=messages, functions=functions, @@ -283,9 +289,14 @@ def construct_messages( def get_embedding(text: str, attempts=3) -> np.array: try: text = text.replace("\n", " ") - embedding = openai.Embedding.create( - input=[text], model="text-embedding-ada-002" - )["data"][0]["embedding"] + if openai.api_type=="azure": + embedding = openai.Embedding.create( + input=[text], deployment_id="text-embedding-ada-002" + )["data"][0]["embedding"] + else: + embedding = openai.Embedding.create( + input=[text], model="text-embedding-ada-002" + )["data"][0]["embedding"] return tuple(embedding) except Exception as e: attempt += 1 From 6045f491fb7c01172752c1ba643611a027b0ef24 Mon Sep 17 00:00:00 2001 From: JetSquirrel Date: Sun, 8 Oct 2023 04:04:35 +0000 Subject: [PATCH 2/3] fix miss register --- agentverse/llms/openai.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/agentverse/llms/openai.py b/agentverse/llms/openai.py index cdf72d99b..f523f67ed 100644 --- a/agentverse/llms/openai.py +++ b/agentverse/llms/openai.py @@ -91,8 +91,8 @@ class OpenAIChatArgs(BaseModelArgs): # total_tokens=response["usage"]["total_tokens"], # ) - @llm_registry.register("gpt-35-turbo") +@llm_registry.register("gpt-3.5-turbo") @llm_registry.register("gpt-4") class OpenAIChat(BaseChatModel): args: OpenAIChatArgs = Field(default_factory=OpenAIChatArgs) @@ -100,21 +100,19 @@ class OpenAIChat(BaseChatModel): def __init__(self, max_retry: int = 3, **kwargs): args = OpenAIChatArgs() args = args.dict() - for k, v in args.items(): args[k] = kwargs.pop(k, v) if len(kwargs) > 0: logging.warning(f"Unused arguments: {kwargs}") super().__init__(args=args, max_retry=max_retry) - # def _construct_messages(self, history: List[Message]): # return history + [{"role": "user", "content": query}] - @retry( stop=stop_after_attempt(20), wait=wait_exponential(multiplier=1, min=4, max=10), reraise=True, ) + def generate_response( self, prepend_prompt: str = "", @@ -124,7 +122,6 @@ def generate_response( ) -> LLMResult: messages = self.construct_messages(prepend_prompt, history, append_prompt) logger.log_prompt(messages) - try: # Execute function call if functions != []: From e762c1e2a942d9faacec7b6901abb505838b518a Mon Sep 17 00:00:00 2001 From: Weize Chen <32613237+chenweize1998@users.noreply.github.com> Date: Sun, 8 Oct 2023 12:11:10 +0800 Subject: [PATCH 3/3] Update README.md --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2f915af94..358a22e6d 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ **AgentVerse** offers a versatile framework that streamlines the process of creating custom multi-agent environments for large language models (LLMs). Designed to facilitate swift development and customization with minimal effort, our framework empowers researchers to concentrate on their research, rather than being bogged down by implementation details. -⚠️⚠️⚠️ We're refactoring the code in process, and goal is provid a flexibility to construct simulation(without a predefined goal) and task-solving(with a specific goal) environments. Please note that README is outdated, we will update it soon. If you require a stable version code that exclusively supports simulation environments, you can using [`release-1.0`](https://github.com/OpenBMB/AgentVerse/tree/release-1.0) branch. +⚠️⚠️⚠️ We're refactoring the code, and the goal is to provide a flexibility to construct simulation(without a predefined goal) and task-solving(with a specific goal) environments. Please note that this README is outdated, we will update it soon. If you require a stable version that exclusively supports simulation environments, you can use [`release-1.0`](https://github.com/OpenBMB/AgentVerse/tree/release-1.0) branch. --- @@ -199,7 +199,11 @@ You also need to export your OpenAI API key as follows: ```bash # Export your OpenAI API key export OPENAI_API_KEY="your_api_key_here" +# Or if you are using Azure +export AZURE_OPENAI_API_KEY="your_api_key_here" +export AZURE_OPENAI_API_BASE="your_api_base_here" ``` + If you want use Azure OpenAI services, pleas export your Azure OpenAI key and OpenAI API base as follows: ```bash export AZURE_OPENAI_API_KEY="your_api_key_here"