From 0ac1e5f372dbe6baf16f1b0c3d4df1bf5da5efdb Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Sun, 28 Nov 2021 13:13:39 +0530 Subject: [PATCH] Summarize chat logs and notes returned by semantic search via /chat API --- src/main.py | 18 ++++++++++++++---- src/processor/conversation/gpt.py | 4 ++-- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/main.py b/src/main.py index 9eb3734a..ad4ccf9d 100644 --- a/src/main.py +++ b/src/main.py @@ -9,10 +9,10 @@ from fastapi import FastAPI # Internal Packages from src.search_type import asymmetric, symmetric_ledger, image_search -from src.utils.helpers import get_absolute_path +from src.utils.helpers import get_absolute_path, get_from_dict from src.utils.cli import cli from src.utils.config import SearchType, SearchModels, TextSearchConfig, ImageSearchConfig, SearchConfig, ProcessorConfig, ConversationProcessorConfig -from src.processor.conversation.gpt import converse, message_to_log, message_to_prompt, understand +from src.processor.conversation.gpt import converse, message_to_log, message_to_prompt, understand, summarize # Application Global State @@ -95,8 +95,14 @@ def chat(q: str): meta_log = processor_config.conversation.meta_log # Converse with OpenAI GPT - gpt_response = converse(q, chat_log, api_key=processor_config.conversation.openai_api_key) metadata = understand(q, api_key=processor_config.conversation.openai_api_key) + 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]) + gpt_response = summarize(collated_result, metadata["intent"]["memory-type"], user_query=q, api_key=processor_config.conversation.openai_api_key) + else: + gpt_response = converse(q, chat_log, api_key=processor_config.conversation.openai_api_key) # Update Conversation History processor_config.conversation.chat_log = message_to_prompt(q, chat_log, gpt_message=gpt_response) @@ -169,10 +175,14 @@ def shutdown_event(): elif processor_config.conversation.verbose: print('INFO:\tSaving conversation logs to disk...') + # Summarize Conversation Logs for this Session + session_summary = summarize(processor_config.conversation.chat_log, "chat", api_key=processor_config.conversation.openai_api_key) + # Save Conversation Metadata Logs to Disk + conversation_logs = {"session": { "summary": session_summary, "meta": processor_config.conversation.meta_log}}, conversation_logfile = get_absolute_path(processor_config.conversation.conversation_logfile) with open(conversation_logfile, "w+", encoding='utf-8') as logfile: - json.dump(processor_config.conversation.meta_log, logfile) + json.dump(conversation_logs, logfile) print('INFO:\tConversation logs saved to disk.') diff --git a/src/processor/conversation/gpt.py b/src/processor/conversation/gpt.py index ae321237..e08c54bf 100644 --- a/src/processor/conversation/gpt.py +++ b/src/processor/conversation/gpt.py @@ -18,7 +18,7 @@ def summarize(text, summary_type, user_query=None, api_key=None, temperature=0.5 if summary_type == "chat": prompt = f"You are an AI. Summarize the conversation below from your perspective:\n\n{text}\n\nSummarize the conversation from the AI's first-person perspective:" elif summary_type == "notes": - prompt = f"Summarize the below notes about {user_query}:\n\n{text}\n\nSummarize:" + prompt = f"Summarize the below notes about {user_query}:\n\n{text}\n\nSummarize the notes in second person perspective and use past tense:" # Get Response from GPT response = openai.Completion.create( @@ -33,7 +33,7 @@ def summarize(text, summary_type, user_query=None, api_key=None, temperature=0.5 # Extract, Clean Message from GPT's Response story = response['choices'][0]['text'] - return str(story) + return str(story).replace("\n\n", "") def understand(text, api_key=None, temperature=0.5, max_tokens=100):