{'text': 'How far is it from Denver to Aspen ?', 'coarse_label':5, 'fine_label':40}
对数据进行预处理,代码如下:
1 2 3 4 5 6 7 8 9 10 11
# name of the text and label column label_type = 'coarse_label' text_key = "text" # create mapping of ids2class and class2id id2class = dict((i, label) for i, label inenumerate(dataset['train'].features[label_type].names)) class2id = dict((label, i) for i, label inenumerate(dataset['train'].features[label_type].names)) # create a dictionary with classes as key and containing all the training examples within that class class2TrainDataset = dict((label, []) for label in dataset['train'].features[label_type].names) for example in dataset['train']: label = id2class[example[label_type]] class2TrainDataset[label].append(example[text_key])
# a prompt for asking LLM to perform a task task_prompt = "As a Question Answering agent, your goal is to categorize questions into different semantic classes that impose constraints on potential answers, so that they can be utilized in later stages of the question answering process.\nFollowing are the semantic classes: [" task_prompt += ", ".join([label for label in class2TrainDataset]) + "]" # a prompt for asking LLM to generate the output for current task query_prompt = "\nClassify the following question into one of the above classes. Please answer in a single word.\nquestion: " answer_prompt = "\noutput: "
As a Question Answering agent, your goal is to categorize questions into different semantic classes that impose constraints on potential answers, so that they can be utilized in later stages of the question answering process. Following are the semantic classes: [ABBR, ENTY, DESC, HUM, LOC, NUM] Classify the following question into one of the above classes. Please answer in a single word. question: How far is it from Denver to Aspen ? output:
import tiktoken enc = tiktoken.encoding_for_model(model_name) log_bias_dict = {} for label in dataset['train'].features["coarse_label"].names: for token_id in enc.encode(label): log_bias_dict[token_id] = 5
# Text completion using GPT deftrim_text(text): return text.strip().strip('\n').strip('\\n')
defgenerate_using_gpt(prompt): generated_sentence = "" try: # Create a completion for the provided prompt and parameters response = openai.Completion.create( model=model_name, prompt=prompt, max_tokens=3, temperature=0, top_p=1, stop=None, frequency_penalty=0, presence_penalty=0.0, logit_bias=log_bias_dict )
except openai.error.APIError as e: # Handle API error here, e.g. retry or log print(f"OpenAI API returned an API Error: {e}")
except openai.error.AuthenticationError as e: # Handle Authentication error here, e.g. invalid API key print(f"OpenAI API returned an Authentication Error: {e}")
except openai.error.APIConnectionError as e: # Handle connection error here print(f"Failed to connect to OpenAI API: {e}")
except openai.error.InvalidRequestError as e: # Handle connection error here print(f"Invalid Request Error: {e}")
except openai.error.RateLimitError as e: # Handle rate limit error print(f"OpenAI API request exceeded rate limit: {e}")
except openai.error.ServiceUnavailableError as e: # Handle Service Unavailable error print(f"Service Unavailable: {e}")
# prompt without any examples from the training dataset labels = [] predictions = [] for example in dataset['test']: zeroshot_prompt = task_prompt + query_prompt + example[text_key] + answer_prompt pred = generate_using_gpt(zeroshot_prompt) pred=trim_text(pred) labels.append(example[label_type]) if pred notin class2id: predictions.append(-1) else: predictions.append(class2id[pred])
# function to selection few examples in each of the classes from the training dataset defgenerateFewshotPrompt(class2TrainDataset, N=3): fewshot_prompt = "\nFollowing are some examples." for label in class2TrainDataset: for example in class2TrainDataset[label][:N]: fewshot_prompt += "\nquestion: " + example fewshot_prompt += "\noutput: " + label return fewshot_prompt
# prompt with one example in each of the classes fewshot_examples = generateFewshotPrompt(class2TrainDataset, N=1) fewshot_prompt = task_prompt + fewshot_examples + query_prompt + dataset['test'][0][text_key] + answer_prompt >>> fewshot_prompt
test数据集的第一条数据的Few-Shot prompt如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
As a Question Answering agent, your goal is to categorize questions into different semantic classes that impose constraints on potential answers, so that they can be utilized in later stages of the question answering process. Following are the semantic classes: [ABBR, ENTY, DESC, HUM, LOC, NUM] Following are some examples. question: What is the full form of .com ? output: ABBR question: What films featured the character Popeye Doyle ? output: ENTY question: How did serfdom develop in and then leave Russia ? output: DESC question: What contemptible scoundrel stole the cork from my lunch ? output: HUM question: What sprawling U.S. state boasts the most airports ? output: LOC question: When was Ozzy Osbourne born ? output: NUM Classify the following question into one of the above classes. Please answer in a single word. question: How far is it from Denver to Aspen ? output:
基于Few-Shot prompt,对全量test数据集进行评估,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# prompt is created by adding one example in each of the classes labels = [] predictions = [] for example in dataset['test']: fewshot_prompt = task_prompt + fewshot_examples + query_prompt + example[text_key] + answer_prompt pred = generate_using_gpt(fewshot_prompt) pred=trim_text(pred) labels.append(example[label_type]) if pred notin class2id: predictions.append(-1) else: predictions.append(class2id[pred])
from sentence_transformers import SentenceTransformer, util import numpy as np from torch import cuda device = 'cuda'if cuda.is_available() else'cpu'
# loading Sentence Transformer based model model = SentenceTransformer('all-mpnet-base-v2', device=device)
# extract embeddings for a set of examples defExtractEmbeddings(examples): embedding_ls = [] for example in examples: embedding = model.encode(example) embedding_ls.append(embedding) return embedding_ls
# extract embeddings for all the training examples class2TrainDatasetWithEmbedding = {} for label in class2TrainDataset: embeddings = ExtractEmbeddings(class2TrainDataset[label]) class2TrainDatasetWithEmbedding[label] = [class2TrainDataset[label], embeddings]
# extract similar queries for a given input text from each of the classes defgetSimilarExamples(input_text, dataset, dataset_embedding): input_embedding = model.encode(input_text) sim_score = util.dot_score(input_embedding, dataset_embedding)[0] topN_ids = np.argsort(-sim_score) return [dataset[i] for i in topN_ids]
defgetClasswiseSimilarExamples(input_text, class2TrainDatasetWithEmbedding): classwiseSimilarExamples = {} for label in class2TrainDataset: similarExamples = getSimilarExamples(input_text, class2TrainDatasetWithEmbedding[label][0], class2TrainDatasetWithEmbedding[label][1]) classwiseSimilarExamples[label] = similarExamples return classwiseSimilarExamples
# generate a prompt with similar examples in each of the classes defgenerateDynamicPrompt(input_text, class2TrainDatasetWithEmbedding, N=3): classwiseSimilarExamples = getClasswiseSimilarExamples(input_text, class2TrainDatasetWithEmbedding) dynamic_prompt = "\nFollowing are some examples." for label in classwiseSimilarExamples: for example in classwiseSimilarExamples[label][:N]: dynamic_prompt += "\nquestion: " + example dynamic_prompt += "\noutput: " + label return dynamic_prompt
# dynamic prompt with one similar example in each of the classes fewshot_examples = generateDynamicPrompt(dataset['test'][0][text_key], class2TrainDatasetWithEmbedding, N=1) dynamic_prompt = task_prompt + fewshot_examples + query_prompt + dataset['test'][0][text_key] + answer_prompt >>> dynamic_prompt
此时,test数据集中的第一条样本的Dynamic Few-Shot prompt为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
As a Question Answering agent, your goal is to categorize questions into different semantic classes that impose constraints on potential answers, so that they can be utilized in later stages of the question answering process. Following are the semantic classes: [ABBR, ENTY, DESC, HUM, LOC, NUM] Following are some examples. question: What do the letters D.C. stand forin Washington , D.C. ? output: ABBR question: What race is 1 , 137 miles long ? output: ENTY question: Why is the mile 528 feet ? output: DESC question: Who lives at 39 Stone Canyon Way ? output: HUM question: What Colorado city owns its own glacier ? output: LOC question: How high is the city of Denver ? output: NUM Classify the following question into one of the above classes. Please answer in a single word. question: How far is it from Denver to Aspen ? output: