Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix resource query #904

Merged
merged 6 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 27 additions & 26 deletions superagi/resource_manager/resource_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ def add_to_vector_store_and_create_summary(self, agent_id: int, resource_id: int
ResourceManager(str(agent_id)).save_document_to_vector_store(documents, str(resource_id), model_api_key, model_source)
except Exception as e:
logger.error("add_to_vector_store_and_create_summary: Unable to save document to vector store.", e)
summary = None
try:
summary = LlamaDocumentSummary(model_api_key=model_api_key).generate_summary_of_document(documents)
except Exception as e:
logger.error("add_to_vector_store_and_create_summary - Unable to generate summary of document.", e)
resource = self.session.query(Resource).filter(Resource.id == resource_id).first()
resource.summary = summary
self.session.commit()
# summary = None
Fluder-Paradyne marked this conversation as resolved.
Show resolved Hide resolved
# try:
# summary = LlamaDocumentSummary(model_api_key=model_api_key).generate_summary_of_document(documents)
# except Exception as e:
# logger.error("add_to_vector_store_and_create_summary - Unable to generate summary of document.", e)
# resource = self.session.query(Resource).filter(Resource.id == resource_id).first()
# resource.summary = summary
# self.session.commit()

def generate_agent_summary(self, agent_id: int, generate_all: bool = False) -> str:
"""Generate a summary of all resources for an agent."""
Expand All @@ -58,29 +58,30 @@ def generate_agent_summary(self, agent_id: int, generate_all: bool = False) -> s

summary_texts = [resource.summary for resource in resources if resource.summary is not None]

resource_summary = " ".join([resource.name for resource in resources])
Fluder-Paradyne marked this conversation as resolved.
Show resolved Hide resolved
# generate_all is added because we want to generate summary for all resources when agent is created
# this is set to false when adding individual resources
if len(summary_texts) < len(resources) and generate_all:
file_paths = [resource.path for resource in resources if resource.summary is None]
for file_path in file_paths:
if resources[0].storage_type == 'S3':
documents = ResourceManager(str(agent_id)).create_llama_document_s3(file_path)
else:
documents = ResourceManager(str(agent_id)).create_llama_document(file_path)
if documents is not None and len(documents) > 0:
summary_texts.append(LlamaDocumentSummary(model_api_key=model_api_key, model_source=model_source).generate_summary_of_document(documents))

# if len(summary_texts) < len(resources) and generate_all:
Fluder-Paradyne marked this conversation as resolved.
Show resolved Hide resolved
# file_paths = [resource.path for resource in resources if resource.summary is None]
# for file_path in file_paths:
# if resources[0].storage_type == 'S3':
# documents = ResourceManager(str(agent_id)).create_llama_document_s3(file_path)
# else:
# documents = ResourceManager(str(agent_id)).create_llama_document(file_path)
# if documents is not None and len(documents) > 0:
# summary_texts.append(LlamaDocumentSummary(model_api_key=model_api_key, model_source=model_source).generate_summary_of_document(documents))
#
agent_last_resource = self.session.query(AgentConfiguration). \
filter(AgentConfiguration.agent_id == agent_id,
AgentConfiguration.key == "last_resource_time").first()
if agent_last_resource is not None and \
datetime.strptime(agent_last_resource.value, '%Y-%m-%d %H:%M:%S.%f') == resources[-1].updated_at \
and not generate_all:
return

resource_summary = summary_texts[0] if summary_texts else None
if len(summary_texts) > 1:
resource_summary = LlamaDocumentSummary(model_api_key=model_api_key).generate_summary_of_texts(summary_texts)
# if agent_last_resource is not None and \
Fluder-Paradyne marked this conversation as resolved.
Show resolved Hide resolved
# datetime.strptime(agent_last_resource.value, '%Y-%m-%d %H:%M:%S.%f') == resources[-1].updated_at \
# and not generate_all:
# return
#
# resource_summary = summary_texts[0] if summary_texts else None
# if len(summary_texts) > 1:
# resource_summary = LlamaDocumentSummary(model_api_key=model_api_key).generate_summary_of_texts(summary_texts)

if agent_config_resource_summary is not None:
agent_config_resource_summary.value = resource_summary
Expand Down
2 changes: 1 addition & 1 deletion superagi/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,5 @@ def summarize_resource(agent_id: int, resource_id: int):
resource_summarizer.add_to_vector_store_and_create_summary(agent_id=agent_id,
resource_id=resource_id,
documents=documents)
resource_summarizer.generate_agent_summary(agent_id=agent_id)
# resource_summarizer.generate_agent_summary(agent_id=agent_id)
session.close()