Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

Split code blocks on newlines and <br> (#56)

Dmitrii Khizbullin13eb00680eec
diff --git a/.vscode/settings.json b/.vscode/settings.json
index 65122128b..5d82eafbe 100644
--- a/.vscode/settings.json
+++ b/.vscode/settings.json
@@ -2,10 +2,11 @@
"makefile.extensionOutputFolder": "./.vscode",
"python.formatting.provider": "yapf",
"python.testing.pytestArgs": [
- "test"
+ "."
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,
+ "python.envFile": "${workspaceFolder}/.envs",
"python.linting.enabled": true,
"python.linting.pylintEnabled": false,
"python.linting.pycodestyleEnabled": false,
@@ -25,4 +26,11 @@
"source.organizeImports": true
},
},
+ "python.testing.unittestArgs": [
+ "-v",
+ "-s",
+ ".",
+ "-p",
+ "test_*.py"
+ ],
}
\ No newline at end of file
diff --git a/apps/agents/README.md b/apps/agents/README.md
new file mode 100644
index 000000000..98c8dd93d
--- /dev/null
+++ b/apps/agents/README.md
@@ -0,0 +1,6 @@
+# Agents app showcases Role Playing API as a Gradio web app
+
+Run the app:
+```
+python agents.py --api-key=YOUR-OPENAI-API-KEY
+```
\ No newline at end of file
diff --git a/apps/agents/agents.py b/apps/agents/agents.py
index b3bfdc358..a3fb11259 100644
--- a/apps/agents/agents.py
+++ b/apps/agents/agents.py
@@ -13,6 +13,7 @@ import gradio as gr
import openai
import openai.error
import tenacity
+from text_utils import split_markdown_code
from camel.agent import RolePlaying
from camel.message import AssistantChatMessage
@@ -20,6 +21,8 @@ from camel.message import AssistantChatMessage
REPO_ROOT = os.path.realpath(
os.path.join(os.path.dirname(os.path.abspath(__file__)), "../.."))
+os.chdir(REPO_ROOT)
+
ChatBotHistory = List[Tuple[Optional[str], Optional[str]]]
@@ -143,7 +146,9 @@ def role_playing_start(
try:
session = RolePlaying(assistant, user, original_task,
with_task_specify=True, with_task_planner=False)
- except (openai.error.RateLimitError, tenacity.RetryError) as ex:
+ except (openai.error.RateLimitError, tenacity.RetryError,
+ RuntimeError) as ex:
+ print("OpenAI API exception 0 " + str(ex))
return (state, str(ex), "", [], gr.update())
# Can't re-create a state like below since it
@@ -185,12 +190,13 @@ def role_playing_chat_init(state) -> \
if state.session is None:
print("Error: session is none on role_playing_chat_init call")
- return {} # may fail
+ return state, state.chat, gr.update()
try:
assistant_msg, _ = state.session.init_chat()
assistant_msg: AssistantChatMessage
- except (openai.error.RateLimitError, tenacity.RetryError) as ex:
+ except (openai.error.RateLimitError, tenacity.RetryError,
+ RuntimeError) as ex:
print("OpenAI API exception 1 " + str(ex))
state.session = None
return state, state.chat, gr.update()
@@ -221,7 +227,7 @@ def role_playing_chat_cont(state) -> \
"""
if state.session is None:
- return state, state.chat, gr.update(), gr.update()
+ return state, state.chat, gr.update(visible=False), gr.update()
if state.saved_assistant_msg is None:
return state, state.chat, gr.update(), gr.update()
@@ -229,7 +235,8 @@ def role_playing_chat_cont(state) -> \
try:
assistant_msgs, user_msgs = state.session.step(
state.saved_assistant_msg)
- except (openai.error.RateLimitError, tenacity.RetryError) as ex:
+ except (openai.error.RateLimitError, tenacity.RetryError,
+ RuntimeError) as ex:
print("OpenAI API exception 2 " + str(ex))
state.session = None
return state, state.chat, gr.update(), gr.update()
@@ -239,8 +246,8 @@ def role_playing_chat_cont(state) -> \
state.saved_assistant_msg = a_msg
- state.chat.append((None, u_msg.content))
- state.chat.append((a_msg.content, None))
+ state.chat.append((None, split_markdown_code(u_msg.content)))
+ state.chat.append((split_markdown_code(a_msg.content), None))
if len(state.chat) >= state.max_messages:
state.session = None
@@ -378,8 +385,6 @@ if __name__ == "__main__":
print("Getting Agents web server online...")
- os.chdir(REPO_ROOT)
-
with gr.Blocks() as demo:
construct_demo(args.api_key)
diff --git a/apps/agents/test/test_text_utils.py b/apps/agents/test/test_text_utils.py
new file mode 100644
index 000000000..433704218
--- /dev/null
+++ b/apps/agents/test/test_text_utils.py
@@ -0,0 +1,112 @@
+from unittest import TestCase
+
+import apps.agents.text_utils as text_utils
+
+
+class TestTextUtils(TestCase):
+ def test_split_markdown_code_newline(self):
+ self.assertTrue(True)
+
+ inp = "Solution: To preprocess the historical stock data, we " \
+ "can perform the following steps:\n\n1. Remove any unnecessary" \
+ " columns that do not contribute to the prediction, such as" \
+ " the stock symbol or date.\n2. Check for and handle any " \
+ "missing or null values in the data.\n3. Normalize the data" \
+ " to ensure that all features are on the same scale. This " \
+ "can be done using techniques such as Min-Max scaling or " \
+ "Z-score normalization.\n4. Split the data into training " \
+ "and testing sets. The training set will be used to train " \
+ "the machine learning model, while the testing set will be " \
+ "used to evaluate its performance.\n\nHere is an example " \
+ "code snippet to preprocess the data using Pandas:\n\n```\n" \
+ "import pandas as pd\nfrom sklearn.preprocessing import " \
+ "MinMaxScaler\nfrom sklearn.model_selection import " \
+ "train_test_split\n\n# Read in the historical stock data\ndata" \
+ " = pd.read_csv('historical_stock_data.csv')\n\n# Remove " \
+ "unnecessary columns\ndata = data.drop(['symbol', 'date'], " \
+ "axis=1)\n\n# Handle missing values\ndata = data.fillna(" \
+ "method='ffill')\n\n# Normalize the data\nscaler = " \
+ "MinMaxScaler()\ndata = scaler.fit_transform(data)\n\n# " \
+ "Split the data into training and testing sets\nX_train, " \
+ "X_test, y_train, y_test = train_test_split(data[:, :-1], " \
+ "data[:, -1], test_size=0.2, random_state=42)\n```\n\nNext " \
+ "request."
+ gt = "Solution: To preprocess the historical stock data, we " \
+ "can perform the following steps:\n\n1. Remove any unnecessary" \
+ " columns that do not contribute to the prediction, such as" \
+ " the stock symbol or date.\n2. Check for and handle any missing" \
+ " or null values in the data.\n3. Normalize the data to ensure" \
+ " that all features are on the same scale. This can be done" \
+ " using techniques such as Min-Max scaling or Z-score" \
+ " normalization.\n4. Split the data into training and testing" \
+ " sets. The training set will be used to train the machine" \
+ " learning model, while the testing set will be used to" \
+ " evaluate its performance.\n\nHere is an example code snippet" \
+ " to preprocess the data using Pandas:\n\n\n```import pandas" \
+ " as pd```\n```from sklearn.preprocessing import MinMaxScaler" \
+ "```\n```from sklearn.model_selection import train_test_split" \
+ "```\n\n```# Read in the historical stock data```\n```data =" \
+ " pd.read_csv('historical_stock_data.csv')```\n\n```# Remove" \
+ " unnecessary columns```\n```data = data.drop(['symbol', " \
+ "'date'], axis=1)```\n\n```# Handle missing values```\n```data" \
+ " = data.fillna(method='ffill')```\n\n```# Normalize the data" \
+ "```\n```scaler = MinMaxScaler()```\n```data = scaler." \
+ "fit_transform(data)```\n\n```# Split the data into training" \
+ " and testing sets```\n```X_train, X_test, y_train, y_test" \
+ " = train_test_split(data[:, :-1], data[:, -1], test_size=0.2," \
+ " random_state=42)```\n\n\nNext request."
+
+ out = text_utils.split_markdown_code(inp)
+ self.assertEqual(out, gt)
+
+ def test_split_markdown_code_br(self):
+ self.assertTrue(True)
+
+ inp = \
+ "Solution: Define the Bayesian optimization object." \
+ "\n" \
+ "We can define the Bayesian optimization object using" \
+ " the BayesianOptimization class from the bayes_opt module." \
+ " Here is an example of how to define the Bayesian" \
+ " optimization object:" \
+ "\n" \
+ "```<br># Replace 'objective_function' with the actual" \
+ " objective function<br># Replace 'bounds' with the actual" \
+ " search space<br># Replace 'model' with the actual machine" \
+ " learning model<br>bo = BayesianOptimization(<br> " \
+ "f=objective_function,<br> pbounds=bounds,<br> verbose=2,<br>" \
+ " random_state=1,<br>)<br>```" \
+ "\n" \
+ "This will define the Bayesian optimization object with the" \
+ " specified objective function, search space, and machine" \
+ " learning model. The BayesianOptimization class takes " \
+ "several arguments, including f for the objective function," \
+ " pbounds for the search space, verbose for the verbosity" \
+ " level, and random_state for the random seed." \
+ "\n" \
+ "Next request."
+ gt = \
+ "Solution: Define the Bayesian optimization object." \
+ "\n" \
+ "We can define the Bayesian optimization object using the" \
+ " BayesianOptimization class from the bayes_opt module. Here is" \
+ " an example of how to define the Bayesian optimization object:" \
+ "\n" \
+ "\n```# Replace 'objective_function' with the actual objective" \
+ " function```\n```# Replace 'bounds' with the actual search" \
+ " space```\n```# Replace 'model' with the actual machine" \
+ " learning model```\n```bo = BayesianOptimization(```\n```" \
+ " f=objective_function,```\n``` pbounds=bounds,```\n``` " \
+ "verbose=2,```\n``` random_state=1,```\n```)```\n" \
+ "\n" \
+ "This will define the Bayesian optimization object with " \
+ "the specified objective function, search space, and machine" \
+ " learning model. The BayesianOptimization class takes several" \
+ " arguments, including f for the objective function, pbounds" \
+ " for the search space, verbose for the verbosity level, and" \
+ " random_state for the random seed." \
+ "\n" \
+ "Next request."
+
+ out = text_utils.split_markdown_code(inp)
+ self.assertEqual(out, gt, msg=f"OUT={out}")
diff --git a/apps/agents/text_utils.py b/apps/agents/text_utils.py
new file mode 100644
index 000000000..134bd1601
--- /dev/null
+++ b/apps/agents/text_utils.py
@@ -0,0 +1,31 @@
+import re
+
+
+def split_markdown_code(string: str) -> str:
+ """ Split a multiline block of markdown code (triple-quotes) into
+ line-sized sub-blocks to make newlines stay where they belong.
+ This transformation is a workaround to a known Gradio bug:
+ https://github.com/gradio-app/gradio/issues/3531
+
+ Args:
+ string (str): markdown string incompatible with gr.Chatbot
+
+ Returns:
+ str: markdown string which is compatible with gr.Chatbot
+ """
+ substr_list = string.split("```")
+ out = []
+ for i_subs, subs in enumerate(substr_list):
+ if i_subs % 2 == 0: # outsize code, don't change
+ out.append(subs)
+ else: # inside code
+ br_done = re.sub(r"<br>", "\n", subs)
+
+ def repl(m):
+ return "```{}```".format(m.group(0))
+
+ new_subs = re.sub(r"\n+", repl, br_done)
+ out.append(new_subs)
+ out_str = "```".join(out)
+ out_str_cleanup = re.sub(r"``````", "", out_str)
+ return out_str_cleanup
diff --git a/apps/data_explorer/loader.py b/apps/data_explorer/loader.py
index 8fe1932d7..4df77fd42 100644
--- a/apps/data_explorer/loader.py
+++ b/apps/data_explorer/loader.py
@@ -170,7 +170,3 @@ def load_datasets(path: Optional[str] = None) -> Datasets:
name = os.path.splitext(os.path.basename(file_name))[0]
datasets[name] = load_zip(file_name)
return datasets
-
-
-if __name__ == "__main__":
- data = load_datasets()
diff --git a/apps/data_explorer/test/test_loader.py b/apps/data_explorer/test/test_loader.py
new file mode 100644
index 000000000..23a4ca748
--- /dev/null
+++ b/apps/data_explorer/test/test_loader.py
@@ -0,0 +1,9 @@
+from unittest import TestCase
+
+import apps.data_explorer.loader as loader
+
+
+class TestLoader(TestCase):
+ def test_load_datasets_smoke(self):
+ data = loader.load_datasets()
+ self.assertIsNotNone(data)
diff --git a/test/agent/test_chat_agent.py b/test/agent/test_chat_agent.py
index 09a629592..d82277d23 100644
--- a/test/agent/test_chat_agent.py
+++ b/test/agent/test_chat_agent.py
@@ -9,12 +9,13 @@ from camel.message import ChatMessage
from camel.typing import ModelType, RoleType, TaskType
from camel.utils import get_model_token_limit
-assert os.environ.get("OPENAI_API_KEY") is not None, "Missing OPENAI_API_KEY"
-
@pytest.mark.parametrize(
'model', [ModelType.GPT_3_5_TURBO, ModelType.GPT_4, ModelType.GPT_4_32k])
def test_chat_agent(model):
+ assert os.environ.get(
+ "OPENAI_API_KEY") is not None, "Missing OPENAI_API_KEY"
+
model_config = ChatGPTConfig()
system_message = SystemMessageGenerator(
task_type=TaskType.AI_SOCIETY).from_dict(
diff --git a/test/agent/test_task_agent.py b/test/agent/test_task_agent.py
index e3a9aad5e..d17c35a39 100644
--- a/test/agent/test_task_agent.py
+++ b/test/agent/test_task_agent.py
@@ -4,10 +4,11 @@ from camel.agent import TaskPlannerAgent, TaskSpecifyAgent
from camel.configs import ChatGPTConfig
from camel.typing import TaskType
-assert os.environ.get("OPENAI_API_KEY") is not None, "Missing OPENAI_API_KEY"
-
def test_task_specify_ai_society_agent():
+ assert os.environ.get("OPENAI_API_KEY") is not None, \
+ "Missing OPENAI_API_KEY"
+
original_task_prompt = "Improving stage presence and performance skills"
print(f"Original task prompt:\n{original_task_prompt}\n")
task_specify_agent = TaskSpecifyAgent(model_config=ChatGPTConfig(