|
| 1 | +# LangDiversity Hello World 📝 |
| 2 | + |
| 3 | +Welcome! In this document, we outline how LangDiversity functions along with a simple program that demonstrates its capabilities. |
| 4 | + |
| 5 | +The image below is a visual representation of how LangDiversity works. |
| 6 | + |
| 7 | +<img src="../media/LangDiversityExample.png"/> |
| 8 | + |
| 9 | +1. In the example above, we have a user prepare in 3 prompts. For each prompt, they are trying to obtain 4 separate responses from the language model. |
| 10 | +2. Using those 4 responses, LangDiversity performs a calculation based on the measure chosen (Entropy, Gini Impurity, etc.). This gives us a numerical value representing the diversity of the language model's responses to each prompt. |
| 11 | +3. Each prompt is now associated with its corresponding diversity measure. |
| 12 | +4. Then based on the user's selection method, the prompt with the desired diversity measure and its associated value are returned. |
| 13 | + |
| 14 | +## Code Implementation |
| 15 | + |
| 16 | +### Installation |
| 17 | + |
| 18 | +Before you start using LangDiversity, you'll need to install it. You can do so with pip: |
| 19 | + |
| 20 | +```bash |
| 21 | +pip install langdiversity |
| 22 | +``` |
| 23 | + |
| 24 | +### Importing Diversity Measure |
| 25 | + |
| 26 | +Import the diversity measure you want to use. In this example, we're using [Shannon's entropy](https://github.com/lab-v2/langdiversity/blob/main/langdiversity/measures/shannon_entropy.py). |
| 27 | + |
| 28 | +```python |
| 29 | +from langdiversity.measures import ShannonEntropyMeasure |
| 30 | +diversity_measure = ShannonEntropyMeasure() |
| 31 | +``` |
| 32 | + |
| 33 | +### Configuring the Language Model |
| 34 | + |
| 35 | +In this step, we configure our language model. For this example, we're utilizing [OpenAI's GPT model](https://github.com/lab-v2/langdiversity/blob/main/langdiversity/models/openai.py). Additionally, we import a parser to sanitize and format the responses generated by the language model. |
| 36 | + |
| 37 | +Note: It's recommended to use a custom parser tailored to the specific type of questions you'll be working with. LangDiversity offers a variety of [built-in parsers](https://github.com/lab-v2/diversity_package/tree/main/langdiversity/parser/answer_extractor.py) that you can use as a reference or starting point. |
| 38 | + |
| 39 | +Since our example will involve math-related questions, we opt for the `extract_math_answer` parser to handle the responses. |
| 40 | + |
| 41 | +```python |
| 42 | +from langdiversity.models import OpenAIModel |
| 43 | +from langdiversity.parser import extract_math_answer |
| 44 | +model = OpenAIModel(openai_api_key="[API KEY]", extractor=extract_math_answer) |
| 45 | +``` |
| 46 | + |
| 47 | +### Prompt Selection |
| 48 | + |
| 49 | +Now, we initialize the `PromptSelection` object. This is where we specify how many responses we want from the language model for each prompt, the diversity measure to use, and the selection method. |
| 50 | + |
| 51 | +```python |
| 52 | +from langdiversity.utils import PromptSelection |
| 53 | +prompt_selection = PromptSelection(model=model, num_responses=4, diversity_measure=diversity_measure, selection='min') |
| 54 | +``` |
| 55 | + |
| 56 | +### Generate and Select Prompts |
| 57 | + |
| 58 | +Finally, we pass in a list of prompts to the `PromptSelection` object. It will send these prompts to the language model, calculate the diversity measure for each set of responses, and then select the prompt with the minimum (or maximum) diversity measure. |
| 59 | + |
| 60 | +The selected prompt and its corresponding diversity measure are stored in `selected_prompt` and `selected_measure`, respectively. |
| 61 | + |
| 62 | +Note: The prompts are structured to guide the language model in generating a specific type of response. This makes it easier for the parser to extract clean answers. |
| 63 | + |
| 64 | +```python |
| 65 | +selected_prompt, selected_measure = prompt_selection.generate([ |
| 66 | + "At the end, say 'the answer is [put your numbers here separated by commas]'.\nQuestion: What is the speed of the current if Junior's boat can cover 12 miles downstream in the same time it takes to travel 9 miles upstream, given that his boat's speed in still water is 15 miles per hour?", |
| 67 | + "At the end, say 'the answer is [put your numbers here separated by commas]'.\nQuestion: What is the speed of the current if Junior's boat travels at a constant speed of 15 miles per hour in still water and he spends the same amount of time traveling 12 miles downstream as he does traveling 9 miles upstream?.", |
| 68 | + "At the end, say 'the answer is [put your numbers here separated by commas]'.\nQuestion: Juniors boat will go 15 miles per hour in still water . If he can go 12 miles downstream in the same amount of time as it takes to go 9 miles upstream , then what is the speed of the current?", |
| 69 | +]) |
| 70 | + |
| 71 | +print("Selected Prompt:", selected_prompt) |
| 72 | +print("Selected Measure:", selected_measure) |
| 73 | +``` |
0 commit comments