Skip to content

Commit

Permalink
pass lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin-eecs committed Jul 2, 2023
1 parent 59ce686 commit cd36c5a
Show file tree
Hide file tree
Showing 14 changed files with 898 additions and 15 deletions.
13 changes: 13 additions & 0 deletions camel/embeddings/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# =========== 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. ===========
52 changes: 52 additions & 0 deletions camel/embeddings/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# =========== 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 abc import ABC, abstractmethod
from typing import List

class BaseEmbeddings(ABC):
r"""Base class for embedding models in CAMEL system.
Args:
texts (List[str]): List of texts to be embedded.
"""
texts: List[str]

@abstractmethod
def embed_documents(self) -> List[List[float]]:
r"""Abstract method for embedding documents.
Returns:
List[List[float]]: The embedded documents as a list of vectors.
"""
pass

@abstractmethod
def embed_query(self) -> List[float]:
r"""Abstract method for embedding a query text.
Returns:
List[float]: The embedded query as a vector.
"""
pass

def to_dict(self) -> Dict:
r"""Converts the inputs to a dictionary.
Returns:
dict: The converted dictionary.
"""
return {
"texts": self.texts,
}
13 changes: 13 additions & 0 deletions camel/memory/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# =========== 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 camel.memory.chat_message_histories.simple import SimpleChatMessageHistory
from camel.memory.chat_message_histories.file import FileChatMessageHistory
from camel.memory.buffer import ConversationBufferMemory
Expand Down
31 changes: 25 additions & 6 deletions camel/memory/base.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
# =========== 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 dataclasses import dataclass
from typing import List, Dict, Any, Optional
from typing import Any, Dict, List, Optional

from camel.memory.chat_message_histories import BaseChatMessageHistory


@dataclass
class BaseMemory:
r"""Base interface for memory in the CAMEL chat system.
Expand All @@ -29,7 +43,8 @@ def load_memory_variables(self, inputs: Dict[str, Any]) -> Dict[str, Any]:
"""
raise NotImplementedError

def save_context(self, inputs: Dict[str, Any], outputs: Dict[str, str]) -> None:
def save_context(self, inputs: Dict[str, Any], outputs: Dict[str,
str]) -> None:
r"""Saves the context of this model run to memory.
Args:
Expand Down Expand Up @@ -71,7 +86,8 @@ def __init__(
self.input_key = input_key
self.return_messages = return_messages

def _get_input_output(self, inputs: Dict[str, Any], outputs: Dict[str, str]) -> Tuple[str, str]:
def _get_input_output(self, inputs: Dict[str, Any],
outputs: Dict[str, str]) -> Tuple[str, str]:
r"""Get input and output keys.
Args:
Expand All @@ -82,20 +98,23 @@ def _get_input_output(self, inputs: Dict[str, Any], outputs: Dict[str, str]) ->
Tuple[str, str]: Input and output keys.
"""
if self.input_key is None:
prompt_input_key = get_prompt_input_key(inputs, self.memory_variables)
prompt_input_key = get_prompt_input_key(inputs,
self.memory_variables)
else:
prompt_input_key = self.input_key

if self.output_key is None:
if len(outputs) != 1:
raise ValueError(f"One output key expected, got {outputs.keys()}")
raise ValueError(
f"One output key expected, got {outputs.keys()}")
output_key = list(outputs.keys())[0]
else:
output_key = self.output_key

return inputs[prompt_input_key], outputs[output_key]

def save_context(self, inputs: Dict[str, Any], outputs: Dict[str, str]) -> None:
def save_context(self, inputs: Dict[str, Any], outputs: Dict[str,
str]) -> None:
r"""Save context from this conversation to buffer.
Args:
Expand Down
104 changes: 104 additions & 0 deletions camel/memory/buffer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# =========== 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 Any, Dict, List, Optional

from pydantic import root_validator

from camel.memory.chat_memory import BaseChatMemory, BaseMemory
from camel.memory.utils import get_prompt_input_key
from camel.schema import get_buffer_string


class ConversationBufferMemory(BaseChatMemory):
"""Buffer for storing conversation memory."""

human_prefix: str = "Human"
ai_prefix: str = "AI"
memory_key: str = "history" #: :meta private:

@property
def buffer(self) -> Any:
"""String buffer of memory."""
if self.return_messages:
return self.chat_memory.messages
else:
return get_buffer_string(
self.chat_memory.messages,
human_prefix=self.human_prefix,
ai_prefix=self.ai_prefix,
)

@property
def memory_variables(self) -> List[str]:
"""Will always return list of memory variables.
:meta private:
"""
return [self.memory_key]

def load_memory_variables(self, inputs: Dict[str, Any]) -> Dict[str, Any]:
"""Return history buffer."""
return {self.memory_key: self.buffer}


class ConversationStringBufferMemory(BaseMemory):
"""Buffer for storing conversation memory."""

human_prefix: str = "Human"
ai_prefix: str = "AI"
"""Prefix to use for AI generated responses."""
buffer: str = ""
output_key: Optional[str] = None
input_key: Optional[str] = None
memory_key: str = "history" #: :meta private:

@root_validator()
def validate_chains(cls, values: Dict) -> Dict:
"""Validate that return messages is not True."""
if values.get("return_messages", False):
raise ValueError(
"return_messages must be False for ConversationStringBufferMemory"
)
return values

@property
def memory_variables(self) -> List[str]:
"""Will always return list of memory variables.
:meta private:
"""
return [self.memory_key]

def load_memory_variables(self, inputs: Dict[str, Any]) -> Dict[str, str]:
"""Return history buffer."""
return {self.memory_key: self.buffer}

def save_context(self, inputs: Dict[str, Any], outputs: Dict[str, str]) -> None:
"""Save context from this conversation to buffer."""
if self.input_key is None:
prompt_input_key = get_prompt_input_key(inputs, self.memory_variables)
else:
prompt_input_key = self.input_key
if self.output_key is None:
if len(outputs) != 1:
raise ValueError(f"One output key expected, got {outputs.keys()}")
output_key = list(outputs.keys())[0]
else:
output_key = self.output_key
human = f"{self.human_prefix}: " + inputs[prompt_input_key]
ai = f"{self.ai_prefix}: " + outputs[output_key]
self.buffer += "\n" + "\n".join([human, ai])

def clear(self) -> None:
"""Clear memory contents."""
self.buffer = ""
13 changes: 13 additions & 0 deletions camel/memory/buffer_window.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# =========== 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. ===========
14 changes: 13 additions & 1 deletion camel/memory/chat_message_histories/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
# =========== 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 camel.memory.chat_message_histories.base import BaseChatMessageHistory
from camel.memory.chat_message_histories.file import FileChatMessageHistory
from camel.memory.chat_message_histories.simple import SimpleChatMessageHistory


__all__ = [
"BaseChatMessageHistory",
"FileChatMessageHistory",
Expand Down
23 changes: 19 additions & 4 deletions camel/memory/chat_message_histories/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
from camel.messages import BaseMessage, AssistantChatMessage, UserChatMessage
# =========== 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 camel.messages import AssistantChatMessage, BaseMessage, UserChatMessage


@dataclass
Expand Down Expand Up @@ -42,7 +55,7 @@ def add_user_message(self, message: str) -> None:
Returns:
None
"""
self.add_message(UserChatMessage(role_name=role_name,content=message))
self.add_message(UserChatMessage(role_name=role_name, content=message))

def add_assistant_message(self, message: str) -> None:
r"""Add an AI message to the store.
Expand All @@ -53,7 +66,8 @@ def add_assistant_message(self, message: str) -> None:
Returns:
None
"""
self.add_message(AssistantChatMessage(role_name=role_name,content=message))
self.add_message(
AssistantChatMessage(role_name=role_name, content=message))

def add_message(self, message: BaseMessage) -> None:
r"""Add a self-created message to the store.
Expand All @@ -64,7 +78,8 @@ def add_message(self, message: BaseMessage) -> None:
Raises:
NotImplementedError: This method should be overridden by subclasses.
"""
raise NotImplementedError("The method 'add_message' is not implemented")
raise NotImplementedError(
"The method 'add_message' is not implemented")

def clear(self) -> None:
r"""Remove all messages from the store.
Expand Down
Loading

0 comments on commit cd36c5a

Please sign in to comment.