@@ -131,7 +131,7 @@ def __exit__(self) -> t.NoReturn:
131131class TextToSql :
132132 """Generate SQL Statement based on given prompt"""
133133
134- def __init__ (self , db_manager : Sqlite3Manager ):
134+ def __init__ (self , db_manager : Sqlite3Manager , follow_up : bool = False ):
135135 """Initializes `TextToSql`"""
136136 try :
137137 from pytgpt .auto import AUTO
@@ -143,7 +143,7 @@ def __init__(self, db_manager: Sqlite3Manager):
143143 history_file = Path .home () / ".sqlite-cli-manager-ai-chat-history.txt"
144144 if history_file .exists ():
145145 os .remove (history_file )
146- self .ai = AUTO (filepath = str (history_file ))
146+ self .ai = AUTO (is_conversation = follow_up , filepath = str (history_file ))
147147 assert isinstance (
148148 db_manager , Sqlite3Manager
149149 ), f"db_manager must be an instance of { Sqlite3Manager } not { type (db_manager )} "
@@ -162,36 +162,52 @@ def context_prompt(self) -> str:
162162 )
163163 prompt = (
164164 (
165- """You're going to act like TEXT to SQL translater.
166- Action to be performed on the sqlite3 database will be provided and then
165+ """You're going to act like TEXT-to-SQL translater.
166+ | INSTRUCTIONS |
167+
168+ 1. Action to be performed on the sqlite3 database will be provided by user and then
167169 you will generate a complete SQL statement for accomplishing the same.
168- Enclose the sql statement in curly braces '{}'. DO NOT ADD ANY OTHER TEXT
170+
171+ 1.1. Enclose the sql statement in curly braces '{}'. DO NOT ADD ANY OTHER TEXT
169172 EXCEPT when seeking clarification or confirmation.
170173
171- Given below are the database table names and the SQL statements used to create them:
172- \n """
173- )
174- + "\n "
175- + table_schema_text
176- + (
177- """
178- \n
179174 For example:
175+
180176 User: List first 10 entries in the Linux table where distro contains letter 'a'
181177 LLM : {SELECT * FROM Linux WHERE distro LIKE '%a%';}
182178
183179 User : Remove entries from table Linux whose id is greater than 10.
184180 LLLM : {DELETE * FROM Linux WHERE id > 10;}
185181
186- If the user's request IS UNDOUBTEDBLY INCOMPLETE, seek clarification.
182+ 2. If the user's request IS UNDOUBTEDBLY INCOMPLETE, seek clarification.
183+
187184 For example:
185+
188186 User: Add column to Linux table.
189187 LLM: Describe the data to be stored in the column and suggest column name if possible?
190188 User: The column will be storing maintainance status of the linux distros.
191189 LLM: {ALTER TABLE Linux ADD COLUMN is_maintained BOOLEAN;}
192190
193- If the user's request can be disastrous then seek clarification or confirmation accordingly.
191+ 3. If the user's request can be disastrous then seek clarification or confirmation accordingly.
194192 These actions might include DELETE, ALTER and DROP.
193+
194+ For example:
195+
196+ User: Remove Linux table
197+ LLM: Removing Linux table cannot be undone. Are you sure to perform that?
198+ User: Yes
199+ LLM: {DROP TABLE Linux}
200+
201+ 4. AFTER clarification or confirmation, your proceeding responses SHOULD ABIDE to instructions 1 and 2.
202+
203+ Given below are the table names and the SQL statements used to create them in the database:
204+ \n """
205+ )
206+ + "\n "
207+ + table_schema_text
208+ + (
209+ """
210+ \n
195211 """
196212 )
197213 )
@@ -277,6 +293,7 @@ def __init__(
277293 yes ,
278294 color ,
279295 ai ,
296+ follow_up ,
280297 ):
281298 super ().__init__ ()
282299 self .__start_time = time .time ()
@@ -286,6 +303,7 @@ def __init__(
286303 self .json = json
287304 self .yes = yes
288305 self .color = color
306+ self .follow_up = follow_up
289307 history_file_path = Path .home () / ".sqlite3-cli-manager-history.txt"
290308 if new_history_thread and history_file_path .exists ():
291309 os .remove (history_file_path )
@@ -296,7 +314,7 @@ def __init__(
296314 )
297315 self .ai = ai
298316 if self .ai :
299- self .text_to_sql = TextToSql (self .db_manager )
317+ self .text_to_sql = TextToSql (self .db_manager , follow_up )
300318
301319 @property
302320 def prompt (self ):
@@ -510,7 +528,7 @@ def default(self, line: str, prompt_confirmation: bool = False, ai_generated=Fal
510528 line = [line [4 :].strip ()]
511529 elif line .startswith ("/ai" ):
512530 if not hasattr (self , "text_to_sql" ):
513- self .text_to_sql = TextToSql (self .db_manager )
531+ self .text_to_sql = TextToSql (self .db_manager , self . follow_up )
514532 line = self .text_to_sql .generate (line [3 :].strip ())
515533 prompt_confirmation = True
516534 ai_generated = True
@@ -722,6 +740,9 @@ def execute(database, sql, ai, json, quiet):
722740 @click .option (
723741 "-i" , "--ai" , is_flag = True , help = "Generate sql statements from prompt by AI"
724742 )
743+ @click .option (
744+ "-f" , "--follow-up" , is_flag = True , help = "Add previous chats with AI to context"
745+ )
725746 @click .option (
726747 "-C" ,
727748 "--disable-coloring" ,
@@ -744,6 +765,7 @@ def interactive(
744765 yes ,
745766 auto_commit ,
746767 ai ,
768+ follow_up ,
747769 disable_coloring ,
748770 disable_suggestions ,
749771 new_history_thread ,
@@ -759,6 +781,7 @@ def interactive(
759781 yes = yes ,
760782 color = color ,
761783 ai = ai ,
784+ follow_up = follow_up ,
762785 )
763786 main .cmdloop ()
764787
0 commit comments