1
+ import streamlit as st
2
+ import requests
3
+ import json
4
+ import re
5
+
6
+ def rephrase_prompt (user_request ):
7
+ url = "https://j.gravelle.us/APIs/Groq/groqApiRephrasePrompt.php"
8
+ data = {"user_request" : user_request }
9
+ headers = {"Content-Type" : "application/json" }
10
+
11
+ try :
12
+ response = requests .post (url , json = data , headers = headers ) # Use json=data for correct JSON formatting
13
+ print (f"Debug: API request sent: { json .dumps (data )} " )
14
+ print (f"Debug: API response received: { response .text } " )
15
+
16
+ if response .status_code == 200 :
17
+ try :
18
+ # First, attempt to parse the response as JSON
19
+ json_response = response .json ()
20
+ # If parsing is successful and 'rephrased' key exists, return its value
21
+ if 'rephrased' in json_response :
22
+ return json_response ['rephrased' ]
23
+ else :
24
+ # If JSON is valid but doesn't have 'rephrased' key, log and handle the case
25
+ st .error ("Error: 'rephrased' key not found in the API response." )
26
+ return ""
27
+ except ValueError :
28
+ # If response is not JSON, assume it's plain text and return directly
29
+ return response .text
30
+ else :
31
+ st .error (f"Error: API request failed with status code { response .status_code } " )
32
+ return ""
33
+ except requests .exceptions .RequestException as e :
34
+ st .error (f"Error: { str (e )} " )
35
+ return ""
36
+
37
+ def get_agents_from_text (text ):
38
+ url = "https://j.gravelle.us/APIs/Groq/groqApiGetAgentsFromPrompt.php"
39
+ data = {"user_request" : text }
40
+ headers = {"Content-Type" : "application/json" }
41
+
42
+ try :
43
+ response = requests .post (url , data = json .dumps (data ), headers = headers )
44
+ print (f"Debug: API request sent: { json .dumps (data )} " )
45
+ print (f"Debug: API response received: { response .text } " )
46
+
47
+ if response .status_code == 200 :
48
+ try :
49
+ agents = response .json ()
50
+ return agents
51
+ except json .JSONDecodeError :
52
+ st .error ("Error: Unable to parse the API response as JSON." )
53
+ else :
54
+ st .error (f"Error: API request failed with status code { response .status_code } " )
55
+ except requests .exceptions .RequestException as e :
56
+ st .error (f"Error: { str (e )} " )
57
+
58
+ return []
59
+
60
+ def send_request_to_groq_api (expert_name , request ):
61
+ url = "https://j.gravelle.us/APIs/Groq/groqAPI.php"
62
+ data = {
63
+ "model" : "mixtral-8x7b-32768" ,
64
+ "temperature" : 0.5 ,
65
+ "max_tokens" : 32768 ,
66
+ "top_p" : 1 ,
67
+ "stop" : "TERMINATE" ,
68
+ "messages" : [
69
+ {
70
+ "role" : "system" ,
71
+ "content" : "You are a chatbot capable of anything and everything."
72
+ },
73
+ {
74
+ "role" : "user" ,
75
+ "content" : request
76
+ }
77
+ ]
78
+ }
79
+ headers = {"Content-Type" : "application/json" }
80
+
81
+ try :
82
+ response = requests .post (url , data = json .dumps (data ), headers = headers )
83
+ print (f"Debug: API request sent: { json .dumps (data )} " )
84
+ print (f"Debug: API response received: { response .text } " )
85
+
86
+ if response .status_code == 200 :
87
+ try :
88
+ result = response .json ()
89
+ message_content = result ["choices" ][0 ]["message" ]["content" ]
90
+ return message_content
91
+ except (KeyError , IndexError , json .JSONDecodeError ):
92
+ st .error ("Error: Unable to parse the API response." )
93
+ else :
94
+ st .error (f"Error: API request failed with status code { response .status_code } " )
95
+ except requests .exceptions .RequestException as e :
96
+ st .error (f"Error: { str (e )} " )
97
+
98
+ return ""
99
+
100
+ def extract_code_from_response (response ):
101
+ code_pattern = r"```(.*?)```"
102
+ code_blocks = re .findall (code_pattern , response , re .DOTALL )
103
+
104
+ html_pattern = r"<html.*?>.*?</html>"
105
+ html_blocks = re .findall (html_pattern , response , re .DOTALL | re .IGNORECASE )
106
+
107
+ js_pattern = r"<script.*?>.*?</script>"
108
+ js_blocks = re .findall (js_pattern , response , re .DOTALL | re .IGNORECASE )
109
+
110
+ css_pattern = r"<style.*?>.*?</style>"
111
+ css_blocks = re .findall (css_pattern , response , re .DOTALL | re .IGNORECASE )
112
+
113
+ code_blocks .extend (html_blocks )
114
+ code_blocks .extend (js_blocks )
115
+ code_blocks .extend (css_blocks )
116
+
117
+ code_blocks = [block .strip () for block in code_blocks ]
118
+ return "\n \n " .join (code_blocks )
119
+
120
+ ...
121
+
122
+ agents_management .py =
123
+
124
+ import sys
125
+ import os
126
+
127
+ project_dir = os .path .abspath (os .path .join (os .path .dirname (__file__ ), '../' ))
128
+ print (f"Adding to sys.path: { project_dir } " )
129
+ sys .path .append (project_dir )
130
+ import streamlit as st
131
+
132
+ def add_agent ():
133
+ """Display form to add a new agent."""
134
+ st .title ("Add New Agent" )
135
+ with st .form ("agent_form" , clear_on_submit = True ):
136
+ expert_name = st .text_input ("Expert Name" , key = "new_expert" )
137
+ description = st .text_area ("Description" , key = "new_description" )
138
+ submit = st .form_submit_button ("Submit" )
139
+
140
+ if submit and expert_name and description :
141
+ add_or_update_agent (None , expert_name , description )
142
+ st .success ("Agent added successfully!" )
143
+
144
+ def display_agents ():
145
+ """Display current agents with options to edit or delete."""
146
+ if "agents" in st .session_state :
147
+ for index , agent in enumerate (st .session_state ["agents" ]):
148
+ st .write (f"{ agent ['expert_name' ]} : { agent ['description' ]} " )
149
+ if st .button (f"Edit { agent ['expert_name' ]} " , key = f"edit_{ index } " ):
150
+ st .session_state ["editing_index" ] = index
151
+ st .experimental_rerun ()
152
+ if st .button (f"Delete { agent ['expert_name' ]} " , key = f"delete_{ index } " ):
153
+ delete_agent (index )
154
+
155
+ def edit_agent_form ():
156
+ """Display a form to edit an agent's details if an agent is selected for editing."""
157
+ index = st .session_state .get ("editing_index" )
158
+ if index is not None :
159
+ agent = st .session_state ["agents" ][index ]
160
+ with st .form ("edit_agent_form" ):
161
+ expert_name = st .text_input ("Expert Name" , value = agent ["expert_name" ], key = "edit_expert" )
162
+ description = st .text_area ("Description" , value = agent ["description" ], key = "edit_description" )
163
+ submit_edit = st .form_submit_button ("Update Agent" )
164
+
165
+ if submit_edit :
166
+ add_or_update_agent (index , expert_name , description )
167
+ del st .session_state ["editing_index" ]
168
+ st .success ("Agent updated successfully!" )
169
+
170
+ def add_or_update_agent (index , expert_name , description ):
171
+ """Add a new agent or update an existing one in the session state."""
172
+ agent = {"expert_name" : expert_name , "description" : description }
173
+ if index is None : # Add new agent
174
+ if "agents" not in st .session_state :
175
+ st .session_state .agents = []
176
+ st .session_state .agents .append (agent )
177
+ else : # Update existing agent
178
+ st .session_state .agents [index ] = agent
179
+
180
+ def delete_agent (index ):
181
+ """Delete an agent from the session state."""
182
+ del st .session_state .agents [index ]
183
+ st .experimental_rerun ()
184
+
185
+ def manage_agents ():
186
+ add_agent ()
187
+ edit_agent_form ()
188
+ display_agents ()
0 commit comments