6
6
from .chathub import *
7
7
from .conversation import *
8
8
from .request import *
9
- from re_edge_gpt .utils .utilities import *
10
9
11
10
12
11
class Chatbot :
@@ -53,7 +52,8 @@ async def ask(
53
52
locale : str = guess_locale (),
54
53
simplify_response : bool = False ,
55
54
attachment : dict [str , str ] = None ,
56
- autosave : bool = True
55
+ remove_options : list = None ,
56
+ add_options : list = None
57
57
):
58
58
"""
59
59
Ask a question to the bot
@@ -72,7 +72,8 @@ async def ask(
72
72
attachment={"filename": r"<file_path>"})
73
73
For base64 image using
74
74
attachment={"base64_image": r"<base64_image_str>"})
75
- :param autosave: add autosave on request
75
+ :param remove_options remove options from Style
76
+ :param add_options add options to Style
76
77
"""
77
78
async for final , response in self .chat_hub .ask_stream (
78
79
prompt = prompt ,
@@ -82,47 +83,56 @@ async def ask(
82
83
search_result = search_result ,
83
84
locale = locale ,
84
85
attachment = attachment ,
85
- autosave = autosave
86
+ remove_options = remove_options ,
87
+ add_options = add_options
86
88
):
87
89
if final :
88
90
if not simplify_response :
89
91
return response
90
- messages_left = response ["item" ]["throttling" ][
91
- "maxNumUserMessagesInConversation"
92
- ] - response ["item" ]["throttling" ].get (
92
+ messages_left = (response .get ("item" ).get ("throttling" ).get ("maxNumUserMessagesInConversation" )
93
+ - response .get ("item" ).get ("throttling" ).get (
93
94
"numUserMessagesInConversation" ,
94
95
0 ,
95
- )
96
+ ))
96
97
if messages_left == 0 :
97
98
raise Exception ("Max messages reached" )
98
- message = ""
99
- for msg in reversed (response ["item" ]["messages" ]):
100
- if msg .get ("adaptiveCards" ) and msg ["adaptiveCards" ][0 ]["body" ][
101
- 0
102
- ].get ("text" ):
103
- message = msg
104
- break
99
+ message = {}
100
+ for msg in reversed (response .get ("item" ).get ("messages" )):
101
+ if msg .get ("author" ) == "bot" :
102
+ old_message = message .get ("text" )
103
+ if old_message :
104
+ old_message = old_message + " \n "
105
+ else :
106
+ old_message = ""
107
+ message .update ({
108
+ "author" : "bot" ,
109
+ "text" : old_message + msg .get ("text" )
110
+ })
105
111
if not message :
106
112
raise Exception ("No message found" )
107
- suggestions = [
108
- suggestion ["text" ]
109
- for suggestion in message .get ("suggestedResponses" , [])
110
- ]
111
- adaptive_cards = message .get ("adaptiveCards" , [])
112
- sources = (
113
- adaptive_cards [0 ]["body" ][0 ].get ("text" ) if adaptive_cards else None
114
- )
115
- sources_link = (
116
- adaptive_cards [0 ]["body" ][- 1 ].get ("text" )
117
- if adaptive_cards
118
- else None
119
- )
113
+ image_create_text = ""
114
+ suggestions = []
115
+ source_texts = []
116
+ source_links = []
117
+ for detail in reversed (response .get ("item" ).get ("messages" )):
118
+ suggestion_responses = detail .get ("suggestedResponses" , {})
119
+ source_attr = detail .get ("sourceAttributions" , {})
120
+ if suggestion_responses :
121
+ for suggestion in suggestion_responses :
122
+ suggestions .append (suggestion .get ("text" ))
123
+ if source_attr :
124
+ for source in source_attr :
125
+ source_texts .append (source .get ("providerDisplayName" ))
126
+ source_links .append (source .get ("seeMoreUrl" ))
127
+ if detail .get ("contentType" ) == "IMAGE" and detail .get ("messageType" ) == "GenerateContentQuery" :
128
+ image_create_text = detail .get ("text" )
120
129
return {
121
130
"text" : message ["text" ],
122
131
"author" : message ["author" ],
123
- "sources " : sources ,
124
- "sources_link " : sources_link ,
132
+ "source_texts " : source_texts ,
133
+ "source_links " : source_links ,
125
134
"suggestions" : suggestions ,
135
+ "image_create_text" : image_create_text ,
126
136
"messages_left" : messages_left ,
127
137
"max_messages" : response ["item" ]["throttling" ][
128
138
"maxNumUserMessagesInConversation"
@@ -139,7 +149,8 @@ async def ask_stream(
139
149
webpage_context : str | None = None ,
140
150
search_result : bool = False ,
141
151
locale : str = guess_locale (),
142
- autosave : bool = True
152
+ remove_options : list = None ,
153
+ add_options : list = None
143
154
) -> Generator [bool , dict | str , None ]:
144
155
"""
145
156
Ask a question to the bot
@@ -152,7 +163,8 @@ async def ask_stream(
152
163
webpage_context = webpage_context ,
153
164
search_result = search_result ,
154
165
locale = locale ,
155
- autosave = autosave
166
+ remove_options = remove_options ,
167
+ add_options = add_options
156
168
):
157
169
yield response
158
170
0 commit comments