2021-08-17 03:59:58 -07:00
|
|
|
# Standard Packages
|
2021-11-28 12:05:04 -05:00
|
|
|
import sys, json, yaml
|
2021-08-15 17:50:08 -07:00
|
|
|
from typing import Optional
|
2021-08-17 03:59:58 -07:00
|
|
|
|
|
|
|
# External Packages
|
|
|
|
import uvicorn
|
2021-11-28 12:05:04 -05:00
|
|
|
from fastapi import FastAPI, Request
|
2021-11-26 14:51:11 -05:00
|
|
|
from fastapi.responses import HTMLResponse
|
2021-11-27 10:49:33 -05:00
|
|
|
from fastapi.staticfiles import StaticFiles
|
2021-11-26 14:51:11 -05:00
|
|
|
from fastapi.templating import Jinja2Templates
|
2021-08-17 03:59:58 -07:00
|
|
|
|
|
|
|
# Internal Packages
|
2021-09-30 04:12:14 -07:00
|
|
|
from src.search_type import asymmetric, symmetric_ledger, image_search
|
2021-11-28 13:13:39 +05:30
|
|
|
from src.utils.helpers import get_absolute_path, get_from_dict
|
2021-09-30 04:12:14 -07:00
|
|
|
from src.utils.cli import cli
|
2021-12-09 08:50:38 -05:00
|
|
|
from src.utils.config import SearchType, SearchModels, ProcessorConfigModel, ConversationProcessorConfigModel
|
|
|
|
from src.utils.rawconfig import FullConfig
|
2021-11-28 13:13:39 +05:30
|
|
|
from src.processor.conversation.gpt import converse, message_to_log, message_to_prompt, understand, summarize
|
2021-08-17 03:59:58 -07:00
|
|
|
|
2021-09-29 20:24:27 -07:00
|
|
|
# Application Global State
|
2021-12-11 14:13:14 -05:00
|
|
|
config = FullConfig()
|
2021-09-29 20:24:27 -07:00
|
|
|
model = SearchModels()
|
2021-12-09 08:50:38 -05:00
|
|
|
processor_config = ProcessorConfigModel()
|
2021-11-28 12:34:40 -05:00
|
|
|
config_file = ""
|
2021-12-04 11:43:48 -05:00
|
|
|
verbose = 0
|
2021-08-15 17:50:08 -07:00
|
|
|
app = FastAPI()
|
|
|
|
|
2021-11-27 10:49:33 -05:00
|
|
|
app.mount("/views", StaticFiles(directory="views"), name="views")
|
2021-11-26 14:51:11 -05:00
|
|
|
templates = Jinja2Templates(directory="views/")
|
|
|
|
|
|
|
|
@app.get('/ui', response_class=HTMLResponse)
|
|
|
|
def ui(request: Request):
|
|
|
|
return templates.TemplateResponse("config.html", context={'request': request})
|
2021-08-15 17:50:08 -07:00
|
|
|
|
2021-12-09 08:50:38 -05:00
|
|
|
@app.get('/config', response_model=FullConfig)
|
2021-12-11 14:13:14 -05:00
|
|
|
def config_data():
|
2021-11-27 19:17:15 -05:00
|
|
|
return config
|
|
|
|
|
|
|
|
@app.post('/config')
|
2021-12-11 14:13:14 -05:00
|
|
|
async def config_data(updated_config: FullConfig):
|
2021-11-28 13:28:22 -05:00
|
|
|
global config
|
|
|
|
config = updated_config
|
2021-11-28 12:34:40 -05:00
|
|
|
with open(config_file, 'w') as outfile:
|
2021-11-28 13:28:22 -05:00
|
|
|
yaml.dump(yaml.safe_load(config.json(by_alias=True)), outfile)
|
2021-11-28 12:26:07 -05:00
|
|
|
outfile.close()
|
2021-11-28 13:28:22 -05:00
|
|
|
return config
|
2021-08-15 17:50:08 -07:00
|
|
|
|
|
|
|
@app.get('/search')
|
2021-09-29 19:02:55 -07:00
|
|
|
def search(q: str, n: Optional[int] = 5, t: Optional[SearchType] = None):
|
2021-08-15 17:50:08 -07:00
|
|
|
if q is None or q == '':
|
|
|
|
print(f'No query param (q) passed in API call to initiate search')
|
|
|
|
return {}
|
|
|
|
|
|
|
|
user_query = q
|
|
|
|
results_count = n
|
|
|
|
|
2021-09-30 02:04:04 -07:00
|
|
|
if (t == SearchType.Notes or t == None) and model.notes_search:
|
2021-08-15 17:50:08 -07:00
|
|
|
# query notes
|
2021-09-30 02:04:04 -07:00
|
|
|
hits = asymmetric.query(user_query, model.notes_search)
|
2021-08-15 17:50:08 -07:00
|
|
|
|
|
|
|
# collate and return results
|
2021-09-29 20:24:27 -07:00
|
|
|
return asymmetric.collate_results(hits, model.notes_search.entries, results_count)
|
2021-08-15 17:50:08 -07:00
|
|
|
|
2021-09-30 02:04:04 -07:00
|
|
|
if (t == SearchType.Music or t == None) and model.music_search:
|
2021-08-29 03:07:36 -07:00
|
|
|
# query music library
|
2021-09-30 02:04:04 -07:00
|
|
|
hits = asymmetric.query(user_query, model.music_search)
|
2021-08-29 03:07:36 -07:00
|
|
|
|
|
|
|
# collate and return results
|
2021-09-29 21:09:42 -07:00
|
|
|
return asymmetric.collate_results(hits, model.music_search.entries, results_count)
|
2021-08-29 03:07:36 -07:00
|
|
|
|
2021-09-30 02:04:04 -07:00
|
|
|
if (t == SearchType.Ledger or t == None) and model.ledger_search:
|
2021-08-22 03:16:57 -07:00
|
|
|
# query transactions
|
2021-09-30 02:04:04 -07:00
|
|
|
hits = symmetric_ledger.query(user_query, model.ledger_search)
|
2021-08-22 03:16:57 -07:00
|
|
|
|
|
|
|
# collate and return results
|
2021-09-29 21:09:42 -07:00
|
|
|
return symmetric_ledger.collate_results(hits, model.ledger_search.entries, results_count)
|
2021-08-22 03:16:57 -07:00
|
|
|
|
2021-09-30 02:04:04 -07:00
|
|
|
if (t == SearchType.Image or t == None) and model.image_search:
|
2021-08-22 21:00:54 -07:00
|
|
|
# query transactions
|
2021-09-30 02:04:04 -07:00
|
|
|
hits = image_search.query(user_query, results_count, model.image_search)
|
2021-08-22 21:00:54 -07:00
|
|
|
|
|
|
|
# collate and return results
|
|
|
|
return image_search.collate_results(
|
|
|
|
hits,
|
2021-09-29 21:09:42 -07:00
|
|
|
model.image_search.image_names,
|
2021-12-04 11:43:48 -05:00
|
|
|
config.content_type.image.input_directory,
|
2021-08-22 21:00:54 -07:00
|
|
|
results_count)
|
|
|
|
|
2021-08-15 17:50:08 -07:00
|
|
|
else:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
2021-08-16 18:52:38 -07:00
|
|
|
@app.get('/regenerate')
|
2021-09-29 19:02:55 -07:00
|
|
|
def regenerate(t: Optional[SearchType] = None):
|
2021-12-11 14:13:14 -05:00
|
|
|
initialize_search(config, regenerate=True, t=t)
|
2021-08-16 23:47:33 -07:00
|
|
|
return {'status': 'ok', 'message': 'regeneration completed'}
|
2021-08-16 18:52:38 -07:00
|
|
|
|
|
|
|
|
2021-11-27 01:27:46 +05:30
|
|
|
@app.get('/chat')
|
|
|
|
def chat(q: str):
|
|
|
|
# Load Conversation History
|
2021-12-08 02:34:52 +05:30
|
|
|
chat_session = processor_config.conversation.chat_session
|
2021-11-28 00:04:39 +05:30
|
|
|
meta_log = processor_config.conversation.meta_log
|
2021-11-27 01:27:46 +05:30
|
|
|
|
|
|
|
# Converse with OpenAI GPT
|
2022-01-12 09:06:32 -05:00
|
|
|
metadata = understand(q, api_key=processor_config.conversation.openai_api_key, verbose=verbose)
|
|
|
|
if verbose > 1:
|
|
|
|
print(f'Understood: {get_from_dict(metadata, "intent")}')
|
|
|
|
|
2021-11-28 13:13:39 +05:30
|
|
|
if get_from_dict(metadata, "intent", "memory-type") == "notes":
|
|
|
|
query = get_from_dict(metadata, "intent", "query")
|
|
|
|
result_list = search(query, n=1, t=SearchType.Notes)
|
|
|
|
collated_result = "\n".join([item["Entry"] for item in result_list])
|
2022-01-12 09:06:32 -05:00
|
|
|
if verbose > 1:
|
|
|
|
print(f'Semantically Similar Notes:\n{collated_result}')
|
2021-12-08 02:34:52 +05:30
|
|
|
gpt_response = summarize(collated_result, summary_type="notes", user_query=q, api_key=processor_config.conversation.openai_api_key)
|
2021-11-28 13:13:39 +05:30
|
|
|
else:
|
2021-12-08 02:34:52 +05:30
|
|
|
gpt_response = converse(q, chat_session, api_key=processor_config.conversation.openai_api_key)
|
2021-11-27 01:27:46 +05:30
|
|
|
|
|
|
|
# Update Conversation History
|
2021-12-08 02:34:52 +05:30
|
|
|
processor_config.conversation.chat_session = message_to_prompt(q, chat_session, gpt_message=gpt_response)
|
|
|
|
processor_config.conversation.meta_log['chat'] = message_to_log(q, metadata, gpt_response, meta_log.get('chat', []))
|
2021-11-27 01:27:46 +05:30
|
|
|
|
|
|
|
return {'status': 'ok', 'response': gpt_response}
|
|
|
|
|
|
|
|
|
2021-12-09 08:50:38 -05:00
|
|
|
def initialize_search(config: FullConfig, regenerate: bool, t: SearchType = None):
|
2021-09-30 02:04:04 -07:00
|
|
|
model = SearchModels()
|
2021-08-21 18:47:55 -07:00
|
|
|
|
2021-08-22 03:16:57 -07:00
|
|
|
# Initialize Org Notes Search
|
2021-12-04 11:43:48 -05:00
|
|
|
if (t == SearchType.Notes or t == None) and config.content_type.org:
|
|
|
|
# Extract Entries, Generate Notes Embeddings
|
2022-01-14 16:31:55 -05:00
|
|
|
model.notes_search = asymmetric.setup(config.content_type.org, search_config=config.search_type.asymmetric, regenerate=regenerate, verbose=verbose)
|
2021-08-15 17:50:08 -07:00
|
|
|
|
2021-08-29 03:07:36 -07:00
|
|
|
# Initialize Org Music Search
|
2021-12-04 11:43:48 -05:00
|
|
|
if (t == SearchType.Music or t == None) and config.content_type.music:
|
|
|
|
# Extract Entries, Generate Music Embeddings
|
2022-01-14 16:31:55 -05:00
|
|
|
model.music_search = asymmetric.setup(config.content_type.music, search_config=config.search_type.asymmetric, regenerate=regenerate, verbose=verbose)
|
2021-08-29 03:07:36 -07:00
|
|
|
|
2021-08-22 03:16:57 -07:00
|
|
|
# Initialize Ledger Search
|
2021-12-04 11:43:48 -05:00
|
|
|
if (t == SearchType.Ledger or t == None) and config.content_type.ledger:
|
|
|
|
# Extract Entries, Generate Ledger Embeddings
|
2022-01-14 16:46:56 -05:00
|
|
|
model.ledger_search = symmetric_ledger.setup(config.content_type.ledger, search_config=config.search_type.symmetric, regenerate=regenerate, verbose=verbose)
|
2021-08-15 17:50:08 -07:00
|
|
|
|
2021-08-22 21:00:54 -07:00
|
|
|
# Initialize Image Search
|
2021-12-04 11:43:48 -05:00
|
|
|
if (t == SearchType.Image or t == None) and config.content_type.image:
|
|
|
|
# Extract Entries, Generate Image Embeddings
|
|
|
|
model.image_search = image_search.setup(config.content_type.image, regenerate=regenerate, verbose=verbose)
|
2021-09-30 02:04:04 -07:00
|
|
|
|
2021-12-04 11:43:48 -05:00
|
|
|
return model
|
2021-09-30 02:04:04 -07:00
|
|
|
|
|
|
|
|
2021-12-09 08:50:38 -05:00
|
|
|
def initialize_processor(config: FullConfig):
|
2021-12-04 10:11:00 -05:00
|
|
|
if not config.processor:
|
|
|
|
return
|
2021-12-20 13:26:35 +05:30
|
|
|
|
2021-12-09 08:50:38 -05:00
|
|
|
processor_config = ProcessorConfigModel()
|
2021-09-30 02:04:04 -07:00
|
|
|
|
2021-11-27 01:27:46 +05:30
|
|
|
# Initialize Conversation Processor
|
2021-12-09 08:50:38 -05:00
|
|
|
processor_config.conversation = ConversationProcessorConfigModel(config.processor.conversation, verbose)
|
2021-11-27 01:27:46 +05:30
|
|
|
|
|
|
|
conversation_logfile = processor_config.conversation.conversation_logfile
|
|
|
|
if processor_config.conversation.verbose:
|
2021-11-28 00:04:39 +05:30
|
|
|
print('INFO:\tLoading conversation logs from disk...')
|
2021-11-27 01:27:46 +05:30
|
|
|
|
|
|
|
if conversation_logfile.expanduser().absolute().is_file():
|
2021-11-28 00:04:39 +05:30
|
|
|
# Load Metadata Logs from Conversation Logfile
|
2021-11-27 01:27:46 +05:30
|
|
|
with open(get_absolute_path(conversation_logfile), 'r') as f:
|
2021-11-28 00:04:39 +05:30
|
|
|
processor_config.conversation.meta_log = json.load(f)
|
|
|
|
|
|
|
|
print('INFO:\tConversation logs loaded from disk.')
|
2021-11-27 01:27:46 +05:30
|
|
|
else:
|
2021-11-28 00:04:39 +05:30
|
|
|
# Initialize Conversation Logs
|
2021-12-08 02:34:52 +05:30
|
|
|
processor_config.conversation.meta_log = {}
|
|
|
|
processor_config.conversation.chat_session = ""
|
2021-11-27 01:27:46 +05:30
|
|
|
|
|
|
|
return processor_config
|
|
|
|
|
|
|
|
|
|
|
|
@app.on_event('shutdown')
|
|
|
|
def shutdown_event():
|
2021-11-28 00:04:39 +05:30
|
|
|
# No need to create empty log file
|
|
|
|
if not processor_config.conversation.meta_log:
|
|
|
|
return
|
|
|
|
elif processor_config.conversation.verbose:
|
|
|
|
print('INFO:\tSaving conversation logs to disk...')
|
2021-11-27 01:27:46 +05:30
|
|
|
|
2021-11-28 13:13:39 +05:30
|
|
|
# Summarize Conversation Logs for this Session
|
2021-12-08 02:34:52 +05:30
|
|
|
chat_session = processor_config.conversation.chat_session
|
|
|
|
openai_api_key = processor_config.conversation.openai_api_key
|
|
|
|
conversation_log = processor_config.conversation.meta_log
|
|
|
|
session = {
|
|
|
|
"summary": summarize(chat_session, summary_type="chat", api_key=openai_api_key),
|
|
|
|
"session-start": conversation_log.get("session", [{"session-end": 0}])[-1]["session-end"],
|
|
|
|
"session-end": len(conversation_log["chat"])
|
|
|
|
}
|
|
|
|
if 'session' in conversation_log:
|
|
|
|
conversation_log['session'].append(session)
|
|
|
|
else:
|
|
|
|
conversation_log['session'] = [session]
|
2021-11-28 13:13:39 +05:30
|
|
|
|
2021-11-28 00:04:39 +05:30
|
|
|
# Save Conversation Metadata Logs to Disk
|
2021-11-27 01:27:46 +05:30
|
|
|
conversation_logfile = get_absolute_path(processor_config.conversation.conversation_logfile)
|
|
|
|
with open(conversation_logfile, "w+", encoding='utf-8') as logfile:
|
2021-12-08 02:34:52 +05:30
|
|
|
json.dump(conversation_log, logfile)
|
2021-11-27 01:27:46 +05:30
|
|
|
|
2021-11-28 00:04:39 +05:30
|
|
|
print('INFO:\tConversation logs saved to disk.')
|
2021-11-27 01:27:46 +05:30
|
|
|
|
|
|
|
|
2021-09-30 02:04:04 -07:00
|
|
|
if __name__ == '__main__':
|
|
|
|
# Load config from CLI
|
|
|
|
args = cli(sys.argv[1:])
|
|
|
|
|
2021-11-28 12:34:40 -05:00
|
|
|
# Stores the file path to the config file.
|
|
|
|
config_file = args.config_file
|
2021-09-30 02:04:04 -07:00
|
|
|
|
2021-12-04 11:43:48 -05:00
|
|
|
# Store the verbose flag
|
|
|
|
verbose = args.verbose
|
|
|
|
|
2021-11-28 12:34:40 -05:00
|
|
|
# Store the raw config data.
|
2021-11-27 10:49:33 -05:00
|
|
|
config = args.config
|
|
|
|
|
2021-12-04 11:43:48 -05:00
|
|
|
# Initialize the search model from Config
|
2021-12-09 08:50:38 -05:00
|
|
|
model = initialize_search(args.config, args.regenerate)
|
2021-08-22 21:00:54 -07:00
|
|
|
|
2021-11-27 01:27:46 +05:30
|
|
|
# Initialize Processor from Config
|
2021-12-09 08:50:38 -05:00
|
|
|
processor_config = initialize_processor(args.config)
|
2021-11-27 01:27:46 +05:30
|
|
|
|
2021-08-15 17:50:08 -07:00
|
|
|
# Start Application Server
|
2021-10-02 16:16:33 -07:00
|
|
|
if args.socket:
|
|
|
|
uvicorn.run(app, proxy_headers=True, uds=args.socket)
|
|
|
|
else:
|
|
|
|
uvicorn.run(app, host=args.host, port=args.port)
|