AiLab / aglab.py
AGofficial's picture
Upload aglab.py
e5e4fa5 verified
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))