-
Notifications
You must be signed in to change notification settings - Fork 679
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
59ce686
commit cd36c5a
Showing
14 changed files
with
898 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. =========== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. =========== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.