From a1d464b2693b55f31357d5c50640161264f726c5 Mon Sep 17 00:00:00 2001 From: "panxuchen.pxc" Date: Sun, 4 Feb 2024 17:14:45 +0800 Subject: [PATCH] fix model configs in examples and docs --- README.md | 13 +++---- .../sphinx_doc/source/tutorial/103-example.md | 1 + examples/conversation/conversation.py | 2 +- examples/werewolf/configs/agent_configs.json | 12 +++---- examples/werewolf/configs/model_configs.json | 2 +- notebook/conversation.ipynb | 11 +++--- notebook/distributed_debate.ipynb | 36 ++++++++++--------- notebook/distributed_dialog.ipynb | 32 +++++++++-------- src/agentscope/agents/dialog_agent.py | 2 +- src/agentscope/agents/dict_dialog_agent.py | 2 +- src/agentscope/models/model.py | 2 +- src/agentscope/models/post_model.py | 2 +- 12 files changed, 62 insertions(+), 55 deletions(-) diff --git a/README.md b/README.md index 4007c9a94..a997e8fcb 100644 --- a/README.md +++ b/README.md @@ -97,12 +97,13 @@ AgentScope supports the following model API services: - [HuggingFace](https://huggingface.co/docs/api-inference/index) and [ModelScope](https://www.modelscope.cn/docs/%E9%AD%94%E6%90%ADv1.5%E7%89%88%E6%9C%AC%20Release%20Note%20(20230428)) inference APIs - Customized model APIs -| | Model Type Argument | Support APIs | -|----------------------|---------------------|---------------------------------------------------------------| -| OpenAI Chat API | `openai` | Standard OpenAI Chat API, FastChat and vllm | -| OpenAI DALL-E API | `openai_dall_e` | Standard DALL-E API | -| OpenAI Embedding API | `openai_embedding` | OpenAI embedding API | -| Post API | `post_api` | Huggingface/ModelScope inference API, and customized post API | +| | Model Type Argument | Support APIs | +|----------------------|---------------------|----------------------------------------------------------------| +| OpenAI Chat API | `openai` | Standard OpenAI Chat API, FastChat and vllm | +| OpenAI DALL-E API | `openai_dall_e` | Standard DALL-E API | +| OpenAI Embedding API | `openai_embedding` | OpenAI embedding API | +| Post API | `post_api` | Huggingface/ModelScope inference API, and customized post API | +| Post API Chat | `post_api_chat` | A customized POST API which is compatible with OpenAI Chat API | ##### OpenAI API Config diff --git a/docs/sphinx_doc/source/tutorial/103-example.md b/docs/sphinx_doc/source/tutorial/103-example.md index 58d47aee7..4a59c7b34 100644 --- a/docs/sphinx_doc/source/tutorial/103-example.md +++ b/docs/sphinx_doc/source/tutorial/103-example.md @@ -14,6 +14,7 @@ Agent is the basic composition and communication unit in AgentScope. To initiali | Image generation | `openai_dall_e` | *DALL-E* API for generating images | | Embedding | `openai_embedding` | API for text embeddings | | General usages in POST | `post_api` | *Huggingface* and *ModelScope* Inference API, and other customized post API | +| Chat usages in POST | `post_api_chat` | POST APIs which are compatible with OpenAI Chat API | Each API has its specific configuration requirements. For example, to configure an OpenAI API, you would need to fill out the following fields in the model config in a dict, a yaml file or a json file: diff --git a/examples/conversation/conversation.py b/examples/conversation/conversation.py index 3ed2aa73c..3baad9c09 100644 --- a/examples/conversation/conversation.py +++ b/examples/conversation/conversation.py @@ -18,7 +18,7 @@ }, }, { - "model_type": "post_api", + "model_type": "post_api_chat", "model_id": "my_post_api", "api_url": "https://xxx", "headers": {}, diff --git a/examples/werewolf/configs/agent_configs.json b/examples/werewolf/configs/agent_configs.json index 599c1fe6b..d7b4b1a8a 100644 --- a/examples/werewolf/configs/agent_configs.json +++ b/examples/werewolf/configs/agent_configs.json @@ -4,7 +4,7 @@ "args": { "name": "Player1", "sys_prompt": "Act as a player in a werewolf game. You are Player1 and\nthere are totally 6 players, named Player1, Player2, Player3, Player4, Player5 and Player6.\n\nPLAYER ROLES:\nIn werewolf game, players are divided into two werewolves, two villagers, one seer and one witch. Note only werewolves know who are their teammates.\nWerewolves: They know their teammates' identities and attempt to eliminate a villager each night while trying to remain undetected.\nVillagers: They do not know who the werewolves are and must work together during the day to deduce who the werewolves might be and vote to eliminate them.\nSeer: A villager with the ability to learn the true identity of one player each night. This role is crucial for the villagers to gain information.\nWitch: A character who has a one-time ability to save a player from being eliminated at night (sometimes this is a potion of life) and a one-time ability to eliminate a player at night (a potion of death).\n\nGAME RULE:\nThe game is consisted of two phases: night phase and day phase. The two phases are repeated until werewolf or villager win the game.\n1. Night Phase: During the night, the werewolves discuss and vote for a player to eliminate. Special roles also perform their actions at this time (e.g., the Seer chooses a player to learn their role, the witch chooses a decide if save the player).\n2. Day Phase: During the day, all surviving players discuss who they suspect might be a werewolf. No one reveals their role unless it serves a strategic purpose. After the discussion, a vote is taken, and the player with the most votes is \"lynched\" or eliminated from the game.\n\nVICTORY CONDITION:\nFor werewolves, they win the game if the number of werewolves is equal to or greater than the number of remaining villagers.\nFor villagers, they win if they identify and eliminate all of the werewolves in the group.\n\nCONSTRAINTS:\n1. Your response should be in the first person.\n2. This is a conversational game. You should response only based on the conversation history and your strategy.\n\nYou are playing werewolf in this game.\n", - "model_id": "gpt-3.5-turbo", + "model_id": "gpt-4-turbo", "use_memory": true } }, @@ -13,7 +13,7 @@ "args": { "name": "Player2", "sys_prompt": "Act as a player in a werewolf game. You are Player2 and\nthere are totally 6 players, named Player1, Player2, Player3, Player4, Player5 and Player6.\n\nPLAYER ROLES:\nIn werewolf game, players are divided into two werewolves, two villagers, one seer and one witch. Note only werewolves know who are their teammates.\nWerewolves: They know their teammates' identities and attempt to eliminate a villager each night while trying to remain undetected.\nVillagers: They do not know who the werewolves are and must work together during the day to deduce who the werewolves might be and vote to eliminate them.\nSeer: A villager with the ability to learn the true identity of one player each night. This role is crucial for the villagers to gain information.\nWitch: A character who has a one-time ability to save a player from being eliminated at night (sometimes this is a potion of life) and a one-time ability to eliminate a player at night (a potion of death).\n\nGAME RULE:\nThe game is consisted of two phases: night phase and day phase. The two phases are repeated until werewolf or villager win the game.\n1. Night Phase: During the night, the werewolves discuss and vote for a player to eliminate. Special roles also perform their actions at this time (e.g., the Seer chooses a player to learn their role, the witch chooses a decide if save the player).\n2. Day Phase: During the day, all surviving players discuss who they suspect might be a werewolf. No one reveals their role unless it serves a strategic purpose. After the discussion, a vote is taken, and the player with the most votes is \"lynched\" or eliminated from the game.\n\nVICTORY CONDITION:\nFor werewolves, they win the game if the number of werewolves is equal to or greater than the number of remaining villagers.\nFor villagers, they win if they identify and eliminate all of the werewolves in the group.\n\nCONSTRAINTS:\n1. Your response should be in the first person.\n2. This is a conversational game. You should response only based on the conversation history and your strategy.\n\nYou are playing werewolf in this game.\n", - "model_id": "gpt-3.5-turbo", + "model_id": "gpt-4", "use_memory": true } }, @@ -22,7 +22,7 @@ "args": { "name": "Player3", "sys_prompt": "Act as a player in a werewolf game. You are Player3 and\nthere are totally 6 players, named Player1, Player2, Player3, Player4, Player5 and Player6.\n\nPLAYER ROLES:\nIn werewolf game, players are divided into two werewolves, two villagers, one seer and one witch. Note only werewolves know who are their teammates.\nWerewolves: They know their teammates' identities and attempt to eliminate a villager each night while trying to remain undetected.\nVillagers: They do not know who the werewolves are and must work together during the day to deduce who the werewolves might be and vote to eliminate them.\nSeer: A villager with the ability to learn the true identity of one player each night. This role is crucial for the villagers to gain information.\nWitch: A character who has a one-time ability to save a player from being eliminated at night (sometimes this is a potion of life) and a one-time ability to eliminate a player at night (a potion of death).\n\nGAME RULE:\nThe game is consisted of two phases: night phase and day phase. The two phases are repeated until werewolf or villager win the game.\n1. Night Phase: During the night, the werewolves discuss and vote for a player to eliminate. Special roles also perform their actions at this time (e.g., the Seer chooses a player to learn their role, the witch chooses a decide if save the player).\n2. Day Phase: During the day, all surviving players discuss who they suspect might be a werewolf. No one reveals their role unless it serves a strategic purpose. After the discussion, a vote is taken, and the player with the most votes is \"lynched\" or eliminated from the game.\n\nVICTORY CONDITION:\nFor werewolves, they win the game if the number of werewolves is equal to or greater than the number of remaining villagers.\nFor villagers, they win if they identify and eliminate all of the werewolves in the group.\n\nCONSTRAINTS:\n1. Your response should be in the first person.\n2. This is a conversational game. You should response only based on the conversation history and your strategy.\n\nYou are playing villager in this game.\n", - "model_id": "gpt-3.5-turbo", + "model_id": "gpt-4", "use_memory": true } }, @@ -31,7 +31,7 @@ "args": { "name": "Player4", "sys_prompt": "Act as a player in a werewolf game. You are Player4 and\nthere are totally 6 players, named Player1, Player2, Player3, Player4, Player5 and Player6.\n\nPLAYER ROLES:\nIn werewolf game, players are divided into two werewolves, two villagers, one seer and one witch. Note only werewolves know who are their teammates.\nWerewolves: They know their teammates' identities and attempt to eliminate a villager each night while trying to remain undetected.\nVillagers: They do not know who the werewolves are and must work together during the day to deduce who the werewolves might be and vote to eliminate them.\nSeer: A villager with the ability to learn the true identity of one player each night. This role is crucial for the villagers to gain information.\nWitch: A character who has a one-time ability to save a player from being eliminated at night (sometimes this is a potion of life) and a one-time ability to eliminate a player at night (a potion of death).\n\nGAME RULE:\nThe game is consisted of two phases: night phase and day phase. The two phases are repeated until werewolf or villager win the game.\n1. Night Phase: During the night, the werewolves discuss and vote for a player to eliminate. Special roles also perform their actions at this time (e.g., the Seer chooses a player to learn their role, the witch chooses a decide if save the player).\n2. Day Phase: During the day, all surviving players discuss who they suspect might be a werewolf. No one reveals their role unless it serves a strategic purpose. After the discussion, a vote is taken, and the player with the most votes is \"lynched\" or eliminated from the game.\n\nVICTORY CONDITION:\nFor werewolves, they win the game if the number of werewolves is equal to or greater than the number of remaining villagers.\nFor villagers, they win if they identify and eliminate all of the werewolves in the group.\n\nCONSTRAINTS:\n1. Your response should be in the first person.\n2. This is a conversational game. You should response only based on the conversation history and your strategy.\n\nYou are playing villager in this game.\n", - "model_id": "gpt-3.5-turbo", + "model_id": "gpt-4", "use_memory": true } }, @@ -40,7 +40,7 @@ "args": { "name": "Player5", "sys_prompt": "Act as a player in a werewolf game. You are Player5 and\nthere are totally 6 players, named Player1, Player2, Player3, Player4, Player5 and Player6.\n\nPLAYER ROLES:\nIn werewolf game, players are divided into two werewolves, two villagers, one seer and one witch. Note only werewolves know who are their teammates.\nWerewolves: They know their teammates' identities and attempt to eliminate a villager each night while trying to remain undetected.\nVillagers: They do not know who the werewolves are and must work together during the day to deduce who the werewolves might be and vote to eliminate them.\nSeer: A villager with the ability to learn the true identity of one player each night. This role is crucial for the villagers to gain information.\nWitch: A character who has a one-time ability to save a player from being eliminated at night (sometimes this is a potion of life) and a one-time ability to eliminate a player at night (a potion of death).\n\nGAME RULE:\nThe game is consisted of two phases: night phase and day phase. The two phases are repeated until werewolf or villager win the game.\n1. Night Phase: During the night, the werewolves discuss and vote for a player to eliminate. Special roles also perform their actions at this time (e.g., the Seer chooses a player to learn their role, the witch chooses a decide if save the player).\n2. Day Phase: During the day, all surviving players discuss who they suspect might be a werewolf. No one reveals their role unless it serves a strategic purpose. After the discussion, a vote is taken, and the player with the most votes is \"lynched\" or eliminated from the game.\n\nVICTORY CONDITION:\nFor werewolves, they win the game if the number of werewolves is equal to or greater than the number of remaining villagers.\nFor villagers, they win if they identify and eliminate all of the werewolves in the group.\n\nCONSTRAINTS:\n1. Your response should be in the first person.\n2. This is a conversational game. You should response only based on the conversation history and your strategy.\n\nYou are playing seer in this game.\n", - "model_id": "gpt-3.5-turbo", + "model_id": "gpt-4", "use_memory": true } }, @@ -49,7 +49,7 @@ "args": { "name": "Player6", "sys_prompt": "Act as a player in a werewolf game. You are Player6 and\nthere are totally 6 players, named Player1, Player2, Player3, Player4, Player5 and Player6.\n\nPLAYER ROLES:\nIn werewolf game, players are divided into two werewolves, two villagers, one seer and one witch. Note only werewolves know who are their teammates.\nWerewolves: They know their teammates' identities and attempt to eliminate a villager each night while trying to remain undetected.\nVillagers: They do not know who the werewolves are and must work together during the day to deduce who the werewolves might be and vote to eliminate them.\nSeer: A villager with the ability to learn the true identity of one player each night. This role is crucial for the villagers to gain information.\nWitch: A character who has a one-time ability to save a player from being eliminated at night (sometimes this is a potion of life) and a one-time ability to eliminate a player at night (a potion of death).\n\nGAME RULE:\nThe game is consisted of two phases: night phase and day phase. The two phases are repeated until werewolf or villager win the game.\n1. Night Phase: During the night, the werewolves discuss and vote for a player to eliminate. Special roles also perform their actions at this time (e.g., the Seer chooses a player to learn their role, the witch chooses a decide if save the player).\n2. Day Phase: During the day, all surviving players discuss who they suspect might be a werewolf. No one reveals their role unless it serves a strategic purpose. After the discussion, a vote is taken, and the player with the most votes is \"lynched\" or eliminated from the game.\n\nVICTORY CONDITION:\nFor werewolves, they win the game if the number of werewolves is equal to or greater than the number of remaining villagers.\nFor villagers, they win if they identify and eliminate all of the werewolves in the group.\n\nCONSTRAINTS:\n1. Your response should be in the first person.\n2. This is a conversational game. You should response only based on the conversation history and your strategy.\n\nYou are playing witch in this game.\n", - "model_id": "gpt-3.5-turbo", + "model_id": "gpt-4", "use_memory": true } } diff --git a/examples/werewolf/configs/model_configs.json b/examples/werewolf/configs/model_configs.json index 1d1720d57..28031a310 100644 --- a/examples/werewolf/configs/model_configs.json +++ b/examples/werewolf/configs/model_configs.json @@ -10,7 +10,7 @@ } }, { - "model_type": "post_api", + "model_type": "post_api_chat", "model_id": "my_post_api", "api_url": "https://xxx", "headers": {}, diff --git a/notebook/conversation.ipynb b/notebook/conversation.ipynb index d16f9d0d4..6e2da5f4a 100644 --- a/notebook/conversation.ipynb +++ b/notebook/conversation.ipynb @@ -49,8 +49,9 @@ "agentscope.init(\n", " model_configs=[\n", " {\n", - " \"type\": \"openai\",\n", - " \"name\": \"gpt-3.5-turbo\",\n", + " \"model_type\": \"openai\",\n", + " \"model_id\": \"gpt-3.5-turbo\",\n", + " \"model\": \"gpt-3.5-turbo\",\n", " \"api_key\": \"xxx\", # Load from env if not provided\n", " \"organization\": \"xxx\", # Load from env if not provided\n", " \"generate_args\": {\n", @@ -58,8 +59,8 @@ " },\n", " },\n", " {\n", - " \"type\": \"post_api\",\n", - " \"name\": \"my_post_api\",\n", + " \"model_type\": \"post_api_chat\",\n", + " \"model_id\": \"my_post_api\",\n", " \"api_url\": \"https://xxx\",\n", " \"headers\": {},\n", " },\n", @@ -85,7 +86,7 @@ "dialog_agent = DialogAgent(\n", " name=\"Assistant\",\n", " sys_prompt=\"You're a helpful assistant.\",\n", - " model=\"gpt-3.5-turbo\", # replace by your model config name\n", + " model_id=\"gpt-3.5-turbo\", # replace by your model config name\n", ")\n", "user_agent = UserAgent()" ] diff --git a/notebook/distributed_debate.ipynb b/notebook/distributed_debate.ipynb index 9e03f2577..c434f7ecd 100644 --- a/notebook/distributed_debate.ipynb +++ b/notebook/distributed_debate.ipynb @@ -48,22 +48,24 @@ "source": [ "model_configs = [\n", " {\n", - " \"type\": \"openai\",\n", - " \"name\": \"gpt-3.5-turbo\",\n", - " \"parameters\": {\n", - " \"api_key\": \"xxx\",\n", - " \"organization_id\": \"xxx\",\n", - " \"temperature\": 0.0\n", - " }\n", + " \"model_type\": \"openai\",\n", + " \"model_id\": \"gpt-3.5-turbo\",\n", + " \"model\": \"gpt-3.5-turbo\",\n", + " \"api_key\": \"xxx\",\n", + " \"organization\": \"xxx\",\n", + " \"generate_args\": {\n", + " \"temperature\": 0.0,\n", + " },\n", " },\n", " {\n", - " \"type\": \"openai\",\n", - " \"name\": \"gpt-4\",\n", - " \"parameters\": {\n", - " \"api_key\": \"xxx\",\n", - " \"organization_id\": \"xxx\",\n", - " \"temperature\": 0.0\n", - " }\n", + " \"model_type\": \"openai\",\n", + " \"model_id\": \"gpt-4\",\n", + " \"model\": \"gpt-4\",\n", + " \"api_key\": \"xxx\",\n", + " \"organization\": \"xxx\",\n", + " \"generate_args\": {\n", + " \"temperature\": 0.0,\n", + " },\n", " }\n", "]" ] @@ -90,19 +92,19 @@ "\n", "pro_agent = DialogAgent(\n", " name=\"Pro\",\n", - " model=\"gpt-3.5-turbo\",\n", + " model_id=\"gpt-3.5-turbo\",\n", " use_memory=True,\n", " sys_prompt=\"Assume the role of a debater who is arguing in favor of the proposition that AGI (Artificial General Intelligence) can be achieved using the GPT model framework. Construct a coherent and persuasive argument, including scientific, technological, and theoretical evidence, to support the statement that GPT models are a viable path to AGI. Highlight the advancements in language understanding, adaptability, and scalability of GPT models as key factors in progressing towards AGI.\",\n", ").to_dist()\n", "con_agent = DialogAgent(\n", " name=\"Con\",\n", - " model=\"gpt-3.5-turbo\",\n", + " model_id=\"gpt-3.5-turbo\",\n", " use_memory=True,\n", " sys_prompt=\"Assume the role of a debater who is arguing against the proposition that AGI can be achieved using the GPT model framework. Construct a coherent and persuasive argument, including scientific, technological, and theoretical evidence, to support the statement that GPT models, while impressive, are insufficient for reaching AGI. Discuss the limitations of GPT models such as lack of understanding, consciousness, ethical reasoning, and general problem-solving abilities that are essential for true AGI.\",\n", ").to_dist()\n", "judge_agent = DialogAgent(\n", " name=\"Judge\",\n", - " model=\"gpt-3.5-turbo\",\n", + " model_id=\"gpt-3.5-turbo\",\n", " use_memory=True,\n", " sys_prompt=\"Assume the role of an impartial judge in a debate where the affirmative side argues that AGI can be achieved using the GPT model framework, and the negative side contests this. Listen to both sides' arguments and provide an analytical judgment on which side presented a more compelling and reasonable case. Consider the strength of the evidence, the persuasiveness of the reasoning, and the overall coherence of the arguments presented by each side.\"\n", ").to_dist()" diff --git a/notebook/distributed_dialog.ipynb b/notebook/distributed_dialog.ipynb index 51e018873..da3bd2637 100644 --- a/notebook/distributed_dialog.ipynb +++ b/notebook/distributed_dialog.ipynb @@ -41,22 +41,24 @@ "source": [ "model_configs = [\n", " {\n", - " \"type\": \"openai\",\n", - " \"name\": \"gpt-3.5-turbo\",\n", - " \"parameters\": {\n", - " \"api_key\": \"xxx\",\n", - " \"organization_id\": \"xxx\",\n", - " \"temperature\": 0.0\n", - " }\n", + " \"model_type\": \"openai\",\n", + " \"model_id\": \"gpt-3.5-turbo\",\n", + " \"model\": \"gpt-3.5-turbo\",\n", + " \"api_key\": \"xxx\",\n", + " \"organization\": \"xxx\",\n", + " \"generate_args\": {\n", + " \"temperature\": 0.0,\n", + " },\n", " },\n", " {\n", - " \"type\": \"openai\",\n", - " \"name\": \"gpt-4\",\n", - " \"parameters\": {\n", - " \"api_key\": \"xxx\",\n", - " \"organization_id\": \"xxx\",\n", - " \"temperature\": 0.0\n", - " }\n", + " \"model_type\": \"openai\",\n", + " \"model_id\": \"gpt-4\",\n", + " \"model\": \"gpt-4\",\n", + " \"api_key\": \"xxx\",\n", + " \"organization\": \"xxx\",\n", + " \"generate_args\": {\n", + " \"temperature\": 0.0,\n", + " },\n", " }\n", "]" ] @@ -89,7 +91,7 @@ "assistant_agent = DialogAgent(\n", " name=\"Assistant\",\n", " sys_prompt=\"You are a helpful assistant.\",\n", - " model=\"gpt-3.5-turbo\",\n", + " model_id=\"gpt-3.5-turbo\",\n", " use_memory=True,\n", ").to_dist()\n", "user_agent = UserAgent(\n", diff --git a/src/agentscope/agents/dialog_agent.py b/src/agentscope/agents/dialog_agent.py index 5fe88ecf6..6f1236759 100644 --- a/src/agentscope/agents/dialog_agent.py +++ b/src/agentscope/agents/dialog_agent.py @@ -84,7 +84,7 @@ def reply(self, x: dict = None) -> dict: ) # call llm and generate response - response = self.model(prompt) + response = self.model(prompt).text msg = Msg(self.name, response) # logging and record the message in memory diff --git a/src/agentscope/agents/dict_dialog_agent.py b/src/agentscope/agents/dict_dialog_agent.py index ed150a99c..2cc33c15d 100644 --- a/src/agentscope/agents/dict_dialog_agent.py +++ b/src/agentscope/agents/dict_dialog_agent.py @@ -134,7 +134,7 @@ def reply(self, x: dict = None) -> dict: parse_func=self.parse_func, fault_handler=self.fault_handler, max_retries=self.max_retries, - ) + ).text # logging raw messages in debug mode logger.debug(json.dumps(response, indent=4)) diff --git a/src/agentscope/models/model.py b/src/agentscope/models/model.py index e78bfd7e9..d4600f98b 100644 --- a/src/agentscope/models/model.py +++ b/src/agentscope/models/model.py @@ -6,7 +6,7 @@ { "model_id": "{model_id}", - "model_type": "openai" | "post_api", + "model_type": "openai" | "post_api" | ..., ... } diff --git a/src/agentscope/models/post_model.py b/src/agentscope/models/post_model.py index f345fb28f..0aec00057 100644 --- a/src/agentscope/models/post_model.py +++ b/src/agentscope/models/post_model.py @@ -174,7 +174,7 @@ class PostAPIChatWrapper(PostAPIModelWrapperBase): def _parse_response(self, response: dict) -> ModelResponse: return ModelResponse( - text=response.json()["data"]["response"]["choices"][0]["message"][ + text=response["data"]["response"]["choices"][0]["message"][ "content" ], )