cmagganas commited on
Commit
8e3c024
·
verified ·
1 Parent(s): c9a568e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -0
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import chainlit as cl
2
+ from pydantic import BaseModel, Field
3
+ from dotenv import load_dotenv
4
+ import instructor
5
+ from openai import OpenAI
6
+ import os
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 the Pydantic models
14
+ class UserProposal(BaseModel):
15
+ proposal: str = Field(description="This is the proposal of the original user prompt. It should be a clear concise detailed plan to use simple ai software tools to solve specific problem.")
16
+ is_clear: str = Field(description="Is the proposed plan clear? It specifies which tools it needs to use and how. It lays out each component and how they all connect.")
17
+ is_detailed: str = Field(description="Is the proposed plan detailed? Each component should have a description of what it does.")
18
+ is_explicit: str = Field(description="Is the proposed plan explicit? Each component should have a data model to describe their input and output schema.")
19
+
20
+ class ProposedArchitecture(BaseModel):
21
+ proposed_architecture: str = Field(description="A detailed AI application architecture with all the tools required for the plan proposed. (e.g. Python packages)")
22
+
23
+ class PropositionWithRevision(BaseModel):
24
+ revised_proposed_architecture: str = Field(description="Step by step implementation of software solution.")
25
+
26
+ # Define functions
27
+ def extract_user_proposal_details(user_proposal: str) -> UserProposal:
28
+ return client.chat.completions.create(
29
+ model="gpt-4-turbo-preview",
30
+ response_model=UserProposal,
31
+ messages=[
32
+ {"role": "user", "content": user_proposal},
33
+ ],
34
+ )
35
+
36
+ def generate_proposed_architecture(proposal: str) -> ProposedArchitecture:
37
+ return client.chat.completions.create(
38
+ model="gpt-4-turbo-preview",
39
+ response_model=ProposedArchitecture,
40
+ messages=[
41
+ {"role": "user", "content": f"Write a detailed AI application architecture with all the tools required for the plan proposed: \n\n{proposal}"},
42
+ ],
43
+ )
44
+
45
+ def revise_architecture(proposed_architecture: str) -> PropositionWithRevision:
46
+ return client.chat.completions.create(
47
+ model="gpt-4-turbo-preview",
48
+ response_model=PropositionWithRevision,
49
+ messages=[
50
+ {"role": "user", "content": f"Revise the plan proposed: \n\n{proposed_architecture}\n\nThe plan should be a step by step implementation of software solution."},
51
+ ],
52
+ )
53
+
54
+ # Define the Chainlit message handler
55
+ @cl.on_message
56
+ async def main(message: cl.Message):
57
+ user_proposal = message.content
58
+
59
+ user_proposal_details = extract_user_proposal_details(user_proposal)
60
+
61
+ proposed_architecture = generate_proposed_architecture(user_proposal_details.proposal)
62
+
63
+ await cl.Message(
64
+ content=f"Proposed Architecture:\n{proposed_architecture.proposed_architecture}"
65
+ ).send()
66
+
67
+ feedback_message = await cl.AskUserMessage(content="What do you think about this proposed plan and alleged architecture?", timeout=300).send()
68
+ if feedback_message:
69
+ human_feedback_of_proposed_plan = feedback_message['output']
70
+
71
+ revised_architecture = revise_architecture(proposed_architecture.proposed_architecture)
72
+
73
+ await cl.Message(
74
+ content=f"Revised Architecture:\n{revised_architecture.revised_proposed_architecture}"
75
+ ).send()
76
+
77
+ with open("output.md", "w") as output_file:
78
+ output_file.write("# User Proposal\n")
79
+ output_file.write(user_proposal_details.proposal + "\n\n")
80
+ output_file.write("# Proposed Architecture\n")
81
+ output_file.write(proposed_architecture.proposed_architecture + "\n\n")
82
+ output_file.write("# Revised Architecture\n")
83
+ output_file.write(revised_architecture.revised_proposed_architecture + "\n")
84
+
85
+ await cl.Message(
86
+ content="The results have been saved to output.md"
87
+ ).send()
88
+ else:
89
+ await cl.Message(
90
+ content="No feedback received. Exiting."
91
+ ).send()