File size: 2,697 Bytes
d5b718d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1bb301c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#@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