Spaces:
Sleeping
Sleeping
File size: 1,409 Bytes
27a479a 316a678 5b28103 27a479a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
import json
from typing import List
import sqlalchemy
from sqlalchemy import text
from sqlalchemy.orm import Session
def insert_chat_history(conn: sqlalchemy.engine.Connection, query: str, answer: str):
with Session(conn) as conn:
conn.execute(
text("INSERT INTO chat_history (query, answer) VALUES (:query, :answer);"),
[
{
"query": query,
"answer": answer,
}
],
)
conn.commit()
result = conn.execute(
text("SELECT id FROM chat_history ORDER BY id DESC LIMIT 1;")
)
last_row_id = result.fetchone()[0]
conn.commit()
return last_row_id
def insert_chat_history_articles(
conn: sqlalchemy.engine.Connection, chat_history_id: int, articles: List[str]
):
source_documents = [json.loads(article.page_content) for article in articles]
with Session(conn) as conn:
conn.execute(
text(
"INSERT INTO chat_history_articles (chat_history_id, article_id) VALUES (:chat_history_id, :article_id) ON CONFLICT DO NOTHING;"
),
[
{
"chat_history_id": chat_history_id,
"article_id": article["id"],
}
for article in source_documents
],
)
conn.commit()
|