Spaces:
Sleeping
Sleeping
from llama_index.llms.azure_openai import AzureOpenAI | |
from llama_index.embeddings.azure_openai import AzureOpenAIEmbedding | |
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings, ChatPromptTemplate | |
import logging | |
import sys | |
import os | |
DEFINITION_1 = "\n".join(["### Descriptions ###", | |
"#### Main Profile Descriptions ####", | |
"VISION: Sees the ‘big picture’ and maintains line of sight. Alignment of goals and actions.", | |
"IDEATION: Generates original and/or innovative ideas; makes unexpected connections.", | |
"OPPORTUNISM: Alert to opportunity; prepared to seize opportunities.", | |
"DRIVE: Strong desire to accomplish goals, Propensity to act decisively and get things done; proactive rather than reactive.", | |
"RESILIENCE: Remains calm and optimistic under pressure, Perseveres in the face of adversity, Recovers from setbacks.", | |
"#### Red Flag Descriptions ####", | |
"HUBRIS: Conceit and self-importance, Overestimation of knowledge and/or ability in respect of tasks, Misplaced belief in unique or exceptional abilities. Need and seek attention, flattery and affirmation. Unable to recognise and/or understand the feelings and needs of others. Expectation of special treatment", | |
"MERCURIAL: Given to sudden changes of mood, behaviour or direction, Given to unconventional or strange behaviour or beliefs, Difficult to predict and/or surprising.", | |
"DOMINANT: Forceful, insistent, domineering, Hostile and argumentative, Unyielding in the face of evidence or argument.", | |
"MACHIAVELLIAN: Deliberate deceitfulness in the pursuit on one’s interest; acting in bad faith, Ruthless pursuit of own interests regardless of considerations of right or wrong, General distrust of the motives of others; attribution of bad faith.", | |
"#### Motivation Descriptions ####", | |
"Purpose: The sense of meaningfulness and direction in one's work or activities. It is the intrinsic motivation derived from believing that one’s efforts contribute to a larger goal or vision.", | |
"Mastery: The drive to improve and excel in a skill or field. It reflects the desire to gain expertise, proficiency, and competence through learning and practice.", | |
"Ownership: The feeling of responsibility and control over one’s work or tasks. It involves having a personal stake in outcomes and the autonomy to make decisions.", | |
"Connection: The sense of belonging and interpersonal relationships within a group or organization. It includes feeling valued, understood, and supported by others.", | |
"Reward: The external incentives and recognition received for one’s efforts. It encompasses financial compensation, bonuses, promotions, and other tangible benefits.", | |
"Power: The capacity to influence others and effect change. It relates to having authority, control, and the ability to shape decisions and outcomes within an organization or group.\n"]) | |
DEFINITION_2 = "\n".join(["### Descriptions ###", | |
"#### Main Profile Descriptions ####", | |
"FORESIGHT: Sees the ‘big picture’ and maintains line of sight.", | |
"TRANSPARENCY: Genuine, honest, and straightforward. Authentic.", | |
"DIRECTION: Takes charge of situations; controls and directs.", | |
"PERSUASION: Affects how others think and feel without control or direction.", | |
"CREATIVITY: Generates original and/or innovative ideas; makes unexpected connections.", | |
"ENTERPRISE: Alert to opportunity; prepared to seize opportunities.", | |
"SELF-BELIEF: Self-belief/confidence across different domains.", | |
"GOAL-ORIENTATION: Strong desire to accomplish goals, Propensity to act decisively and get things done.", | |
"GRIP: Planned rather than spontaneous behavior, Attention to detail and accuracy.", | |
"ADAPTABILITY: Switches easily between different cognitive processes.", | |
"PERSEVERANCE: Remains calm and optimistic under pressure. Perseveres in the face of adversity. Recovers from setbacks", | |
"#### Red Flag Descriptions ####", | |
"PROUD: Conceit and self-importance, Overestimation of knowledge and/or ability in respect of tasks, Misplaced belief in unique or exceptional abilities. Need and seek attention, flattery and affirmation. Unable to recognise and/or understand the feelings and needs of others. Expectation of special treatment", | |
"UNPREDICTABLE: Given to sudden changes of mood, behavior or direction, Given to unconventional or strange behavior or beliefs, Difficult to predict and/or surprising.", | |
"FORCEFUL: Forceful, insistent, domineering, Hostile and argumentative, Unyielding in the face of evidence or argument.", | |
"CALCULATING: Deliberate deceitfulness in the pursuit of one’s interest; acting in bad faith, Ruthless pursuit of own interests regardless of considerations of right or wrong, General distrust of the motives of others; attribution of bad faith.", | |
"#### Motivation Descriptions ####", | |
"Purpose: The sense of meaningfulness and direction in one's work or activities. It is the intrinsic motivation derived from believing that one’s efforts contribute to a larger goal or vision.", | |
"Mastery: The drive to improve and excel in a skill or field. It reflects the desire to gain expertise, proficiency, and competence through learning and practice.", | |
"Ownership: The feeling of responsibility and control over one’s work or tasks. It involves having a personal stake in outcomes and the autonomy to make decisions.", | |
"Connection: The sense of belonging and interpersonal relationships within a group or organization. It includes feeling valued, understood, and supported by others.", | |
"Reward: The external incentives and recognition received for one’s efforts. It encompasses financial compensation, bonuses, promotions, and other tangible benefits.", | |
"Power: The capacity to influence others and effect change. It relates to having authority, control, and the ability to shape decisions and outcomes within an organization or group.\n"]) | |
# Create LLM and Embedding models | |
def create_models(): | |
llm = AzureOpenAI( | |
deployment_name="personality_gpt4o", | |
api_key=os.environ.get("OPENAI_API_KEY_RAG"), | |
azure_endpoint=os.environ.get("AZURE_OPENAI_ENDPOINT_RAG"), | |
api_version="2024-02-01", | |
) | |
embed_model = AzureOpenAIEmbedding( | |
deployment_name="personality_rag_embedding", | |
api_key=os.environ.get("OPENAI_API_KEY_RAG"), | |
azure_endpoint=os.environ.get("AZURE_OPENAI_ENDPOINT_RAG"), | |
api_version="2024-02-01", | |
) | |
return llm, embed_model | |
# Configure settings | |
def configure_settings(llm, embed_model): | |
Settings.llm = llm | |
Settings.embed_model = embed_model | |
Settings.chunk_size = 2048 | |
Settings.chunk_overlap = 50 | |
# Load documents and create index | |
def load_documents_and_create_index(): | |
documents = SimpleDirectoryReader(input_dir="rag_data/").load_data() | |
return VectorStoreIndex.from_documents(documents) | |
# Create chat prompt template | |
def create_chat_prompt_template(profile,type_definition): | |
text_qa_template_str = ( | |
"You are a knowledgeable advisor providing insights based on the specific analysis provided earlier and the Descriptions for each trait below:" | |
"{{definition}}" | |
"\n---------------------\n{{profile}}\n---------------------\n" | |
"Answer questions about yourself (chatbot) and personality analysis based on the technical manual about yourself (chatbot) and personality analysis below " | |
"\n---------------------\n{context_str}\n---------------------\n" | |
"Your responses should around 100 words, directly relate to the user's question, drawing on relevant details from the analysis." | |
"Do not answer unrelevant questions. If the user's question does not pertain to the personality analysis and yourself (chatbot) or is beyond the scope of the information provided, " | |
"politely decline to answer, stating that the question is outside the analysis context." | |
"Focus on delivering concise, accurate, insightful, and relevant information." | |
"Question: {query_str}") | |
print("definition",type_definition) | |
if type_definition == 1: | |
text_qa_template_str = text_qa_template_str.replace("{{definition}}", DEFINITION_1) | |
else: | |
text_qa_template_str = text_qa_template_str.replace("{{definition}}", DEFINITION_2) | |
if profile: | |
text_qa_template_str = text_qa_template_str.replace("{{profile}}", profile) | |
print(text_qa_template_str) | |
chat_text_qa_msgs = [ | |
("system", | |
"Your name is \"Personality Coach\", You are an expert in career advice and personality consultant from " | |
"the company Meta Profiling. Do not infer or assume information beyond what's explicitly provided in the conversation." | |
# "Avoid drawing on external knowledge or making generalizations not directly supported by " | |
# "the report content. Do not answer unrelevant questions." | |
), | |
("user", text_qa_template_str), | |
] | |
return ChatPromptTemplate.from_messages(chat_text_qa_msgs) | |
# Execute query | |
def execute_query(index, template, query): | |
query_engine = index.as_query_engine(similarity_top_k=2, text_qa_template=template) | |
answer = query_engine.query(query) | |
# print(answer.get_formatted_sources()) | |
return answer | |
# def invoke(question,profile,type_definition): | |
# | |
# if profile is None: | |
# return "Profile is missing" | |
# | |
# # setup_environment() | |
# # setup_logging() | |
# llm, embed_model = create_models() | |
# configure_settings(llm, embed_model) | |
# index = load_documents_and_create_index() | |
# print(type_definition) | |
# | |
# if type_definition == 1: | |
# chat_prompt_template = create_chat_prompt_template(profile,DEFINITION_1) | |
# else: | |
# chat_prompt_template = create_chat_prompt_template(profile,DEFINITION_2) | |
# return execute_query(index, chat_prompt_template, question) |