File size: 8,167 Bytes
533e375
e5e4fa5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
533e375
 
 
 
e5e4fa5
 
 
533e375
6f0338c
 
 
 
 
 
 
 
533e375
6f0338c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e5e4fa5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
533e375
e5e4fa5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
533e375
 
6f0338c
e5e4fa5
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
from model import AIModel
import random
import os
import requests
from bs4 import BeautifulSoup

def fetch_wikipedia_text(title):
    url = f"https://en.wikipedia.org/api/rest_v1/page/html/{title}"
    headers = {"User-Agent": "Mozilla/5.0"}
    response = requests.get(url, headers=headers)
    if response.status_code == 200:
        soup = BeautifulSoup(response.content, "html.parser")
        return soup.get_text()
    else:
        print(f"Failed to fetch '{title}'")
        return ""

def collect_wikipedia_data(titles, min_chars=25000):
    all_text = ""
    for title in titles:
        print(f"Fetching: {title}")
        text = fetch_wikipedia_text(title)
        all_text += f"\n\n=== {title.replace('_', ' ')} ===\n\n{text}"
        if len(all_text) >= min_chars:
            break
    return all_text

file_path = 'agailab/data.txt'

if not os.path.exists(file_path):
    os.makedirs(os.path.dirname(file_path), exist_ok=True)

    article_titles = [
        "Artificial_intelligence", "Machine_learning", "Deep_learning", "Neural_network",
        "Natural_language_processing", "Computer_vision", "Reinforcement_learning",
        "Supervised_learning", "Unsupervised_learning", "Turing_test", "ChatGPT",
        "Large_language_model", "OpenAI", "Automation", "Data_science", "AI_ethics",
        "Robotics", "Cognitive_computing", "Computer_science", "Algorithm",
        "Big_data", "Pattern_recognition", "Knowledge_representation", "Expert_system", "Intelligent_agent"
    ]

    text_data = collect_wikipedia_data(article_titles)
    
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(text_data)

    print(f"Saved {len(text_data)} characters of Wikipedia text to {file_path}")
else:
    print("File already exists.")


with open(file_path, 'r', encoding='utf-8') as f:
    words = f.read().split()

with open(file_path, 'r', encoding='utf-8') as f:
    corpus = f.read()

class AgLab:
    def __init__(self, system_prompt: str = ""):
        self.system_prompt = system_prompt
        with open(file_path, 'r', encoding='utf-8') as f:
            self.corpus = f.read()
        self.words = self.corpus.split()

    def __raw_ask(self, prompt: str) -> str:
        '''
        Internal method: Ask a question to the AgLab LLM and get a response as text.
        '''
        model = AIModel()
        response = model.AskAI(prompt)
        return response

    def AskAgLabLLM(self, prompt: str) -> str:
        '''
        Ask a question to the AgLab LLM and get a response as text.
        '''
        full_prompt = self.system_prompt + " " + prompt
        response = self.__raw_ask(full_prompt)
        return response

    def AskAgLabLLMWithContext(self, prompt: str, context: str) -> str:
        '''
        Ask a question to the AgLab LLM with context and get a response as text.
        '''
        full_prompt = (
            self.system_prompt +
            " Context of the chat so far: " + context +
            " User said: " + prompt
        )
        response = self.__raw_ask(full_prompt)
        return response

    def SummarizeText(self, text: str) -> str:
        '''
        Summarize a text using the AgLab LLM and get a response as text.
        '''
        full_prompt = " Summarize the following text: " + text + " send only the summary of the previous text, do not reply, send only the summary."
        response = self.__raw_ask(full_prompt)
        return response
    
    def TurnToBulletPoints(self, text: str) -> str:
        '''
        Turn a text into bullet points using the AgLab LLM and get a response as text.
        '''
        full_prompt = " Turn the following text into bullet points: " + text + " send only the bullet points of the previous text, do not reply, send only the bullet points."
        response = self.__raw_ask(full_prompt)
        return response
    
    def RandomEmojiSequence(self, length: int = 5) -> str:
        '''
        Generate a random sequence of emojis.
        '''
        emojis = [
            "๐Ÿ˜€", "๐Ÿ˜‚", "๐Ÿ˜ƒ", "๐Ÿ˜„", "๐Ÿ˜…", "๐Ÿ˜†", "๐Ÿ˜‰", "๐Ÿ˜Š", "๐Ÿ˜‹", "๐Ÿ˜Ž",
            "๐Ÿ˜", "๐Ÿ˜˜", "๐Ÿ˜—", "๐Ÿ˜™", "๐Ÿ˜š", "๐Ÿ˜‡", "๐Ÿ˜ˆ", "๐Ÿ‘ฟ", "๐Ÿ‘ป", "๐Ÿ’€",
            "๐Ÿค–", "๐Ÿ‘ฝ", "๐Ÿ‘พ", "๐ŸŽƒ", "๐Ÿ˜บ", "๐Ÿ˜ธ", "๐Ÿ˜น", "๐Ÿ˜ป", "๐Ÿ˜ผ", "๐Ÿ˜ฝ",
            "๐Ÿ™€", "๐Ÿ˜ฟ", "๐Ÿ˜พ", "๐Ÿถ", "๐Ÿฑ", "๐Ÿญ", "๐Ÿน", "๐Ÿฐ", "๐ŸฆŠ", "๐Ÿป",
            "๐Ÿผ", "๐Ÿจ", "๐Ÿฏ", "๐Ÿฆ", "๐Ÿฎ", "๐Ÿท", "๐Ÿฝ", "๐Ÿธ", "๐Ÿต", "๐Ÿ™ˆ",
            "๐Ÿ™‰", "๐Ÿ™Š", "๐Ÿ’", "๐Ÿ”", "๐Ÿง", "๐Ÿฆ", "๐Ÿค", "๐Ÿฃ", "๐Ÿฅ", "๐Ÿฆ†",
            "๐Ÿฆ…", "๐Ÿฆ‰", "๐Ÿฆ‡", "๐Ÿบ", "๐Ÿ—", "๐Ÿด", "๐Ÿฆ„", "๐Ÿ", "๐Ÿ›", "๐Ÿฆ‹",
            "๐ŸŒ", "๐Ÿž", "๐Ÿœ", "๐Ÿชฒ", "๐Ÿชณ", "๐Ÿ•ท", "๐Ÿ•ธ", "๐Ÿข", "๐Ÿ", "๐ŸฆŽ",
            "๐Ÿฆ‚", "๐Ÿฆ€", "๐Ÿฆž", "๐Ÿฆ", "๐Ÿฆ‘", "๐Ÿ™", "๐Ÿชธ", "๐Ÿ ", "๐ŸŸ", "๐Ÿก",
            "๐Ÿฆˆ", "๐Ÿฌ", "๐Ÿณ", "๐Ÿ‹", "๐Ÿฆญ", "๐ŸŠ", "๐Ÿฆง", "๐Ÿฆ", "๐Ÿฆฃ", "๐Ÿ˜",
            "๐Ÿฆ", "๐Ÿฆ›", "๐Ÿช", "๐Ÿซ", "๐Ÿฆ’", "๐Ÿฆ˜", "๐Ÿฆฌ", "๐Ÿƒ", "๐Ÿ‚", "๐Ÿ„",
            "๐ŸŽ", "๐Ÿ–", "๐Ÿ", "๐Ÿ‘", "๐Ÿฆ™", "๐Ÿ", "๐ŸฆŒ", "๐Ÿ•", "๐Ÿฉ", "๐Ÿฆฎ",
            "๐Ÿ•โ€๐Ÿฆบ", "๐Ÿˆ", "๐Ÿˆโ€โฌ›", "๐Ÿชถ", "๐Ÿ“", "๐Ÿฆƒ", "๐Ÿฆค", "๐Ÿฆš", "๐Ÿฆœ",
            "๐Ÿฆข", "๐Ÿฆฉ", "๐Ÿ•Š", "๐Ÿ‡", "๐Ÿฆ", "๐Ÿฆจ", "๐Ÿฆก", "๐Ÿฆซ", "๐Ÿฆฆ", "๐Ÿฆฅ",
            "๐Ÿ", "๐Ÿ€", "๐Ÿฟ", "๐Ÿฆ”", "๐Ÿพ", "๐Ÿ‰", "๐Ÿฒ", "๐ŸŒต", "๐ŸŽ„", "๐ŸŒฒ",
            "๐ŸŒณ", "๐ŸŒด", "๐Ÿชต", "๐ŸŒฑ", "๐ŸŒฟ", "โ˜˜", "๐Ÿ€", "๐ŸŽ", "๐Ÿชด", "๐ŸŽ‹",
            "๐Ÿƒ", "๐Ÿ‚", "๐Ÿ", "๐Ÿ„", "๐Ÿš", "๐Ÿชจ", "๐ŸŒพ", "๐Ÿ’", "๐ŸŒท", "๐ŸŒน",
            "๐Ÿฅ€", "๐ŸŒบ", "๐ŸŒธ", "๐ŸŒผ", "๐ŸŒป", "๐ŸŒž", "๐ŸŒ", "๐ŸŒ›", "๐ŸŒœ", "๐ŸŒš",
            "๐ŸŒ•", "๐ŸŒ–", "๐ŸŒ—", "๐ŸŒ˜", "๐ŸŒ‘", "๐ŸŒ’", "๐ŸŒ“", "๐ŸŒ”", "๐ŸŒ™", "๐ŸŒŽ",
            "๐ŸŒ", "๐ŸŒ", "๐Ÿช", "๐Ÿ’ซ", "โญ", "๐ŸŒŸ", "โœจ", "โšก", "โ˜„", "๐Ÿ’ฅ",
            "๐Ÿ”ฅ", "๐ŸŒช", "๐ŸŒˆ", "โ˜€", "๐ŸŒค", "โ›…", "๐ŸŒฅ", "โ˜", "๐ŸŒฆ", "๐ŸŒง",
            "โ›ˆ", "๐ŸŒฉ", "๐ŸŒจ", "โ„", "โ˜ƒ", "โ›„", "๐ŸŒฌ", "๐Ÿ’จ", "๐Ÿ’ง", "๐Ÿ’ฆ",
            "โ˜”", "โ˜‚", "๐ŸŒŠ", "๐ŸŒซ"
        ]
        return ''.join(random.choice(emojis) for _ in range(length))
    
    def RandomTextSequence(self, length: int = 5) -> str:
        '''
        Generate a random sequence of words.
        '''
        return ' '.join(random.choice(words) for _ in range(length))
    
    def PredictNextWord(self, text_string: str) -> str:
        '''
        Predict the next word in a given text string using a local LLM approach.
        It finds the longest matching suffix in the corpus and returns the next word.
        '''
        text_words = text_string.split()
        if not text_words:
            return ""

        for i in range(len(text_words), 0, -1):
            sequence = ' '.join(text_words[-i:])
            pattern = f"{sequence} "
            start_index = self.corpus.find(pattern)
            if start_index != -1:
                end_index = start_index + len(pattern)
                remaining = self.corpus[end_index:].strip()
                if remaining:
                    return remaining.split()[0]
        
        return "<|endoftext|>"
    
    def GenerateLocalText(self, text_string: str, length=10) -> str:
        '''
        Generate text based on a given text string using a local LLM approach.
        It finds the longest matching suffix in the corpus and returns the next words.
        '''
        for i in range(length):
            next_word = self.PredictNextWord(text_string)
            if next_word == "<|endoftext|>":
                break
            text_string += " " + next_word
        return text_string
        
if __name__ == "__main__":
    aglab = AgLab("You are a helpful assistant called ag lab llm.")
    print(aglab.AskAgLabLLM("What is the capital of France, also what is your name?"))
    print(aglab.RandomEmojiSequence(10))
    print(aglab.SummarizeText("The quick brown fox jumps over the lazy dog."))
    print(aglab.TurnToBulletPoints("The quick brown fox jumps over the lazy dog."))
    print(aglab.RandomTextSequence(10))
    print(aglab.PredictNextWord("Artificial"))
    print(aglab.GenerateLocalText("Artificial", 10))