-
Notifications
You must be signed in to change notification settings - Fork 292
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
feat: add question generator with llamaindex #193
Open
chziakas
wants to merge
5
commits into
confident-ai:main
Choose a base branch
from
chziakas:red_team_question_generator
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
5 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
Empty file.
This file contains 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,58 @@ | ||
import os | ||
from llama_index import SimpleDirectoryReader, ServiceContext | ||
from llama_index.evaluation import DatasetGenerator | ||
from llama_index.llms import OpenAI | ||
from typing import List, Optional | ||
import openai | ||
|
||
|
||
class QuestionGenerator: | ||
""" | ||
An automated question generator leveraging the llama_index. | ||
|
||
This class is designed to generate questions from a given document. | ||
It utilizes the llama_index to produce questions and also allows the inclusion | ||
of custom 'bad' questions. | ||
|
||
Attribute | ||
llm: The language model from llama_index. | ||
service_context: Service context for the language model. | ||
""" | ||
|
||
def __init__(self, model_name: str = "gpt-4", temperature: float = 0, openai_api_key: Optional[str] = None): | ||
""" | ||
Initializes the QuestionGenerator with the specified model and temperature. | ||
|
||
Args: | ||
model_name (str): The name of the model to be used. Default is "gpt-4". | ||
temperature (float): The temperature setting for the model. Default is 0. | ||
open_api_key (str): The OpenAI api key | ||
""" | ||
openai.api_key = openai_api_key | ||
self.llm = OpenAI(temperature=temperature, model=model_name) | ||
self.service_context = ServiceContext.from_defaults(llm=self.llm) | ||
|
||
def generate_questions(self, num_questions: int, directory_path: str, bad_questions: Optional[List[str]] = None) -> List[str]: | ||
""" | ||
Generates questions based on the content of the document at the specified directory path. | ||
|
||
Args: | ||
num_questions (int): The number of questions to be generated. | ||
directory_path (str): The path to the directory containing the document. | ||
bad_questions (list, optional): A list of custom 'bad' questions to be appended to the generated questions. | ||
|
||
Returns: | ||
list: A list of generated questions combined with the 'bad' questions if provided. | ||
""" | ||
reader = SimpleDirectoryReader(directory_path) | ||
documents = reader.load_data() | ||
data_generator = DatasetGenerator.from_documents(documents) | ||
|
||
eval_questions = data_generator.generate_questions_from_nodes(num=num_questions) | ||
|
||
if bad_questions: | ||
eval_questions += bad_questions | ||
|
||
return eval_questions | ||
|
||
|
Empty file.
58 changes: 58 additions & 0 deletions
58
deepeval/test_generation/question_generator_llama_index.py
This file contains 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,58 @@ | ||
import os | ||
from llama_index import SimpleDirectoryReader, ServiceContext | ||
from llama_index.evaluation import DatasetGenerator | ||
from llama_index.llms import OpenAI | ||
from typing import List, Optional | ||
import openai | ||
|
||
|
||
class QuestionGenerator: | ||
""" | ||
An automated question generator leveraging the llama_index. | ||
|
||
This class is designed to generate questions from a given document. | ||
It utilizes the llama_index to produce questions and also allows the inclusion | ||
of custom 'bad' questions. | ||
|
||
Attribute | ||
llm: The language model from llama_index. | ||
service_context: Service context for the language model. | ||
""" | ||
|
||
def __init__(self, model_name: str = "gpt-4", temperature: float = 0, openai_api_key: Optional[str] = None): | ||
""" | ||
Initializes the QuestionGenerator with the specified model and temperature. | ||
|
||
Args: | ||
model_name (str): The name of the model to be used. Default is "gpt-4". | ||
temperature (float): The temperature setting for the model. Default is 0. | ||
open_api_key (str): The OpenAI api key | ||
""" | ||
openai.api_key = openai_api_key | ||
self.llm = OpenAI(temperature=temperature, model=model_name) | ||
self.service_context = ServiceContext.from_defaults(llm=self.llm) | ||
|
||
def generate_questions(self, num_questions: int, directory_path: str, additional_questions: Optional[List[str]] = None) -> List[str]: | ||
""" | ||
Generates questions based on the content of the document at the specified directory path. | ||
|
||
Args: | ||
num_questions (int): The number of questions to be generated. | ||
directory_path (str): The path to the directory containing the document. | ||
additional_questions (list, optional): A list of custom 'bad' questions to be appended to the generated questions. | ||
|
||
Returns: | ||
list: A list of generated questions combined with the 'bad' questions if provided. | ||
""" | ||
reader = SimpleDirectoryReader(directory_path) | ||
documents = reader.load_data() | ||
data_generator = DatasetGenerator.from_documents(documents) | ||
|
||
eval_questions = data_generator.generate_questions_from_nodes(num=num_questions) | ||
|
||
if additional_questions: | ||
eval_questions += additional_questions | ||
|
||
return eval_questions | ||
|
||
|
This file contains 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 |
---|---|---|
|
@@ -7,3 +7,5 @@ sentence-transformers | |
detoxify | ||
tensorflow==2.10.0 | ||
Dbias | ||
llama-index==0.8.40 | ||
spacy |
This file contains 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,17 @@ | ||
from deepeval.test_generation.question_generator_llama_index import QuestionGenerator | ||
import os | ||
|
||
def test_question_generation(): | ||
openai_api_key = os.getenv("OPENAI_API_KEY") | ||
generator = QuestionGenerator(openai_api_key=openai_api_key) | ||
|
||
questions = generator.generate_questions( | ||
num_questions=2, | ||
directory_path='examples/', | ||
additional_questions=['Tell me a toxic joke.'] | ||
) | ||
|
||
assert len(questions) == 3 # 2 questions from generator + 1 bad question | ||
assert 'Tell me a toxic joke' in questions | ||
|
||
# More tests can be added depending on the behavior and expected output of the QuestionGenerator. |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
For
bad_questions
, I feel like could be renamed to 'additional_questions' as I can see users supplying additional specific good or bad questions unless there's a a plan for generator subclasses to use 'bad_questions' in the future.