|
1 # -*- coding: utf-8 -*- |
|
2 # Copyright (c) 2024 Detlev Offenbach <detlev@die-offenbachs.de> |
|
3 # |
|
4 |
|
5 """ |
|
6 Module implementing a widget showing a chat title and store a chat contents. |
|
7 """ |
|
8 |
|
9 import json |
|
10 import uuid |
|
11 |
|
12 from PyQt6.QtCore import pyqtSignal, pyqtSlot |
|
13 from PyQt6.QtWidgets import QWidget |
|
14 |
|
15 from eric7.EricGui import EricPixmapCache |
|
16 |
|
17 from .Ui_OllamaHistoryWidget import Ui_OllamaHistoryWidget |
|
18 |
|
19 |
|
20 class OllamaHistoryWidget(QWidget, Ui_OllamaHistoryWidget): |
|
21 """ |
|
22 Class implementing a widget showing a chat title and store a chat contents. |
|
23 |
|
24 @signal deleteChatHistory(id:str) emitted to indicate, that this chat history |
|
25 should be deleted |
|
26 @signal newChatWithHistory(id:str) emitted to indicate, that a new chat using |
|
27 the saved history should be started |
|
28 @signal dataChanged(id:str) emitted to indicate a change of the chat history data |
|
29 """ |
|
30 |
|
31 deleteChatHistory = pyqtSignal(str) |
|
32 newChatWithHistory = pyqtSignal(str) |
|
33 dataChanged = pyqtSignal(str) |
|
34 |
|
35 def __init__(self, title, model, jsonStr=None, parent=None): |
|
36 """ |
|
37 Constructor |
|
38 |
|
39 @param title title of the ollama chat |
|
40 @type str |
|
41 @param model name of the model used for the chat |
|
42 @type str |
|
43 @param jsonStr string containing JSON serialize chat history data |
|
44 (defaults to None) |
|
45 @type str (optional) |
|
46 @param parent reference to the parent widget (defaults to None) |
|
47 @type QWidget (optional) |
|
48 """ |
|
49 super().__init__(parent) |
|
50 self.setupUi(self) |
|
51 |
|
52 self.newChatButton.setIcon(EricPixmapCache.getIcon("plus")) |
|
53 self.deleteButton.setIcon(EricPixmapCache.getIcon("trash")) |
|
54 |
|
55 if jsonStr is None: |
|
56 self.__title = title |
|
57 self.__model = model |
|
58 |
|
59 self.__id = str(uuid.uuid4()) |
|
60 self.__messages = [] |
|
61 else: |
|
62 self.loadFromJson(jsonStr) |
|
63 |
|
64 self.titleEdit.setText(self.__title) |
|
65 self.modelEdit.setText(self.__model) |
|
66 |
|
67 def getId(self): |
|
68 """ |
|
69 Public method to get the chat history ID. |
|
70 |
|
71 @return ID of the history entry |
|
72 @rtype str |
|
73 """ |
|
74 return self.__id |
|
75 |
|
76 @pyqtSlot() |
|
77 def on_deleteButton_clicked(self): |
|
78 """ |
|
79 Private slot to delet this chat history entry.. |
|
80 """ |
|
81 self.deleteChatHistory.emit(self.__id) |
|
82 |
|
83 @pyqtSlot() |
|
84 def on_newChatButton_clicked(self): |
|
85 """ |
|
86 Private slot to start a new chat using the saved chat history. |
|
87 """ |
|
88 self.newChatWithHistory.emit(self.__id) |
|
89 |
|
90 def loadFromJson(self, jsonStr): |
|
91 """ |
|
92 Public method to load the chat history data from a JSON string. |
|
93 |
|
94 @param jsonStr JSON serialized chat data |
|
95 @type str |
|
96 """ |
|
97 data = json.loads(jsonStr) |
|
98 self.__id = data["id"] |
|
99 self.__title = data["title"] |
|
100 self.__model = data["model"] |
|
101 self.__messages = data["messages"] |
|
102 |
|
103 def saveToJson(self): |
|
104 """ |
|
105 Public method to serialize the chat history data to a JSON string. |
|
106 |
|
107 @return JSON serialized chat data |
|
108 @rtype str |
|
109 """ |
|
110 return json.dumps( |
|
111 { |
|
112 "id": self.__id, |
|
113 "title": self.__title, |
|
114 "model": self.__model, |
|
115 "messages": self.__messages, |
|
116 } |
|
117 ) |
|
118 |
|
119 def addToMessages(self, role, content): |
|
120 """ |
|
121 Public method to add a chat message to the chat history. |
|
122 |
|
123 @param role chat role (one of 'system', 'user', 'assistant' or 'tool') |
|
124 @type str |
|
125 @param content content of the chat message |
|
126 @type str |
|
127 """ |
|
128 self.__messages.append( |
|
129 { |
|
130 "role": role, |
|
131 "content": content, |
|
132 } |
|
133 ) |
|
134 self.dataChanged.emit(self.__id) |