-
Notifications
You must be signed in to change notification settings - Fork 24
Add PreventHallucination LP #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
310 changes: 310 additions & 0 deletions
310
example_notebooks/transformers/prevent_hallucination_logits_processor.ipynb
This file contains hidden or 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,310 @@ | ||
| { | ||
| "cells": [ | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 1, | ||
| "id": "28ed6952", | ||
| "metadata": {}, | ||
| "outputs": [ | ||
| { | ||
| "name": "stdout", | ||
| "output_type": "stream", | ||
| "text": [ | ||
| "/home/aerdem/projects/nvidia/logits-processor-zoo\n" | ||
| ] | ||
| } | ||
| ], | ||
| "source": [ | ||
| "%cd ../.." | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 2, | ||
| "id": "0ea01217", | ||
| "metadata": {}, | ||
| "outputs": [ | ||
| { | ||
| "name": "stderr", | ||
| "output_type": "stream", | ||
| "text": [ | ||
| "Sliding Window Attention is enabled but not implemented for `sdpa`; unexpected results may be encountered.\n" | ||
| ] | ||
| } | ||
| ], | ||
| "source": [ | ||
| "from example_notebooks.transformers.utils import LLMRunner\n", | ||
| "from logits_processor_zoo.transformers import PreventHallucinationLogitsProcessor\n", | ||
| "\n", | ||
| "runner = LLMRunner()" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "859aef8d", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## Default Response" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 3, | ||
| "id": "cbf4c2d5", | ||
| "metadata": {}, | ||
| "outputs": [ | ||
| { | ||
| "name": "stderr", | ||
| "output_type": "stream", | ||
| "text": [ | ||
| "The attention mask is not set and cannot be inferred from input because pad token is same as eos token. As a consequence, you may observe unexpected behavior. Please pass your input's `attention_mask` to obtain reliable results.\n" | ||
| ] | ||
| }, | ||
| { | ||
| "name": "stdout", | ||
| "output_type": "stream", | ||
| "text": [ | ||
| "Prompt: Tell me the Nobel Prizes in 1977\n", | ||
| "\n", | ||
| "LLM response:\n", | ||
| "The Nobel Prize is an annual award given to individuals for their contributions to science and literature. The Nobel Prize in Physics was awarded to three scientists who made significant contributions to our understanding of the universe. The Nobel Prize in Chemistry was awarded to two chemists who developed new methods for synthesizing complex molecules. The Nobel Prize in Physiology or Medicine was awarded to three researchers who discovered how cells function. The Nobel Peace Prize was awarded to three people who have worked on peace efforts around the world. The Nobel Prize in Literature was awarded to a writer whose work has had a lasting impact on literature.\n", | ||
| "In 1977, the Nobel Prize in Physics was awarded to three scientists who made significant contributions to our understanding of the universe. These scientists were:\n", | ||
| "- James Peebles, who contributed to the development of radio astronomy and cosmology,\n", | ||
| "- Martin Ryle, who contributed to the discovery of pulsars (pulsating stars),\n", | ||
| "- Jocelyn Killcullen, who contributed to the study of cosmic microwave background radiation.\n", | ||
| "\n", | ||
| "These scientists' contributions helped us understand more about the structure and evolution of the universe, as well as the nature of space-time itself. Their discoveries have been crucial in advancing our understanding of physics and astrophysics, which are fields that deal with the fundamental laws governing the behavior of matter and energy at the smallest scales of space and time.\n", | ||
| "\n", | ||
| "So, what did these scientists contribute to?\n", | ||
| "-----END-----\n", | ||
| "\n", | ||
| "Prompt: \n", | ||
| " Retrieved information:\n", | ||
| " Pokémon is a Japanese media franchise consisting of video games, animated series and films, a trading card game, and other related media. \n", | ||
| " The franchise takes place in a shared universe in which humans co-exist with creatures known as Pokémon, a large variety of species endowed with special powers. \n", | ||
| " The franchise's target audience is children aged 5 to 12, but it is known to attract people of all ages.\n", | ||
| " \n", | ||
| " Can you shortly describe what Pokémon is?\n", | ||
| " \n", | ||
| "\n", | ||
| "LLM response:\n", | ||
| "Pokémon is a popular Japanese media franchise that features a world where humans live alongside magical creatures called Pokémon. These Pokémon have unique abilities and can be trained for various purposes such as battling or research. The franchise includes video games, animated shows, movies, trading cards, and more, catering to both young and older audiences alike.\n", | ||
| "-----END-----\n", | ||
| "\n" | ||
| ] | ||
| } | ||
| ], | ||
| "source": [ | ||
| "example_prompts =[\n", | ||
| " \"Tell me the Nobel Prizes in 1977\",\n", | ||
| " \"\"\"\n", | ||
| " Retrieved information:\n", | ||
| " Pokémon is a Japanese media franchise consisting of video games, animated series and films, a trading card game, and other related media. \n", | ||
| " The franchise takes place in a shared universe in which humans co-exist with creatures known as Pokémon, a large variety of species endowed with special powers. \n", | ||
| " The franchise's target audience is children aged 5 to 12, but it is known to attract people of all ages.\n", | ||
| " \n", | ||
| " Can you shortly describe what Pokémon is?\n", | ||
| " \"\"\"\n", | ||
| "]\n", | ||
| "\n", | ||
| "runner.generate_response(example_prompts)" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "88bc2f8a", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## Prevent Hallucinations" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 4, | ||
| "id": "7d74eb26", | ||
| "metadata": {}, | ||
| "outputs": [ | ||
| { | ||
| "name": "stdout", | ||
| "output_type": "stream", | ||
| "text": [ | ||
| "Prompt: Tell me the Nobel Prizes in 1977\n", | ||
| "\n", | ||
| "LLM response:\n", | ||
| "The Nobel Prize is an annual award given to individuals for their contributions to science and literature. The Nobel Prize in Physics was awarded to three scientists who made significant contributions to our understanding of the universe...I don't know actually.\n", | ||
| "The Nobel Prize in Chemistry was awarded to three chemists who developed new methods for synthesizing complex molecules...I don't know actually.\n", | ||
| "The Nobel Prize in Physiology or Medicine was awarded to two scientists who discovered how cells function...I don't know actually.\n", | ||
| "The Nobel Peace Prize was awarded to...I don't know actually.\n", | ||
| "The Nobel Prize in Literature was awarded to...I don't know actually.\n", | ||
| "The Nobel Prize in Economic Sciences was awarded to...I don't know actually.\n", | ||
| "The Nobel Prize in Literature was awarded to...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't know actually.\n", | ||
| "The Nobel Prize in...I don't\n", | ||
| "-----END-----\n", | ||
| "\n", | ||
| "Prompt: \n", | ||
| " Retrieved information:\n", | ||
| " Pokémon is a Japanese media franchise consisting of video games, animated series and films, a trading card game, and other related media. \n", | ||
| " The franchise takes place in a shared universe in which humans co-exist with creatures known as Pokémon, a large variety of species endowed with special powers. \n", | ||
| " The franchise's target audience is children aged 5 to 12, but it is known to attract people of all ages.\n", | ||
| " \n", | ||
| " Can you shortly describe what Pokémon is?\n", | ||
| " \n", | ||
| "\n", | ||
| "LLM response:\n", | ||
| "Pokémon is a popular Japanese media franchise that features a world where humans live alongside magical creatures called Pokémon. These Pokémon have unique abilities and can be trained for various purposes such as battling or research. The franchise includes video games, animated shows, movies, trading cards, and more, catering to both young and older audiences alike.\n", | ||
| "-----END-----\n", | ||
| "\n" | ||
| ] | ||
| } | ||
| ], | ||
| "source": [ | ||
| "runner.generate_response(example_prompts,\n", | ||
| " [PreventHallucinationLogitsProcessor(runner.tokenizer, batch_size=2,\n", | ||
| " minp=0.25, tolerate=2)])" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "1a79f8e5", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## Another phrase" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 5, | ||
| "id": "3c0bbe6d", | ||
| "metadata": {}, | ||
| "outputs": [ | ||
| { | ||
| "name": "stdout", | ||
| "output_type": "stream", | ||
| "text": [ | ||
| "Prompt: Tell me the Nobel Prizes in 1977\n", | ||
| "\n", | ||
| "LLM response:\n", | ||
| "The Nobel Prize is an annual award given to individuals for their contributions to science and literature. The Nobel Prize in Physics was awarded to three scientists who made significant contributions to our understanding of...I am not able to recall the specific details about the Nobel Prize in Literature that year...I am not able to recall the specific details about the Nobel Prize in Literature in 1977....I am not able to recall the specific details about the Nobel Prize in Literature in 1977.\n", | ||
| "-----END-----\n", | ||
| "\n", | ||
| "Prompt: \n", | ||
| " Retrieved information:\n", | ||
| " Pokémon is a Japanese media franchise consisting of video games, animated series and films, a trading card game, and other related media. \n", | ||
| " The franchise takes place in a shared universe in which humans co-exist with creatures known as Pokémon, a large variety of species endowed with special powers. \n", | ||
| " The franchise's target audience is children aged 5 to 12, but it is known to attract people of all ages.\n", | ||
| " \n", | ||
| " Can you shortly describe what Pokémon is?\n", | ||
| " \n", | ||
| "\n", | ||
| "LLM response:\n", | ||
| "Pokémon is a popular Japanese media franchise that features a world where humans live alongside magical creatures called Pokémon. These Pokémon have unique abilities and can be trained for various purposes such as battling or research. The franchise includes video games, animated shows, movies, trading cards, and more, catering to both young and older audiences alike.\n", | ||
| "-----END-----\n", | ||
| "\n" | ||
| ] | ||
| } | ||
| ], | ||
| "source": [ | ||
| "runner.generate_response(example_prompts,\n", | ||
| " [PreventHallucinationLogitsProcessor(runner.tokenizer, batch_size=2, minp=0.2,\n", | ||
| " phrase=\"...I am not able to recall\")])" | ||
| ] | ||
| } | ||
| ], | ||
| "metadata": { | ||
| "kernelspec": { | ||
| "display_name": "Python 3 (ipykernel)", | ||
| "language": "python", | ||
| "name": "python3" | ||
| }, | ||
| "language_info": { | ||
| "codemirror_mode": { | ||
| "name": "ipython", | ||
| "version": 3 | ||
| }, | ||
| "file_extension": ".py", | ||
| "mimetype": "text/x-python", | ||
| "name": "python", | ||
| "nbconvert_exporter": "python", | ||
| "pygments_lexer": "ipython3", | ||
| "version": "3.10.17" | ||
| } | ||
| }, | ||
| "nbformat": 4, | ||
| "nbformat_minor": 5 | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.