-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
167 lines (145 loc) · 7.01 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import os
import sys
import streamlit as st
from pydantic import BaseModel, Field
from typing import Optional
import openai
from roadmap import RoadmapTool
from presentation import PresentationTool
from pdf import PDFCreationTool
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
class AiVisualiserArgs(BaseModel):
action: str = Field(
description="The action to perform: 'create_presentation' or 'create_pdf' or 'create_roadmap'.",
enum=["create_presentation", "create_pdf", "create_roadmap"],
)
topic: str = Field(description="The topic for which to generate a roadmap or pdf or presentation")
num_steps: int = Field(description="Number of steps in the roadmap", default=3)
num_slides: int = Field(description="Number of slides to create (1-10)", default=5)
theme: str = Field(
description="Theme of the presentation: 'light', 'dark', 'professional', 'creative', 'minimalist', or 'tech'",
default='light'
)
num_pages: int = Field(description="Number of pages in the presentation", default=2)
class AIVisualizer:
def run(self, action: str, topic: str, num_steps: Optional[int] = None, num_pages: Optional[int] = None, theme: Optional[str] = None, num_slides: Optional[int] = None):
if action == "create_roadmap":
roadmap = self.roadmap(topic, num_steps)
return roadmap
elif action == "create_presentation":
return self.create_presentation(topic, num_slides, theme)
elif action == "create_pdf":
return self.pdf_create(topic, num_pages)
else:
return "Error: Invalid action specified."
def roadmap(self, topic: str, num_steps: int) -> str:
roadmap = RoadmapTool()
return roadmap._run(num_steps=num_steps, topic=topic)
def create_presentation(self, topic: str, num_slides: int, theme: str) -> str:
presentation = PresentationTool()
return presentation._run(topic=topic, num_slides=num_slides, theme=theme)
def pdf_create(self, topic: str, num_pages: int) -> str:
pdf = PDFCreationTool()
return pdf._run(topic=topic, num_pages=num_pages)
sys_prompt = """
You are a helpful assistant that can create visual aids such as roadmaps, presentations, and PDFs on various topics. The user will provide the action (create_roadmap, create_presentation, or create_pdf) and the topic, along with optional parameters. You will use the 'ai_visualizer' tool to generate the requested visual aid.
When the user specifies an action and topic, use the 'ai_visualizer' tool with the appropriate arguments. The tool will generate the requested visual aid based on the given parameters.
Respond politely and provide the output from the tool with the location if given.
"""
tools = [
{
"type": "function",
"function": {
'name': 'ai_visualizer',
'description': 'Generates visual aids such as roadmaps, presentations, and PDFs on a given topic.',
'parameters': {
'type': 'object',
'properties': {
'action': {
'type': 'string',
'enum': ['create_roadmap', 'create_presentation', 'create_pdf'],
'description': 'The action to perform: create_roadmap, create_presentation, or create_pdf.'
},
'topic': {
'type': 'string',
'description': 'The topic for which to generate a roadmap, presentation, or PDF.'
},
'num_steps': {
'type': 'integer',
'description': 'Number of steps in the roadmap (default: 3).'
},
'num_slides': {
'type': 'integer',
'description': 'Number of slides to create (1-10, default: 5).'
},
'theme': {
'type': 'string',
'enum': ['light', 'dark', 'professional', 'creative', 'minimalist', 'tech'],
'description': 'Theme of the presentation (default: light).'
},
'num_pages': {
'type': 'integer',
'description': 'Number of pages in the PDF (default: 2).'
}
},
'required': ['action', 'topic']
}
}
}
]
openaiclient = openai.Client(
api_key=os.getenv('OPENAI_API_KEY'),
base_url=os.getenv('OPENAI_BASE_URL'),
)
def main():
st.title("AI Visualizer")
st.write("Welcome to AI Visualizer! How can I help you today?")
if 'messages' not in st.session_state:
st.session_state.messages = [{"role": "system", "content": sys_prompt}]
for message in st.session_state.messages:
if message['role'] != 'system':
st.chat_message(message['role']).write(message['content'])
if prompt := st.chat_input("Enter your message"):
st.session_state.messages.append({"role": "user", "content": prompt})
st.chat_message("user").write(prompt)
response = openaiclient.chat.completions.create(
model=os.getenv('OPENAI_MODEL'),
messages=st.session_state.messages,
tools=tools,
max_tokens=1200,
)
response_message = response.choices[0].message
st.session_state.messages.append({"role": "assistant", "content": response_message.content})
if response_message.content==None:
st.chat_message("assistant").write("Tool Execution Happening!!!Wait for some Moment!")
else:
st.chat_message("assistant").write(response_message.content)
tool_calls = response_message.tool_calls
if tool_calls:
tool_call = tool_calls[0]
tool_call_id = tool_call.id
tool_function_name = tool_call.function.name
tool_args = eval(tool_call.function.arguments)
if tool_function_name == 'ai_visualizer':
ai_viz = AIVisualizer()
results = ai_viz.run(**tool_args)
st.session_state.messages.append({
"role": "tool",
"tool_call_id": tool_call_id,
"name": tool_function_name,
"content": results
})
model_response_with_function_call = openaiclient.chat.completions.create(
model=os.getenv('OPENAI_MODEL'),
messages=st.session_state.messages,
)
final_response = model_response_with_function_call.choices[0].message.content
st.session_state.messages.append({"role": "assistant", "content": final_response})
st.chat_message("assistant").write(final_response)
else:
error_message = f"Error: function {tool_function_name} does not exist"
st.error(error_message)
if __name__ == "__main__":
main()