tool_use #1
							
								
								
									
										36
									
								
								main.py
									
									
									
									
									
								
							
							
						
						
									
										36
									
								
								main.py
									
									
									
									
									
								
							| @@ -98,20 +98,17 @@ def init_db(): | |||||||
|                 api_key TEXT NOT NULL UNIQUE |                 api_key TEXT NOT NULL UNIQUE | ||||||
|             ); |             ); | ||||||
|         """) |         """) | ||||||
|         db.execute( |         db.execute(''' | ||||||
|             """ |  | ||||||
|             CREATE TABLE IF NOT EXISTS Queries ( |             CREATE TABLE IF NOT EXISTS Queries ( | ||||||
|                 id INTEGER PRIMARY KEY AUTOINCREMENT, |                 id TEXT PRIMARY KEY, | ||||||
|                 request_id TEXT NOT NULL UNIQUE, |  | ||||||
|                 ip TEXT NOT NULL, |                 ip TEXT NOT NULL, | ||||||
|                 timestamp DATETIME DEFAULT CURRENT_TIMESTAMP, |                 timestamp DATETIME DEFAULT CURRENT_TIMESTAMP, | ||||||
|                 query TEXT NOT NULL, |                 query TEXT NOT NULL, | ||||||
|                 api_key_id INTEGER, |                 api_key_id INTEGER, | ||||||
|                 conversation_history TEXT, |                 conversation_history TEXT, | ||||||
|                 FOREIGN KEY (api_key_id) REFERENCES Keys (id) |                 FOREIGN KEY (api_key_id) REFERENCES Keys (id) | ||||||
|             ); |             ) | ||||||
|         """ |         ''') | ||||||
|         ) |  | ||||||
|         db.commit() |         db.commit() | ||||||
|  |  | ||||||
|  |  | ||||||
| @@ -124,8 +121,7 @@ CREATE TABLE IF NOT EXISTS Keys ( | |||||||
| ); | ); | ||||||
|  |  | ||||||
| CREATE TABLE IF NOT EXISTS Queries ( | CREATE TABLE IF NOT EXISTS Queries ( | ||||||
|     id INTEGER PRIMARY KEY AUTOINCREMENT, |     id TEXT PRIMARY KEY, | ||||||
|     request_id TEXT NOT NULL UNIQUE, |  | ||||||
|     ip TEXT NOT NULL, |     ip TEXT NOT NULL, | ||||||
|     timestamp DATETIME DEFAULT CURRENT_TIMESTAMP, |     timestamp DATETIME DEFAULT CURRENT_TIMESTAMP, | ||||||
|     query TEXT NOT NULL, |     query TEXT NOT NULL, | ||||||
| @@ -458,7 +454,7 @@ def api_query(body: QueryRequest): | |||||||
|     if not ENABLE_API_ENDPOINTS: |     if not ENABLE_API_ENDPOINTS: | ||||||
|         return jsonify({"error": "API endpoints are disabled"}), 404 |         return jsonify({"error": "API endpoints are disabled"}), 404 | ||||||
|  |  | ||||||
|     api_key = request.headers.get("X-API-Key") |     api_key = request.headers.get('X-API-Key') | ||||||
|     if not api_key: |     if not api_key: | ||||||
|         return jsonify({"error": "API key is required"}), 401 |         return jsonify({"error": "API key is required"}), 401 | ||||||
|  |  | ||||||
| @@ -474,7 +470,7 @@ def api_query(body: QueryRequest): | |||||||
|         cursor = db.cursor() |         cursor = db.cursor() | ||||||
|         cursor.execute( |         cursor.execute( | ||||||
|             "INSERT INTO Queries (id, ip, query, api_key_id) VALUES (?, ?, ?, ?)", |             "INSERT INTO Queries (id, ip, query, api_key_id) VALUES (?, ?, ?, ?)", | ||||||
|             (query_id, request.remote_addr, user_input, api_key_id), |             (query_id, request.remote_addr, user_input, api_key_id) | ||||||
|         ) |         ) | ||||||
|         db.commit() |         db.commit() | ||||||
|  |  | ||||||
| @@ -505,9 +501,7 @@ def get_query_status(query_id: str): | |||||||
|     try: |     try: | ||||||
|         db = get_db() |         db = get_db() | ||||||
|         cursor = db.cursor() |         cursor = db.cursor() | ||||||
|         cursor.execute( |         cursor.execute("SELECT conversation_history FROM Queries WHERE id = ?", (query_id,)) | ||||||
|             "SELECT conversation_history FROM Queries WHERE id = ?", (query_id,) |  | ||||||
|         ) |  | ||||||
|         result = cursor.fetchone() |         result = cursor.fetchone() | ||||||
|  |  | ||||||
|         if result is None: |         if result is None: | ||||||
| @@ -610,22 +604,16 @@ def process_queries(): | |||||||
|  |  | ||||||
|                 if result: |                 if result: | ||||||
|                     query_id, user_input = result |                     query_id, user_input = result | ||||||
|                     conversation_history = [ |                     conversation_history = [{"role": "system", "content": ANSWER_QUESTION_PROMPT}] | ||||||
|                         {"role": "system", "content": ANSWER_QUESTION_PROMPT} |                     final_conversation_history = answer_question_tools_api(user_input, conversation_history) | ||||||
|                     ] |  | ||||||
|                     final_conversation_history = answer_question_tools_api( |  | ||||||
|                         user_input, conversation_history |  | ||||||
|                     ) |  | ||||||
|  |  | ||||||
|                     cursor.execute( |                     cursor.execute( | ||||||
|                         "UPDATE Queries SET conversation_history = ? WHERE id = ?", |                         "UPDATE Queries SET conversation_history = ? WHERE id = ?", | ||||||
|                         (json.dumps(final_conversation_history), query_id), |                         (json.dumps(final_conversation_history), query_id) | ||||||
|                     ) |                     ) | ||||||
|                     db.commit() |                     db.commit() | ||||||
|                 else: |                 else: | ||||||
|                     time.sleep( |                     time.sleep(1)  # Wait for 1 second before checking again if no queries are found | ||||||
|                         1 |  | ||||||
|                     )  # Wait for 1 second before checking again if no queries are found |  | ||||||
|             except Exception as e: |             except Exception as e: | ||||||
|                 logger.exception("Error processing query", error=str(e)) |                 logger.exception("Error processing query", error=str(e)) | ||||||
|                 time.sleep(1)  # Wait for 1 second before retrying in case of an error |                 time.sleep(1)  # Wait for 1 second before retrying in case of an error | ||||||
|   | |||||||
| @@ -5,8 +5,7 @@ CREATE TABLE IF NOT EXISTS Keys ( | |||||||
| ); | ); | ||||||
|  |  | ||||||
| CREATE TABLE IF NOT EXISTS Queries ( | CREATE TABLE IF NOT EXISTS Queries ( | ||||||
|     id INTEGER PRIMARY KEY AUTOINCREMENT, |     id TEXT PRIMARY KEY, | ||||||
|     request_id TEXT NOT NULL UNIQUE, |  | ||||||
|     ip TEXT NOT NULL, |     ip TEXT NOT NULL, | ||||||
|     timestamp DATETIME DEFAULT CURRENT_TIMESTAMP, |     timestamp DATETIME DEFAULT CURRENT_TIMESTAMP, | ||||||
|     query TEXT NOT NULL, |     query TEXT NOT NULL, | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user