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

feat: llama model tool calling (#965)

Wendong-Fanb8e1f5cca79c
diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.yml
index ba98fa9b2..a7fabb18d 100644
--- a/.github/ISSUE_TEMPLATE/bug_report.yml
+++ b/.github/ISSUE_TEMPLATE/bug_report.yml
@@ -27,7 +27,7 @@ body:
attributes:
label: What version of camel are you using?
description: Run command `python3 -c 'print(__import__("camel").__version__)'` in your shell and paste the output here.
- placeholder: E.g., 0.2.1
+ placeholder: E.g., 0.2.1a
validations:
required: true
diff --git a/README.md b/README.md
index b394b85d6..a307101eb 100644
--- a/README.md
+++ b/README.md
@@ -119,7 +119,7 @@ conda create --name camel python=3.10
conda activate camel
# Clone github repo
-git clone -b v0.2.1 https://github.com/camel-ai/camel.git
+git clone -b v0.2.1a https://github.com/camel-ai/camel.git
# Change directory into project directory
cd camel
diff --git a/camel/__init__.py b/camel/__init__.py
index 15f5b34aa..9a656ef72 100644
--- a/camel/__init__.py
+++ b/camel/__init__.py
@@ -12,7 +12,7 @@
# limitations under the License.
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
-__version__ = '0.2.1'
+__version__ = '0.2.1a'
__all__ = [
'__version__',
diff --git a/camel/agents/chat_agent.py b/camel/agents/chat_agent.py
index 9538c9712..02ea9ff98 100644
--- a/camel/agents/chat_agent.py
+++ b/camel/agents/chat_agent.py
@@ -15,6 +15,8 @@ from __future__ import annotations
import json
import logging
+import re
+import uuid
from collections import defaultdict
from typing import (
TYPE_CHECKING,
@@ -28,6 +30,7 @@ from typing import (
)
from openai.types.chat import ChatCompletionMessageToolCall
+from openai.types.chat.chat_completion_message_tool_call import Function
from pydantic import BaseModel
from camel.agents.base import BaseAgent
@@ -190,7 +193,7 @@ class ChatAgent(BaseAgent):
tool.get_openai_tool_schema() for tool in all_tools
]
self.model_backend.model_config_dict['tools'] = tool_schema_list
-
+ self.tool_schema_list = tool_schema_list
self.model_config_dict = self.model_backend.model_config_dict
self.model_token_limit = token_limit or self.model_backend.token_limit
@@ -206,6 +209,56 @@ class ChatAgent(BaseAgent):
self.response_terminators = response_terminators or []
self.init_messages()
+ # ruff: noqa: E501
+ def _generate_tool_prompt(self, tool_schema_list: List[Dict]) -> str:
+ tool_prompts = []
+
+ for tool in tool_schema_list:
+ tool_info = tool['function']
+ tool_name = tool_info['name']
+ tool_description = tool_info['description']
+ tool_json = json.dumps(tool_info, indent=4)
+
+ prompt = f"Use the function '{tool_name}' to '{tool_description}':\n{tool_json}\n"
+ tool_prompts.append(prompt)
+
+ tool_prompt_str = "\n".join(tool_prompts)
+
+ final_prompt = f'''
+ # Tool prompt
+ TOOL_PROMPT = f"""
+ You have access to the following functions:
+
+ {tool_prompt_str}
+
+ If you choose to call a function ONLY reply in the following format with no prefix or suffix:
+
+ <function=example_function_name>{{"example_name": "example_value"}}</function>
+
+ Reminder:
+ - Function calls MUST follow the specified format, start with <function= and end with </function>
+ - Required parameters MUST be specified
+ - Only call one function at a time
+ - Put the entire function call reply on one line
+ - If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls
+ """
+ '''
+ return final_prompt
+
+ def _parse_tool_response(self, response: str):
+ function_regex = r"<function=(\w+)>(.*?)</function>"
+ match = re.search(function_regex, response)
+
+ if match:
+ function_name, args_string = match.groups()
+ try:
+ args = json.loads(args_string)
+ return {"function": function_name, "arguments": args}
+ except json.JSONDecodeError as error:
+ print(f"Error parsing function arguments: {error}")
+ return None
+ return None
+
def reset(self):
r"""Resets the :obj:`ChatAgent` to its initial state and returns the
stored messages.
@@ -367,89 +420,221 @@ class ChatAgent(BaseAgent):
a boolean indicating whether the chat session has terminated,
and information about the chat session.
"""
- self.update_memory(input_message, OpenAIBackendRole.USER)
+ if (
+ isinstance(self.model_type, ModelType)
+ and "lama" in self.model_type.value
+ or isinstance(self.model_type, str)
+ and "lama" in self.model_type
+ ):
+ if self.model_backend.model_config_dict['tools']:
+ tool_prompt = self._generate_tool_prompt(self.tool_schema_list)
+
+ tool_sys_msg = BaseMessage.make_assistant_message(
+ role_name="Assistant",
+ content=tool_prompt,
+ )
- tool_call_records: List[FunctionCallingRecord] = []
- while True:
- # Check if token has exceeded
- try:
- openai_messages, num_tokens = self.memory.get_context()
- except RuntimeError as e:
- return self._step_token_exceed(
- e.args[1], tool_call_records, "max_tokens_exceeded"
+ self.update_memory(tool_sys_msg, OpenAIBackendRole.SYSTEM)
+
+ self.update_memory(input_message, OpenAIBackendRole.USER)
+
+ tool_call_records: List[FunctionCallingRecord] = []
+ while True:
+ # Check if token has exceeded
+ try:
+ openai_messages, num_tokens = self.memory.get_context()
+ except RuntimeError as e:
+ return self._step_token_exceed(
+ e.args[1], tool_call_records, "max_tokens_exceeded"
+ )
+
+ (
+ response,
+ output_messages,
+ finish_reasons,
+ usage_dict,
+ response_id,
+ ) = self._step_model_response(openai_messages, num_tokens)
+ # If the model response is not a function call, meaning the
+ # model has generated a message response, break the loop
+ if (
+ not self.is_tools_added()
+ or not isinstance(response, ChatCompletion)
+ or "</function>" not in response.choices[0].message.content # type: ignore[operator]
+ ):
+ break
+
+ parsed_content = self._parse_tool_response(
+ response.choices[0].message.content # type: ignore[arg-type]
)
- (
- response,
+ response.choices[0].message.tool_calls = [
+ ChatCompletionMessageToolCall(
+ id=str(uuid.uuid4()),
+ function=Function(
+ arguments=str(parsed_content["arguments"]).replace(
+ "'", '"'
+ ),
+ name=str(parsed_content["function"]),
+ ),
+ type="function",
+ )
+ ]
+
+ # Check for external tool call
+ tool_call_request = response.choices[0].message.tool_calls[0]
+ if tool_call_request.function.name in self.external_tool_names:
+ # if model calls an external tool, directly return the
+ # request
+ info = self._step_get_info(
+ output_messages,
+ finish_reasons,
+ usage_dict,
+ response_id,
+ tool_call_records,
+ num_tokens,
+ tool_call_request,
+ )
+ return ChatAgentResponse(
+ msgs=output_messages,
+ terminated=self.terminated,
+ info=info,
+ )
+
+ # Normal function calling
+ tool_call_records.append(
+ self._step_tool_call_and_update(response)
+ )
+
+ if (
+ output_schema is not None
+ and self.model_type.supports_tool_calling
+ ):
+ (
+ output_messages,
+ finish_reasons,
+ usage_dict,
+ response_id,
+ tool_call,
+ num_tokens,
+ ) = self._structure_output_with_function(output_schema)
+ tool_call_records.append(tool_call)
+
+ info = self._step_get_info(
output_messages,
finish_reasons,
usage_dict,
response_id,
- ) = self._step_model_response(openai_messages, num_tokens)
+ tool_call_records,
+ num_tokens,
+ )
- # If the model response is not a function call, meaning the model
- # has generated a message response, break the loop
- if (
- not self.is_tools_added()
- or not isinstance(response, ChatCompletion)
- or response.choices[0].message.tool_calls is None
- ):
- break
+ if len(output_messages) == 1:
+ # Auto record if the output result is a single message
+ self.record_message(output_messages[0])
+ else:
+ logger.warning(
+ "Multiple messages returned in `step()`, message won't be "
+ "recorded automatically. Please call `record_message()` "
+ "to record the selected message manually."
+ )
- # Check for external tool call
- tool_call_request = response.choices[0].message.tool_calls[0]
- if tool_call_request.function.name in self.external_tool_names:
- # if model calls an external tool, directly return the request
- info = self._step_get_info(
+ return ChatAgentResponse(
+ msgs=output_messages, terminated=self.terminated, info=info
+ )
+
+ else:
+ self.update_memory(input_message, OpenAIBackendRole.USER)
+
+ tool_call_records: List[FunctionCallingRecord] = [] # type: ignore[no-redef]
+ while True:
+ # Check if token has exceeded
+ try:
+ openai_messages, num_tokens = self.memory.get_context()
+ except RuntimeError as e:
+ return self._step_token_exceed(
+ e.args[1], tool_call_records, "max_tokens_exceeded"
+ )
+
+ (
+ response,
output_messages,
finish_reasons,
usage_dict,
response_id,
- tool_call_records,
- num_tokens,
- tool_call_request,
- )
- return ChatAgentResponse(
- msgs=output_messages, terminated=self.terminated, info=info
+ ) = self._step_model_response(openai_messages, num_tokens)
+ # If the model response is not a function call, meaning the
+ # model has generated a message response, break the loop
+ if (
+ not self.is_tools_added()
+ or not isinstance(response, ChatCompletion)
+ or response.choices[0].message.tool_calls is None
+ ):
+ break
+
+ # Check for external tool call
+ tool_call_request = response.choices[0].message.tool_calls[0]
+
+ if tool_call_request.function.name in self.external_tool_names:
+ # if model calls an external tool, directly return the
+ # request
+ info = self._step_get_info(
+ output_messages,
+ finish_reasons,
+ usage_dict,
+ response_id,
+ tool_call_records,
+ num_tokens,
+ tool_call_request,
+ )
+ return ChatAgentResponse(
+ msgs=output_messages,
+ terminated=self.terminated,
+ info=info,
+ )
+
+ # Normal function calling
+ tool_call_records.append(
+ self._step_tool_call_and_update(response)
)
- # Normal function calling
- tool_call_records.append(self._step_tool_call_and_update(response))
+ if (
+ output_schema is not None
+ and self.model_type.supports_tool_calling
+ ):
+ (
+ output_messages,
+ finish_reasons,
+ usage_dict,
+ response_id,
+ tool_call,
+ num_tokens,
+ ) = self._structure_output_with_function(output_schema)
+ tool_call_records.append(tool_call)
- if output_schema is not None and self.model_type.supports_tool_calling:
- (
+ info = self._step_get_info(
output_messages,
finish_reasons,
usage_dict,
response_id,
- tool_call,
+ tool_call_records,
num_tokens,
- ) = self._structure_output_with_function(output_schema)
- tool_call_records.append(tool_call)
+ )
- info = self._step_get_info(
- output_messages,
- finish_reasons,
- usage_dict,
- response_id,
- tool_call_records,
- num_tokens,
- )
+ if len(output_messages) == 1:
+ # Auto record if the output result is a single message
+ self.record_message(output_messages[0])
+ else:
+ logger.warning(
+ "Multiple messages returned in `step()`, message won't be "
+ "recorded automatically. Please call `record_message()` "
+ "to record the selected message manually."
+ )
- if len(output_messages) == 1:
- # Auto record if the output result is a single message
- self.record_message(output_messages[0])
- else:
- logger.warning(
- "Multiple messages returned in `step()`, message won't be "
- "recorded automatically. Please call `record_message()` to "
- "record the selected message manually."
+ return ChatAgentResponse(
+ msgs=output_messages, terminated=self.terminated, info=info
)
- return ChatAgentResponse(
- msgs=output_messages, terminated=self.terminated, info=info
- )
-
async def step_async(
self,
input_message: BaseMessage,
diff --git a/docs/conf.py b/docs/conf.py
index 9d7ce67e1..7e04afb10 100644
--- a/docs/conf.py
+++ b/docs/conf.py
@@ -27,7 +27,7 @@ sys.path.insert(0, os.path.abspath('..'))
project = 'CAMEL'
copyright = '2024, CAMEL-AI.org'
author = 'CAMEL-AI.org'
-release = '0.2.1'
+release = '0.2.1a'
html_favicon = (
'https://raw.githubusercontent.com/camel-ai/camel/master/misc/favicon.png'
diff --git a/docs/get_started/setup.md b/docs/get_started/setup.md
index c620e37e3..00bad6d15 100644
--- a/docs/get_started/setup.md
+++ b/docs/get_started/setup.md
@@ -61,7 +61,7 @@ conda create --name camel python=3.10
conda activate camel
# Clone github repo
-git clone -b v0.2.1 https://github.com/camel-ai/camel.git
+git clone -b v0.2.1a https://github.com/camel-ai/camel.git
# Change directory into project directory
cd camel
diff --git a/examples/models/role_playing_with_sambanova.py b/examples/models/role_playing_with_sambanova.py
new file mode 100644
index 000000000..93bcbc7bb
--- /dev/null
+++ b/examples/models/role_playing_with_sambanova.py
@@ -0,0 +1,142 @@
+# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========
+
+from typing import List
+
+from colorama import Fore
+
+from camel.agents.chat_agent import FunctionCallingRecord
+from camel.configs import SambaCloudAPIConfig
+from camel.models import ModelFactory
+from camel.societies import RolePlaying
+from camel.toolkits import (
+ MathToolkit,
+ SearchToolkit,
+)
+from camel.types import ModelPlatformType
+from camel.utils import print_text_animated
+
+
+def main(
+ model_platform=ModelPlatformType.SAMBA,
+ model_type="Meta-Llama-3.1-70B-Instruct",
+ chat_turn_limit=10,
+) -> None:
+ task_prompt = (
+ "Assume now is 2024 in the Gregorian calendar, "
+ "estimate the current age of University of Oxford "
+ "and then add 10 more years to this age."
+ )
+
+ user_model_config = SambaCloudAPIConfig(temperature=0.2, max_tokens=2200)
+
+ tools_list = [
+ *MathToolkit().get_tools(),
+ *SearchToolkit().get_tools(),
+ ]
+ assistant_model_config = SambaCloudAPIConfig(
+ temperature=0.2, max_tokens=2200
+ )
+
+ role_play_session = RolePlaying(
+ assistant_role_name="Searcher",
+ user_role_name="Professor",
+ assistant_agent_kwargs=dict(
+ model=ModelFactory.create(
+ model_platform=model_platform,
+ model_type=model_type,
+ url="https://api.sambanova.ai/v1",
+ model_config_dict=assistant_model_config.as_dict(),
+ ),
+ tools=tools_list,
+ message_window_size=5,
+ ),
+ user_agent_kwargs=dict(
+ model=ModelFactory.create(
+ model_platform=model_platform,
+ model_type=model_type,
+ url="https://api.sambanova.ai/v1",
+ model_config_dict=user_model_config.as_dict(),
+ ),
+ message_window_size=5,
+ ),
+ task_prompt=task_prompt,
+ with_task_specify=False,
+ )
+
+ print(
+ Fore.GREEN
+ + f"AI Assistant sys message:\n{role_play_session.assistant_sys_msg}\n"
+ )
+ print(
+ Fore.BLUE + f"AI User sys message:\n{role_play_session.user_sys_msg}\n"
+ )
+
+ print(Fore.YELLOW + f"Original task prompt:\n{task_prompt}\n")
+ print(
+ Fore.CYAN
+ + "Specified task prompt:"
+ + f"\n{role_play_session.specified_task_prompt}\n"
+ )
+ print(Fore.RED + f"Final task prompt:\n{role_play_session.task_prompt}\n")
+
+ n = 0
+ input_msg = role_play_session.init_chat()
+ while n < chat_turn_limit:
+ n += 1
+ assistant_response, user_response = role_play_session.step(input_msg)
+
+ if assistant_response.terminated:
+ print(
+ Fore.GREEN
+ + (
+ "AI Assistant terminated. Reason: "
+ f"{assistant_response.info['termination_reasons']}."
+ )
+ )
+ break
+ if user_response.terminated:
+ print(
+ Fore.GREEN
+ + (
+ "AI User terminated. "
+ f"Reason: {user_response.info['termination_reasons']}."
+ )
+ )
+ break
+
+ # Print output from the user
+ print_text_animated(
+ Fore.BLUE + f"AI User:\n\n{user_response.msg.content}\n"
+ )
+
+ # Print output from the assistant, including any function
+ # execution information
+ print_text_animated(Fore.GREEN + "AI Assistant:")
+ tool_calls: List[FunctionCallingRecord] = [
+ FunctionCallingRecord(**call.as_dict())
+ for call in assistant_response.info['tool_calls']
+ ]
+ for func_record in tool_calls:
+ print_text_animated(f"{func_record}")
+ print_text_animated(f"{assistant_response.msg.content}\n")
+
+ if "CAMEL_TASK_DONE" in user_response.msg.content:
+ break
+
+ input_msg = assistant_response.msg
+
+
+if __name__ == "__main__":
+ main()
diff --git a/pyproject.toml b/pyproject.toml
index 00824d543..de119e65b 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"
[tool.poetry]
name = "camel-ai"
-version = "0.2.1"
+version = "0.2.1a"
authors = ["CAMEL-AI.org"]
description = "Communicative Agents for AI Society Study"
readme = "README.md"