1hangzhao commited on
Commit
cbdf3e1
·
verified ·
1 Parent(s): ddf482a

Update ontochat/functions.py

Browse files
Files changed (1) hide show
  1. ontochat/functions.py +18 -57
ontochat/functions.py CHANGED
@@ -6,42 +6,31 @@ import gradio as gr
6
 
7
  openai_api_key = None
8
 
9
- # Global dictionaries to hold pre-identified prompts and their corresponding long prompts
10
- preidentified_prompts = {
11
- "expand_skills_interests": "My skills are [skills] and my interests are [interests]. Can you help me expand these and ensure they align with my occupation?",
12
-
13
- "expand_user_goal": "My user goal description is [user goal description]. Can you help me refine this goal, without changing the actions or keywords, and ensure it aligns with my interests and domain?",
14
-
15
- "expand_actions": "The actions I need to take to achieve my goal are [actions]. Can you help me expand these actions, ensuring they align with my occupation and skills, without changing the goal description or keywords?",
16
-
17
- "expand_keywords": "The keywords for my user goal and actions are [keywords]. Can you help me refine these keywords, ensuring they are relevant to my goal and actions, without changing the goal or actions themselves?",
18
-
19
- "expand_current_methods": "The current methods I use to perform my actions are [current methods]. Can you help me elaborate on these methods, ensuring they align with my occupation, skills, and goal, without changing the challenges, new methods, or outcomes?",
20
-
21
- "expand_challenges": "The challenges I face with my current methods are [challenges]. Can you help me expand on these challenges, ensuring they logically relate to my skills and goal, without changing the current methods, new methods, or outcomes?",
22
-
23
- "expand_new_methods": "The new methods I will use through this ontology to address my challenges are [new methods]. Can you help me elaborate on these new methods, ensuring they align with my occupation, skills, and goal, without changing the current methods, challenges, or outcomes?",
24
-
25
- "expand_outcomes": "The outcomes I expect after implementing the new methods are [outcomes]. Can you help me refine these outcomes, ensuring they address the challenges and benefit my goal, without changing the current methods, challenges, or new methods?"
26
- }
27
-
28
  current_preidentified_prompts = []
29
  current_preidentified = [
30
- "Cultural Heritage Preservation",
31
- "Accessibility and Inclusion",
32
- "Multisensory Interactions",
33
- "Digital Rights and Preservation"
 
 
 
 
34
  ]
35
 
36
  def load_example(selection):
37
  return current_preidentified[selection]
38
-
39
- def update_examples():
40
- current_samples = current_preidentified_prompts
41
- return gr.Dataset(samples=current_samples)
42
 
43
  def set_openai_api_key(api_key: str):
44
  global openai_api_key
 
 
 
 
 
 
 
 
45
  openai_api_key = api_key
46
  return "API key has been set! Now you can chat with the chatbot. Enjoy :)"
47
 
@@ -49,9 +38,6 @@ def check_api_key():
49
  if openai_api_key is None:
50
  raise ValueError("OpenAI API key is not set. Please set it using the 'Set API Key' button.")
51
 
52
- def get_preidentified_prompts():
53
- return list(preidentified_prompts.keys())
54
-
55
  def user_story_generator(message, history):
56
  instructions = [
57
  {
@@ -97,33 +83,8 @@ def user_story_generator(message, history):
97
  "content": message
98
  })
99
  bot_message = chat_completion(openai_api_key, instructions + messages)
100
- history.append([message, bot_message])
101
- questions = generate_elicitation_questions(history)
102
- return history, "", questions
103
-
104
- def generate_elicitation_questions(history):
105
- instructions = [{
106
- "role": "system",
107
- "content": f"In the dictionary {preidentified_prompts}, the key expand_skills_interests corresponds to section 1, '1. Persona'. The keys expand_user_goal, expand_actions, and expand_keywords correspond to section 2, '2. Goal'. The keys expand_current_methods, expand_challenges, expand_new_methods, and expand_outcomes correspond to section 3, '3. Scenario'. All keys in this dictionary are related to section 4, '4. Create User Story'."
108
- }]
109
- messages = build_messages(history)
110
- messages.append({
111
- "role": "user",
112
- "content": "Identify which section ('1. Persona', '2. Goal', '3. Scenario', '4. Create User Story') your most current answer or elicitation question belongs to."
113
- "If your answer or elicitation question relates to [name], [age], [occupation], [skills], or [interests], it belongs to section 1. If it's about [user goal description], [actions], or [keywords], it belongs to section 2. If it addresses [current methods], [challenges], [new methods], or [outcomes], it belongs to section 3. If sections 1 through 3 are already completed or when the final user story is generated, it belongs to section 4. Once it belongs to section 4, it will always remain in section 4, regardless of what happens afterward."
114
- "Then return all the keys associated with that section. List each key name on a separate line, starting with the first key name and providing only the key names."
115
- })
116
- bot_message = chat_completion(openai_api_key, instructions + messages)
117
- keys = bot_message.split('\n')
118
- global current_preidentified_prompts
119
- current_preidentified_prompts = [[key] for key in keys if key in preidentified_prompts]
120
- global current_preidentified
121
- current_preidentified=[]
122
- for sublist in current_preidentified_prompts:
123
- key = sublist[0]
124
- if key in preidentified_prompts:
125
- current_preidentified.append(preidentified_prompts[key])
126
- return [[key] for key in keys if key in preidentified_prompts]
127
 
128
  def cq_generator(message, history):
129
  check_api_key()
 
6
 
7
  openai_api_key = None
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  current_preidentified_prompts = []
10
  current_preidentified = [
11
+ "My name is [Name], I am [Age] years old, and I work as a [Occupation]. My skills include [skills], and I have a strong interest in [interests]. Could you assist me in refining and expanding my skills and interests, ensuring they are aligned with my occupation?",
12
+ "My user goal description is [user goal description]. Can you help me refine this goal, without changing the actions or keywords, and ensure it aligns with my interests and domain?",
13
+ "The actions I need to take to achieve my goal are [actions]. Can you help me expand these actions, ensuring they align with my occupation and skills, without changing the goal description or keywords?",
14
+ "The keywords for my user goal and actions are [keywords]. Can you help me refine these keywords, ensuring they are relevant to my goal and actions, without changing the goal or actions themselves?",
15
+ "The current methods I use to perform my actions are [current methods]. Can you help me elaborate on these methods, ensuring they align with my occupation, skills, and goal, without changing the challenges, new methods, or outcomes?",
16
+ "The challenges I face with my current methods are [challenges]. Can you help me expand on these challenges, ensuring they logically relate to my skills and goal, without changing the current methods, new methods, or outcomes?",
17
+ "The new methods I will use through this ontology to address my challenges are [new methods]. Can you help me elaborate on these new methods, ensuring they align with my occupation, skills, and goal, without changing the current methods, challenges, or outcomes?",
18
+ "The outcomes I expect after implementing the new methods are [outcomes]. Can you help me refine these outcomes, ensuring they address the challenges and benefit my goal, without changing the current methods, challenges, or new methods?"
19
  ]
20
 
21
  def load_example(selection):
22
  return current_preidentified[selection]
 
 
 
 
23
 
24
  def set_openai_api_key(api_key: str):
25
  global openai_api_key
26
+ # Strip any leading or trailing spaces
27
+ api_key = api_key.strip()
28
+
29
+ # Check if the API key contains any spaces or starts with "sk-"
30
+ if " " in api_key or not api_key.startswith("sk-"):
31
+ return "Invalid API key format. Please ensure the key does not contain spaces and is a valid OpenAI API key."
32
+
33
+ # Set the API key if the format is valid
34
  openai_api_key = api_key
35
  return "API key has been set! Now you can chat with the chatbot. Enjoy :)"
36
 
 
38
  if openai_api_key is None:
39
  raise ValueError("OpenAI API key is not set. Please set it using the 'Set API Key' button.")
40
 
 
 
 
41
  def user_story_generator(message, history):
42
  instructions = [
43
  {
 
83
  "content": message
84
  })
85
  bot_message = chat_completion(openai_api_key, instructions + messages)
86
+ history.append([message, bot_message])
87
+ return history, ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
 
89
  def cq_generator(message, history):
90
  check_api_key()