Future-House / paper-qa

High accuracy RAG for answering questions from scientific documents with citations
Apache License 2.0
6.35k stars 602 forks source link

Support BetterBibTeX citekeys in ZoteroDB #67

Open g-simmons opened 1 year ago

g-simmons commented 1 year ago

It would be nice to be able to use the citekeys provided by Better BibTeX if the user has it installed. For Better BibTeX users, these citekeys are probably the ones that will be used in writing with citeproc.

This can be done by querying the Better BibTeX RPC server.

def _get_citation_key(item: dict, better_bibtex: bool = False) -> str:
    if better_bibtex:
        import requests

        url = "http://localhost:23119/better-bibtex/json-rpc"
        headers = {"Content-Type": "application/json", "Accept": "application/json"}
        payload = {
            "jsonrpc": "2.0",
            "method": "item.citationkey",
            "params": [[item["key"]]],
        }

        response = requests.post(url, headers=headers, json=payload)

        return response.json()["result"][item["key"]]

    if (
        "data" not in item
        or "creators" not in item["data"]
        or len(item["data"]["creators"]) == 0
        or "lastName" not in item["data"]["creators"][0]
        or "title" not in item["data"]
        or "date" not in item["data"]
    ):
        return item["key"]

    last_name = item["data"]["creators"][0]["lastName"]
    short_title = "".join(item["data"]["title"].split(" ")[:3])
    date = item["data"]["date"]

    # Delete non-alphanumeric characters:
    short_title = "".join([c for c in short_title if c.isalnum()])
    last_name = "".join([c for c in last_name if c.isalnum()])
    date = "".join([c for c in date if c.isalnum()])

    return f"{last_name}_{short_title}_{date}_{item['key']}".replace(" ", "")
MilesCranmer commented 1 year ago

Good idea. Maybe better_bibtex could be a parameter to ZoteroDB, with the port number as another parameter. (Or is it always the same port?)

g-simmons commented 1 year ago

@MilesCranmer that's how I did it on my end.

Looks like the port is configurable: https://www.zotero.org/support/preferences/hidden_preferences, so I would be in favor of making it a parameter.

Can submit a PR for this if you like

MilesCranmer commented 1 year ago

+1 from me in favor of a PR! (It’s @whitead’s repo though)

whitead commented 1 year ago

Sure sounds great! Defer to @MilesCranmer on the zotero details