File size: 18,109 Bytes
32ad276
 
99d0cac
32ad276
99d0cac
 
32ad276
8489337
d2d66c1
0c39b50
5a15b7d
89c77c7
32ad276
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c39b50
32ad276
910dbfd
c144d94
0c39b50
32ad276
5a15b7d
c144d94
 
0c39b50
5a15b7d
 
0c39b50
 
 
 
e7bbb2c
 
 
 
0c39b50
 
 
 
 
 
e7bbb2c
 
 
0c39b50
 
 
 
 
 
 
 
 
32ad276
c144d94
 
 
 
5a15b7d
32ad276
0c39b50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e7bbb2c
0c39b50
 
 
 
 
e7bbb2c
0c39b50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32ad276
 
 
 
39ee51b
 
 
 
 
 
 
 
 
c81fd70
 
 
1ead00b
 
 
99d0cac
1ead00b
 
 
99d0cac
c81fd70
99d0cac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39ee51b
f7ff440
910dbfd
32ad276
6cd005a
 
 
 
0211c96
6cd005a
32ad276
 
 
bdf6434
2f98b46
62eb6b0
22441f4
ceac283
 
32ad276
43d6899
0c39b50
32ad276
 
8489337
 
32ad276
 
 
910dbfd
32ad276
 
 
 
 
 
 
 
 
910dbfd
 
32ad276
 
 
910dbfd
32ad276
 
e7c2e79
 
32ad276
 
 
8489337
 
 
910dbfd
8489337
 
 
 
0c39b50
8489337
 
 
 
16f3173
d2d66c1
 
 
16f3173
32ad276
 
 
 
 
 
 
39ee51b
7902830
 
99d0cac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c81fd70
99d0cac
c81fd70
 
 
39ee51b
 
 
 
 
 
 
 
 
 
99d0cac
7902830
 
 
3033ee9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7902830
3033ee9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7902830
 
 
3033ee9
7902830
 
 
 
 
 
 
c81fd70
39ee51b
412551f
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import gradio as gr
import json
import io
import boto3
import base64
from PIL import Image

from settings_mgr import generate_download_settings_js, generate_upload_settings_js
from llm import LLM, log_to_console
from code_exec import eval_restricted_script
from botocore.config import Config

dump_controls = False

def dump(history):
    return str(history)

def load_settings():  
    # Dummy Python function, actual loading is done in JS  
    pass  

def save_settings(acc, sec, prompt, temp):  
    # Dummy Python function, actual saving is done in JS  
    pass  

def process_values_js():
    return """

    () => {

        return ["access_key", "secret_key", "token"];

    }

    """

def bot(message, history, aws_access, aws_secret, aws_token, system_prompt, temperature, max_tokens, model: str, region, python_use):
    try:
        llm = LLM.create_llm(model)
        messages = llm.generate_body(message, history)
        sys_prompt = [{"text": system_prompt}] if system_prompt else []

        config = Config(
            read_timeout = 600,
            connect_timeout = 30,
            retries = {'max_attempts': 10, 'mode': 'adaptive'}
        )

        tool_config = {
            "tools": [{
                "toolSpec": {
                    "name": "eval_python",
                    "description": "Evaluate a simple script written in a conservative, restricted subset of Python."
                                "Note: Augmented assignments, in-place operations (e.g., +=, -=), lambdas (e.g. list comprehensions) are not supported. "
                                "Use regular assignments and operations instead. Only 'import math' is allowed. "
                                "Returns: unquoted results without HTML encoding.",
                    "inputSchema": {
                        "json": {
                            "type": "object",
                            "properties": {
                                "script": {
                                    "type": "string",
                                    "description": "The Python script that will run in a RestrictedPython context. "
                                                "Avoid using augmented assignments or in-place operations (+=, -=, etc.), as well as lambdas (e.g. list comprehensions). "
                                                "Use regular assignments and operations instead. Only 'import math' is allowed."
                                }
                            },
                            "required": ["script"]
                        }
                    }
                }
            }]
        } if python_use else None

        sess = boto3.Session(
            aws_access_key_id = aws_access,
            aws_secret_access_key = aws_secret,
            aws_session_token = aws_token,
            region_name = region)
        br = sess.client(service_name="bedrock-runtime", config = config)

        whole_response = ""
        while True:
            response = br.converse_stream(
                modelId = model,
                messages = messages,
                system = sys_prompt,
                inferenceConfig = {
                    "temperature": temperature,
                    "maxTokens": max_tokens,
                },
                **({'toolConfig': tool_config} if python_use else {})
            )

            for stop_reason, message in llm.read_response(response.get('stream')):
                if isinstance(message, str):
                    whole_response += message
                    yield whole_response

                if stop_reason:
                    if stop_reason == "tool_use":
                        messages.append(message)

                        for content in message['content']:
                            if 'toolUse' in content:
                                tool = content['toolUse']

                                if tool['name'] == 'eval_python':
                                    tool_result = {}
                                    try:
                                        tool_script = tool["input"]["script"]

                                        whole_response += f"\n``` script\n{tool_script}\n```\n"
                                        yield whole_response

                                        tool_result = eval_restricted_script(tool_script)
                                        tool_result_message = {
                                            "role": "user",
                                            "content": [
                                                {
                                                    "toolResult": {
                                                        "toolUseId": tool['toolUseId'],
                                                        "content": [{"json": tool_result }]
                                                    }
                                                }
                                            ]
                                        }

                                        whole_response += f"\n``` result\n{tool_result if not tool_result['success'] else tool_result['prints']}\n```\n"
                                        yield whole_response
                                    except Exception as e:
                                        tool_result_message = {
                                            "role": "user",
                                            "content": [
                                                {
                                                    "toolResult": {
                                                        "content": [{"text":  e.args[0]}],
                                                        "status": 'error'
                                                    }
                                                }
                                            ]
                                        }
                                        whole_response += f"\n``` error\n{e.args[0]}\n```\n"
                                        yield whole_response

                                    messages.append(tool_result_message)
                    else:
                        return

    except Exception as e:
        raise gr.Error(f"Error: {str(e)}")

def import_history(history, file):
    with open(file.name, mode="rb") as f:
        content = f.read()

        if isinstance(content, bytes):
            content = content.decode('utf-8', 'replace')
        else:
            content = str(content)

    # Deserialize the JSON content
    import_data = json.loads(content)

    # Check if 'history' key exists for backward compatibility
    if 'history' in import_data:
        history = import_data['history']
        system_prompt_value = import_data.get('system_prompt', '')  # Set default if not present
    else:
        # Assume it's an old format with only history data
        history = import_data
        system_prompt_value = ''

    # Process the history to handle image data
    processed_history = []
    for pair in history:
        processed_pair = []
        for message in pair:
            if isinstance(message, dict) and 'file' in message and 'data' in message['file']:
                # Create a gradio.Image from the base64 data
                image_data = base64.b64decode(message['file']['data'].split(',')[1])
                img = Image.open(io.BytesIO(image_data))
                gr_image = gr.Image(img)
                processed_pair.append(gr_image)

                gr.Warning("Reusing images across sessions is limited to one conversation turn")
            else:
                processed_pair.append(message)
        processed_history.append(processed_pair)

    return processed_history, system_prompt_value

def export_history(h, s):
    pass

with gr.Blocks(delete_cache=(86400, 86400)) as demo:
    gr.Markdown("# Amazon™️ Bedrock™️ Chat™️ (Nils' Version™️) feat. Mistral™️ AI & Anthropic™️ Claude™️")

    with gr.Accordion("Startup"):
        gr.Markdown("""Use of this interface permitted under the terms and conditions of the 

                    [MIT license](https://github.com/ndurner/amz_bedrock_chat/blob/main/LICENSE).

                    Third party terms and conditions apply, particularly

                    those of the LLM vendor (AWS) and hosting provider (Hugging Face). This software and the AI models may make mistakes, so verify all outputs.""")
        
        aws_access = gr.Textbox(label="AWS Access Key", elem_id="aws_access")
        aws_secret = gr.Textbox(label="AWS Secret Key", elem_id="aws_secret")
        aws_token = gr.Textbox(label="AWS Session Token", elem_id="aws_token")
        model = gr.Dropdown(label="Model", value="anthropic.claude-3-5-sonnet-20241022-v2:0", allow_custom_value=True, elem_id="model",
                            choices=["anthropic.claude-3-5-sonnet-20240620-v1:0", "anthropic.claude-3-opus-20240229-v1:0", "meta.llama3-1-405b-instruct-v1:0", "anthropic.claude-3-sonnet-20240229-v1:0", "anthropic.claude-3-haiku-20240307-v1:0", "anthropic.claude-v2:1", "anthropic.claude-v2",
                                     "mistral.mistral-7b-instruct-v0:2", "mistral.mixtral-8x7b-instruct-v0:1", "mistral.mistral-large-2407-v1:0", "anthropic.claude-3-5-sonnet-20241022-v2:0"])
        system_prompt = gr.TextArea("You are a helpful yet diligent AI assistant. Answer faithfully and factually correct. Respond with 'I do not know' if uncertain.", label="System Prompt", lines=3, max_lines=250, elem_id="system_prompt")  
        region = gr.Dropdown(label="Region", value="us-west-2", allow_custom_value=True, elem_id="region",
                            choices=["eu-central-1", "eu-west-3", "us-east-1", "us-west-1", "us-west-2"])
        temp = gr.Slider(0, 1, label="Temperature", elem_id="temp", value=1)
        max_tokens = gr.Slider(1, 8192, label="Max. Tokens", elem_id="max_tokens", value=4096)
        python_use = gr.Checkbox(label="Python Use")
        save_button = gr.Button("Save Settings")  
        load_button = gr.Button("Load Settings")  
        dl_settings_button = gr.Button("Download Settings")
        ul_settings_button = gr.Button("Upload Settings")

        load_button.click(load_settings, js="""  

            () => {  

                let elems = ['#aws_access textarea', '#aws_secret textarea', '#aws_token textarea', '#system_prompt textarea', '#temp input', '#max_tokens input', '#model', '#region'];

                elems.forEach(elem => {

                    let item = document.querySelector(elem);

                    let event = new InputEvent('input', { bubbles: true });

                    item.value = localStorage.getItem(elem.split(" ")[0].slice(1)) || '';

                    item.dispatchEvent(event);

                });

            }  

        """)

        save_button.click(save_settings, [aws_access, aws_secret, aws_token, system_prompt, temp, max_tokens, model, region], js="""  

            (acc, sec, tok, system_prompt, temp, ntok, model, region) => {  

                localStorage.setItem('aws_access', acc);  

                localStorage.setItem('aws_secret', sec);  

                localStorage.setItem('aws_token', tok);  

                localStorage.setItem('system_prompt', system_prompt);

                localStorage.setItem('temp', document.querySelector('#temp input').value);  

                localStorage.setItem('max_tokens', document.querySelector('#max_tokens input').value);  

                localStorage.setItem('model', model);  

                localStorage.setItem('region', region);  

            }  

        """) 

        control_ids = [('aws_access', '#aws_access textarea'),
                       ('aws_secret', '#aws_secret textarea'),
                       ('aws_token', '#aws_token textarea'),
                       ('system_prompt', '#system_prompt textarea'),
                       ('temp', '#temp input'),
                       ('max_tokens', '#max_tokens input'),
                       ('model', '#model'),
                       ('region', '#region')]
        controls = [aws_access, aws_secret, aws_token, system_prompt, temp, max_tokens, model, region, python_use]

        dl_settings_button.click(None, controls, js=generate_download_settings_js("amz_chat_settings.bin", control_ids))
        ul_settings_button.click(None, None, None, js=generate_upload_settings_js(control_ids))

    chat = gr.ChatInterface(fn=bot, multimodal=True, additional_inputs=controls, autofocus = False)
    chat.textbox.file_count = "multiple"
    chatbot = chat.chatbot
    chatbot.show_copy_button = True
    chatbot.height = 450

    if dump_controls:
        with gr.Row():
            dmp_btn = gr.Button("Dump")
            txt_dmp = gr.Textbox("Dump")
            dmp_btn.click(dump, inputs=[chatbot], outputs=[txt_dmp])

    with gr.Accordion("Import/Export", open = False):
        import_button = gr.UploadButton("History Import")
        export_button = gr.Button("History Export")
        export_button.click(export_history, [chatbot, system_prompt], js="""

            async (chat_history, system_prompt) => {

                console.log('Chat History:', JSON.stringify(chat_history, null, 2));



                async function fetchAndEncodeImage(url) {

                    const response = await fetch(url);

                    const blob = await response.blob();

                    return new Promise((resolve, reject) => {

                        const reader = new FileReader();

                        reader.onloadend = () => resolve(reader.result);

                        reader.onerror = reject;

                        reader.readAsDataURL(blob);

                    });

                }



                const processedHistory = await Promise.all(chat_history.map(async (pair) => {

                    return await Promise.all(pair.map(async (message) => {

                        if (message && message.file && message.file.url) {

                            const base64Image = await fetchAndEncodeImage(message.file.url);

                            return {

                                ...message,

                                file: {

                                    ...message.file,

                                    data: base64Image

                                }

                            };

                        }

                        return message;

                    }));

                }));



                const export_data = {

                    history: processedHistory,

                    system_prompt: system_prompt

                };

                const history_json = JSON.stringify(export_data);

                const blob = new Blob([history_json], {type: 'application/json'});

                const url = URL.createObjectURL(blob);

                const a = document.createElement('a');

                a.href = url;

                a.download = 'chat_history.json';

                document.body.appendChild(a);

                a.click();

                document.body.removeChild(a);

                URL.revokeObjectURL(url);

            }

        """)
        dl_button = gr.Button("File download")
        dl_button.click(lambda: None, [chatbot], js="""

            (chat_history) => {

                // Only define exception mappings

                const languageToExt = {

                    'python': 'py',

                    'javascript': 'js',

                    'typescript': 'ts',

                    'csharp': 'cs',

                    'ruby': 'rb',

                    'shell': 'sh',

                    'bash': 'sh',

                    'markdown': 'md',

                    'yaml': 'yml',

                    'rust': 'rs',

                    'golang': 'go',

                    'kotlin': 'kt'

                };



                const contentRegex = /```(?:([^\\n]+)?\\n)?([\\s\\S]*?)```/;

                const match = contentRegex.exec(chat_history[chat_history.length - 1][1]);

                

                if (match && match[2]) {

                    const specifier = match[1] ? match[1].trim() : '';

                    const content = match[2];

                    

                    let filename = 'download';

                    let fileExtension = 'txt'; // default



                    if (specifier) {

                        if (specifier.includes('.')) {

                            // If specifier contains a dot, treat it as a filename

                            const parts = specifier.split('.');

                            filename = parts[0];

                            fileExtension = parts[1];

                        } else {

                            // Use mapping if exists, otherwise use specifier itself

                            const langLower = specifier.toLowerCase();

                            fileExtension = languageToExt[langLower] || langLower;

                            filename = 'code';

                        }

                    }



                    const blob = new Blob([content], {type: 'text/plain'});

                    const url = URL.createObjectURL(blob);

                    const a = document.createElement('a');

                    a.href = url;

                    a.download = `${filename}.${fileExtension}`;

                    document.body.appendChild(a);

                    a.click();

                    document.body.removeChild(a);

                    URL.revokeObjectURL(url);

                }

            }

        """)
        import_button.upload(import_history, inputs=[chatbot, import_button], outputs=[chatbot, system_prompt])

demo.queue().launch()