Skip to main content

Diagnose and find hallucinations in your grounded Large Language Model prompts with Anchor-GPT!

Project description

anchor-gpt

Diagnose and find hallucinations in your grounded Large Language Model prompts with Anchor-GPT!

Installation

pip install anchor-gpt

Example Usage

from anchor_gpt import PromptLogger, Prompt

# 1. Define a retriever function. The retriever function receives a PromptStore and a number and returns
#  a list of prompts. In this case, we sort the prompts by their average score and return the top n.
def retriever(store, n):
    def prompt_average_score(prompt):
        return sum(prompt.scores) / len(prompt.scores)

    return sorted(store.select_prompts(), key=prompt_average_score, reverse=True)[:n]

prompt_logger = PromptLogger(retriever)

# 2. Handle your prompts normally, and log them with their grounding scores.
@app.route("/chat", methods=["POST"])
def chat():
    # Do your grounding as normal:
    prompt_embeddings = model.encode(request.json["prompt"])
    vector_store_results = vector_store.query(prompt_embeddings, top_k=10)

    # Then log the prompt with the scores and embeddings. Prompts are stored locally in a SQLite database.
    prompt_logger.log(Prompt(
        text=request.json["prompt"], 
        scores=[r.distance for r in vector_store_results],
        embeddings=prompt_embeddings,
    ))

    # Do your chatbot prompt engineering stuff here, return the response.

# 3. Retrieve the worst grounded prompts, the best candidates to improve your grounding database.
@app.route("/hallucinations", methods=["GET"])
def hallucinations():
    # Retrieve a list of the top 100 most hallucinatory prompts.
    # This will call retriever defined above.
    top_prompts = prompt_logger.retrieve_n(100)

    # Optionally, calculate the coreset of the top 100 prompts.
    # This will return a list of the top 10 most hallucinatory prompts
    # with good diversity and coverage.
    coreset = prompt_logger.get_prompts_coreset(top_prompts, 10)

    return jsonify([p.text for p in coreset])

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

anchor-gpt-0.0.1.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

anchor_gpt-0.0.1-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file anchor-gpt-0.0.1.tar.gz.

File metadata

  • Download URL: anchor-gpt-0.0.1.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.6

File hashes

Hashes for anchor-gpt-0.0.1.tar.gz
Algorithm Hash digest
SHA256 2988660f16a0d7a72b370b2b94f3e6a02daf8f188f66d4f7c375f4849bc848bb
MD5 f2df8ec406ad4bf008c08c89e068b402
BLAKE2b-256 d87f1fb064a17f36b576e29f3a7ddf7e9182cbd700b36f769f9f9a60d70ea9ce

See more details on using hashes here.

File details

Details for the file anchor_gpt-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: anchor_gpt-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.6

File hashes

Hashes for anchor_gpt-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 51c45d76f72fe4ba75a25ed3828ed572604a32ce2303183ff90302e18a10293d
MD5 22f9ff76ac636b152053b0467506a805
BLAKE2b-256 bdaa0fb04e9b3dd6fda77ef055988f0c642b772f4dfed56a24bcb2828c43724a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page