From 948210d73c18fb950b0117f91de0cdb122656c42 Mon Sep 17 00:00:00 2001 From: Jonas Braus Date: Thu, 12 Dec 2024 13:05:21 +0100 Subject: [PATCH 1/2] redis cache --- lib.py | 5 ++++- main.py | 7 ++++++- redis_conn.py | 17 +++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 redis_conn.py diff --git a/lib.py b/lib.py index 883cd5a..81cdaf7 100644 --- a/lib.py +++ b/lib.py @@ -10,6 +10,7 @@ from PIL import Image from aiohttp.web_response import json_response import ollama_conn +import redis_conn def read_files(path, output, filetypes=None): @@ -91,7 +92,9 @@ def prompt_embedding(prompt): res = get_data_from_mongo(new_result) - print(ollama_conn.ask_ollama(prompt, res)) + ollama_response = ollama_conn.ask_ollama(prompt, res) + print(ollama_response) + redis_conn.cache_prompt_to_redis(prompt.lower().strip(), ollama_response) conn.close() diff --git a/main.py b/main.py index dfd6bd2..2a15a22 100644 --- a/main.py +++ b/main.py @@ -8,6 +8,8 @@ import json import psycopg2 import pymongo +import redis_conn + count = 0 def retrieve_file_contents(path): @@ -101,7 +103,10 @@ def add_files(): def prompt_cycle(): while True: prompt = input("Please enter prompt: ") - lib.prompt_embedding(prompt) + + response = redis_conn.load_response_from_redis(prompt.lower().strip()) + + lib.prompt_embedding(prompt) if response is None else print("Cached Response:", response) def get_user_action(): diff --git a/redis_conn.py b/redis_conn.py new file mode 100644 index 0000000..54f69c3 --- /dev/null +++ b/redis_conn.py @@ -0,0 +1,17 @@ +import redis + +def cache_prompt_to_redis(prompt, response): + r = redis.Redis(host="localhost", port=6379, decode_responses=True) + r.set(prompt, response) + r.close() + +def load_response_from_redis(prompt): + r = redis.Redis(host="localhost", port=6379, decode_responses=True) + response = r.get(prompt) + r.close() + return response + +def clear_redis(): + r = redis.Redis(host="localhost", port=6379, decode_responses=True) + r.flushall() + r.close() \ No newline at end of file From ed5090110cc9fc3e08e4fb77594030f880c68579 Mon Sep 17 00:00:00 2001 From: Jonas Braus Date: Thu, 12 Dec 2024 13:21:43 +0100 Subject: [PATCH 2/2] auto flush databases --- main.py | 10 +++++++++- mongo_conn.py | 7 +++++++ pgvec_conn.py | 16 ++++++++++++++++ redis_conn.py | 2 +- 4 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 mongo_conn.py create mode 100644 pgvec_conn.py diff --git a/main.py b/main.py index 2a15a22..3e03098 100644 --- a/main.py +++ b/main.py @@ -8,6 +8,8 @@ import json import psycopg2 import pymongo +import mongo_conn +import pgvec_conn import redis_conn count = 0 @@ -91,7 +93,13 @@ def insert_data_mongo(id, filepath,pChunk): def reload_files(): # path = input("Please provider path to folder: ") - contents = retrieve_file_contents("C:\\SoftwareEng") + pgvec_conn.flush_pg() + redis_conn.flush_redis() + mongo_conn.flush_mongo() + + provided_path = input("Please Provide Full Qualified Path: ") + + contents = retrieve_file_contents(provided_path) create_embeddings(contents) diff --git a/mongo_conn.py b/mongo_conn.py new file mode 100644 index 0000000..8e55ea1 --- /dev/null +++ b/mongo_conn.py @@ -0,0 +1,7 @@ +import pymongo + +def flush_mongo(): + client = pymongo.MongoClient('mongodb://python:PasswordPassword123@localhost:27017/') + mongodb = client['document_table'] + mongodb.drop_collection("documents") + client.close() \ No newline at end of file diff --git a/pgvec_conn.py b/pgvec_conn.py new file mode 100644 index 0000000..94a3f7e --- /dev/null +++ b/pgvec_conn.py @@ -0,0 +1,16 @@ +import psycopg2 + +def flush_pg(): + conn = psycopg2.connect( + dbname="embeddings", + user="python", + password="PasswordPassword123", + host="localhost", + port="5555" + ) + cur = conn.cursor() + + cur.execute("drop table if exists dbtable;") + conn.commit() + + conn.close() \ No newline at end of file diff --git a/redis_conn.py b/redis_conn.py index 54f69c3..619bdf9 100644 --- a/redis_conn.py +++ b/redis_conn.py @@ -11,7 +11,7 @@ def load_response_from_redis(prompt): r.close() return response -def clear_redis(): +def flush_redis(): r = redis.Redis(host="localhost", port=6379, decode_responses=True) r.flushall() r.close() \ No newline at end of file