generated from cheshire-cat-ai/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
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
7 changed files
with
113 additions
and
133 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
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,39 @@ | ||
from cat.mad_hatter.decorators import hook | ||
from .rankers import litm, get_settings, recentness | ||
|
||
|
||
@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. | ||
""" | ||
settings = get_settings() | ||
if settings["RECENTNESS"]: | ||
if cat.working_memory['episodic_memories']: | ||
recent_docs = recentness(cat.working_memory['episodic_memories']) | ||
cat.working_memory['episodic_memories'] = recent_docs | ||
else: | ||
print("#HicSuntGattones") | ||
|
||
if settings["LITM"]: | ||
if cat.working_memory['declarative_memories']: | ||
litm_docs = litm(cat.working_memory['declarative_memories']) | ||
cat.working_memory['declarative_memories'] = litm_docs | ||
else: | ||
print("#HicSuntGattones") | ||
|
||
if settings["FILTER"]: | ||
if cat.working_memory['procedural_memories']: | ||
print("NON PUò ENTRAREEEEH!") | ||
else: | ||
print("#HicSuntGattones") | ||
pass # do nothing | ||
|
This file was deleted.
Oops, something went wrong.
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,10 +1,10 @@ | ||
{ | ||
"name": "My plugin", | ||
"name": "Cheshire Cat Reranker", | ||
"version": "0.0.1", | ||
"description": "Description of my_plugin.", | ||
"author_name": "Me", | ||
"description": "Description of cranker.", | ||
"author_name": "nickprock", | ||
"author_url": "https://mywebsite.me", | ||
"plugin_url": "https://github.com/my_name/my_plugin", | ||
"plugin_url": "https://github.com/my_name/cranker", | ||
"tags": "cat, template, example", | ||
"thumb": "https://raw.githubusercontent.com/my_repo_path/my_plugin.png" | ||
"thumb": "https://raw.githubusercontent.com/my_repo_path/cranker.png" | ||
} |
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,67 @@ | ||
import os | ||
import json | ||
def get_settings(): | ||
if os.path.isfile("cat/plugins/ccat_reranker/settings.json"): | ||
with open("cat/plugins/ccat_reranker/settings.json", "r") as json_file: | ||
settings = json.load(json_file) | ||
return settings | ||
|
||
def recent_ranker(documents): | ||
""" | ||
Returns document in memory sorted by date from most recent. | ||
Parameters | ||
---------- | ||
documents: List of documents (the episodic working memories) | ||
Returns | ||
---------- | ||
recent_docs: The same list but reordered | ||
""" | ||
if len(documents) == 1: | ||
return documents | ||
|
||
recent_docs = sorted(documents, key=lambda d: d[0].metadata["when"], reverse=True) | ||
return recent_docs | ||
|
||
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 | ||
|
||
def filter_ranker(documents): | ||
""" | ||
Returns list of tools with a similarity score higher than 0.5. | ||
Parameters | ||
---------- | ||
documents: List of documents (the procedural working memories) | ||
Returns | ||
---------- | ||
tools: The same list but reordered | ||
""" |