generated from cheshire-cat-ai/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
3 changed files
with
55 additions
and
36 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 |
---|---|---|
@@ -1,31 +1,51 @@ | ||
from cat.mad_hatter.decorators import tool, hook, plugin | ||
from pydantic import BaseModel | ||
from datetime import datetime, date | ||
|
||
class MySettings(BaseModel): | ||
required_int: int | ||
optional_int: int = 69 | ||
required_str: str | ||
optional_str: str = "meow" | ||
required_date: date | ||
optional_date: date = 1679616000 | ||
|
||
@plugin | ||
def settings_schema(): | ||
return MySettings.schema() | ||
|
||
@tool | ||
def get_the_day(tool_input, cat): | ||
"""Get the day of the week. Input is always None.""" | ||
|
||
dt = datetime.now() | ||
|
||
return dt.strftime('%A') | ||
|
||
@hook | ||
def before_cat_sends_message(message, cat): | ||
|
||
prompt = f'Rephrase the following sentence in a grumpy way: {message["content"]}' | ||
message["content"] = cat.llm(prompt) | ||
|
||
return message | ||
from cat.mad_hatter.decorators import hook | ||
|
||
def litm(documents): | ||
""" | ||
Function based on Haystack's LITM ranker: | ||
https://github.com/deepset-ai/haystack/blob/main/haystack/nodes/ranker/lost_in_the_middle.py | ||
Lost In The Middle is based on the paper https://arxiv.org/abs/2307.03172 | ||
Check it for mor details. | ||
Parameters | ||
---------- | ||
documents: List of documents (the declarative working memories) | ||
Returns | ||
---------- | ||
litm_docs: The same list but reordered | ||
""" | ||
if len(documents) == 1: | ||
return documents | ||
|
||
document_index = list(range(len(documents))) | ||
lost_in_the_middle_indices = [0] | ||
|
||
for doc_idx in document_index[1:]: | ||
insertion_index = len(lost_in_the_middle_indices) // 2 + len(lost_in_the_middle_indices) % 2 | ||
lost_in_the_middle_indices.insert(insertion_index, doc_idx) | ||
litm_docs = [documents[idx] for idx in lost_in_the_middle_indices] | ||
return litm_docs | ||
|
||
|
||
@hook(priority=1) | ||
def after_cat_recalls_memories(cat) -> None: | ||
"""Hook after semantic search in memories. | ||
The hook is executed just after the Cat searches for the meaningful context in memories | ||
and stores it in the *Working Memory*. | ||
Parameters | ||
---------- | ||
cat : CheshireCat | ||
Cheshire Cat instance. | ||
""" | ||
if cat.working_memory['declarative_memories']: | ||
litm_docs = litm(cat.working_memory['declarative_memories']) | ||
cat.working_memory['declarative_memories'] = litm_docs | ||
else: | ||
print("#HicSuntGattones") | ||
pass # do nothing |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.