condenser.py 926 B

123456789101112131415161718192021222324
  1. from openhands.core.logger import openhands_logger as logger
  2. from openhands.llm.llm import LLM
  3. class MemoryCondenser:
  4. def condense(self, summarize_prompt: str, llm: LLM):
  5. """Attempts to condense the memory by using the llm
  6. Parameters:
  7. - llm (LLM): llm to be used for summarization
  8. Raises:
  9. - Exception: the same exception as it got from the llm or processing the response
  10. """
  11. try:
  12. messages = [{'content': summarize_prompt, 'role': 'user'}]
  13. resp = llm.completion(messages=messages)
  14. summary_response = resp['choices'][0]['message']['content']
  15. return summary_response
  16. except Exception as e:
  17. logger.error('Error condensing thoughts: %s', str(e), exc_info=False)
  18. # TODO If the llm fails with ContextWindowExceededError, we can try to condense the memory chunk by chunk
  19. raise