Asyncio with Parallel calls = all your data at once returned in smallest time frame.
Here this will help many:
async def fetch_data_and_summarize(self, username):
try:
# Parallel fetch operations
pets_data, preferences_data, people_data, relationship_data, mental_health_data, physical_health_data = await asyncio.gather(
self.agent.fetch_pets_data_for_user(username),
self.agent.fetch_preferences_data_for_user(username),
self.agent.fetch_people_data_for_user(username),
self.agent.fetch_relationship_data_for_user(username),
self.agent.fetch_mental_health_data_for_user(username),
self.agent.fetch_physical_health_data_for_user(username)
)
# Generate summaries for each category using GPT-3.5-turbo
summaries = await asyncio.gather(
self._generate_summary(pets_data, "pets", username),
self._generate_summary(preferences_data, "preferences"),
self._generate_summary(people_data, "people"),
self._generate_summary(relationship_data, "relationship"),
self._generate_health_summary(mental_health_data, physical_health_data)
)
summaries_dict = {"pets": summaries[0], "preferences": summaries[1], "people": summaries[2], "relationship": summaries[3], "health": summaries[4]}
await self.agent.update_user_profile_with_summaries(username, summaries_dict)
except Exception as e:
logging.error(f"Error summarizing data for user {username}: {e}")
async def _generate_summary(self, data, category, username=None):
if not data:
return "No data available."
if category == "people" or category == "pets":
# Adjusting prompt for detailed list generation
prompt = f"Given the following data for {category}, generate a detailed list including every known detail about each entity. Ensure no entity is omitted and provide insights that encapsulate their characteristics, relationships, and any notable anecdotes. {category.capitalize()} data for user {username}: {data}"
else:
prompt = f"Please summarize the following data to extract key insights and provide a comprehensive understanding as a data point for the AI system, minimizing duplication and maximizing clarity: [{category}]: {data}"
try:
response = await self.async_client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "system", "content": prompt}],
max_tokens=200 # Increased token count for more detailed summaries
)
if response.choices:
return response.choices[0].message.content.strip()
else:
logging.error(f"No valid choice in the OpenAI response for {category}")
return f"Summary generation failed for {category}."
except Exception as e:
logging.error(f"Error generating summary for {category}: {e}")
return f"Summary generation failed for {category}."
Basically this is a simple category system that calls the same function to summarize based on the classification.
you can also do same concept using a list of prompts and passing each through the same system in similar way to get what you want. be sure to check out kruel.ai a full working memex system on gpt3.5 16k token model as it’s the cheapest model to get the most bang for the $