#@title Utility Functions def get_history_from_prompt(prompt:str): if "Here are previous chats for your reference (only use this if you need further information to infer the intent):" in prompt: history = prompt.split("Here are previous chats for your reference (only use this if you need further information to infer the intent):") else: history = prompt.split("Here are previous chats or summary conversation for your reference (only use this if you need further information to infer the intent):") return history[1].replace("""The Intent:""", '') def get_latest_user_input_from_prompt(prompt:str): input = prompt.split("Here is the message you are to classify:") if "Here are previous chats for your reference (only use this if you need further information to infer the intent):" in prompt: input = input[1].split("Here are previous chats for your reference (only use this if you need further information to infer the intent):") else: input = input[1].split("Here are previous chats or summary conversation for your reference (only use this if you need further information to infer the intent)") return input[0] # Get the top 5 intents with the highest values def get_top_intents(intent_list:list, similarity, n=5, threshold=0.3, flow=None) -> str: result = dict() for i in range(len(intent_list)): if flow: if intent_list[i] in flow: # print("intent {} is ignored, because it's not in the possible intent".format(intent_list[i])) if similarity[0][i].item() > threshold: result[intent_list[i]] = similarity[0][i].item() else: if similarity[0][i].item() > threshold: result[intent_list[i]] = similarity[0][i].item() top_intents = sorted(result.items(), key=lambda item: item[1], reverse=True)[:n] if not top_intents: top_intents.append(('unknown', 1.0)) return top_intents def create_embedding(intents:dict, model_en): intents_description_en = [] for k,v in intents.items(): intents_description_en.append(v) intents_embedding = model_en.encode(intents_description_en) return intents_embedding # def get_embedding(text, model="text-embedding-ada-002"): # text = text.replace("\n", " ") # return client.embeddings.create(input = [text], model=model).data[0].embedding # from openai import OpenAI # import numpy as np # client = OpenAI() # def create_embedding_openai(intents:dict): # intents_description_en = [] # for k,v in intents.items(): # intents_description_en.append(v) # embeddings = np.zeros((len(intents_description_en), 1536)) # for i, text in enumerate(intents_description_en): # embeddings[i,:] = get_embedding(text) # return embeddings