faldeus0092 commited on
Commit
1bb301c
1 Parent(s): d5ae014

big update

Browse files
Files changed (6) hide show
  1. .gitignore +3 -0
  2. app.py +22 -6
  3. embeddings.npy +3 -0
  4. openai_embeddings.npy +3 -0
  5. prompt.py +1 -1
  6. utils.py +18 -1
.gitignore ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ asdf
2
+ __pycache__
3
+ env
app.py CHANGED
@@ -10,9 +10,15 @@ import pandas as pd
10
  import langchain
11
  from langchain import PromptTemplate, LLMChain
12
  from langchain.chat_models import ChatOpenAI
 
 
 
13
 
14
  model_en = SentenceTransformer("intfloat/multilingual-e5-base")
15
- intents_embedding = create_embedding(intents_sentence_similarity_en, model_en)
 
 
 
16
 
17
  llm = None
18
  llm_chain = None
@@ -95,9 +101,10 @@ def check_accuracy(n_samples, threshold):
95
  print(score, grouped_data)
96
  return score, grouped_data
97
 
98
- def classify_intent(input_text:str, history:str, answer, api_key):
99
- # os.environ['OPENAI_API_KEY'] = api_key
100
- llm = ChatOpenAI(model='gpt-3.5-turbo', temperature='0.1', openai_api_key=api_key)
 
101
  prompt = PromptTemplate(template=prompt_template, input_variables=["intents", "INPUT", "chatHistory"])
102
  llm_chain = LLMChain(prompt=prompt, llm=llm, verbose=False)
103
 
@@ -119,8 +126,14 @@ with gr.Blocks(title="Intent Classification Demo", theme=theme) as interface:
119
 
120
  with gr.Row(equal_height=True):
121
  with gr.Column():
 
 
 
 
 
 
122
  api_key = gr.Textbox(label="OpenAI API Key", info="get it at https://platform.openai.com/account/api-keys",visible=True, lines=1, type="password")
123
- n_samples = gr.Slider(1, 10, value=5, step=1, label="N samples", info="Number of samples to be retrieved. Default is 5")
124
  threshold = gr.Slider(0.0, 1.0, value=0.75, step=0.01, label="Threshold", info="Threshold of cosine similarity which intent will be considered similar to the input. The higher, the more similar the intent will be. Default is 0.75")
125
  with gr.Tab("Input from raw text"):
126
  raw_input_text = gr.Textbox(label="Input Chat", info="Input your chat here, the model will predict the intent")
@@ -188,7 +201,10 @@ with gr.Blocks(title="Intent Classification Demo", theme=theme) as interface:
188
  accuracy_button.click(fn=check_accuracy, inputs=[n_samples, threshold], outputs=[accuracy_score, accuracy_table])
189
  raw_ask_button.click(fn=raw_inference, inputs=[raw_input_text, raw_state, n_samples, threshold], outputs=[answer, ask_llm_button_raw])
190
  ask_button.click(fn=raw_inference, inputs=[input_text, state, n_samples, threshold], outputs=[answer, ask_llm_button])
 
 
191
  ask_llm_button.click(fn=classify_intent, inputs=[input_text, history, answer, api_key], outputs=[LLM_prediction, LLM_prompt])
192
  ask_llm_button_raw.click(fn=classify_intent, inputs=[raw_input_text, raw_history, answer, api_key], outputs=[LLM_prediction, LLM_prompt])
193
 
194
- interface.launch(debug=True)
 
 
10
  import langchain
11
  from langchain import PromptTemplate, LLMChain
12
  from langchain.chat_models import ChatOpenAI
13
+ from datetime import date
14
+ import numpy as np
15
+ from openai import OpenAI
16
 
17
  model_en = SentenceTransformer("intfloat/multilingual-e5-base")
18
+ with open('embeddings.npy', 'rb') as f:
19
+ intents_embedding = np.load(f)
20
+ with open('openai_embeddings.npy', 'rb') as f:
21
+ openai_intents_embedding = np.load(f)
22
 
23
  llm = None
24
  llm_chain = None
 
101
  print(score, grouped_data)
102
  return score, grouped_data
103
 
104
+ def classify_intent(input_text:str, history:str, answer, model_name, api_key):
105
+ print(f"predicting with llm... date: {date.today()}")
106
+ print(f"model name: {model_name}")
107
+ llm = ChatOpenAI(model=model_name, temperature='0.1')
108
  prompt = PromptTemplate(template=prompt_template, input_variables=["intents", "INPUT", "chatHistory"])
109
  llm_chain = LLMChain(prompt=prompt, llm=llm, verbose=False)
110
 
 
126
 
127
  with gr.Row(equal_height=True):
128
  with gr.Column():
129
+ model_name = gr.Dropdown(["gpt-3.5-turbo",
130
+ "gpt-3.5-turbo-1106",
131
+ "gpt-4",
132
+ "gpt-4-1106-preview"],
133
+ label="Model name",
134
+ info="Select model name for GPT")
135
  api_key = gr.Textbox(label="OpenAI API Key", info="get it at https://platform.openai.com/account/api-keys",visible=True, lines=1, type="password")
136
+ n_samples = gr.Slider(1, 10, value=10, step=1, label="N samples", info="Number of samples to be retrieved. Default is 5")
137
  threshold = gr.Slider(0.0, 1.0, value=0.75, step=0.01, label="Threshold", info="Threshold of cosine similarity which intent will be considered similar to the input. The higher, the more similar the intent will be. Default is 0.75")
138
  with gr.Tab("Input from raw text"):
139
  raw_input_text = gr.Textbox(label="Input Chat", info="Input your chat here, the model will predict the intent")
 
201
  accuracy_button.click(fn=check_accuracy, inputs=[n_samples, threshold], outputs=[accuracy_score, accuracy_table])
202
  raw_ask_button.click(fn=raw_inference, inputs=[raw_input_text, raw_state, n_samples, threshold], outputs=[answer, ask_llm_button_raw])
203
  ask_button.click(fn=raw_inference, inputs=[input_text, state, n_samples, threshold], outputs=[answer, ask_llm_button])
204
+ # ask_llm_button.click(fn=classify_intent, inputs=[input_text, history, answer, model_name], outputs=[LLM_prediction, LLM_prompt])
205
+ # ask_llm_button_raw.click(fn=classify_intent, inputs=[raw_input_text, raw_history, answer, model_name], outputs=[LLM_prediction, LLM_prompt])
206
  ask_llm_button.click(fn=classify_intent, inputs=[input_text, history, answer, api_key], outputs=[LLM_prediction, LLM_prompt])
207
  ask_llm_button_raw.click(fn=classify_intent, inputs=[raw_input_text, raw_history, answer, api_key], outputs=[LLM_prediction, LLM_prompt])
208
 
209
+ # interface.launch(debug=True)
210
+ interface.launch(share=True, debug=True)
embeddings.npy ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8acf625b84e53b36e293c99f1bccb7e3cb7024357c4489e8d19000c1d0878846
3
+ size 98432
openai_embeddings.npy ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2ae474b523b222e95d39016a80eb507b962dc0cebe105744879af1e695d4d456
3
+ size 393344
prompt.py CHANGED
@@ -1,6 +1,6 @@
1
  #@title prompt.py
2
  prompt_template = """
3
- You are to classify the following message into exactly one of the intents described below:
4
 
5
  {intents}
6
  unknown: You don't find the matching intent from the above list
 
1
  #@title prompt.py
2
  prompt_template = """
3
+ The given message needs to be mapped to exactly one of the intents described below. Only answer with the intent name.
4
 
5
  {intents}
6
  unknown: You don't find the matching intent from the above list
utils.py CHANGED
@@ -38,4 +38,21 @@ def create_embedding(intents:dict, model_en):
38
  for k,v in intents.items():
39
  intents_description_en.append(v)
40
  intents_embedding = model_en.encode(intents_description_en)
41
- return intents_embedding
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  for k,v in intents.items():
39
  intents_description_en.append(v)
40
  intents_embedding = model_en.encode(intents_description_en)
41
+ return intents_embedding
42
+
43
+ # def get_embedding(text, model="text-embedding-ada-002"):
44
+ # text = text.replace("\n", " ")
45
+ # return client.embeddings.create(input = [text], model=model).data[0].embedding
46
+
47
+ # from openai import OpenAI
48
+ # import numpy as np
49
+ # client = OpenAI()
50
+
51
+ # def create_embedding_openai(intents:dict):
52
+ # intents_description_en = []
53
+ # for k,v in intents.items():
54
+ # intents_description_en.append(v)
55
+ # embeddings = np.zeros((len(intents_description_en), 1536))
56
+ # for i, text in enumerate(intents_description_en):
57
+ # embeddings[i,:] = get_embedding(text)
58
+ # return embeddings