Spaces:
Running
on
Zero
Running
on
Zero
df
Browse files
command/postgresz.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import psycopg2
|
2 |
+
|
3 |
+
# PostgreSQLの設定
|
4 |
+
conn_params = {
|
5 |
+
"dbname": "neondb",
|
6 |
+
"user": "miyataken999",
|
7 |
+
"password": "yz1wPf4KrWTm",
|
8 |
+
"host": "ep-odd-mode-93794521.us-east-2.aws.neon.tech",
|
9 |
+
"port": 5432,
|
10 |
+
"sslmode": "require"
|
11 |
+
}
|
12 |
+
|
13 |
+
def initialize_db():
|
14 |
+
# PostgreSQLに接続
|
15 |
+
conn = psycopg2.connect(**conn_params)
|
16 |
+
cursor = conn.cursor()
|
17 |
+
|
18 |
+
# テーブルを作成するSQL文
|
19 |
+
create_table_query = """
|
20 |
+
CREATE TABLE IF NOT EXISTS chat_history (
|
21 |
+
id SERIAL PRIMARY KEY,
|
22 |
+
role TEXT,
|
23 |
+
type TEXT,
|
24 |
+
content TEXT,
|
25 |
+
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
26 |
+
)
|
27 |
+
"""
|
28 |
+
cursor.execute(create_table_query)
|
29 |
+
|
30 |
+
# 別のテーブルを作成するSQL文
|
31 |
+
create_history_table_query = """
|
32 |
+
CREATE TABLE IF NOT EXISTS history (
|
33 |
+
id SERIAL PRIMARY KEY,
|
34 |
+
role TEXT,
|
35 |
+
type TEXT,
|
36 |
+
content TEXT,
|
37 |
+
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
38 |
+
)
|
39 |
+
"""
|
40 |
+
cursor.execute(create_history_table_query)
|
41 |
+
|
42 |
+
conn.commit()
|
43 |
+
cursor.close()
|
44 |
+
conn.close()
|
45 |
+
print("データベースとテーブルが作成されました。")
|
46 |
+
|
47 |
+
def add_message_to_db(role, message_type, content):
|
48 |
+
conn = psycopg2.connect(**conn_params)
|
49 |
+
cursor = conn.cursor()
|
50 |
+
cursor.execute(
|
51 |
+
"INSERT INTO history (role, type, content, timestamp) VALUES (%s, %s, %s, CURRENT_TIMESTAMP)",
|
52 |
+
(role, message_type, content)
|
53 |
+
)
|
54 |
+
conn.commit()
|
55 |
+
cursor.close()
|
56 |
+
conn.close()
|
57 |
+
|
58 |
+
def get_recent_messages(limit=5):
|
59 |
+
conn = psycopg2.connect(**conn_params)
|
60 |
+
cursor = conn.cursor()
|
61 |
+
cursor.execute(
|
62 |
+
"SELECT role, type, content FROM history ORDER BY timestamp DESC LIMIT %s",
|
63 |
+
(limit,)
|
64 |
+
)
|
65 |
+
messages = cursor.fetchall()
|
66 |
+
cursor.close()
|
67 |
+
conn.close()
|
68 |
+
return messages[::-1] # 最新のlimit件を取得して逆順にする
|
69 |
+
|
70 |
+
# データベースの初期化
|
71 |
+
initialize_db()
|
routers/gra_02_openInterpreter/OpenInterpreter.py
CHANGED
@@ -63,11 +63,12 @@ def format_response(chunk, full_response):
|
|
63 |
|
64 |
import sqlite3
|
65 |
from datetime import datetime
|
|
|
66 |
|
67 |
# SQLiteの設定
|
68 |
db_name = "chat_historys.db"
|
69 |
|
70 |
-
def
|
71 |
# データベースファイルが存在しない場合に新しく作成
|
72 |
if not os.path.exists(db_name):
|
73 |
conn = sqlite3.connect(db_name)
|
@@ -102,14 +103,14 @@ def initialize_db():
|
|
102 |
conn.commit()
|
103 |
conn.close()
|
104 |
|
105 |
-
def
|
106 |
conn = sqlite3.connect(db_name)
|
107 |
cursor = conn.cursor()
|
108 |
cursor.execute("INSERT INTO history (role, type, content) VALUES (?, ?, ?)", (role, message_type, content))
|
109 |
conn.commit()
|
110 |
conn.close()
|
111 |
|
112 |
-
def
|
113 |
conn = sqlite3.connect(db_name)
|
114 |
cursor = conn.cursor()
|
115 |
cursor.execute("SELECT role, type, content FROM history ORDER BY timestamp DESC LIMIT ?", (limit,))
|
|
|
63 |
|
64 |
import sqlite3
|
65 |
from datetime import datetime
|
66 |
+
from command.postgresz import initialize_db,add_message_to_db,get_recent_messages
|
67 |
|
68 |
# SQLiteの設定
|
69 |
db_name = "chat_historys.db"
|
70 |
|
71 |
+
def initialize_dbs():
|
72 |
# データベースファイルが存在しない場合に新しく作成
|
73 |
if not os.path.exists(db_name):
|
74 |
conn = sqlite3.connect(db_name)
|
|
|
103 |
conn.commit()
|
104 |
conn.close()
|
105 |
|
106 |
+
def add_message_to_dbs(role, message_type, content):
|
107 |
conn = sqlite3.connect(db_name)
|
108 |
cursor = conn.cursor()
|
109 |
cursor.execute("INSERT INTO history (role, type, content) VALUES (?, ?, ?)", (role, message_type, content))
|
110 |
conn.commit()
|
111 |
conn.close()
|
112 |
|
113 |
+
def get_recent_messagess(limit=5):
|
114 |
conn = sqlite3.connect(db_name)
|
115 |
cursor = conn.cursor()
|
116 |
cursor.execute("SELECT role, type, content FROM history ORDER BY timestamp DESC LIMIT ?", (limit,))
|