1
1
from __future__ import annotations
2
2
3
3
from pathlib import Path
4
- from typing import TYPE_CHECKING
4
+ from typing import TYPE_CHECKING , Union
5
5
6
- from PySide6 .QtCore import Qt
6
+ from PySide6 .QtCore import Qt , QTimer
7
7
from PySide6 .QtGui import QFontDatabase
8
- from PySide6 .QtWidgets import QWidget , QPlainTextEdit , QScrollArea , QLabel , QComboBox , QGridLayout , QMessageBox
8
+ from PySide6 .QtWidgets import QWidget , QPlainTextEdit , QScrollArea , QLabel , QComboBox , QGridLayout , QPushButton , \
9
+ QMessageBox , QSizePolicy , QLineEdit
9
10
10
- from je_editor .pyside_ui .main_ui .ai_widget .ai_config import AIConfig
11
+ from je_editor .pyside_ui .dialog .ai_dialog .set_ai_dialog import SetAIDialog
12
+ from je_editor .pyside_ui .main_ui .ai_widget .ai_config import AIConfig , ai_config
13
+ from je_editor .pyside_ui .main_ui .ai_widget .ask_thread import AskThread
14
+ from je_editor .pyside_ui .main_ui .ai_widget .langchain_interface import LangChainInterface
11
15
from je_editor .utils .json .json_file import read_json
12
16
from je_editor .utils .multi_language .multi_language_wrapper import language_wrapper
13
17
14
18
if TYPE_CHECKING :
15
19
from je_editor .pyside_ui .main_ui .main_editor import EditorMain
16
20
17
21
22
+ def set_ai_config ():
23
+ # Set and output AI a config file
24
+ set_ai_config_dialog = SetAIDialog ()
25
+ set_ai_config_dialog .show ()
26
+
27
+
18
28
class ChatUI (QWidget ):
19
29
20
30
def __init__ (self , main_window : EditorMain ):
@@ -30,41 +40,84 @@ def __init__(self, main_window: EditorMain):
30
40
self .chat_panel_scroll_area .setViewportMargins (0 , 0 , 0 , 0 )
31
41
self .chat_panel_scroll_area .setWidget (self .chat_panel )
32
42
self .chat_panel .setFont (QFontDatabase .font (self .font ().family (), "" , 16 ))
43
+ # Prompt input
44
+ self .prompt_input = QLineEdit ()
45
+ self .prompt_input .setSizePolicy (QSizePolicy .Policy .Expanding , QSizePolicy .Policy .Minimum )
46
+ self .prompt_input .returnPressed .connect (self .call_ai_model )
33
47
# Font size combobox
34
48
self .font_size_label = QLabel (language_wrapper .language_word_dict .get ("font_size" ))
35
49
self .font_size_combobox = QComboBox ()
36
50
for font_size in range (2 , 101 , 2 ):
37
51
self .font_size_combobox .addItem (str (font_size ))
38
52
self .font_size_combobox .setCurrentText ("16" )
39
53
self .font_size_combobox .currentTextChanged .connect (self .update_panel_text_size )
54
+ # Buttons
55
+ self .set_ai_config_button = QPushButton (language_wrapper .language_word_dict .get ("chat_ui_set_ai_button" ))
56
+ self .set_ai_config_button .clicked .connect (set_ai_config )
57
+ self .load_ai_config_button = QPushButton (language_wrapper .language_word_dict .get ("chat_ui_load_ai_button" ))
58
+ self .load_ai_config_button .clicked .connect (self .load_ai_config )
59
+ self .call_ai_model_button = QPushButton (language_wrapper .language_word_dict .get ("chat_ui_call_ai_model_button" ))
60
+ self .call_ai_model_button .clicked .connect (self .call_ai_model )
40
61
# Add to layout
41
62
self .grid_layout = QGridLayout ()
42
- self .grid_layout .addWidget (self .chat_panel_scroll_area , 1 , 0 , - 1 , - 1 )
43
- self .grid_layout .addWidget (self .font_size_combobox , 0 , 1 )
44
- ai_config = AIConfig ()
45
- # Load AI config if exists
63
+ self .grid_layout .addWidget (self .chat_panel_scroll_area , 0 , 0 , 1 , 4 )
64
+ self .grid_layout .addWidget (self .call_ai_model_button , 1 , 0 )
65
+ self .grid_layout .addWidget (self .font_size_combobox , 1 , 1 )
66
+ self .grid_layout .addWidget (self .set_ai_config_button , 1 , 2 )
67
+ self .grid_layout .addWidget (self .load_ai_config_button , 1 , 3 )
68
+ self .grid_layout .addWidget (self .prompt_input , 2 , 0 , 1 , 4 )
69
+
70
+ # Variable
71
+ self .ai_config : AIConfig = ai_config
72
+ self .lang_chain_interface : Union [LangChainInterface , None ] = None
73
+ self .pull_message_timer = QTimer (self )
74
+ self .pull_message_timer .setInterval (1000 )
75
+ self .pull_message_timer .timeout .connect (self .pull_message )
76
+ self .pull_message_timer .start ()
77
+
78
+ # Set layout
79
+ self .setLayout (self .grid_layout )
80
+
81
+ self .load_ai_config ()
82
+
83
+ def update_panel_text_size (self ):
84
+ self .chat_panel .setFont (
85
+ QFontDatabase .font (self .font ().family (), "" , int (self .font_size_combobox .currentText ())))
86
+
87
+ def load_ai_config (self ):
46
88
ai_config_file = Path (str (Path .cwd ()) + "/" + ".jeditor/ai_config.json" )
47
89
if ai_config_file .exists ():
48
- with open (ai_config_file , "r" , encoding = "utf-8" ) as file :
90
+ with open (ai_config_file , "r" , encoding = "utf-8" ):
49
91
json_data : dict = read_json (str (ai_config_file ))
50
92
if json_data :
51
- if json_data .get ("AI_model" ) and len (json_data .get ("AI_model" )) == 3 :
93
+ if json_data .get ("AI_model" ) and len (json_data .get ("AI_model" )) == 4 :
52
94
ai_info : dict = json_data .get ("AI_model" )
53
95
if ai_info .get ("ai_base_url" ) and ai_info .get ("chat_model" ):
54
96
ai_config .choosable_ai .update (json_data )
55
- else :
56
- QMessageBox .warning (self .main_window ,
57
- language_wrapper .language_word_dict .get ("set_ai_model_warring_title" ),
58
- language_wrapper .language_word_dict .get ("set_ai_model_warring_text" ))
59
-
60
- def update_panel_text_size (self ):
61
- self .chat_panel .setFont (
62
- QFontDatabase .font (self .font ().family (), "" , int (self .font_size_combobox .currentText ())))
97
+ self .lang_chain_interface = LangChainInterface (
98
+ main_window = self ,
99
+ api_key = ai_info .get ("ai_api_key" ),
100
+ base_url = ai_info .get ("ai_base_url" ),
101
+ chat_model = ai_info .get ("chat_model" ),
102
+ prompt_template = ai_info .get ("prompt_template" ),
103
+ )
63
104
64
- def set_ai_config (self ):
65
- # Set and output AI a config file
66
- pass
105
+ def call_ai_model (self ):
106
+ if isinstance (self .lang_chain_interface , LangChainInterface ):
107
+ thread = AskThread (lang_chain_interface = self .lang_chain_interface , prompt = self .prompt_input .text ())
108
+ thread .start ()
109
+ else :
110
+ ai_info = ai_config .choosable_ai .get ('AI_model' )
111
+ QMessageBox .warning (self ,
112
+ language_wrapper .language_word_dict .get ("call_ai_model_error_title" ),
113
+ language_wrapper .language_word_dict .get (
114
+ f"ai_api_key: { ai_info .get ('ai_api_key' )} , \n "
115
+ f"ai_base_url: { ai_info .get ('ai_base_url' )} , \n "
116
+ f"chat_model: { ai_info .get ('chat_model' )} , \n "
117
+ f"prompt_template: { ai_info .get ('prompt_template' )} " ))
67
118
68
- def load_ai_config (self ):
69
- # Load exists an AI config file
70
- pass
119
+ def pull_message (self ):
120
+ if not ai_config .message_queue .empty ():
121
+ ai_response = ai_config .message_queue .get_nowait ()
122
+ self .chat_panel .appendPlainText (ai_response )
123
+ self .chat_panel .appendPlainText ("\n " )
0 commit comments