Melhore ao máximo este código:

OUTPUST:

PS C:\Users\Luan\Documents\SNAKE GAME> & C:/Users/Luan/AppData/Local/Programs/Python/Python310/python.exe "c:/Users/Luan/Documents/SNAKE GAME/TRAINGPT/trainai.py"
[nltk_data] Downloading package punkt to
[nltk_data]     C:\Users\Luan\AppData\Roaming\nltk_data...
[nltk_data]   Package punkt is already up-to-date!
[nltk_data] Downloading package averaged_perceptron_tagger to
[nltk_data]     C:\Users\Luan\AppData\Roaming\nltk_data...
[nltk_data]   Unzipping taggers\averaged_perceptron_tagger.zip.
Modelo treinado com sucesso usando C:/Users/Luan/Documents/SNAKE GAME/TRAINGPT/dados_treino.txt
Vocabulário: 3127 palavras
Chatbot iniciado! (Digite 'sair' para encerrar)

Você: Hello!
Chatbot: in that case , i 'll write a big story that 's a bit of everything .

Você: oh, sure
Chatbot: together , the group set out to find a way reverse the great disruption and restore the world to its former glory .

Você: 1 + 1 = ?
Chatbot: 1 hour and 45 minutes ... what is the world 's largest living species of insect ?

Você: Answer: what is the world's largest living species of insect?
Chatbot: pneumonoultramicroscopicsilicovolcanoconiosis ... what is the world 's largest living species of mammal , the blue whale , can grow up to 15 centimeters .

Você: what is the longest word in english?
Chatbot: it is the seventh main entry in the grand theft auto series , following 2002 's grand : vice city .

Você: tell me about Grand theft 
Chatbot: `` me too , i 've been looking forward to it all week !

Você: tell me about Grand theft auto
Chatbot: `` me too , i 've been looking forward to it all week !

Você: tell me about Grand theft auto
Chatbot: `` me too , i 've been looking forward to it all week !

Você: tell me about Grand theft auto
Chatbot: `` me too , i 've been looking forward to it all week !

Você: tell me about Grand theft atuo, pleas?
Chatbot: `` me too , i 've been looking forward to it all week !

Você: hey
Chatbot: in that case , i 'll write a big story that 's a bit of everything .

Você: store
Chatbot: in that case , i 'll write a big story that 's a bit of everything .

Você: stop 
Chatbot: in that case , i 'll write a big story that 's a bit of everything .

Você: write a story about CHATGPT
Chatbot: in a climactic showdown , ava and her companions stood victorious , but scarred by their experiences .

Você: what is your name?
Chatbot: def greet ( name ) : ... print f '' hello , { name } !

Você: write a code in python
Chatbot: on a tour in mid-october , williams-isom threaded through the rows of chairs , stray baby 
strollers and a toddler making a break toward a bar where snacks drinks were available , to point out the intake center ’ s many functions .

Você:

CÓDIGO:

import numpy as np
from collections import defaultdict
import re
import random
from nltk.tokenize import word_tokenize, sent_tokenize
import nltk

class AdvancedLLM:
    def __init__(self, context_window=5):
        self.context_window = context_window
        self.ngram_model = defaultdict(lambda: defaultdict(float))
        self.sentence_starts = []
        self.word_vectors = {}
        self.vocabulary = set()
        self.context_memory = []
        
    def download_nltk_data(self):
        """Download recursos necessários do NLTK."""
        try:
            nltk.download('punkt')
            nltk.download('averaged_perceptron_tagger')
        except:
            print("Aviso: Não foi possível baixar recursos NLTK. Usando fallback simples.")
    
    def tokenize_text(self, text):
        """Tokeniza texto usando NLTK quando possível."""
        try:
            return word_tokenize(text.lower())
        except:
            return text.lower().split()
    
    def create_ngrams(self, tokens, n):
        """Cria n-gramas a partir dos tokens."""
        return list(zip(*[tokens[i:] for i in range(n)]))
    
    def train(self, file_path):
        """Treina o modelo usando arquivo de texto."""
        try:
            self.download_nltk_data()
            
            with open(file_path, 'r', encoding='utf-8') as file:
                text = file.read()
            
            # Divide em sentenças
            sentences = sent_tokenize(text)
            
            # Processa cada sentença
            for sentence in sentences:
                tokens = self.tokenize_text(sentence)
                if len(tokens) < 3:  # Ignora sentenças muito curtas
                    continue
                    
                # Atualiza vocabulário
                self.vocabulary.update(tokens)
                
                # Armazena começos de sentenças
                self.sentence_starts.append(tuple(tokens[:self.context_window]))
                
                # Cria n-gramas e atualiza modelo
                for n in range(1, self.context_window + 1):
                    ngrams = self.create_ngrams(tokens, n + 1)
                    
                    for ngram in ngrams:
                        context = ngram[:-1]
                        word = ngram[-1]
                        self.ngram_model[context][word] += 1
                
                # Normaliza probabilidades
                for context in self.ngram_model:
                    total = sum(self.ngram_model[context].values())
                    for word in self.ngram_model[context]:
                        self.ngram_model[context][word] /= total
            
            print(f"Modelo treinado com sucesso usando {file_path}")
            print(f"Vocabulário: {len(self.vocabulary)} palavras")
            
        except Exception as e:
            print(f"Erro ao treinar o modelo: {str(e)}")
    
    def get_best_context(self, input_text):
        """Encontra o melhor contexto para a resposta."""
        tokens = self.tokenize_text(input_text)
        
        best_context = None
        max_overlap = -1
        
        for start in self.sentence_starts:
            overlap = sum(1 for t1, t2 in zip(tokens, start) if t1 == t2)
            if overlap > max_overlap:
                max_overlap = overlap
                best_context = start
        
        return best_context if best_context else random.choice(self.sentence_starts)
    
    def generate_response(self, context, max_length=50):
        """Gera resposta baseada no contexto."""
        if not self.ngram_model:
            return "Modelo não treinado."
        
        response = list(context)
        current_length = len(response)
        
        while current_length < max_length:
            # Pega o contexto atual
            for n in range(min(self.context_window, len(response)), 0, -1):
                current_context = tuple(response[-n:])
                
                if current_context in self.ngram_model:
                    # Escolhe próxima palavra baseada nas probabilidades
                    next_word = random.choices(
                        list(self.ngram_model[current_context].keys()),
                        weights=list(self.ngram_model[current_context].values())
                    )[0]
                    
                    response.append(next_word)
                    current_length += 1
                    break
            else:
                # Se não encontrar contexto, escolhe palavra aleatória do vocabulário
                response.append(random.choice(list(self.vocabulary)))
                current_length += 1
            
            # Verifica se formou uma sentença completa
            if response[-1] in '.!?':
                break
        
        # Mantém coerência removendo repetições próximas
        final_response = []
        prev_words = set()
        
        for word in response:
            if word not in prev_words or word in '.!?,':
                final_response.append(word)
                prev_words.add(word)
                if len(prev_words) > 5:
                    prev_words.remove(next(iter(prev_words)))
        
        return ' '.join(final_response)
    
    def process_input(self, user_input):
        """Processa entrada do usuário e mantém contexto."""
        # Adiciona input ao contexto
        self.context_memory.append(user_input)
        if len(self.context_memory) > 5:
            self.context_memory.pop(0)
        
        # Encontra melhor contexto
        context = self.get_best_context(user_input)
        
        # Gera resposta
        response = self.generate_response(context)
        
        return response
    
    def chat(self):
        """Inicia sessão de chat."""
        print("Chatbot iniciado! (Digite 'sair' para encerrar)")
        
        while True:
            user_input = input("\nVocê: ").strip()
            
            if user_input.lower() == 'sair':
                print("Chatbot: Até logo!")
                break
            
            response = self.process_input(user_input)
            print(f"Chatbot: {response}")


if __name__ == "__main__":
    # Cria e treina o modelo
    llm = AdvancedLLM(context_window=5)
    llm.train("C:/Users/Luan/Documents/SNAKE GAME/TRAINGPT/dados_treino.txt")
    
    # Inicia o chat
    llm.chat()