cmagganas commited on
Commit
e8d7047
·
verified ·
1 Parent(s): 0e3509b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -37
app.py CHANGED
@@ -4,22 +4,24 @@ import instructor
4
  from openai import OpenAI
5
  import os
6
  import json
 
7
 
8
  load_dotenv()
9
 
 
 
 
10
  # Patch the OpenAI client with Instructor
11
  client = instructor.from_openai(OpenAI(api_key=os.environ['OPENAI_API_KEY']))
12
 
13
  # Define prompt templates
14
  PRD_PROMPT_TEMPLATE = """
15
  ### Product Requirements Document (PRD) for AI Applications
16
-
17
  #### Introduction
18
  - **Objective/Goal**:
19
  - Explain the purpose of the AI application and what it aims to achieve.
20
  - Provide a high-level overview of the release purpose.
21
  - User Proposal: {user_proposal}
22
-
23
  #### Features
24
  - **Feature 1**:
25
  - **Description**: Detailed description of the feature.
@@ -32,20 +34,16 @@ PRD_PROMPT_TEMPLATE = """
32
  - **Goal**: What this feature aims to achieve.
33
  - **Use Case**: Example of how a user would utilize this feature.
34
  - **Additional Details**: Specifics such as out-of-scope items and prompt structure.
35
-
36
  *(Repeat for each feature)*
37
-
38
  #### UX Flow & Design Notes
39
  - **Overview**: General guidance required to ensure the release objectives are met.
40
  - **User Workflow**: Describe the overall user workflow.
41
  - **Design Considerations**: Any specific design notes relevant at this stage.
42
-
43
  #### System & Environment Requirements
44
  - **Supported Environments**:
45
  - Browsers (e.g., Chrome, Firefox, Safari)
46
  - Operating Systems (e.g., Windows 10, macOS)
47
  - Memory and Processing Power requirements
48
-
49
  #### Assumptions, Constraints & Dependencies
50
  - **Assumptions**:
51
  - List anything expected to be in place (e.g., all users will have Internet connectivity).
@@ -55,18 +53,15 @@ PRD_PROMPT_TEMPLATE = """
55
 
56
  - **Dependencies**:
57
  - Conditions or items the product will rely on (e.g., relying on Google Maps for directions).
58
-
59
  #### Prompt Engineering Practices
60
  - **Ambiguity Mitigation**: Ensure clear, specific prompts to avoid ambiguous outputs. Evaluate prompts using examples and test consistency with different inputs.
61
  - **Versioning**: Track and version each prompt to monitor performance changes over time.
62
  - **Optimization**: Apply techniques like chain-of-thought prompting, majority voting, and breaking tasks into smaller prompts to improve output quality.
63
  - **Cost & Latency Management**: Balance detailed prompts with cost efficiency and manage latency by optimizing token usage.
64
-
65
  #### Task Composability
66
  - **Multiple Tasks Composition**: Outline how multiple tasks interact, including sequential and parallel executions, and control flows like if statements and loops.
67
  - **Agents and Tools**: Describe the use of agents to manage tasks and the integration of tools such as SQL executors and web browsers.
68
  - **Control Flows**: Detail the use of control flows in managing tasks, including sequential, parallel, if statements, and loops.
69
-
70
  #### Review & Approval Process
71
  - **Initial Review**:
72
  - Steps for internal product team review.
@@ -81,9 +76,7 @@ PRD_PROMPT_TEMPLATE = """
81
  - **Final Approval**:
82
  - Ensuring no surprises later on.
83
  - Agreement from all teams involved (UX design, engineering, QA).
84
-
85
  ---
86
-
87
  This PRD template is configured specifically for the user proposal: "{user_proposal}", ensuring a consistent format for every PRD, covering all necessary aspects from features to dependencies, review processes, and incorporating best practices in prompt engineering and task composability.
88
  """
89
 
@@ -131,11 +124,15 @@ def llm_call(user_prompt, system_prompt=None):
131
  {"role": "user", "content": user_prompt},
132
  ]
133
 
134
- return client.chat.completions.create(
135
- model="gpt-4-turbo-preview",
136
- response_model=None,
137
- messages=messages,
138
- )
 
 
 
 
139
 
140
  # Define the Chainlit message handler
141
  @cl.on_chat_start
@@ -156,24 +153,31 @@ async def start():
156
  user_proposal = res['output']
157
 
158
  prd_sys1 = format_template(PRD_PROMPT_TEMPLATE, user_proposal=user_proposal) # system message to create PRD
159
- prd_response_raw = llm_call(user_prompt=user_proposal, system_prompt=prd_sys1).choices[0].message.content # get PRD from LLM
160
-
161
- # send PRD output to UI
162
- prd_msg = cl.Message(content=prd_response_raw)
163
- await prd_msg.send()
164
-
165
- prd_json = json.dumps({"objective_goal": "Develop a chatbot...", "features": [], "ux_flow_design_notes": "...", "system_environment_requirements": "...", "assumptions": [], "constraints": [], "dependencies": [], "prompt_engineering_practices": "...", "task_composability": "...", "review_approval_process": "..."})
166
- designer_prompt = format_template(DESIGNER_PROMPT_TEMPLATE, prd_response_raw=prd_response_raw, prd_json=prd_json, user_proposal=user_proposal)
167
- designer_output = llm_call(designer_prompt).choices[0].message.content
168
-
169
- designer_output_msg = cl.Message(content=designer_output)
170
- await designer_output_msg.send()
171
-
172
- # update outputs in UI
173
- for secs in [1,5,10,20]:
174
- await cl.sleep(secs)
175
- await prd_msg.update()
176
- await designer_output_msg.update()
177
-
178
- # Load the starters ... overrided by on_chat_start
179
- import starters
 
 
 
 
 
 
 
 
4
  from openai import OpenAI
5
  import os
6
  import json
7
+ import logging
8
 
9
  load_dotenv()
10
 
11
+ # Configure logging
12
+ logging.basicConfig(level=logging.INFO)
13
+
14
  # Patch the OpenAI client with Instructor
15
  client = instructor.from_openai(OpenAI(api_key=os.environ['OPENAI_API_KEY']))
16
 
17
  # Define prompt templates
18
  PRD_PROMPT_TEMPLATE = """
19
  ### Product Requirements Document (PRD) for AI Applications
 
20
  #### Introduction
21
  - **Objective/Goal**:
22
  - Explain the purpose of the AI application and what it aims to achieve.
23
  - Provide a high-level overview of the release purpose.
24
  - User Proposal: {user_proposal}
 
25
  #### Features
26
  - **Feature 1**:
27
  - **Description**: Detailed description of the feature.
 
34
  - **Goal**: What this feature aims to achieve.
35
  - **Use Case**: Example of how a user would utilize this feature.
36
  - **Additional Details**: Specifics such as out-of-scope items and prompt structure.
 
37
  *(Repeat for each feature)*
 
38
  #### UX Flow & Design Notes
39
  - **Overview**: General guidance required to ensure the release objectives are met.
40
  - **User Workflow**: Describe the overall user workflow.
41
  - **Design Considerations**: Any specific design notes relevant at this stage.
 
42
  #### System & Environment Requirements
43
  - **Supported Environments**:
44
  - Browsers (e.g., Chrome, Firefox, Safari)
45
  - Operating Systems (e.g., Windows 10, macOS)
46
  - Memory and Processing Power requirements
 
47
  #### Assumptions, Constraints & Dependencies
48
  - **Assumptions**:
49
  - List anything expected to be in place (e.g., all users will have Internet connectivity).
 
53
 
54
  - **Dependencies**:
55
  - Conditions or items the product will rely on (e.g., relying on Google Maps for directions).
 
56
  #### Prompt Engineering Practices
57
  - **Ambiguity Mitigation**: Ensure clear, specific prompts to avoid ambiguous outputs. Evaluate prompts using examples and test consistency with different inputs.
58
  - **Versioning**: Track and version each prompt to monitor performance changes over time.
59
  - **Optimization**: Apply techniques like chain-of-thought prompting, majority voting, and breaking tasks into smaller prompts to improve output quality.
60
  - **Cost & Latency Management**: Balance detailed prompts with cost efficiency and manage latency by optimizing token usage.
 
61
  #### Task Composability
62
  - **Multiple Tasks Composition**: Outline how multiple tasks interact, including sequential and parallel executions, and control flows like if statements and loops.
63
  - **Agents and Tools**: Describe the use of agents to manage tasks and the integration of tools such as SQL executors and web browsers.
64
  - **Control Flows**: Detail the use of control flows in managing tasks, including sequential, parallel, if statements, and loops.
 
65
  #### Review & Approval Process
66
  - **Initial Review**:
67
  - Steps for internal product team review.
 
76
  - **Final Approval**:
77
  - Ensuring no surprises later on.
78
  - Agreement from all teams involved (UX design, engineering, QA).
 
79
  ---
 
80
  This PRD template is configured specifically for the user proposal: "{user_proposal}", ensuring a consistent format for every PRD, covering all necessary aspects from features to dependencies, review processes, and incorporating best practices in prompt engineering and task composability.
81
  """
82
 
 
124
  {"role": "user", "content": user_prompt},
125
  ]
126
 
127
+ try:
128
+ return client.chat.completions.create(
129
+ model="gpt-4-turbo-preview",
130
+ response_model=None,
131
+ messages=messages,
132
+ )
133
+ except Exception as e:
134
+ logging.error(f"Error during LLM call: {e}")
135
+ return None
136
 
137
  # Define the Chainlit message handler
138
  @cl.on_chat_start
 
153
  user_proposal = res['output']
154
 
155
  prd_sys1 = format_template(PRD_PROMPT_TEMPLATE, user_proposal=user_proposal) # system message to create PRD
156
+ prd_response = llm_call(user_prompt=user_proposal, system_prompt=prd_sys1)
157
+
158
+ if prd_response:
159
+ prd_response_raw = prd_response.choices[0].message.content
160
+
161
+ # send PRD output to UI
162
+ prd_msg = cl.Message(content=prd_response_raw)
163
+ await prd_msg.send()
164
+
165
+ prd_json = json.dumps({"objective_goal": "Develop a chatbot...", "features": [], "ux_flow_design_notes": "...", "system_environment_requirements": "...", "assumptions": [], "constraints": [], "dependencies": [], "prompt_engineering_practices": "...", "task_composability": "...", "review_approval_process": "..."})
166
+ designer_prompt = format_template(DESIGNER_PROMPT_TEMPLATE, prd_response_raw=prd_response_raw, prd_json=prd_json, user_proposal=user_proposal)
167
+ designer_response = llm_call(designer_prompt)
168
+
169
+ if designer_response:
170
+ designer_output = designer_response.choices[0].message.content
171
+
172
+ designer_output_msg = cl.Message(content=designer_output)
173
+ await designer_output_msg.send()
174
+
175
+ # update outputs in UI
176
+ for secs in [1, 5, 10, 20]:
177
+ await cl.sleep(secs)
178
+ await prd_msg.update()
179
+ await designer_output_msg.update()
180
+ else:
181
+ await cl.Message(content="Error generating designer output. Please try again.").send()
182
+ else:
183
+ await cl.Message(content="Error generating PRD. Please