Simplify in research mode check in api_chat.

- Dedent code for readability
- Use better name for in research mode check
- Continue to remove inferred summarize command when multiple files in
  file filter even when not in research mode
- Continue to show select information source train of thought.
  It was removed by mistake earlier
This commit is contained in:
Debanjum 2024-11-01 11:55:13 -07:00
parent 73750ef286
commit 0b0cfb35e6

View file

@ -693,7 +693,7 @@ async def chat(
meta_log = conversation.conversation_log meta_log = conversation.conversation_log
is_automated_task = conversation_commands == [ConversationCommand.AutomatedTask] is_automated_task = conversation_commands == [ConversationCommand.AutomatedTask]
pending_research = True in_research_mode = False
researched_results = "" researched_results = ""
online_results: Dict = dict() online_results: Dict = dict()
code_results: Dict = dict() code_results: Dict = dict()
@ -712,6 +712,11 @@ async def chat(
agent=agent, agent=agent,
tracer=tracer, tracer=tracer,
) )
conversation_commands_str = ", ".join([cmd.value for cmd in conversation_commands])
async for result in send_event(
ChatEvent.STATUS, f"**Chose Data Sources to Search:** {conversation_commands_str}"
):
yield result
mode = await aget_relevant_output_modes( mode = await aget_relevant_output_modes(
q, meta_log, is_automated_task, user, uploaded_images, agent, tracer=tracer q, meta_log, is_automated_task, user, uploaded_images, agent, tracer=tracer
@ -738,7 +743,7 @@ async def chat(
): ):
if isinstance(research_result, InformationCollectionIteration): if isinstance(research_result, InformationCollectionIteration):
if research_result.summarizedResult: if research_result.summarizedResult:
pending_research = False in_research_mode = True
if research_result.onlineContext: if research_result.onlineContext:
online_results.update(research_result.onlineContext) online_results.update(research_result.onlineContext)
if research_result.codeContext: if research_result.codeContext:
@ -752,11 +757,9 @@ async def chat(
yield research_result yield research_result
# researched_results = await extract_relevant_info(q, researched_results, agent) # researched_results = await extract_relevant_info(q, researched_results, agent)
in_research_mode = False
logger.info(f"Researched Results: {researched_results}") logger.info(f"Researched Results: {researched_results}")
pending_research = False
for cmd in conversation_commands: for cmd in conversation_commands:
await conversation_command_rate_limiter.update_and_check_if_valid(request, cmd) await conversation_command_rate_limiter.update_and_check_if_valid(request, cmd)
q = q.replace(f"/{cmd.value}", "").strip() q = q.replace(f"/{cmd.value}", "").strip()
@ -771,11 +774,9 @@ async def chat(
and not used_slash_summarize and not used_slash_summarize
# but we can't actually summarize # but we can't actually summarize
and len(file_filters) != 1 and len(file_filters) != 1
# not pending research
and not pending_research
): ):
conversation_commands.remove(ConversationCommand.Summarize) conversation_commands.remove(ConversationCommand.Summarize)
elif ConversationCommand.Summarize in conversation_commands and pending_research: elif ConversationCommand.Summarize in conversation_commands and not in_research_mode:
response_log = "" response_log = ""
agent_has_entries = await EntryAdapters.aagent_has_entries(agent) agent_has_entries = await EntryAdapters.aagent_has_entries(agent)
if len(file_filters) == 0 and not agent_has_entries: if len(file_filters) == 0 and not agent_has_entries:
@ -869,7 +870,7 @@ async def chat(
# Gather Context # Gather Context
## Extract Document References ## Extract Document References
if pending_research: if not in_research_mode:
try: try:
async for result in extract_references_and_questions( async for result in extract_references_and_questions(
request, request,
@ -916,99 +917,96 @@ async def chat(
if ConversationCommand.Notes in conversation_commands and is_none_or_empty(compiled_references): if ConversationCommand.Notes in conversation_commands and is_none_or_empty(compiled_references):
conversation_commands.remove(ConversationCommand.Notes) conversation_commands.remove(ConversationCommand.Notes)
if pending_research: ## Gather Online References
## Gather Online References if ConversationCommand.Online in conversation_commands and not in_research_mode:
if ConversationCommand.Online in conversation_commands: try:
try: async for result in search_online(
async for result in search_online( defiltered_query,
defiltered_query, meta_log,
meta_log, location,
location, user,
user, partial(send_event, ChatEvent.STATUS),
partial(send_event, ChatEvent.STATUS), custom_filters,
custom_filters, query_images=uploaded_images,
query_images=uploaded_images, agent=agent,
agent=agent, tracer=tracer,
tracer=tracer, ):
): if isinstance(result, dict) and ChatEvent.STATUS in result:
if isinstance(result, dict) and ChatEvent.STATUS in result: yield result[ChatEvent.STATUS]
yield result[ChatEvent.STATUS] else:
else: online_results = result
online_results = result except Exception as e:
except Exception as e: error_message = f"Error searching online: {e}. Attempting to respond without online results"
error_message = f"Error searching online: {e}. Attempting to respond without online results" logger.warning(error_message)
logger.warning(error_message) async for result in send_event(
async for result in send_event( ChatEvent.STATUS, "Online search failed. I'll try respond without online references"
ChatEvent.STATUS, "Online search failed. I'll try respond without online references" ):
): yield result
yield result
if pending_research: ## Gather Webpage References
## Gather Webpage References if ConversationCommand.Webpage in conversation_commands and not in_research_mode:
if ConversationCommand.Webpage in conversation_commands: try:
try: async for result in read_webpages(
async for result in read_webpages( defiltered_query,
defiltered_query, meta_log,
meta_log, location,
location, user,
user, partial(send_event, ChatEvent.STATUS),
partial(send_event, ChatEvent.STATUS), query_images=uploaded_images,
query_images=uploaded_images, agent=agent,
agent=agent, tracer=tracer,
tracer=tracer, ):
): if isinstance(result, dict) and ChatEvent.STATUS in result:
if isinstance(result, dict) and ChatEvent.STATUS in result: yield result[ChatEvent.STATUS]
yield result[ChatEvent.STATUS] else:
else: direct_web_pages = result
direct_web_pages = result webpages = []
webpages = [] for query in direct_web_pages:
for query in direct_web_pages: if online_results.get(query):
if online_results.get(query): online_results[query]["webpages"] = direct_web_pages[query]["webpages"]
online_results[query]["webpages"] = direct_web_pages[query]["webpages"] else:
else: online_results[query] = {"webpages": direct_web_pages[query]["webpages"]}
online_results[query] = {"webpages": direct_web_pages[query]["webpages"]}
for webpage in direct_web_pages[query]["webpages"]: for webpage in direct_web_pages[query]["webpages"]:
webpages.append(webpage["link"]) webpages.append(webpage["link"])
async for result in send_event(ChatEvent.STATUS, f"**Read web pages**: {webpages}"): async for result in send_event(ChatEvent.STATUS, f"**Read web pages**: {webpages}"):
yield result yield result
except Exception as e: except Exception as e:
logger.warning( logger.warning(
f"Error reading webpages: {e}. Attempting to respond without webpage results", f"Error reading webpages: {e}. Attempting to respond without webpage results",
exc_info=True, exc_info=True,
) )
async for result in send_event( async for result in send_event(
ChatEvent.STATUS, "Webpage read failed. I'll try respond without webpage references" ChatEvent.STATUS, "Webpage read failed. I'll try respond without webpage references"
): ):
yield result yield result
if pending_research: ## Gather Code Results
## Gather Code Results if ConversationCommand.Code in conversation_commands and not in_research_mode:
if ConversationCommand.Code in conversation_commands and pending_research: try:
try: context = f"# Iteration 1:\n#---\nNotes:\n{compiled_references}\n\nOnline Results:{online_results}"
context = f"# Iteration 1:\n#---\nNotes:\n{compiled_references}\n\nOnline Results:{online_results}" async for result in run_code(
async for result in run_code( defiltered_query,
defiltered_query, meta_log,
meta_log, context,
context, location,
location, user,
user, partial(send_event, ChatEvent.STATUS),
partial(send_event, ChatEvent.STATUS), query_images=uploaded_images,
query_images=uploaded_images, agent=agent,
agent=agent, tracer=tracer,
tracer=tracer, ):
): if isinstance(result, dict) and ChatEvent.STATUS in result:
if isinstance(result, dict) and ChatEvent.STATUS in result: yield result[ChatEvent.STATUS]
yield result[ChatEvent.STATUS] else:
else: code_results = result
code_results = result async for result in send_event(ChatEvent.STATUS, f"**Ran code snippets**: {len(code_results)}"):
async for result in send_event(ChatEvent.STATUS, f"**Ran code snippets**: {len(code_results)}"): yield result
yield result except ValueError as e:
except ValueError as e: logger.warning(
logger.warning( f"Failed to use code tool: {e}. Attempting to respond without code results",
f"Failed to use code tool: {e}. Attempting to respond without code results", exc_info=True,
exc_info=True, )
)
## Send Gathered References ## Send Gathered References
async for result in send_event( async for result in send_event(