mirror of
				https://github.com/anthropics/claude-cookbooks.git
				synced 2025-10-06 01:00:28 +03:00 
			
		
		
		
	WIP Text to SQL - checkpoint
This commit is contained in:
		| @@ -19,7 +19,6 @@ prompts: | ||||
|   - prompts.py:generate_prompt_with_examples | ||||
|   - prompts.py:generate_prompt_with_cot | ||||
|   - prompts.py:generate_prompt_with_rag | ||||
|   - prompts.py:generate_prompt_with_self_improvement | ||||
|  | ||||
| tests: | ||||
|   - description: "Check syntax of simple query" | ||||
|   | ||||
| @@ -168,77 +168,3 @@ def generate_prompt_with_rag(context): | ||||
|  | ||||
|     Ensure your SQL query is compatible with SQLite syntax. | ||||
|     """ | ||||
|  | ||||
|  | ||||
| def generate_prompt_with_self_improvement(context): | ||||
|     from vectordb import VectorDB | ||||
|  | ||||
|     # Load the vector database | ||||
|     vectordb = VectorDB() | ||||
|     vectordb.load_db() | ||||
|  | ||||
|     user_query = context['vars']['user_query'] | ||||
|  | ||||
|     if not vectordb.embeddings: | ||||
|         with sqlite3.connect(DATABASE_PATH) as conn: | ||||
|             cursor = conn.cursor() | ||||
|             cursor.execute("SELECT name FROM sqlite_master WHERE type='table';") | ||||
|             schema_data = [ | ||||
|                 {"text": f"Table: {table[0]}, Column: {col[1]}, Type: {col[2]}",  | ||||
|                 "metadata": {"table": table[0], "column": col[1], "type": col[2]}} | ||||
|                 for table in cursor.fetchall() | ||||
|                 for col in cursor.execute(f"PRAGMA table_info({table[0]})").fetchall() | ||||
|             ] | ||||
|         vectordb.load_data(schema_data) | ||||
|  | ||||
|     relevant_schema = vectordb.search(user_query, k=10, similarity_threshold=0.3) | ||||
|     schema_info = "\n".join([f"Table: {item['metadata']['table']}, Column: {item['metadata']['column']}, Type: {item['metadata']['type']}"  | ||||
|                              for item in relevant_schema]) | ||||
|      | ||||
|     return f"""You are an AI assistant that converts natural language queries into SQL. | ||||
|     Given the following relevant columns from the SQL database schema: | ||||
|  | ||||
|     <schema> | ||||
|     {schema_info} | ||||
|     </schema> | ||||
|  | ||||
|     Convert the following natural language query into SQL: | ||||
|  | ||||
|     <query> | ||||
|     {user_query} | ||||
|     </query> | ||||
|  | ||||
|     Please provide up to three attempts to generate the correct SQL query. For each attempt: | ||||
|     1. Explain your thought process in <thought_process> tags. | ||||
|     2. Provide the SQL query in <sql> tags. | ||||
|     3. Imagine you are executing this query. If you think it might fail, explain why in <error> tags. | ||||
|     4. If you think the query might fail, provide an improved version in the next attempt. | ||||
|  | ||||
|     After your attempts, provide your final, best SQL query in <final_sql> tags. | ||||
|  | ||||
|     Format your response like this: | ||||
|  | ||||
|     <attempt1> | ||||
|     <thought_process> | ||||
|     Your thought process here | ||||
|     </thought_process> | ||||
|     <sql> | ||||
|     Your SQL query here | ||||
|     </sql> | ||||
|     <error> | ||||
|     Potential error message here (if any) | ||||
|     </error> | ||||
|     </attempt1> | ||||
|  | ||||
|     <attempt2> | ||||
|     ... | ||||
|     </attempt2> | ||||
|  | ||||
|     <attempt3> | ||||
|     ... | ||||
|     </attempt3> | ||||
|  | ||||
|     <final_sql> | ||||
|     Your final, best SQL query here | ||||
|     </final_sql> | ||||
|     """ | ||||
		Reference in New Issue
	
	Block a user
	 Mahesh Murag
					Mahesh Murag