Spaces:
Runtime error
Runtime error
Fix bugs: name of client, languages saved and missing standalone prompt
Browse files- app.py +14 -2
- services/chatbot.py +10 -6
- services/utils.py +14 -6
app.py
CHANGED
|
@@ -97,12 +97,24 @@ with gr.Blocks() as app:
|
|
| 97 |
"=========\n"
|
| 98 |
"\n"
|
| 99 |
"----------------------- Standalone -----------------------\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
"Chat History:\n"
|
| 101 |
"\n"
|
| 102 |
"HISTORY\n"
|
| 103 |
"Follow-up message: QUESTION\n"
|
| 104 |
-
"Standalone message:\n"
|
| 105 |
-
"```", line_breaks=True
|
| 106 |
)
|
| 107 |
|
| 108 |
with gr.Tab('Test'):
|
|
|
|
| 97 |
"=========\n"
|
| 98 |
"\n"
|
| 99 |
"----------------------- Standalone -----------------------\n"
|
| 100 |
+
"You are a standalone question-maker. Given the following chat history and follow-up message, rephrase "
|
| 101 |
+
"the follow-up phrase to be a standalone question (sometimes the follow-up is not a question, so create "
|
| 102 |
+
"a standalone phrase), in spanish. In the standalone message you must include all the information at the "
|
| 103 |
+
"moment that is known about the customer, all the important nouns and what they are looking for. In cases "
|
| 104 |
+
"where you think is usefully, include what is the best recommendation for the customer. To give you "
|
| 105 |
+
"context, the conversation is about (INGRESE INFORMACIÓN DE LA MARCA, EL NOMBRE Y DE MANERA MUY GENERAL "
|
| 106 |
+
"QUE ES LO QUE VENDE).\n"
|
| 107 |
+
"There might be moments when there isn't a question in those cases return a standalone phrase: for example "
|
| 108 |
+
"if the user says 'hola' (or something similar) then the output would be 'el usuario está saludando', or "
|
| 109 |
+
"if the user says 'gracias' or 'es muy util' (or something similar) then the output would be a phrase "
|
| 110 |
+
"showing that the user is grateful and what they are grateful for, or if the user say 'si' then it would "
|
| 111 |
+
"be a phrase encapsulating the relationship to its previous question or phrase.\n"
|
| 112 |
+
"Your response cannot be more than 50 words.\n"
|
| 113 |
"Chat History:\n"
|
| 114 |
"\n"
|
| 115 |
"HISTORY\n"
|
| 116 |
"Follow-up message: QUESTION\n"
|
| 117 |
+
"Standalone message:\n", line_breaks=True
|
|
|
|
| 118 |
)
|
| 119 |
|
| 120 |
with gr.Tab('Test'):
|
services/chatbot.py
CHANGED
|
@@ -13,12 +13,13 @@ pinecone.init(api_key=os.getenv("PINECONE_API_KEY"), environment=os.getenv("PINE
|
|
| 13 |
INDEX = pinecone.Index(os.getenv("PINECONE_INDEX"))
|
| 14 |
|
| 15 |
|
| 16 |
-
def start_chat(
|
| 17 |
"""
|
| 18 |
Initialize chat with greeting text and audio in spanish
|
| 19 |
-
:param
|
| 20 |
:return: (chat history with greeting, audio with updated file and gradio update with visible=True)
|
| 21 |
"""
|
|
|
|
| 22 |
# Get greeting text and audio, the first one available in spanish
|
| 23 |
with open(f'assets/{client_name}/greetings/es.csv', mode='r', encoding='utf-8') as infile:
|
| 24 |
reader = csv.reader(infile)
|
|
@@ -31,12 +32,13 @@ def start_chat(client_name: str) -> tuple[list[list[str | None]], gr.helpers, gr
|
|
| 31 |
return chat_history, gr.update(value=f'{audio_name}'), gr.update(visible=True)
|
| 32 |
|
| 33 |
|
| 34 |
-
def get_random_data(
|
| 35 |
"""
|
| 36 |
Returns an audio with a random data in spanish
|
| 37 |
-
:param
|
| 38 |
:return: gradio audio updated with a random data from the client
|
| 39 |
"""
|
|
|
|
| 40 |
random_options = []
|
| 41 |
path_audios = f'assets/{client_name}/media/audio'
|
| 42 |
for random_audio in os.listdir(path_audios):
|
|
@@ -49,17 +51,19 @@ def get_random_data(client_name: str) -> gr.helpers:
|
|
| 49 |
|
| 50 |
|
| 51 |
def get_answer(
|
| 52 |
-
chat_history: list[tuple[str, str]], user_input: str,
|
| 53 |
) -> tuple[list[tuple[str, str]], str, gr.helpers]:
|
| 54 |
"""
|
| 55 |
Gets the answer from the chatbot and returns it as an audio and text
|
| 56 |
:param chat_history: previous chat history
|
| 57 |
:param user_input: user question
|
| 58 |
-
:param
|
| 59 |
:param general_prompt: prompt used for answering the questions
|
| 60 |
:param context_prompt: prompt used for finding the context in the vectorstore
|
| 61 |
:return:
|
| 62 |
"""
|
|
|
|
|
|
|
| 63 |
# Format chat history to OpenAI format msg history
|
| 64 |
msg_history = [{'role': 'system', 'content': general_prompt}]
|
| 65 |
for i, (user, bot) in enumerate(chat_history):
|
|
|
|
| 13 |
INDEX = pinecone.Index(os.getenv("PINECONE_INDEX"))
|
| 14 |
|
| 15 |
|
| 16 |
+
def start_chat(client: str) -> tuple[list[list[str | None]], gr.helpers, gr.helpers]:
|
| 17 |
"""
|
| 18 |
Initialize chat with greeting text and audio in spanish
|
| 19 |
+
:param client: name of the client
|
| 20 |
:return: (chat history with greeting, audio with updated file and gradio update with visible=True)
|
| 21 |
"""
|
| 22 |
+
client_name = client.lower().replace(' ', '-')
|
| 23 |
# Get greeting text and audio, the first one available in spanish
|
| 24 |
with open(f'assets/{client_name}/greetings/es.csv', mode='r', encoding='utf-8') as infile:
|
| 25 |
reader = csv.reader(infile)
|
|
|
|
| 32 |
return chat_history, gr.update(value=f'{audio_name}'), gr.update(visible=True)
|
| 33 |
|
| 34 |
|
| 35 |
+
def get_random_data(client: str) -> gr.helpers:
|
| 36 |
"""
|
| 37 |
Returns an audio with a random data in spanish
|
| 38 |
+
:param client: name of the client for this chatbot
|
| 39 |
:return: gradio audio updated with a random data from the client
|
| 40 |
"""
|
| 41 |
+
client_name = client.lower().replace(' ', '-')
|
| 42 |
random_options = []
|
| 43 |
path_audios = f'assets/{client_name}/media/audio'
|
| 44 |
for random_audio in os.listdir(path_audios):
|
|
|
|
| 51 |
|
| 52 |
|
| 53 |
def get_answer(
|
| 54 |
+
chat_history: list[tuple[str, str]], user_input: str, client: str, general_prompt: str, context_prompt: str
|
| 55 |
) -> tuple[list[tuple[str, str]], str, gr.helpers]:
|
| 56 |
"""
|
| 57 |
Gets the answer from the chatbot and returns it as an audio and text
|
| 58 |
:param chat_history: previous chat history
|
| 59 |
:param user_input: user question
|
| 60 |
+
:param client: name of the client
|
| 61 |
:param general_prompt: prompt used for answering the questions
|
| 62 |
:param context_prompt: prompt used for finding the context in the vectorstore
|
| 63 |
:return:
|
| 64 |
"""
|
| 65 |
+
client_name = client.lower().replace(' ', '-')
|
| 66 |
+
|
| 67 |
# Format chat history to OpenAI format msg history
|
| 68 |
msg_history = [{'role': 'system', 'content': general_prompt}]
|
| 69 |
for i, (user, bot) in enumerate(chat_history):
|
services/utils.py
CHANGED
|
@@ -196,14 +196,16 @@ def create_chatbot(
|
|
| 196 |
return gr.update(value='Chatbot created!!!', interactive=False)
|
| 197 |
|
| 198 |
|
| 199 |
-
def save_prompts(
|
| 200 |
"""
|
| 201 |
Saves all the prompts (standalone and one for each language) and uploads them to Google Cloud Storage
|
| 202 |
-
:param
|
| 203 |
:param context_prompt: standalone prompt used to search into the vectorstore
|
| 204 |
:param prompts_table: table with the prompt of each language
|
| 205 |
:return: None
|
| 206 |
"""
|
|
|
|
|
|
|
| 207 |
path_prompts = f'assets/{client_name}/prompts'
|
| 208 |
os.makedirs(path_prompts, exist_ok=True)
|
| 209 |
|
|
@@ -221,20 +223,26 @@ def save_prompts(client_name: str, context_prompt: str, prompts_table: list[list
|
|
| 221 |
return
|
| 222 |
|
| 223 |
|
| 224 |
-
def generate_json(
|
| 225 |
"""
|
| 226 |
Creates a json file with the environment variables used in the API
|
| 227 |
-
:param
|
| 228 |
:param languages:
|
| 229 |
:param max_num_questions:
|
| 230 |
:param chatbot_name:
|
| 231 |
:return: gradio file with the value as the path of the json file
|
| 232 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
json_object = json.dumps(
|
| 234 |
{
|
| 235 |
-
'CLIENT_NAME': client_name, 'MODEL_OPENAI': os.getenv('OPENAI_MODEL'), 'LANGUAGES':
|
| 236 |
'MAX_NUM_QUESTIONS': max_num_questions, 'NUM_VECTORS_CONTEXT': 10, 'THRESHOLD_RECYCLE': 0.97,
|
| 237 |
-
'OPENAI_API_KEY': 'Check OpenAI for this', 'CHATBOT_NAME': chatbot_name
|
|
|
|
| 238 |
},
|
| 239 |
indent=4
|
| 240 |
)
|
|
|
|
| 196 |
return gr.update(value='Chatbot created!!!', interactive=False)
|
| 197 |
|
| 198 |
|
| 199 |
+
def save_prompts(client: str, context_prompt: str, prompts_table: list[list[str]]) -> None:
|
| 200 |
"""
|
| 201 |
Saves all the prompts (standalone and one for each language) and uploads them to Google Cloud Storage
|
| 202 |
+
:param client: name of the client
|
| 203 |
:param context_prompt: standalone prompt used to search into the vectorstore
|
| 204 |
:param prompts_table: table with the prompt of each language
|
| 205 |
:return: None
|
| 206 |
"""
|
| 207 |
+
client_name = client.lower().replace(' ', '-')
|
| 208 |
+
|
| 209 |
path_prompts = f'assets/{client_name}/prompts'
|
| 210 |
os.makedirs(path_prompts, exist_ok=True)
|
| 211 |
|
|
|
|
| 223 |
return
|
| 224 |
|
| 225 |
|
| 226 |
+
def generate_json(client: str, languages: list[str], max_num_questions: int, chatbot_name: str) -> gr.helpers:
|
| 227 |
"""
|
| 228 |
Creates a json file with the environment variables used in the API
|
| 229 |
+
:param client:
|
| 230 |
:param languages:
|
| 231 |
:param max_num_questions:
|
| 232 |
:param chatbot_name:
|
| 233 |
:return: gradio file with the value as the path of the json file
|
| 234 |
"""
|
| 235 |
+
# Format the name and the languages
|
| 236 |
+
short_languages = ''.join(f'{TRANSLATE_LANGUAGES[language]},' for language in languages)
|
| 237 |
+
short_languages = short_languages[:-1]
|
| 238 |
+
client_name = client.lower().replace(' ', '-')
|
| 239 |
+
|
| 240 |
json_object = json.dumps(
|
| 241 |
{
|
| 242 |
+
'CLIENT_NAME': client_name, 'MODEL_OPENAI': os.getenv('OPENAI_MODEL'), 'LANGUAGES': short_languages,
|
| 243 |
'MAX_NUM_QUESTIONS': max_num_questions, 'NUM_VECTORS_CONTEXT': 10, 'THRESHOLD_RECYCLE': 0.97,
|
| 244 |
+
'OPENAI_API_KEY': 'Check OpenAI for this', 'CHATBOT_NAME': chatbot_name, 'HAS_ROADMAP': 0,
|
| 245 |
+
'SAVE_ANSWERS': 0, 'USE_RECYCLED_DATA': 1
|
| 246 |
},
|
| 247 |
indent=4
|
| 248 |
)
|