Skip to content

Commit

Permalink
Merge pull request #29 from RobotSail/feedback-updates
Browse files Browse the repository at this point in the history
add initial feedback code
  • Loading branch information
oindrillac authored Jan 17, 2024
2 parents 82e9982 + 024516d commit b93c425
Show file tree
Hide file tree
Showing 7 changed files with 254 additions and 3 deletions.
12 changes: 12 additions & 0 deletions app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
eval_using_model,
indicate_key_presence,
)
from feedback import store_feedback
import os
import streamlit as st
import logging
Expand All @@ -15,6 +16,8 @@
from sklearn.metrics.pairwise import cosine_similarity
from textstat import textstat
import os
from streamlit_feedback import streamlit_feedback


# Set theme, title, and icon
st.set_page_config(page_title="API Docs Generator", page_icon="📄", layout="wide")
Expand Down Expand Up @@ -219,6 +222,7 @@ def main(prompt_success: bool, prompt_diff: int, actual_doc: str):
top_p,
GENAI_KEY(),
)

col1, col2, col3 = st.columns([1.5, 1.5, 0.5])

with col1:
Expand Down Expand Up @@ -308,3 +312,11 @@ def main(prompt_success: bool, prompt_diff: int, actual_doc: str):
main(prompt_success, prompt_diff, actual_doc)
else:
main(True, True, actual_doc)

# generate the feedback section now
streamlit_feedback(
feedback_type="thumbs",
on_submit=store_feedback,
optional_text_label="Please tell us how we could make this more useful",
align="flex-start",
)
84 changes: 84 additions & 0 deletions app/feedback.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import logging
import sqlite3
import os
import time

logger = logging.getLogger(__name__)


def get_db_file() -> str:
"""
Returns either the database location that was provided
via the FEEDBACK_DB environment variable, or returns
a default location.
"""
db_file = os.getenv("FEEDBACK_DB")
if not db_file:
db_file = "/tmp/feedback.db"
logger.warning(
"No feedback database file specified, storing feedback in default location: '%s'",
db_file,
)
return db_file


def create_connection() -> sqlite3.Connection | None:
"""
Creates connection with the Sqlite database.
"""

db_file = get_db_file()
conn = None
try:
conn = sqlite3.connect(db_file)
except sqlite3.Error as e:
logger.error(e)
return conn


def create_table(conn: sqlite3.Connection):
"""
Creates the feedback table if it does not exist.
"""
try:
sql_create_feedback_table = """ CREATE TABLE IF NOT EXISTS feedback (
id integer PRIMARY KEY AUTOINCREMENT,
type text NOT NULL,
score text NOT NULL,
text text,
timestamp text NOT NULL
); """
c = conn.cursor()
c.execute(sql_create_feedback_table)
except sqlite3.Error as e:
logger.error(e)


def store_feedback(feedback: dict) -> None:
"""
Stores feedback from the provided feedback dict, which
comes from the streamlit-feedback component.
If the database does not exist, it will be created.
"""
logger.info("Got feedback: %s", str(feedback))
conn = create_connection()
if conn is not None:
create_table(conn)
sql = """ INSERT INTO feedback(type,score,text,timestamp)
VALUES(?,?,?,?) """
cur = conn.cursor()
logger.debug("Storing feedback in database.")
cur.execute(
sql,
(
feedback["type"],
feedback["score"],
feedback.get("text", ""),
time.ctime(),
),
)
conn.commit()
logger.debug("Feedback stored in database.")
else:
logger.error("Error! cannot create the database connection.")
1 change: 1 addition & 0 deletions app/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ py-readability-metrics
openai
textstat
scikit-learn
streamlit-feedback
9 changes: 9 additions & 0 deletions app/resources/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ spec:
spec:
imagePullSecrets:
- name: dockerhub-secret
volumes:
- name: user-feedback-db
persistentVolumeClaim:
claimName: user-feedback-db
containers:
- name: api-doc-container
image: docker.io/ochatterjee/api-docs-gen:v0.1.0
Expand All @@ -29,6 +33,8 @@ spec:
secretKeyRef:
name: api-doc-secret
key: OPENAI_API_KEY
- name: FEEDBACK_DB
value: /mnt/user-feedback-db/feedback.db
ports:
- containerPort: 8501
resources:
Expand All @@ -38,3 +44,6 @@ spec:
requests:
memory: "200Mi"
cpu: "250m"
volumeMounts:
- mountPath: "/mnt/user-feedback-db"
name: user-feedback-db
11 changes: 11 additions & 0 deletions app/resources/pvc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: user-feedback-db
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
6 changes: 3 additions & 3 deletions app/utils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import json
import os
from openai import OpenAI
from genai import Credentials, Client
from genai.text.generation import TextGenerationParameters
from genai.text.tokenization import (
TextTokenizationParameters,
TextTokenizationReturnOptions,
TextTokenizationCreateResults,
)
import json
from openai import OpenAI
import os


def generate_prompt(
Expand Down
134 changes: 134 additions & 0 deletions notebooks/data-collection/user-feedback.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Analyzing Feedback\n",
"\n",
"This Jupyter notebook demonstrates how to extract\n",
"user feedback from the database we created earlier.\n",
"\n",
"The data is stored in a table which was created with the\n",
"following schema:\n",
"\n",
"```sql\n",
"CREATE TABLE IF NOT EXISTS feedback (\n",
"\tid integer PRIMARY KEY AUTOINCREMENT,\n",
"\ttype text NOT NULL,\n",
"\tscore text NOT NULL,\n",
"\ttext text,\n",
"\ttimestamp text NOT NULL\n",
");\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"import sqlite3\n",
"import os"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"def get_db_file() -> str:\n",
" db_file = os.getenv(\"FEEDBACK_DB\")\n",
" if not db_file:\n",
" db_file = \"/tmp/feedback.db\"\n",
" return db_file\n",
"\n",
"\n",
"def create_connection() -> sqlite3.Connection | None:\n",
" \"\"\"\n",
" Creates connection with the Sqlite database.\n",
" \"\"\"\n",
"\n",
" db_file = get_db_file()\n",
" conn = None\n",
" try:\n",
" conn = sqlite3.connect(db_file)\n",
" except sqlite3.Error as e:\n",
" print(e)\n",
" return conn"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"conn = create_connection()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(4, 'thumbs', '👍', 'The API response is already perfect 👍', 'Tue Jan 16 17:22:09 2024')\n",
"(5, 'thumbs', '👎', 'The API was way too long', 'Tue Jan 16 17:22:50 2024')\n"
]
}
],
"source": [
"conn = create_connection()\n",
"\n",
"# Execute the SELECT * query\n",
"cursor = conn.cursor()\n",
"cursor.execute(\"SELECT * FROM feedback\")\n",
"\n",
"# Fetch all rows from the result set\n",
"rows = cursor.fetchall()\n",
"\n",
"# Print the rows\n",
"for row in rows:\n",
" print(row)\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"# Close the cursor and connection\n",
"cursor.close()\n",
"conn.close()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit b93c425

Please sign in to comment.