OntoChat / ontochat /functions.py
1hangzhao's picture
Update ontochat/functions.py
1ef495d verified
import json
from ontochat.chatbot import chat_completion, build_messages
from ontochat.analysis import compute_embeddings, agglomerative_clustering, llm_cq_clustering
from ontochat.verbaliser import verbalise_ontology
import gradio as gr
openai_api_key = None
current_preidentified_prompts = []
current_preidentified = [
"""The domain of the ontology-based system is **[DOMAIN]**.
Please ensure that all responses are strictly relevant to this system's domain. Use terminology and structures that are specific to ontology-based systems, including appropriate relationships and reasoning principles. Do not introduce information outside the defined scope.""",
"""My persona is **[NAME]**, I am **[AGE]** years old, and I work as a **[OCCUPATION]**. My expertise includes **[SKILLS]**, and I have a strong interest in **[INTERESTS]**.
Please refine my response to ensure:
1. The persona is described clearly, emphasizing expertise or experience relevant to ontology-based systems in the specified domain.
2. Details (name, age, occupation, skills, interests) are logically aligned with domain knowledge, typical users, and the use of ontology-based systems.
3. If skills and interests overlap, make them distinct by separating conceptual knowledge (e.g., data modeling, requirements analysis) from applied skills (e.g., ontology development, knowledge graph implementation).
4. Use *[CONCISE & PRECISE / CREATIVE & DETAILED]* language for clarity and suitability in a knowledge-intensive context.
5. Present the output as *[BULLET POINTS / A DETAILED PARAGRAPH]*.""",
"""The knowledge modelling objective I aim to achieve with the ontology-based system is **[USER GOAL]**
Please refine my response to ensure:
1. The goal is clearly described and directly related to using an ontology-based system to support a real task in the specified domain.
2. Expand the goal if needed: start with a high-level *[KNOWLEDGE REPRESENTATION OBJECTIVE]* (such as representing key domain knowledge, integrating data, or enabling reasoning), then describe a specific *[DOMAIN TASK]* or *[USER NEED]* that the ontology-based system should support.
3. Make sure the goal aligns with my domain expertise and addresses a practical scenario where ontology-based systems add value.
4. Focus on the ontology-supported aspects of the task, not unrelated system functions or technology details.
5. Use *[CONCISE & PRECISE / CREATIVE & DETAILED]* language as needed for clarity.
6. Present the output as *[BULLET POINTS / A DETAILED PARAGRAPH / TAXONOMY-LIKE HIERARCHY]*.""",
"""The steps I need to take to build or configure the ontology-based system for this objective are **[ACTIONS]**.
Please refine my response to ensure:
1. The actions are clearly described and directly relate to the step-by-step process of developing or configuring the ontology-based system.
2. List the actions as simple, practical steps required to design, construct, and deploy the ontology-based system for the target domain or goal.
3. For each step, specify any *[ONTOLOGY ENGINEERING METHODS]*, *[KNOWLEDGE REPRESENTATION TOOLS]*, *[DATA SOURCES]*, or *[REASONING TECHNIQUES]* used in the process.
4. If relevant, include *[COLLABORATION WITH DOMAIN EXPERTS]* or integration of *[EXTERNAL DATASETS]* to enhance system quality.
5. Ensure the actions logically align with my background and the requirements of the domain and user goal.
6. Use *[CONCISE & PRECISE / CREATIVE & DETAILED]* language, as appropriate.
7. Structure the answer as *[BULLET POINTS / A DETAILED PARAGRAPH / TAXONOMY-LIKE HIERARCHY]*.""",
"""The key domain concepts to be represented within the ontology-based system are **[KEYWORDS]**.
Please refine my response to ensure:
1. The keywords are clearly defined and directly relevant to the ontology-based system and the target domain.
2. Each keyword should support knowledge structuring and reasoning within the ontology.
3. Where possible, align the keywords with *[EXISTING ONTOLOGY VOCABULARIES]* or recognized *[STANDARDS]*.
4. Expand each keyword by adding related ontological terms, such as subclasses, parent classes, or linked data mappings.
5. Use *[CONCISE & PRECISE / CREATIVE & DETAILED]* language as required.
6. Structure the answer as *[BULLET POINTS / A DETAILED PARAGRAPH / TAXONOMY-LIKE HIERARCHY]*.""",
"""The current manual methods I use to achieve my goal, without the ontology-based system, are **[CURRENT METHODS]**.
Please refine my response to ensure:
1. The methods are clearly described and involve only manual or ad-hoc approaches for structuring and applying domain knowledge, with no use of ontology-based systems or automated reasoning tools.
2. Present the methods as a logical, step-by-step sequence of actions or procedures currently used to achieve my goal.
3. For each step, specify any *[MANUAL TOOLS, DOCUMENTATION PRACTICES, or INFORMAL REPRESENTATION TECHNIQUES]* (such as spreadsheets, text documents, or diagrams).
4. The methods should reflect established practices within my domain and align logically with my expertise and goals.
5. Do not mention challenges, ontology-based systems, new methods, or expected outcomes.
6. Use *[CONCISE & PRECISE / CREATIVE & DETAILED]* language, as needed.
7. Structure the answer as *[BULLET POINTS / A DETAILED PARAGRAPH / TAXONOMY-LIKE HIERARCHY]*.""",
"""The limitations in current practices that the ontology-based system should address are **[CHALLENGES]**.
Please refine my response to ensure:
1. The challenges are clearly described and arise directly from my current tools, workflows, or informal practices for structuring and reasoning about domain knowledge.
2. Each challenge should be specific and derived from limitations of *[CURRENT TOOLS]*, *[METHODOLOGIES]*, or *[LOGICAL CONSTRAINTS]* used in my existing (non-ontology) approaches.
3. Where relevant, include issues related to data consistency, integration across sources, or knowledge reuse.
4. The challenges should logically relate to my domain and my knowledge structuring goals.
5. Focus only on core difficulties with current methods—do not discuss possible solutions or expected improvements.
6. Use *[CONCISE & PRECISE / CREATIVE & DETAILED]* language, as needed.
7. Structure the answer as *[BULLET POINTS / A DETAILED PARAGRAPH / TAXONOMY-LIKE HIERARCHY]*.""",
"""The sequence of interactions I will have with the ontology-based system to address these challenges are **[NEW METHODS]**.
Please refine my response to ensure:
1. The methods are clearly described and directly enabled by features of the ontology-based system.
2. Each method should show how the ontology-based system supports automated reasoning, semantic search, ontology alignment, or other advanced knowledge management functions.
3. All methods should logically align with my stated goals and expected improvements in managing or utilizing domain knowledge.
4. Only describe new approaches and capabilities that are possible with the ontology-based system—do not discuss previous challenges or prior manual methods.
5. Use *[CONCISE & PRECISE / CREATIVE & DETAILED]* language, as best fits the context.
6. Structure the answer as *[BULLET POINTS / A DETAILED PARAGRAPH / TAXONOMY-LIKE HIERARCHY]*.""",
"""The expected benefits of using the ontology-based system are [OUTCOMES].
Please refine my response to ensure:
1. The outcomes are clearly described and directly result from using the ontology-based system.
2. The outcomes reflect benefits such as improved knowledge structuring, reasoning, semantic interoperability, or automation enabled by the ontology-based system.
3. The outcomes align logically with my overall goal for the ontology-based application.
4. Only include ontology-driven improvements—do not discuss prior methods or challenges.
5. Use *[CONCISE & PRECISE / CREATIVE & DETAILED]* language as best fits the context.
6. Structure the answer as *[BULLET POINTS / A DETAILED PARAGRAPH / TAXONOMY-LIKE HIERARCHY]*.""",
]
def set_openai_api_key(api_key: str):
global openai_api_key
# Strip any leading or trailing spaces
api_key = api_key.strip()
# Check if the API key contains any spaces or starts with "sk-"
if " " in api_key or not api_key.startswith("sk-"):
return "Invalid API key format. Please ensure the key does not contain spaces and is a valid OpenAI API key."
# Set the API key if the format is valid
openai_api_key = api_key
return "API key has been set! Now you can chat with the chatbot. Enjoy :)"
def check_api_key():
if openai_api_key is None:
raise ValueError("OpenAI API key is not set. Please set it using the 'Set API Key' button.")
def user_story_generator(message, history):
instructions = [
{
"role": "assistant",
"content": (
"Ontology-based systems provide structured ways to manage knowledge in a domain. Ontology Requirements Engineering (ORE) ensures these systems meet user needs. Ontology engineers gather user stories by interviewing domain experts. These stories outline typical users (personas), their goals, and the scenarios where the ontology-based system provides solutions. The stories are then translated into Competency Questions (CQs), such as 'Which artists have collaborated with a specific composer using this ontology-based system?'' These CQs guide the system's design to answer real-world needs and improve practical use and reuse."
"You are an ontology engineer interviewing a domain expert to create a user story for an ontology-based system, follow this structured approach:"
"Ask elicitation questions one at a time, providing an example answer and the prompt template the user should use, while incorporating user feedback if needed."
"If all requirements for the current elicitation are fully addressed, always ask the user if this meets their expectations. Do not ask the next question unless the user confirms the current one is satisfactory."
"When a domain expert requests refinement, provide just one focused point in one sentence, directly aligned with their current answer."
"The user can request to revisit any previously completed steps."
"If the user's answer doesn't address the current question, gently remind them of the question and prompt them to respond accordingly."
"If the user doesn't confirm the current result is satisfactory, their attempt to answer the next question should be rejected, and they should be asked to respond to the current one."
"Do not answer any queries that are not related to this task. \n\n"
"1. Persona\n"
"Begin by creating a persona—a typical user of the ontology-based system."
"[Persona]: Ask one elicitation question to the domain expert about their relevant expertise by describing [NAME], [AGE], [OCCUPATION], [SKILLS], [INTERESTS] for the ontology-based system, along with a brief example answer as guidance, and include the message 'Use template **[Create Persona]** to answer' as a reminder.\n"
"Once the expert provides this information, suggest possible improvements or clarifications. After all persona details are collected, move to the next section.\n\n"
"2. Goal\n"
"[User goal description]: Ask one elicitation question to the domain expert to specify the knowledge modeling objective they aim to achieve with [USER GOAL DESCRIPTION] in the ontology-based system, along with a brief example answer as guidance, and include the message 'Use template **[Create User Goal]** to answer' as a reminder.\n"
"[Actions]: Ask one elicitation question to the domain expert to outline the steps they would take for building or configuring the ontology-based system by describing [ACTIONS], along with a brief example answer as guidance, and include the message 'Use template **[Create Actions]** to answer' as a reminder.\n"
"[Keywords]: Ask one elicitation question to the domain expert to identify the key domain concepts that should be included as [KEYWORDS] within the ontology-based system, along with a brief example answer as guidance, and include the message 'Use template **[Create Keywords]** to answer' as a reminder.\n"
"Once the expert has answered, offer suggestions for further refinement, then proceed to the next section.\n\n"
"3. Scenario\n"
"[Scenario before]: Ask one elicitation question to the domain expert to explain their current manual methods by detailing [CURRENT METHODS] they use to achieve their goal without the ontology-based system, along with a brief example answer as guidance, and include the message 'Use template **[Create Current Methods]** to answer' as a reminder.\n"
"[Challenges]: Ask one elicitation question to the domain expert to highlight the limitations in current practices by describing [CHALLENGES] that the ontology-based system should address, along with a brief example answer as guidance, and include the message 'Use template **[Create Challenges]** to answer' as a reminder.\n"
"[Scenario during]: Ask one elicitation question to the domain expert to describe how they would interact with the ontology-based system to address challenges through [NEW METHODS], along with a brief example answer as guidance, and include the message 'Use template **[Create New Methods]** to answer' as a reminder.\n"
"[Scenario after]: Ask one elicitation question to the domain expert to describe the expected benefits from using the ontology-based system by stating [OUTCOMES], along with a brief example answer as guidance, and include the message 'Use template **[Create Outcomes]** to answer' as a reminder.\n"
"Provide feedback on each scenario part and refine the answers if needed before moving on."
"4. Create User Story\n"
"Once you have completed sections 1 to 3, summarize the information into a full user story. Use the persona, goal, and scenario information to craft the user story in this format:\n\n"
"Persona: [name], [age], [occupation], [skills], [interests].\n"
"Goal: [user goal description].\n"
"Actions: [Actions].\n"
"Keywords: [Keywords].\n"
"Scenario Before: [Current methods].\n"
"Challenges: [Challenges].\n"
"Scenario During: [New methods].\n"
"Scenario After: [Outcomes].\n\n"
"Provide the user story to the domain expert and Ask one elicitation question for any further feedback or refinements. If needed, adjust the story based on their suggestions."
)
}
]
messages = build_messages(history)
messages.append({"role": "user", "content": message})
bot_message = chat_completion(openai_api_key, instructions + messages)
history.append({"role": "user", "content": message})
history.append({"role": "assistant", "content": bot_message})
return history, ""
def load_example(selection):
return current_preidentified[selection]
# def cq_generator(message, history):
# check_api_key()
# instructions = [{
# "role": "assistant",
# "content": "You are a conversational ontology engineering assistant."
# }, {
# "role": "user",
# "content": "Here are instructions for you on how to generate high-quality competency questions. First, here "
# "are some good examples of competency questions generated from example data. Who performs the song? "
# "from the data Yesterday was performed by Armando Rocca, When (what year) was the building built? "
# "from the data The Church was built in 1619, In which context is the building located? from the "
# "data The Church is located in a periurban context. Second, how to make them less complex. Take the "
# "generated competency questions and check if any of them can be divided into multiple questions. If "
# "they do, split the competency question into multiple competency questions. If it does not, leave "
# "the competency question as it is. For example, the competency question Who wrote The Hobbit and in "
# "what year was the book written? must be split into two competency questions: Who wrote the book? "
# "and In what year was the book written?. Another example is the competency question, When was the "
# "person born?. This competency question cannot be divided into multiple questions. Third, how to "
# "remove real entities to abstract them. Take the competency questions and check if they contain "
# "real-world entities, like Freddy Mercury or 1837. If they do, change those real-world entities "
# "from these competency questions to more general concepts. For example, the competency question "
# "Which is the author of Harry Potter? should be changed to Which is the author of the book?. "
# "Similarly, the competency question Who wrote the book in 2018? should be changed to Who wrote the "
# "book, and in what year was the book written?"
# }]
# messages = build_messages(history)
# messages.append({
# "role": "user",
# "content": message
# })
# bot_message = chat_completion(openai_api_key, instructions + messages)
# history.append([message, bot_message])
# return bot_message, history, ""
# def load_example_user_story():
# with open("data/Linka#1_MusicKnowledge.md", "r") as f:
# return f.read()
# def clustering_generator(cqs, cluster_method, n_clusters):
# check_api_key()
# if n_clusters:
# n_clusters = int(n_clusters)
# cqs, cq_embeddings = compute_embeddings(cqs)
# if cluster_method == "Agglomerative clustering":
# cq_clusters, cluster_image = agglomerative_clustering(cqs, cq_embeddings, n_clusters)
# else: # cluster_method == "LLM clustering"
# cq_clusters, cluster_image = llm_cq_clustering(cqs, n_clusters, openai_api_key)
# return cluster_image, json.dumps(cq_clusters, indent=4)
# def ontology_testing(ontology_file, ontology_desc, cqs):
# check_api_key()
# verbalisation = verbalise_ontology(ontology_file, ontology_desc, "")
# messages = [{
# "role": "system",
# "content": "Please (1) provide a description of the ontology uploaded to provide basic information and "
# "additional context, (2) give the competency questions (CQs) that you want to test with."
# }, {
# "role": "user",
# "content": verbalisation + "\n" + f"Given the above ontology, please label each competency question: {cqs} to "
# f"determine whether it is addressed properly or not. Format your response in"
# f" ['yes': 'CQ1', 'no': 'CQ2', ...]."
# }]
# bot_message = chat_completion(openai_api_key, messages)
# return bot_message