mirror of
https://github.com/khoj-ai/khoj.git
synced 2024-11-23 15:38:55 +01:00
Summarize chat logs and notes returned by semantic search via /chat API
This commit is contained in:
parent
5cd920544d
commit
0ac1e5f372
2 changed files with 16 additions and 6 deletions
18
src/main.py
18
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.')
|
||||
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Reference in a new issue