-
Notifications
You must be signed in to change notification settings - Fork 0
/
Anthropic_Claude.py
1102 lines (900 loc) · 45.9 KB
/
Anthropic_Claude.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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import wx
import wx.stc as stc
import wx.lib.agw.aui as aui
import time, glob,threading, traceback
#import os, openai
from pubsub import pub
from pprint import pprint as pp
from include.Common import *
from include.fmt import fmt
from include.Copilot.Base.Base_InputPanel_Anthropic_Claude import Base_InputPanel_Anthropic_Claude
import include.config.init_config as init_config
apc = init_config.apc
default_chat_template='SYSTEM'
default_copilot_template='SYSTEM_CHATTY'
#DEFAULT_MODEL = 'gpt-4o'
DEFAULT_MODEL='claude-3-haiku-20240307'
model_list=['claude-3-5-sonnet-20240620','claude-3-opus-20240229',
'claude-3-sonnet-20240229','claude-3-haiku-20240307']
dir_path = 'template'
#openai.api_key = os.getenv("OPENAI_API_KEY")
chatHistory, currentQuestion, currentModel = apc.chatHistory, apc.currentQuestion, apc.currentModel
questionHistory= apc.questionHistory
all_templates, all_chats, all_system_templates = apc.all_templates, apc.all_chats, apc.all_system_templates
panels = AttrDict(dict(workspace='WorkspacePanel', vendor='ChatDisplayNotebookPanel',chat='DisplayPanel', input='InputPanel'))
class History_ResponseStreamer:
subscribed=False
def __init__(self):
# Set your OpenAI API key here
self.model={}
self.chat_history={}
def stream_response(self, text_prompt, chatHistory, receiveing_tab_id):
# Create a chat completion request with streaming enabled
if receiveing_tab_id not in self.chat_history:
self.chat_history[receiveing_tab_id]=[]
chat_history=self.chat_history[receiveing_tab_id]
out=[]
from os.path import isfile
chat=apc.chats[receiveing_tab_id]
txt='\n'.join(split_text_into_chunks(text_prompt,80))
header = fmt([[f'{txt}Answer:\n']],['Question | '+chat.model])
pub.sendMessage('chat_output', message=f'{header}\n', tab_id=receiveing_tab_id)
try:
import base64
from anthropic import Anthropic
client = Anthropic()
#b_data=get_base64_encoded_image("test.jpeg")
content = []
prompt=text_prompt
content.append({"type": "text", "text": prompt})
message_list = chat_history + [
{
"role": 'user',
"content": content
}
]
pp(chat)
stream = client.messages.create(
model=chat.model,
max_tokens=int(chat.max_tokens),
temperature=float(chat.temperature),
top_p=float(chat.top_p),
stop_sequences=["Human:", "User:", "Assistant:", "AI:"],
#system="You have perfect artistic sense and pay great attention to detail which makes you an expert at describing images.",
system=chat.system_prompt,
messages=message_list,
stream=True
)
#print(response.content[0].text)
msg=[]
for chunk in stream:
#pp(chunk)
if chunk.type == 'content_block_delta':
text = chunk.delta.text
print(text, end='', flush=True)
out.append(text)
msg.append(text)
pub.sendMessage('chat_output', message=f'{text}', tab_id=receiveing_tab_id)
assistant_message = {
"role": "assistant",
"content": ''.join(msg)
}
chat_history.append(message_list[-1]) # Add user's message to history
chat_history.append(assistant_message) # Add assistant's response to history
#prompt2 = "Now, can you tell me about the color palette used in this artwork?"
except Exception as e:
log(f'Error in stream_response', 'red')
log(format_stacktrace(), 'red')
print(f"An error occurred: {e}")
raise
#return ''
if out:
pub.sendMessage('chat_output', message=f'\n', tab_id=receiveing_tab_id)
return ''.join(out)
class Claude_Anthropic_Copilot_DisplayPanel(wx.Panel):
def __init__(self, parent, tab_id, chat):
super(Claude_Anthropic_Copilot_DisplayPanel, self).__init__(parent)
apc.chats[tab_id]=chat
# Create a splitter window
self.copilot_splitter = wx.SplitterWindow(self, style=wx.SP_LIVE_UPDATE)
#splitter = wx.SplitterWindow(self, style = wx.SP_3D| wx.SP_LIVE_UPDATE)
self.tab_id=tab_id
# Initialize the notebook_panel and logPanel
self.notebook_panel=notebook_panel = MyNotebookCodePanel(self.copilot_splitter, tab_id)
notebook_panel.SetMinSize((-1, 50))
#notebook_panel.SetMinSize((800, -1))
self.chatPanel = Copilot_DisplayPanel(self.copilot_splitter, tab_id)
self.chatPanel.SetMinSize((-1, 50))
# Add notebook panel and log panel to the splitter window
#self.splitter.AppendWindow(notebook_panel)
#self.splitter.AppendWindow(self.logPanel)
self.copilot_splitter.SplitVertically( self.chatPanel, notebook_panel)
#print(111, self.GetSize().GetWidth() // 2)
self.copilot_splitter.SetSashPosition(500)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.copilot_splitter, 1, wx.EXPAND)
self.SetSizer(sizer)
# Set initial sash positions
#
self.Bind(wx.EVT_SIZE, self.OnResize)
def GetCode(self, tab_id):
assert tab_id==self.tab_id, self.__class__.__name__
return self.notebook_panel.codeCtrl.GetText()
def OnResize(self, event):
# Adjust the sash position to keep the vertical splitter size constant
width, height = self.GetSize()
self.copilot_splitter.SetSashPosition(width // 2)
event.Skip()
class StyledTextDisplay(stc.StyledTextCtrl, GetClassName, NewChat, Scroll_Handlet):
def __init__(self, parent):
super(StyledTextDisplay, self).__init__(parent, style=wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_WORDWRAP)
GetClassName.__init__(self)
NewChat.__init__(self)
Scroll_Handlet.__init__(self)
#self.Bind(wx.EVT_CHAR_HOOK, self.OnCharHook)
self.SetWrapMode(stc.STC_WRAP_WORD)
self.SetLexer(stc.STC_LEX_PYTHON)
text_keywords = 'nude Ukraine Ukrainian Tryzub flag blue yellow picture model image file artist artistic artistically color light scene question answer description mood texture emotion feeling sense impression atmosphere tone style technique brushstroke composition perspective'
self.StyleSetSpec(stc.STC_P_DEFAULT, "fore:#000000,back:#FFFFFF") # Default
self.StyleSetSpec(stc.STC_P_NUMBER, "fore:#FF8C00,back:#FFFFFF") # Number
#self.StyleSetSpec(stc.STC_P_STRING, "fore:#FF0000,back:#FFFFFF") # String
#self.StyleSetSpec(stc.STC_P_CHARACTER, "fore:#FF0000,back:#FFFFFF") # Character
self.StyleSetSpec(stc.STC_P_WORD, "fore:#0000FF,back:#FFFFFF,weight:bold")
self.StyleSetSpec(stc.STC_P_TRIPLE, "fore:#FF0000,back:#FFFFFF") # Triple quotes
self.StyleSetSpec(stc.STC_P_TRIPLEDOUBLE, "fore:#FF0000,back:#FFFFFF") # Triple double quotes
self.StyleSetSpec(stc.STC_P_IDENTIFIER, "fore:#000000,back:#FFFFFF") # Identifiers
self.StyleSetSpec(stc.STC_STYLE_DEFAULT, 'face:Courier New')
self.SetKeyWords(0, text_keywords)
# Set face
self.StyleSetSpec(stc.STC_STYLE_DEFAULT, 'face:Courier New')
#pub.subscribe(self.AddOutput, 'chat_output')
self.Bind(wx.EVT_SIZE, self.OnResize)
def OnResize(self, event):
self.Refresh()
event.Skip()
def _OnCharHook(self, event):
if event.ControlDown() and (event.GetKeyCode() == ord('A') or event.GetKeyCode() == wx.WXK_RETURN):
self.AskQuestion()
else:
event.Skip()
def AskQuestion(self):
pass # Implement if needed
def IsTextInvisible(self):
last_position = self.GetTextLength()
line_number = self.LineFromPosition(last_position)
first_visible_line = self.GetFirstVisibleLine()
lines_on_screen = self.LinesOnScreen()
return not (first_visible_line <= line_number < first_visible_line + lines_on_screen)
def AddOutput(self, message):
wx.CallAfter(self._AddOutput, message)
def _AddOutput(self, message):
self.AppendText(message)
if self.IsTextInvisible():
if self.scrolled:
#self.answer_output.MakeCellVisible(i, 0)
self.GotoPos(self.GetTextLength())
class code_StyledTextDisplay(stc.StyledTextCtrl, GetClassName, NewChat, Scroll_Handlet):
def __init__(self, parent):
super(code_StyledTextDisplay, self).__init__(parent, style=wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_WORDWRAP)
GetClassName.__init__(self)
NewChat.__init__(self)
Scroll_Handlet.__init__(self)
#self.Bind(wx.EVT_CHAR_HOOK, self.OnCharHook)
self.SetLexer(stc.STC_LEX_PYTHON)
self.SetWrapMode(stc.STC_WRAP_WORD)
python_keywords = 'self False None True and as assert async await break class continue def del elif else except finally for from global if import in is lambda nonlocal not or pass raise return try while with both yield'
self.SetKeyWords(0, python_keywords)
# Set Python styles
self.StyleSetSpec(stc.STC_P_DEFAULT, "fore:#000000,back:#FFFFFF") # Default
self.StyleSetSpec(stc.STC_P_COMMENTLINE, "fore:#008000,back:#FFFFFF") # Comment
self.StyleSetSpec(stc.STC_P_NUMBER, "fore:#FF8C00,back:#FFFFFF") # Number
self.StyleSetSpec(stc.STC_P_STRING, "fore:#FF0000,back:#FFFFFF") # String
self.StyleSetSpec(stc.STC_P_CHARACTER, "fore:#FF0000,back:#FFFFFF") # Character
self.StyleSetSpec(stc.STC_P_WORD, "fore:#0000FF,back:#FFFFFF,weight:bold")
self.StyleSetSpec(stc.STC_P_TRIPLE, "fore:#FF0000,back:#FFFFFF") # Triple quotes
self.StyleSetSpec(stc.STC_P_TRIPLEDOUBLE, "fore:#FF0000,back:#FFFFFF") # Triple double quotes
self.StyleSetSpec(stc.STC_P_CLASSNAME, "fore:#00008B,back:#FFFFFF") # Class name
self.StyleSetSpec(stc.STC_P_DEFNAME, "fore:#00008B,back:#FFFFFF") # Function or method name
self.StyleSetSpec(stc.STC_P_OPERATOR, "fore:#000000,back:#FFFFFF") # Operators
self.StyleSetSpec(stc.STC_P_IDENTIFIER, "fore:#000000,back:#FFFFFF") # Identifiers
self.StyleSetSpec(stc.STC_STYLE_DEFAULT, 'face:Courier New')
# Set face
self.StyleSetSpec(stc.STC_STYLE_DEFAULT, 'face:Courier New')
#pub.subscribe(self.AddOutput, 'chat_output')
self.Bind(wx.EVT_SIZE, self.OnResize)
def OnResize(self, event):
self.Refresh()
event.Skip()
def _OnCharHook(self, event):
if event.ControlDown() and (event.GetKeyCode() == ord('A') or event.GetKeyCode() == wx.WXK_RETURN):
self.AskQuestion()
else:
event.Skip()
def AskQuestion(self):
pass # Implement if needed
def IsTextInvisible(self):
last_position = self.GetTextLength()
line_number = self.LineFromPosition(last_position)
first_visible_line = self.GetFirstVisibleLine()
lines_on_screen = self.LinesOnScreen()
return not (first_visible_line <= line_number < first_visible_line + lines_on_screen)
def AddOutput(self, message):
wx.CallAfter(self._AddOutput, message)
def _AddOutput(self, message):
self.AppendText(message)
if self.IsTextInvisible():
if self.scrolled:
#self.answer_output.MakeCellVisible(i, 0)
self.GotoPos(self.GetTextLength())
class Claude_Anthropic_Chat_DisplayPanel(code_StyledTextDisplay):
def __init__(self, parent, tab_id, chat):
code_StyledTextDisplay.__init__(self,parent)
font = wx.Font(10, wx.FONTFAMILY_TELETYPE, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL)
self.SetFont(font)
self.tab_id=tab_id
pub.subscribe(self.AddChatOutput, 'chat_output')
#pub.subscribe(lambda message, tab_id: self.AddOutput(message, tab_id), 'chat_output')
pub.subscribe(self.OnShowTabId, 'show_tab_id')
def IsTabVisible(self):
# Get the parent notebook
parent_notebook = self.GetParent()
# Check if the current page is the selected page in the parent notebook
return parent_notebook.GetPage(parent_notebook.GetSelection()) == self
def AddChatOutput(self, message, tab_id):
#print(1111, self.tab_id,tab_id, self.tab_id==tab_id, message)
#print('Chat', tab_id, self.IsTabVisible())
if self.tab_id==tab_id:
#start_pos = self.GetLastPosition()
if 1: #for line in message.splitlines():
wx.CallAfter(self.AddOutput, message)
#end_pos = self.chatDisplay.GetLastPosition()
#self.chatDisplay.SetStyle(start_pos, end_pos, wx.TextAttr(wx.BLACK))
def OnShowTabId(self):
print('show_tab_id', self.tab_id)
class Claude_Anthropic_ChatDisplayNotebookPanel(wx.Panel):
def __init__(self, parent, vendor_tab_id, ws_name):
super(Claude_Anthropic_ChatDisplayNotebookPanel, self).__init__(parent)
self.tabs={}
self.ws_name=ws_name
self.chat_notebook = wx.Notebook(self, style=wx.NB_BOTTOM)
self.vendor_tab_id=vendor_tab_id
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.chat_notebook, 1, wx.EXPAND)
#self.chat_notebook.SetActiveTabColour(wx.RED)
#self.chat_notebook.SetNonActiveTabTextColour(wx.BLUE)
self.SetSizer(sizer)
self.chat_notebook.Bind(wx.EVT_MOTION, self.OnMouseMotion)
self.chat_notebook.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGING, self.OnPageChanging)
self.chat_notebook.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.OnPageChanged)
pub.subscribe(self.OnWorkspaceTabChanging, 'workspace_tab_changing')
pub.subscribe(self.OnWorkspaceTabChanged, 'workspace_tab_changed')
pub.subscribe(self.OnVendorspaceTabChanging, 'vendor_tab_changing')
pub.subscribe(self.OnVendorspaceTabChanged, 'vendor_tab_changed')
def get_active_chat_panel(self):
active_chat_tab_index = self.chat_notebook.GetSelection()
if active_chat_tab_index == wx.NOT_FOUND:
return None
return self.chat_notebook.GetPage(active_chat_tab_index)
def OnWorkspaceTabChanging(self, message):
print(self.__class__.__name__)
print('OnWorkspaceTabChanging', message, self.ws_name)
if message==self.ws_name:
active_chat_panel = self.get_active_chat_panel()
if active_chat_panel is not None:
active_tab_id = active_chat_panel.tab_id
#print('OnWorkspaceTabChanging dd', message, self.vendor_tab_id, self.ws_name, active_tab_id)
pub.sendMessage('save_question_for_tab_id', message=active_tab_id)
else:
print('No active chat panel found')
def OnWorkspaceTabChanged(self, message):
if message==self.ws_name:
active_chat_panel = self.get_active_chat_panel()
if active_chat_panel is not None:
active_tab_id = active_chat_panel.tab_id
#print('OnWorkspaceTabChanged', message, self.vendor_tab_id, self.ws_name, active_tab_id)
if 1:
#pub.sendMessage('restore_question_for_tab_id', tab_id=active_tab_id)
assert active_tab_id in apc.chats
chat=apc.chats[active_tab_id]
print('swapping', active_tab_id )
pub.sendMessage('swap_input_panel', tab_id=active_tab_id)
def OnVendorspaceTabChanging(self, message):
#print('TODO OnVendorspaceTabChanging', message)
#raise NotImplementedError
pass
def OnVendorspaceTabChanged(self, message):
#print('TODO OnVendorspaceTabChanged', message)
#raise NotImplementedError
pass
def OnMouseMotion(self, event):
# Get the mouse position
position = event.GetPosition()
# Get the tab index under the mouse position
#print(self.notebook.HitTest(position))
tab_index, _= self.chat_notebook.HitTest(position)
#print(tab_index)
# If the mouse is over a tab
if tab_index >= 0:
# Get the tab text
tab_text = self.chat_notebook.GetPageText(tab_index)
# Set the tab tooltip
tt=self.GetToolTipText()
self.chat_notebook.SetToolTip(f'{tt}/{tab_text}')
else:
self.chat_notebook.SetToolTip(None)
event.Skip()
def GetToolTipText(self):
tab_id=self.tabs[self.chat_notebook.GetSelection()]
return f'{apc.default_workspace.name}/{apc.default_workspace.vendor} {apc.chats[tab_id].chat_type}'
def AddTab(self, chat):
chat_notebook=self.chat_notebook
title=f'{chat.chat_type}: {chat.name}'
title=f'{chat.name}'
chatDisplay=None
tab_id=(chat.workspace, chat.chat_type, chat.vendor,self.vendor_tab_id, chat_notebook.GetPageCount())
self.tabs[chat_notebook.GetPageCount()]=tab_id
if 1:
#pp(panels.__dict__)
#pp(chat.__dict__)
display_panel = f'{chat.vendor}_{chat.workspace}_{chat.chat_type}_{panels.chat}'
#print('display_panel', display_panel)
try:
assert display_panel in globals(), display_panel
print(f'\t\tAdding {chat.workspace} "{chat.chat_type}" panel:', display_panel)
cls= globals()[display_panel]
# Gpt4_Chat_DisplayPanel/ Gpt4_Copilot_DisplayPanel
chatDisplay = cls (chat_notebook, tab_id=tab_id, chat=chat)
#chatDisplay.SetFocus()
if 1:
apc.chats[tab_id]=chat
pub.sendMessage('swap_input_panel', tab_id=tab_id)
except AssertionError:
#raise AssertionError(f"Display class '{display_panel}' does not exist.")
raise
assert chatDisplay
chat_notebook.AddPage(chatDisplay, title)
chat_notebook.SetSelection(chat_notebook.GetPageCount() - 1)
chat_tab_id = chat_notebook.GetPageCount() - 1
#self.SetTabLabelColor(chat_tab_id, wx.Colour(255, 0, 0))
chatDisplay.tab_id=self.tab_id=tab_id=(chat.workspace,chat.chat_type, chat.vendor, self.vendor_tab_id, chat_tab_id)
apc.chats[tab_id]=chat
apc.chat_panels[tab_id]=chatDisplay
pub.sendMessage('set_chat_defaults', tab_id=tab_id)
def OnPageChanging(self, event):
# Code to execute when the notebook page is about to be changed
#print("Notebook page is about to be changed")
# Get the index of the new tab that is about to be selected
nb=event.GetEventObject()
oldTabIndex = event.GetSelection()
current_chatDisplay = nb.GetPage(oldTabIndex)
#print('OnPageChanging 111', current_chatDisplay.tab_id)
pub.sendMessage('save_question_for_tab_id', message=current_chatDisplay.tab_id)
event.Skip()
def OnPageChanged(self, event):
# Code to execute when the notebook page has been changed
nb=event.GetEventObject()
newtabIndex = nb.GetSelection()
current_chatDisplay = nb.GetPage(newtabIndex)
tab_id=current_chatDisplay.tab_id
#print('OnPageChanged 222', tab_id)
#pub.sendMessage('restore_question_for_tab_id', tab_id=tab_id)
current_chatDisplay = nb.GetPage(newtabIndex)
#pp(current_chatDisplay.tab_id)
#e()
if tab_id in apc.chats:
chat=apc.chats[tab_id]
pub.sendMessage('swap_input_panel', tab_id=tab_id)
# Continue processing the event
event.Skip()
def get_latest_chat_tab_id(self):
return self.GetPageCount() - 1
class Claude_Anthropic_Copilot_InputPanel(wx.Panel, NewChat, GetClassName, Base_InputPanel_Anthropic_Claude):
def __init__(self, parent, tab_id):
global chatHistory, currentQuestion, currentModel
super(Claude_Anthropic_Copilot_InputPanel, self).__init__(parent)
NewChat.__init__(self)
GetClassName.__init__(self)
self.tabs={}
self.tab_id=tab_id
chat= apc.chats[tab_id]
self.chat_type=chat.chat_type
chatHistory[self.tab_id]=[]
chatHistory[self.tab_id]= [{"role": "system", "content": all_system_templates[chat.workspace].Copilot[default_copilot_template]}]
self.askLabel = wx.StaticText(self, label=f'Ask copilot {tab_id}:')
self.model_dropdown = wx.ComboBox(self, choices=model_list, style=wx.CB_READONLY)
self.model_dropdown.SetValue(chat.model_name)
self.model_dropdown.Bind(wx.EVT_COMBOBOX, self.OnModelChange)
self.askButton = wx.Button(self, label='Ask')
self.askButton.Bind(wx.EVT_BUTTON, self.onAskButton)
askSizer = wx.BoxSizer(wx.HORIZONTAL)
askSizer.Add(self.askLabel, 0, wx.ALIGN_CENTER)
askSizer.Add(self.model_dropdown, 0, wx.ALIGN_CENTER)
self.pause_panel=pause_panel=PausePanel(self, self.tab_id)
askSizer.Add(pause_panel, 0, wx.ALL)
Base_InputPanel_Anthropic_Claude.AddButtons_Level_1(self, askSizer)
askSizer.Add(self.askButton, 0, wx.ALIGN_CENTER)
self.inputCtrl = wx.TextCtrl(self, style=wx.TE_PROCESS_ENTER | wx.TE_MULTILINE)
if 1:
q= apc.chats[self.tab_id].question
self.tabs[self.tab_id]=dict(q=q)
questionHistory[self.tab_id]=[q]
currentQuestion[self.tab_id]=0
currentModel[self.tab_id]=chat.model_name
chatHistory[self.tab_id]= [{"role": "system", "content": chat.system}]
self.inputCtrl.SetValue(self.tabs[self.tab_id]['q'])
#self.inputCtrl.SetMinSize((-1, 120))
self.inputCtrl.Bind(wx.EVT_CHAR_HOOK, self.OnCharHook)
sizer = wx.BoxSizer(wx.VERTICAL)
h_sizer = wx.BoxSizer(wx.HORIZONTAL)
Base_InputPanel_Anthropic_Claude.AddButtons_Level_2(self, h_sizer)
sizer.Add(askSizer, 0, wx.ALIGN_CENTER)
sizer.Add(h_sizer, 0, wx.ALIGN_LEFT)
sizer.Add(self.inputCtrl, 1, wx.EXPAND)
self.SetSizer(sizer)
self.ex=None
self.receiveing_tab_id=0
#pub.subscribe(self.SetException, 'fix_exception')
pub.subscribe(self.SetChatDefaults , 'set_chat_defaults')
#pub.subscribe(self.SaveQuestionForTabId , 'save_question_for_tab_id')
#pub.subscribe(self.RestoreQuestionForTabId , 'restore_question_for_tab_id')
wx.CallAfter(self.inputCtrl.SetFocus)
self.rs={}
def SetTabId(self, tab_id):
self.tab_id=tab_id
self.askLabel.SetLabel(f'Ask copilot {tab_id}:')
def SetChatDefaults(self, tab_id):
global chatHistory, questionHistory, currentModel
if tab_id ==self.tab_id:
assert self.chat_type==tab_id[1]
chat=apc.chats[tab_id]
self.tabs[self.tab_id]=dict(q=chat.question)
chatHistory[self.tab_id]= [{"role": "system", "content": chat.system}]
questionHistory[self.tab_id]=[]
currentModel[self.tab_id]=chat.model_name
def OnModelChange(self, event):
# Get the selected model
selected_model = self.model_dropdown.GetValue()
chat=apc.chats[self.tab_id]
chat.model=selected_model
# Print the selected model
#print(f"Selected model: {selected_model}")
# You can add more code here to do something with the selected model
# Continue processing the event
event.Skip()
def _SaveQuestionForTabId(self, message):
global currentModel
q=self.inputCtrl.GetValue()
self.tabs[message]=dict(q=q)
currentModel[message]=self.model_dropdown.GetValue()
if 0:
d={"role": "user", "content":q}
if self.tab_id in chatHistory:
if d not in chatHistory[self.tab_id]:
chatHistory[self.tab_id] += [{"role": "user", "content":q}]
def SetException(self, message):
self.ex=message
def onAskButton(self, event):
# Code to execute when the Ask button is clicked
#print('Ask button clicked')
self.AskQuestion()
def AskQuestion(self):
global chatHistory, questionHistory, currentQuestion,currentModel
# Get the content of the StyledTextCtrl
#print('current tab_id', self.q_tab_id)
#pub.sendMessage('show_tab_id')
self.Base_OnAskQuestion()
question = self.inputCtrl.GetValue()
if not question:
self.log('There is no question!', color=wx.RED)
else:
question = self.inputCtrl.GetValue()
self.log(f'Asking question: {question}')
pub.sendMessage('start_progress')
#code=???
chatDisplay=apc.chat_panels[self.tab_id]
code=chatDisplay.GetCode(self.tab_id)
#print(888, chatDisplay.__class__.__name__)
#code='print(1223)'
chat=apc.chats[self.tab_id]
prompt=evaluate(all_system_templates[chat.workspace].Copilot.FIX_CODE, AttrDict(dict(code=code, input=question)))
chatHistory[self.tab_id] += [{"role": "user", "content": prompt}]
questionHistory[self.tab_id].append(question)
currentQuestion[self.tab_id]=len(questionHistory[self.tab_id])-1
currentModel[self.tab_id]=self.model_dropdown.GetValue()
header=fmt([['\n'.join(self.split_text_into_chunks(question, 80))+'\nAnswer:']], [f'User Question | {chat.model}'])
# DO NOT REMOVE THIS LINE
print(header)
pub.sendMessage('chat_output', message=f'{header}\n', tab_id=self.tab_id)
#pub.sendMessage('chat_output', message=f'{prompt}\n')
#out=rs.stream_response(prompt, chatHistory[self.q_tab_id])
chat.question=question
if 'system_prompt' not in chat:
system= chat.get('system', 'SYSTEM')
chat.question=question
chat.system_prompt=evaluate(all_system_templates[chat.workspace].Copilot[system], dict2(question=chat.question))
pub.sendMessage('set_system_prompt', message=chat.system_prompt, tab_id=self.tab_id)
else:
pub.sendMessage('set_system_prompt', message=chat.system_prompt, tab_id=self.tab_id)
threading.Thread(target=self.stream_response, args=(prompt, chatHistory, self.tab_id, self.model_dropdown.GetValue())).start()
def stream_response(self, prompt, chatHistory, tab_id, model):
# Call stream_response and store the result in out
self.receiveing_tab_id=tab_id
chat=apc.chats[tab_id]
rs = self.get_chat_streamer(tab_id,globals())
#rs=ResponseStreamer()
out = rs.stream_response(prompt, chatHistory[tab_id], self.receiveing_tab_id)
if out:
chatHistory[tab_id].append({"role": "assistant", "content": out})
pub.sendMessage('stop_progress')
log('Done.')
set_status('Done.')
def PrevQuestion(self):
qid=currentQuestion[self.tab_id]
if qid:
q=questionHistory[self.tab_id][qid-1]
self.inputCtrl.SetValue(q)
self.inputCtrl.SetFocus()
currentQuestion[self.tab_id]=qid-1
else:
self.log('No previous question.', color=wx.RED)
def NextQuestion(self):
qid=currentQuestion[self.tab_id]
if len(questionHistory[self.tab_id])>qid+1:
q=questionHistory[self.tab_id][qid+1]
self.inputCtrl.SetValue(q)
self.inputCtrl.SetFocus()
currentQuestion[self.tab_id]=qid+1
else:
self.log('No next question.', color=wx.RED)
def OnCharHook(self, event):
if event.ControlDown() and event.GetKeyCode() == wx.WXK_RETURN:
self.AskQuestion()
elif event.ControlDown() and event.GetKeyCode() == wx.WXK_RIGHT:
log("Ctrl+-> pressed")
set_status("Ctrl+-> pressed")
self.NextQuestion()
elif event.ControlDown() and event.GetKeyCode() == wx.WXK_LEFT:
self.log("Ctrl+<- pressed")
set_status("Ctrl+<- pressed")
self.PrevQuestion()
else:
event.Skip()
def log(self, message, color=wx.BLUE):
pub.sendMessage('log', message=f'{message}', color=color)
class MyNotebookCodePanel(wx.Panel):
def __init__(self, parent, tab_id):
super(MyNotebookCodePanel, self).__init__(parent)
notebook = aui.AuiNotebook(self)
self.notebook = notebook
self.codeCtrl = stc.StyledTextCtrl(notebook)
self.codeCtrl.SetMarginType(0, stc.STC_MARGIN_NUMBER)
self.codeCtrl.SetMarginWidth(0, self.codeCtrl.TextWidth(stc.STC_STYLE_LINENUMBER, '100'))
self.codeCtrl.StyleSetForeground(stc.STC_STYLE_LINENUMBER, wx.Colour(75, 75, 75))
self.codeCtrl.Bind(wx.EVT_CHAR_HOOK, self.OnCharHook)
self.codeCtrl.SetLexer(stc.STC_LEX_PYTHON)
python_keywords = 'self False None True and as assert async await break class continue def del elif else except finally for from global if import in is lambda nonlocal not or pass raise return try while with both yield'
self.codeCtrl.SetKeyWords(0, python_keywords)
# Set Python styles
self.codeCtrl.StyleSetSpec(stc.STC_P_DEFAULT, 'fore:#000000')
self.codeCtrl.StyleSetSpec(stc.STC_P_COMMENTLINE, 'fore:#008000')
self.codeCtrl.StyleSetSpec(stc.STC_P_NUMBER, 'fore:#008080')
self.codeCtrl.StyleSetSpec(stc.STC_P_STRING, 'fore:#008000')
self.codeCtrl.StyleSetSpec(stc.STC_P_CHARACTER, 'fore:#008000')
self.codeCtrl.StyleSetSpec(stc.STC_P_WORD, 'fore:#000080,bold')
self.codeCtrl.StyleSetSpec(stc.STC_P_TRIPLE, 'fore:#008000')
self.codeCtrl.StyleSetSpec(stc.STC_P_TRIPLEDOUBLE, 'fore:#008000')
self.codeCtrl.StyleSetSpec(stc.STC_P_CLASSNAME, 'fore:#0000FF,,weight:bold')
self.codeCtrl.StyleSetSpec(stc.STC_P_DEFNAME, 'fore:#008080,,weight:bold')
self.codeCtrl.StyleSetSpec(stc.STC_P_OPERATOR, 'fore:#000000')
self.codeCtrl.StyleSetSpec(stc.STC_P_IDENTIFIER, 'fore:#0000FF')
self.codeCtrl.StyleSetSpec(stc.STC_P_COMMENTBLOCK, 'fore:#008000')
self.codeCtrl.StyleSetSpec(stc.STC_P_STRINGEOL, 'fore:#008000,back:#E0C0E0,eol')
self.codeCtrl.StyleSetSpec(stc.STC_P_DECORATOR, 'fore:#805000')
self.codeCtrl.StyleSetSpec(stc.STC_P_WORD2, 'fore:#800080,bold')
# Set Python styles
self.codeCtrl.StyleSetSpec(stc.STC_P_DEFAULT, "fore:#000000,back:#FFFFFF")
self.codeCtrl.StyleSetSpec(stc.STC_P_COMMENTLINE, "fore:#008000,back:#FFFFFF")
self.codeCtrl.StyleSetSpec(stc.STC_P_NUMBER, "fore:#FF8C00,back:#FFFFFF")
self.codeCtrl.StyleSetSpec(stc.STC_P_STRING, "fore:#FF0000,back:#FFFFFF")
self.codeCtrl.StyleSetSpec(stc.STC_P_CHARACTER, "fore:#FF0000,back:#FFFFFF")
self.codeCtrl.StyleSetSpec(stc.STC_P_WORD, "fore:#0000FF,back:#FFFFFF,weight:bold")
self.codeCtrl.StyleSetSpec(stc.STC_P_TRIPLE, "fore:#FF0000,back:#FFFFFF")
self.codeCtrl.StyleSetSpec(stc.STC_P_TRIPLEDOUBLE, "fore:#FF0000,back:#FFFFFF")
self.codeCtrl.StyleSetSpec(stc.STC_P_CLASSNAME, "fore:#00008B,back:#FFFFFF")
self.codeCtrl.StyleSetSpec(stc.STC_P_DEFNAME, "fore:#00008B,back:#FFFFFF")
self.codeCtrl.StyleSetSpec(stc.STC_P_OPERATOR, "fore:#000000,back:#FFFFFF")
self.codeCtrl.StyleSetSpec(stc.STC_P_IDENTIFIER, "fore:#000000,back:#FFFFFF")
self.codeCtrl.StyleSetSpec(stc.STC_P_TRIPLE, "fore:#FF0000,back:#FFFFFF")
self.codeCtrl.StyleSetSpec(stc.STC_P_TRIPLEDOUBLE, "fore:#FF0000,back:#FFFFFF")
self.codeCtrl.StyleSetSpec(stc.STC_STYLE_DEFAULT, 'face:Courier New')
apc.editor = self.codeCtrl
#
#pp(apc.chats)
chat=apc.chats[tab_id]
#pp(chat)
if 'default_file' in chat:
fn=chat.default_file
print(fn)
#e()
else:
fn=__file__
self.load_file(fn)
notebook.AddPage(self.codeCtrl, fn)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.notebook, 1, wx.EXPAND)
self.SetSizer(sizer)
self.Layout()
pub.subscribe(self.ExecuteFile, 'execute')
pub.subscribe(self.OnSaveFile, 'save_file')
def OnSaveFile(self):
#print('Saving file...')
with open(fn, 'w') as file:
data = self.codeCtrl.GetValue().replace('\r\n', '\n')
file.write(data)
def OnCharHook(self, event):
if event.ControlDown() and (event.GetKeyCode() == ord('A') or event.GetKeyCode() == wx.WXK_RETURN):
print('Executing Ctrl+A...')
else:
event.Skip()
def load_file(self, file_path):
with open(file_path, 'r') as file:
data = file.read()
data = data.replace('\r\n', '\n')
self.codeCtrl.SetValue(data)
def OnCharHook(self, event):
if event.ControlDown() and (event.GetKeyCode() == ord('E') or event.GetKeyCode() == wx.WXK_RETURN):
self.log('Executing...')
self.ExecuteFile()
self.log('Done.')
else:
event.Skip()
def log(self, message):
pub.sendMessage('log', message=f'{self.__class__.__name__}: {message}')
def output(self, message):
pub.sendMessage('output', message=f'{message}')
def exception(self, message):
pub.sendMessage('exception', message=f'{message}')
def ExecuteFile(self):
code = self.codeCtrl.GetText()
with tempfile.NamedTemporaryFile(suffix='.py', delete=False) as f:
f.write(code.encode())
temp_file_name = f.name
local_dir = os.getcwd()
command = f'start cmd /k "python {fn} "'
#remove existing conda env variables from shell initialization
#new_env = {k: v for k, v in os.environ.items() if not k.startswith('CONDA') and not k.startswith('VIRTUAL') and not k.startswith('PROMPT')}
apc.process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
cwd=local_dir, creationflags=subprocess.CREATE_NO_WINDOW)
if 0:
stdout, stderr = process.communicate()
if stderr:
self.output(stdout.decode())
self.exception(stderr.decode())
ex = CodeException(stderr.decode())
#self.fixButton.Enable()
pub.sendMessage('fix_exception', message=ex)
else:
self.output(stdout.decode())
def _ExecuteFile(self):
code = self.codeCtrl.GetText()
with tempfile.NamedTemporaryFile(suffix='.py', delete=False) as f:
f.write(code.encode())
temp_file_name = f.name
predefined_dir = 'path/to/your/directory'
local_dir = os.getcwd()
process = subprocess.Popen(['python', fn],
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
cwd=local_dir)
stdout, stderr = process.communicate()
if stderr:
self.output(stdout.decode())
self.exception(stderr.decode())
ex = CodeException(stderr.decode())
#self.fixButton.Enable()
pub.sendMessage('fix_exception', message=ex)
else:
self.output(stdout.decode())
class Copilot_DisplayPanel(code_StyledTextDisplay):
def __init__(self, parent, tab_id):
code_StyledTextDisplay.__init__(self,parent)
font = wx.Font(10, wx.FONTFAMILY_TELETYPE, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL)
self.SetFont(font)
self.tab_id=tab_id
pub.subscribe(self.AddChatOutput, 'chat_output')
#pub.subscribe(lambda message, tab_id: self.AddOutput(message, tab_id), 'chat_output')
pub.subscribe(self.OnShowTabId, 'show_tab_id')
def IsTabVisible(self):
# Get the parent notebook
parent_notebook = self.GetParent().GetParent().GetParent()
#print ('Copilot', self.tab_id, parent_notebook, parent_notebook.GetSelection())
# Check if the current page is the selected page in the parent notebook
return parent_notebook.GetPage(parent_notebook.GetSelection())
def AddChatOutput(self, message, tab_id):
#print(1111, self.tab_id,tab_id, self.tab_id==tab_id, message)
#print('Copilot', self.IsTabVisible(), self.tab_id)
if self.tab_id==tab_id:
#start_pos = self.GetLastPosition()
if 1: #for line in message.splitlines():
wx.CallAfter(self.AddOutput, message)
#end_pos = self.chatDisplay.GetLastPosition()
#self.chatDisplay.SetStyle(start_pos, end_pos, wx.TextAttr(wx.BLACK))
def OnShowTabId(self):
print('show_tab_id', self.tab_id)
class Claude_Anthropic_Chat_InputPanel(wx.Panel, NewChat,GetClassName, Base_InputPanel_Anthropic_Claude):
def __init__(self, parent, tab_id):
global chatHistory, currentQuestion, currentModel
super(Claude_Anthropic_Chat_InputPanel, self).__init__(parent)
NewChat.__init__(self)
GetClassName.__init__(self)
self.tabs={}
self.tab_id=tab_id
chat= apc.chats[tab_id]
chatHistory[self.tab_id]=[]
#pp(chat)
chatHistory[self.tab_id]= [{"role": "system", "content": all_system_templates[chat.workspace].Chat[default_chat_template]}]
self.askLabel = wx.StaticText(self, label=f'Ask chatgpt {tab_id}:')
self.chat_type=chat.chat_type
self.model_dropdown = wx.ComboBox(self, choices=model_list, style=wx.CB_READONLY)
self.model_dropdown.SetValue(chat.model_name)
self.model_dropdown.Bind(wx.EVT_COMBOBOX, self.OnModelChange)
self.askButton = wx.Button(self, label='Ask')
self.askButton.Bind(wx.EVT_BUTTON, self.onAskButton)
askSizer = wx.BoxSizer(wx.HORIZONTAL)
askSizer.Add(self.askLabel, 0, wx.ALIGN_CENTER)
askSizer.Add(self.model_dropdown, 0, wx.ALIGN_CENTER)
self.pause_panel=pause_panel=PausePanel(self, self.tab_id)
askSizer.Add(pause_panel, 0, wx.ALL)
askSizer.Add((1,1), 1, wx.ALIGN_CENTER|wx.ALL)
Base_InputPanel_Anthropic_Claude.AddButtons_Level_1(self, askSizer)
askSizer.Add(self.askButton, 0, wx.ALIGN_CENTER)
self.inputCtrl = wx.TextCtrl(self, style=wx.TE_PROCESS_ENTER | wx.TE_MULTILINE)
if 1:
q=apc.chats[tab_id].question
self.tabs[self.tab_id]=dict(q=q)
questionHistory[self.tab_id]=[q]
currentQuestion[self.tab_id]=0
currentModel[self.tab_id]=chat.model_name
chat=apc.chats[tab_id]
chatHistory[self.tab_id]= [{"role": "system", "content": all_system_templates[chat.workspace].Chat[default_chat_template]}]
self.inputCtrl.SetValue(self.tabs[self.tab_id]['q'])
#self.inputCtrl.SetMinSize((-1, 120))
self.inputCtrl.Bind(wx.EVT_CHAR_HOOK, self.OnCharHook)
sizer = wx.BoxSizer(wx.VERTICAL)
h_sizer = wx.BoxSizer(wx.HORIZONTAL)
Base_InputPanel_Anthropic_Claude.AddButtons_Level_2(self, h_sizer)
sizer.Add(askSizer, 0, wx.ALIGN_CENTER)
sizer.Add(h_sizer, 0, wx.ALIGN_LEFT)
sizer.Add(self.inputCtrl, 1, wx.EXPAND)
self.SetSizer(sizer)
self.ex=None
self.receiveing_tab_id=0
pub.subscribe(self.SetException, 'fix_exception')
pub.subscribe(self.SetChatDefaults , 'set_chat_defaults')
#pub.subscribe(self.SaveQuestionForTabId , 'save_question_for_tab_id')
#pub.subscribe(self.RestoreQuestionForTabId , 'restore_question_for_tab_id')
wx.CallAfter(self.inputCtrl.SetFocus)
self.rs={}
def SetTabId(self, tab_id):
self.tab_id=tab_id
self.askLabel.SetLabel(f'Ask chatgpt {tab_id}:')
def SetChatDefaults(self, tab_id):
global chatHistory, questionHistory, currentModel
if tab_id ==self.tab_id:
assert self.chat_type==tab_id[1]
#pp(apc.chats[tab_id])
#e()
self.tabs[self.tab_id]=dict(q=apc.chats[tab_id].question)
chat=apc.chats[tab_id]
chatHistory[self.tab_id]= [{"role": "system", "content": all_system_templates[chat.workspace].Chat[default_chat_template]}]
questionHistory[self.tab_id]=[]
currentModel[self.tab_id]=chat.model_name
def _SetTabId(self, tab_id):
global chatHistory, questionHistory, currentModel
self.tab_id=tab_id
return self
def OnModelChange(self, event):
# Get the selected model
selected_model = self.model_dropdown.GetValue()
# Print the selected model
print(f"Selected model 111: {selected_model}")
chat=apc.chats[self.tab_id]
chat.model=selected_model
# You can add more code here to do something with the selected model
# Continue processing the event
event.Skip()
def _SaveQuestionForTabId(self, message):
global currentModel
q=self.inputCtrl.GetValue()
self.tabs[message]=dict(q=q)
currentModel[message]=self.model_dropdown.GetValue()
if 0:
d={"role": "user", "content":q}
if self.tab_id in chatHistory:
if d not in chatHistory[self.tab_id]: