Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,57 +1,171 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
from langchain.agents.format_scratchpad import format_log_to_str
|
| 4 |
-
from langchain.agents.output_parsers import ReActJsonSingleInputOutputParser
|
| 5 |
-
from langchain.tools.render import render_text_description
|
| 6 |
-
from langchain_community.llms import HuggingFaceHub
|
| 7 |
-
from langchain_community.utilities import SerpAPIWrapper
|
| 8 |
-
from langchain_community.chat_models.huggingface import ChatHuggingFace
|
| 9 |
import chainlit as cl
|
| 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 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import chainlit as cl
|
| 4 |
|
| 5 |
+
from embedchain import Pipeline as App
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
try:
|
| 10 |
+
HF_TOKEN = os.environ['HUGGINGFACEHUB_API_TOKEN']
|
| 11 |
+
if HF_TOKEN is None:
|
| 12 |
+
raise ValueError('HUGGINGFACE_API_KEY is not set')
|
| 13 |
+
except Exception as err:
|
| 14 |
+
raise(err)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class DatabaseError(Exception):
|
| 18 |
+
pass
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class JSONDB:
|
| 22 |
+
def __init__(self, file_path):
|
| 23 |
+
self.file_path = file_path
|
| 24 |
+
|
| 25 |
+
def _create_file_if_not_exists(self):
|
| 26 |
+
if not os.path.exists(self.file_path):
|
| 27 |
+
with open(self.file_path, 'w') as fp:
|
| 28 |
+
json.dump([], fp)
|
| 29 |
+
|
| 30 |
+
def add_record(self, record):
|
| 31 |
+
try:
|
| 32 |
+
self._create_file_if_not_exists()
|
| 33 |
+
|
| 34 |
+
with open(self.file_path, 'r+') as fp:
|
| 35 |
+
try:
|
| 36 |
+
data = json.load(fp)
|
| 37 |
+
|
| 38 |
+
if record not in data:
|
| 39 |
+
data.append(record)
|
| 40 |
+
else:
|
| 41 |
+
pass
|
| 42 |
+
except Exception as err:
|
| 43 |
+
print(f'[DEBUG] Error adding record: {str(err)}')
|
| 44 |
+
raise(err)
|
| 45 |
+
|
| 46 |
+
fp.seek(0)
|
| 47 |
+
json.dump(data, fp, indent=4)
|
| 48 |
+
|
| 49 |
+
except (FileNotFoundError, json.JSONDecodeError, IOError) as e:
|
| 50 |
+
raise DatabaseError(f"Error adding record: {str(e)}")
|
| 51 |
+
|
| 52 |
+
def get_all_records(self):
|
| 53 |
+
try:
|
| 54 |
+
with open(self.file_path, 'r') as fp:
|
| 55 |
+
# Attempt to load data, handle empty file scenario
|
| 56 |
+
try:
|
| 57 |
+
data = json.load(fp)
|
| 58 |
+
except json.JSONDecodeError:
|
| 59 |
+
data = []
|
| 60 |
+
|
| 61 |
+
return data
|
| 62 |
+
|
| 63 |
+
except (FileNotFoundError, IOError) as e:
|
| 64 |
+
raise DatabaseError(f"Error getting all records: {str(e)}")
|
| 65 |
+
|
| 66 |
+
def get_top_records(self, n):
|
| 67 |
+
try:
|
| 68 |
+
records = self.get_all_records()
|
| 69 |
+
sorted_records = sorted(records, key=lambda x: x.get('added', 0), reverse=True)
|
| 70 |
+
return sorted_records[:n]
|
| 71 |
+
except (FileNotFoundError, json.JSONDecodeError, IOError) as e:
|
| 72 |
+
raise DatabaseError(f"Error getting top records: {str(e)}")
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
@cl.on_chat_start
|
| 76 |
+
async def setup_app():
|
| 77 |
+
app = App.from_config(config_path='data/config.yaml')
|
| 78 |
+
app.collect_metrics = False
|
| 79 |
+
cl.user_session.set('app', app)
|
| 80 |
+
db = JSONDB('data/index.json')
|
| 81 |
+
cl.user_session.set('db', db)
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def update_db(data):
|
| 85 |
+
db = cl.user_session.get('db')
|
| 86 |
+
record = {
|
| 87 |
+
'url': data, # Store the URL as a JSON field
|
| 88 |
+
'added': datetime.now().strftime('%d/%m/%Y %H:%M:%S')
|
| 89 |
}
|
| 90 |
+
db.add_record(record)
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
@cl.on_message
|
| 94 |
+
async def main(message: cl.Message):
|
| 95 |
+
task_list = cl.TaskList()
|
| 96 |
+
task_list.status = 'Running...'
|
| 97 |
+
|
| 98 |
+
app = cl.user_session.get('app')
|
| 99 |
+
msg = cl.Message(content='')
|
| 100 |
+
|
| 101 |
+
user_message = message.content
|
| 102 |
+
|
| 103 |
+
if user_message.startswith('/help'):
|
| 104 |
+
markdown_content = "| Command | Description |\n| --- | --- |\n"
|
| 105 |
+
markdown_content += "| /add | Add a document to the knowledge base |\n"
|
| 106 |
+
markdown_content += "| /kb | Display the knowledge base |\n"
|
| 107 |
+
markdown_content += "| /help | Display the available commands |\n"
|
| 108 |
+
markdown_content += "| * | Chat with the AI |\n"
|
| 109 |
+
await cl.Message(
|
| 110 |
+
content=markdown_content
|
| 111 |
+
).send()
|
| 112 |
+
|
| 113 |
+
elif user_message.startswith('/add'):
|
| 114 |
+
data = user_message.replace('/add', '').strip()
|
| 115 |
+
db = cl.user_session.get('db')
|
| 116 |
+
|
| 117 |
+
records = db.get_all_records()
|
| 118 |
+
if data in [record['url'] for record in records]:
|
| 119 |
+
await cl.Message(
|
| 120 |
+
content='This document already exists in the knowledge base!'
|
| 121 |
+
).send()
|
| 122 |
+
else:
|
| 123 |
+
add_task = cl.Task(title='Adding to knowledge base', status=cl.TaskStatus.RUNNING)
|
| 124 |
+
await task_list.add_task(add_task)
|
| 125 |
+
await task_list.send()
|
| 126 |
+
|
| 127 |
+
app.add(data)
|
| 128 |
+
update_db(data)
|
| 129 |
+
add_task.status = cl.TaskStatus.DONE
|
| 130 |
+
await task_list.send()
|
| 131 |
+
|
| 132 |
+
await cl.Message(
|
| 133 |
+
content='Added data to knowledge base!'
|
| 134 |
+
).send()
|
| 135 |
+
|
| 136 |
+
elif user_message.startswith('/kb'):
|
| 137 |
+
kb_task = cl.Task(title='Getting records', status=cl.TaskStatus.RUNNING)
|
| 138 |
+
await task_list.add_task(kb_task)
|
| 139 |
+
await task_list.send()
|
| 140 |
+
|
| 141 |
+
data = cl.user_session.get('db').get_top_records(25)
|
| 142 |
+
kb_task.status = cl.TaskStatus.DONE
|
| 143 |
+
await task_list.send()
|
| 144 |
+
|
| 145 |
+
if len(data) == 0:
|
| 146 |
+
await cl.Message(
|
| 147 |
+
content='No documents in json index!'
|
| 148 |
+
).send()
|
| 149 |
+
|
| 150 |
+
else:
|
| 151 |
+
markdown_content = "| URL | Added |\n| --- | --- |\n"
|
| 152 |
+
for record in data:
|
| 153 |
+
url = record['url']
|
| 154 |
+
added = record['added']
|
| 155 |
+
markdown_content += f"| {url} | {added} |\n"
|
| 156 |
+
await cl.Message(
|
| 157 |
+
content=markdown_content
|
| 158 |
+
).send()
|
| 159 |
+
|
| 160 |
+
else:
|
| 161 |
+
chat_task = cl.Task(title='Querying LLM', status=cl.TaskStatus.RUNNING)
|
| 162 |
+
await task_list.add_task(chat_task)
|
| 163 |
+
await task_list.send()
|
| 164 |
+
|
| 165 |
+
for chunk in await cl.make_async(app.chat)(message.content):
|
| 166 |
+
await msg.stream_token(chunk)
|
| 167 |
+
|
| 168 |
+
chat_task.status = cl.TaskStatus.DONE
|
| 169 |
+
await task_list.send()
|
| 170 |
+
|
| 171 |
+
await msg.send()
|