coding-alt/AutoGPT
0
1import random2import string3import sys4import unittest5from pathlib import Path6 7from autogpt.config import Config8from autogpt.memory.local import LocalCache9 10 11class TestLocalCache(unittest.TestCase):12 def random_string(self, length):13 return "".join(random.choice(string.ascii_letters) for _ in range(length))14 15 def setUp(self):16 cfg = cfg = Config()17 self.cache = LocalCache(cfg)18 self.cache.clear()19 20 # Add example texts to the cache21 self.example_texts = [22 "The quick brown fox jumps over the lazy dog",23 "I love machine learning and natural language processing",24 "The cake is a lie, but the pie is always true",25 "ChatGPT is an advanced AI model for conversation",26 ]27 28 for text in self.example_texts:29 self.cache.add(text)30 31 # Add some random strings to test noise32 for _ in range(5):33 self.cache.add(self.random_string(10))34 35 def test_get_relevant(self):36 query = "I'm interested in artificial intelligence and NLP"37 k = 338 relevant_texts = self.cache.get_relevant(query, k)39 40 print(f"Top {k} relevant texts for the query '{query}':")41 for i, text in enumerate(relevant_texts, start=1):42 print(f"{i}. {text}")43 44 self.assertEqual(len(relevant_texts), k)45 self.assertIn(self.example_texts[1], relevant_texts)46 47 48if __name__ == "__main__":49 unittest.main()50 