Skip to content

Commit e349940

Browse files
committed
doc: updated /docs and added hello-world.md
1 parent daa30c9 commit e349940

File tree

7 files changed

+104
-6
lines changed

7 files changed

+104
-6
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Paper: https://arxiv.org/abs/2308.11189
1212

1313
Video: https://www.youtube.com/watch?v=BekDOLm6qBI&t=10s&ab_channel=NeuroSymbolic
1414

15+
Check out [LangDiversity Hello World](https://github.com/lab-v2/langdiversity/blob/main/docs/hello-world.md) if you're new.
16+
1517
## Table of Contents
1618

1719
- [Introduction](#introduction)

docs/hello-world.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
```

docs/langdiversity_library.md

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pypi project: https://pypi.org/project/langdiversity/
55
## Install
66

77
```bash
8-
pip install langchain
8+
pip install langdiversity
99
```
1010

1111
## Usage
@@ -16,21 +16,45 @@ Example:
1616
from langdiversity.models import OpenAIModel
1717
from langdiversity.measures import ShannonEntropyMeasure
1818
from langdiversity.utils import PromptSelection
19+
from langdiversity.parser import # Select a parser that suits your question set
1920

2021
# Initialize the OpenAI model and diversity measure
21-
model = OpenAIModel(openai_api_key="YOUR_OPENAI_API_KEY")
22+
model = OpenAIModel(openai_api_key="[YOUR API KEY]", extractor="[SELECT YOUR PARSER](optional)")
2223
diversity_measure = ShannonEntropyMeasure()
2324

2425
# Use the PromptSelection utility
2526
prompt_selection = PromptSelection(model=model, num_responses=10, diversity_measure=diversity_measure)
2627

27-
# Selects the prompt with the configured diversity measure criteria from the LLM's 10 responses
28+
# Pass in question set to the LLM & selects the prompt with the configured diversity measure criteria from the LLM's 10 responses
2829
selected_prompt, selected_measure = prompt_selection.generate(["Your list of prompts here..."])
2930

3031
print("Selected Prompt:", selected_prompt)
3132
print("Selected Measure:", selected_measure)
3233
```
3334

35+
### Modules:
36+
37+
LangDiversity offers a variety of modules for different use-cases. Below are the essential modules you can either directly import or use as a foundation for creating your own custom solutions:
38+
39+
- [Language Models](https://github.com/lab-v2/langdiversity/tree/main/langdiversity/models) (`langdiversity.models`)
40+
41+
- `OpenAIModel`: Interfaces with OpenAI's GPT models.
42+
43+
- [Diversity Measures](https://github.com/lab-v2/langdiversity/tree/main/langdiversity/measures) (`langdiversity.measures`)
44+
45+
- `ShannonEntropyMeasure`: Implements Shannon's entropy as a diversity measure.
46+
- `GiniImpurityMeasure`: Implements Gini Impurity as a diversity measure.
47+
48+
- [Utility Classes](https://github.com/lab-v2/langdiversity/tree/main/langdiversity/utils) (`langdiversity.utils`)
49+
50+
- `PromptSelection`: Handles the selection of prompts based on diversity measures.
51+
- `DiversityCalculator`: Calculates various diversity measures for a given set of values. Supports Shannon's entropy and Gini impurity by default.
52+
53+
- [Parsers](https://github.com/lab-v2/langdiversity/tree/main/langdiversity/parser) (`langdiversity.parsers`)
54+
- `extract_last_letters(response: str)`: Extracts the last letters of each word in the response.
55+
- `extract_math_answer(response: str)`: Extracts numerical answers from a mathematical question in the response.
56+
- `extract_multi_choice_answer(response: str)`: Extracts the selected choice (A, B, C, D, E) from a multiple-choice question in the response.
57+
3458
### PromptSelection Paramaters:
3559

3660
- `model`: The language model you want to use. In this example, we're using OpenAI's model.

examples/prompt_selection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
diversity_measure = ShannonEntropyMeasure()
1313
model = OpenAIModel(openai_api_key=openai_api_key, extractor=extract_last_letters)
1414
prompt_selection = PromptSelection(
15-
model=model, num_responses=10, diversity_measure=diversity_measure, selection="min"
15+
model=model, num_responses=4, diversity_measure=diversity_measure, selection="min"
1616
)
1717
selected_prompt, selected_diversity = prompt_selection.generate(
1818
[

langdiversity/utils/calculate_measures.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ def calculate(self, values, measures=None):
1212

1313
results = {}
1414

15-
# TODO: maybe include more? Or allow users to insert more?
1615
if "entropy" in measures:
1716
entropy_measure = ShannonEntropyMeasure()
1817
results["entropy"] = entropy_measure.generate(values)

media/LangDiversityExample.png

105 KB
Loading

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
setup(
99
name='langdiversity',
1010
packages=find_packages(exclude=['tests']),
11-
version='0.0.3',
11+
version='1.0.0',
1212
description='A tool to elevate your language models with insightful diversity metrics.',
1313
long_description=long_description,
1414
long_description_content_type="text/markdown",

0 commit comments

Comments
 (0)