Skip to content

Commit

Permalink
Fix resource query (#904)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fluder-Paradyne authored Aug 2, 2023
1 parent 55edc23 commit 2509da4
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 36 deletions.
35 changes: 1 addition & 34 deletions superagi/resource_manager/resource_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,6 @@ 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()

def generate_agent_summary(self, agent_id: int, generate_all: bool = False) -> str:
"""Generate a summary of all resources for an agent."""
Expand All @@ -51,36 +43,11 @@ def generate_agent_summary(self, agent_id: int, generate_all: bool = False) -> s
if not resources:
return

agent = self.session.query(Agent).filter(Agent.id == agent_id).first()
organization = agent.get_agent_organisation(self.session)
model_api_key = Configuration.fetch_configuration(self.session, organization.id, "model_api_key")
model_source = Configuration.fetch_configuration(self.session, organization.id, "model_source")

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

# 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))

resource_summary = " ".join([resource.name for resource in resources])
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_config_resource_summary is not None:
agent_config_resource_summary.value = resource_summary
Expand Down
2 changes: 1 addition & 1 deletion superagi/tools/resource/query_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class QueryResourceTool(BaseTool):
args_schema: Type[BaseModel] = QueryResource
description: str = "Tool searches resources content and extracts relevant information to perform the given task." \
"Tool is given preference over other search/read file tools for relevant data." \
"Resources content includes: {summary}"
"Resources content is taken from the files: {summary}"
agent_id: int = None
llm: Optional[BaseLlm] = None

Expand Down
1 change: 0 additions & 1 deletion superagi/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,4 @@ 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)
session.close()

0 comments on commit 2509da4

Please sign in to comment.