Skip to content

Commit

Permalink
reranker first version
Browse files Browse the repository at this point in the history
  • Loading branch information
nickprock committed Nov 20, 2023
1 parent f3c70e5 commit f355194
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 30 deletions.
37 changes: 19 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
# Cranker

[![awesome plugin](https://custom-icon-badges.demolab.com/static/v1?label=&message=awesome+plugin&color=383938&style=for-the-badge&logo=cheshire_cat_ai)](https://)
[![Awesome plugin](https://custom-icon-badges.demolab.com/static/v1?label=&message=Awesome+plugin&color=000000&style=for-the-badge&logo=cheshire_cat_ai)](https://)
# Cheshire Cat ReRanker

[![awesome plugin](https://custom-icon-badges.demolab.com/static/v1?label=&message=awesome+plugin&color=F4F4F5&style=for-the-badge&logo=cheshire_cat_black)](https://)

Write here all the useful information about your plugin.
Cheshire Cat ReRanker apply a rearrangement at each memory with different criteria:
* the [*episodic memory*](https://cheshire-cat-ai.github.io/docs/conceptual/memory/episodic_memory/) is reordered according to a temporal criteria, from the newest to the oldest.
* the [*declarative memory*](https://cheshire-cat-ai.github.io/docs/conceptual/memory/declarative_memory/) is reordered according the [**lost in the middle** paper](https://arxiv.org/abs/2307.03172) method
* the [*procedural memory*](https://cheshire-cat-ai.github.io/docs/conceptual/memory/procedural_memory/) isn't reordered but filtered using a threshold, the default value is 0.5 but you can set it.

This repository is the template to automate the release of official Cheshire Cat AI plugins.
The ReRanker can be enabled and disabled for each memory independently using the "Settings".

## Usage
## Installation

1. Create a new repository clicking on the `Use this template` button.
2. Clone your new repo directly in the Cat's `plugins` folder.
3. Run the `setup.py` script:
```bash
python setup.py
```
The script will prompt you to write the name of your plugin and make an initial setup setting the name in the files.
1. Navigate to the `Plugins` page;
2. Scroll until you find the "Cheshire Cat ReRanker" plugin;
3. Click on `Install

4. Start developing!
## Usage

> **Important**
> A new release of your plugin is triggered every time you set a new `version` in the `plugin.json` file.
> Please, remember to set it correctly every time to want to release an update.
1. Navigate to the `Plugins` page;
2. Scroll until you find the "Cheshire Cat ReRanker" plugin;
3. Click on the "Settings" icon on the bottom right of the plugin card;
4. Edit the available settings and save.

> **Important**
> ReRankers (in particular Lost In The Middle) are very useful if you get at least more than 10 documents returned from the memories.
> Before using it download and enable the [C.A.T. plugin](https://github.com/Furrmidable-Crew/cat_advanced_tools) from the Plugins store, [follow the instructions](https://github.com/Furrmidable-Crew/cat_advanced_tools#usage) to increase the k parameter of the memories.
6 changes: 3 additions & 3 deletions cranker.py → ccat_reranker.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from cat.mad_hatter.decorators import hook
from .rankers import litm, get_settings, recentness
from .rankers import get_settings, recent_ranker, litm, filter_ranker


@hook(priority=1)
Expand All @@ -18,7 +18,7 @@ def after_cat_recalls_memories(cat) -> None:
settings = get_settings()
if settings["RECENTNESS"]:
if cat.working_memory['episodic_memories']:
recent_docs = recentness(cat.working_memory['episodic_memories'])
recent_docs = recent_ranker(cat.working_memory['episodic_memories'])
cat.working_memory['episodic_memories'] = recent_docs
else:
print("#HicSuntGattones")
Expand All @@ -32,7 +32,7 @@ def after_cat_recalls_memories(cat) -> None:

if settings["FILTER"]:
if cat.working_memory['procedural_memories']:
print("NON PUò ENTRAREEEEH!")
filtered = filter_ranker(cat.working_memory['procedural_memories'], settings["tool_threshold"])
else:
print("#HicSuntGattones")
pass # do nothing
Expand Down
Binary file added img.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 5 additions & 6 deletions plugin.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
{
"name": "Cheshire Cat Reranker",
"version": "0.0.1",
"description": "Description of cranker.",
"version": "0.1.0",
"description": "This plugin apply a reranking to each memory. It rearranges: the episodic memory from the most recent to the oldest, the declarative using lost in the middle and the procedural using a filter.",
"author_name": "nickprock",
"author_url": "https://mywebsite.me",
"plugin_url": "https://github.com/my_name/cranker",
"tags": "cat, template, example",
"thumb": "https://raw.githubusercontent.com/my_repo_path/cranker.png"
"plugin_url": "https://github.com/nickprock/ccat_reranker",
"tags": "cat, memory, advanced, reranker",
"thumb": "https://raw.githubusercontent.com/nickprock/ccat_reranker/main/img.jpg"
}
8 changes: 5 additions & 3 deletions rankers.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def litm(documents):
litm_docs = [documents[idx] for idx in lost_in_the_middle_indices]
return litm_docs

def filter_ranker(documents):
def filter_ranker(documents, tool_threshold):
"""
Returns list of tools with a similarity score higher than 0.5.
Expand All @@ -63,5 +63,7 @@ def filter_ranker(documents):
Returns
----------
tools: The same list but reordered
"""
filtered: The same list but filtered
"""
filtered = [d for d in documents if d[1]>tool_threshold]
return filtered
13 changes: 13 additions & 0 deletions settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from cat.mad_hatter.decorators import plugin
from pydantic import BaseModel, Field


class MySettings(BaseModel):
LITM: bool = True,
RECENTNESS: bool = True,
FILTER: bool = True,
tool_threshold: float = 0.5

@plugin
def settings_schema():
return MySettings.schema()

0 comments on commit f355194

Please sign in to comment.