import gradio as gr from dotenv import load_dotenv from langchain import PromptTemplate, LLMChain, HuggingFaceHub from langchain.llms import CTransformers from transformers import AutoModelForCausalLM, AutoTokenizer from transformers import pipeline from langchain.llms.huggingface_pipeline import HuggingFacePipeline from sentence_transformers import SentenceTransformer, util from sklearn.cluster import KMeans import nltk nltk.download('punkt') from nltk.tokenize import word_tokenize from nltk import tokenize import numpy as np import scipy.spatial import csv load_dotenv() def generate_prompts(user_input): prompt_template = PromptTemplate( input_variables=["Question"], template=f"Just list 10 question prompts for {user_input} and don't put number before each of the prompts." ) config = {'max_new_tokens': 64, 'temperature': 0.7, 'context_length': 64} llm = CTransformers(model="TheBloke/Mistral-7B-Instruct-v0.1-GGUF", config=config) hub_chain = LLMChain(prompt = prompt_template, llm = llm) input_data = {"Question": user_input} generated_prompts = hub_chain.run(input_data) questions_list = generated_prompts.split('\n') formatted_questions = "\n".join(f"Question: {question}" for i, question in enumerate(questions_list) if question.strip()) questions_list = formatted_questions.split("Question:")[1:] return questions_list def answer_question(prompt): prompt_template = PromptTemplate( input_variables=["Question"], template=f"give one answer for {prompt} and do not consider the number behind it." ) config = {'max_new_tokens': 64, 'temperature': 0.7, 'context_length': 64} llm = CTransformers(model="TheBloke/Llama-2-7B-Chat-GGML", config=config) hub_chain = LLMChain(prompt = prompt_template, llm = llm) input_data = {"Question": prompt} generated_answer = hub_chain.run(input_data) return generated_answer def calculate_similarity(word, other_words, model, threshold=0.5): embeddings_word = model.encode([word]) embeddings_other_words = model.encode(other_words) for i, embedding in enumerate(embeddings_other_words): similarity = 1 - scipy.spatial.distance.cosine(embeddings_word[0], embedding) if similarity > threshold and similarity < 0.85: return i, similarity return None, None def highlight_similar_paragraphs_with_colors(paragraphs, similarity_threshold=0.25): # Load a pre-trained sentence-transformer model model = SentenceTransformer('all-MiniLM-L6-v2') # Split each paragraph into sentences all_sentences = [tokenize.sent_tokenize(paragraph) for paragraph in paragraphs] flattened_sentences = [sentence for sublist in all_sentences for sentence in sublist] # Flatten the list # Encode all sentences into vectors sentence_embeddings = model.encode(flattened_sentences) # Calculate cosine similarities between sentence vectors cosine_similarities = util.pytorch_cos_sim(sentence_embeddings, sentence_embeddings) # A list of colors for highlighting, add more if needed colors = ['yellow', 'lightgreen', 'lightblue', 'pink', 'lavender', 'salmon', 'peachpuff', 'powderblue', 'khaki', 'wheat'] # Initialize a list to keep track of which sentences are semantically similar highlighted_sentences = [''] * len(flattened_sentences) # Pre-fill with empty strings # Iterate over the matrix to find sentences with high cosine similarity color_index = 0 # Initialize color index for i in range(len(cosine_similarities)): for j in range(i + 1, len(cosine_similarities)): if cosine_similarities[i, j] > similarity_threshold and not highlighted_sentences[i]: # Select color for highlighting color = colors[color_index % len(colors)] color_index += 1 # Move to the next color # Highlight the similar sentences highlighted_sentences[i] = (""+ flattened_sentences[i]+"") highlighted_sentences[j] = (""+ flattened_sentences[j]+"") # Reconstruct the paragraphs with highlighted sentences highlighted_paragraphs = [] sentence_index = 0 for paragraph_sentences in all_sentences: highlighted_paragraph = '' for _ in paragraph_sentences: # Use the original sentence if it wasn't highlighted; otherwise, use the highlighted version. highlighted_sentence = highlighted_sentences[sentence_index] if highlighted_sentences[sentence_index] else flattened_sentences[sentence_index] highlighted_paragraph += highlighted_sentence + ' ' sentence_index += 1 highlighted_paragraphs.append(highlighted_paragraph) # Combine all paragraphs into one HTML string html_output = '
"+ cbg[idx] +"
"+ sentence +"
First, select the LLM you wish to audit. Then, enter your question. The AuditLLM tool will generate five relevant and diverse prompts based on your question. You can now select these prompts for auditing the LLMs. Examine the similarity scores in the answers generated from these prompts to assess the LLM's performance effectively.
In batch auditing mode, you have the capability to probe the LLM. To begin, you must first select the LLM you wish to audit and then input the questions you intend to explore. For each question submitted, the model will generate five prompts, each accompanied by its respective answers.
To tailor the generation of these five prompts from your original question, you can adjust the relevance and diversity scores. The relevance score determines how closely the generated prompts should align with the original question, while the diversity score dictates the variance among the prompts themselves.
Upon completion, please provide your email address. We will compile and send the answers to you promptly.