Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
nickprock committed Nov 19, 2023
1 parent 8b9745b commit f3c70e5
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 133 deletions.
18 changes: 1 addition & 17 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,10 @@ permissions:
env:
PLUGIN_JSON: "0.0.1"
TAG_EXISTS: false
PLUGIN_NAME: "my_plugin"
PLUGIN_NAME: "cranker"

jobs:
# This will be deleted by setup.py
check:
runs-on: ubuntu-latest
outputs:
plugin_name: ${{ steps.init.outputs.plugin_name }}
steps:
- name: Get plugin name
id: init
run: |
echo "plugin_name=${{ env.PLUGIN_NAME }}" >> $GITHUB_OUTPUT
# This is the end of the removed section
release:
# This will be deleted by setup.py
needs: check
if: startsWith(needs.check.outputs.plugin_name, 'MY_PLUGIN') == false
# This is the end of the removed section
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# My plugin
# 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://)
Expand Down
39 changes: 39 additions & 0 deletions cranker.py
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

31 changes: 0 additions & 31 deletions my_plugin.py

This file was deleted.

10 changes: 5 additions & 5 deletions plugin.json
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"
}
67 changes: 67 additions & 0 deletions rankers.py
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
"""
79 changes: 0 additions & 79 deletions setup.py

This file was deleted.

0 comments on commit f3c70e5

Please sign in to comment.