Package: mindcraft
MindCraftThe open-source NLP library to craft the minds of your NPC characters for your video games.Requires Python 3.10 or higher.It includes the following features:Text generation using LLMsMotivations, personality, personal goalsKnowledge and awareness about the world (RAG)Short and Long-term memory (RAG)Conversational stylesSupervised finetuning by human feedback (SFT)Integration with vLLM forfast inferenceandstreaminglocally, remotelly or in Docker.Usage of quantized AWQ modelsIntegration with API and RPC (to come!)Create a World from a bookfrommindcraft.infra.engine.llm_typesimportLLMTypefrommindcraft.infra.vectorstore.stores_typesimportStoresTypesfrommindcraft.infra.embeddings.embeddings_typesimportEmbeddingsTypesfrommindcraft.lore.worldimportWorldworld=World(world_name="Middle Earth from the Lord of the Rings",embeddings=EmbeddingsTypes.MINILM,store_type=StoresTypes.CHROMA,llm_type=LLMType.YI_6B_AWQ,fast=False,# <--- fast=True to switch on vLLMremote=False,# <--- remote=True to use a remote vLLM serverstreaming=True)# <--- streaming=True to use vLLM streamingNow we use some book to carry out chunk splitting and add it to our favourite Vector Database (in our case, ChromaDB)frommindcraft.infra.splitters.text_splitters_typesimportTextSplitterTypesworld.book_to_world(book_path="/content/lotr1.txt",text_splitter=TextSplitterTypes.SENTENCE_SPLITTER,max_units=3,overlap=1,encoding='utf-8')Query the lore of the worldOnce a world has been created and populated with lore, query the lore known by NPCs by doing:results=world.get_lore("What do you think about the Rings of Power?",num_results=5,min_similarity=0.95)fori,dinenumerate(results.documents):print(f"SENTENCE{i}:\n{d}")print()Instantiate an NPC in a worldOnce a world has been created and populated with lore, instantiate an NPC in it by doing:frommindcraft.features.motivationimportMotivationfrommindcraft.features.personalityimportPersonalityfrommindcraft.features.moodimportMoodfrommindcraft.mind.npcimportNPCname="Galadriel"description="The Elven Queen of Lothlorien, bearer of Nenya, wife to Celeborn"personalities=[Personality(x)forxin['fair','mighty','wise','carying','kind']]motivations=[Motivation(x)forxin['Destroying the Evil','Protecting Middle Earth','Travelling West']]mood=Mood('worried')galadriel=NPC(name,description,personalities,motivations,mood,StoresTypes.CHROMA,EmbeddingsTypes.MINILM)galadriel.add_npc_to_world()Ask questions to the NPCWe get an iterator for the responses, to allow inference in streaming way.answer_iter=galadriel.react_to("What do you think about the Rings of Power",min_similarity=0.85,ltm_num_results=3,world_num_results=7,max_tokens=600)So your answers will be in the iterator, don't forget to loop through it!for answer in answer_iter:
    print(answer)Example of answer using Zephyr 7B quantized to 4bAlas, my dear friend, the Rings of Power are a perilous burden that should not be wielded by those of mortal race. They possess a sinister force that overcomes the spirit, enslaving their very soul. I have observed their destructive potential in Eregion long ago, where many Elven-rings were created, each infused with its own unique potency. Some more potent than others, and often, they seem to have a propensity to gravitate towards the unsuspecting. In light of our shared concern for the wellbeing of Middle Earth, I implore you to heed my words; let us not succumb to the allure of these fateful rings, lest they consume us entirely.Creating custom Supervised Finetuning datasets for NPCsThere are two loops integrated in the framework which allow you to create your own datasets.NPC.extract_conversational_styles_from_world: Retrieving conversations from the world and tagging the question and mood yourselfNPC.extract_conversational_styles_talking_to_user: Creates conversations by talking to youSupervised FinetuningYou can create  your own datasets with supervised finetuning,
accepting or rejecting the interactions. As a result, you will come up
with a csv of interactions you can use to train or finetune your own models.name||mood||question||answer
Galadriel||default||Good night, Galadriel!||'Good night, my friends! '\nsaid Galadriel. '\nSleep in peace!
Galadriel||grave||why he could say that?||....`He would be rash indeed that said that thing,' said Galadriel gravely.LLM integratedQuantizedTheBloke/mistral_7b_norobots-AWQTheBloke/zephyr-7B-beta-AWQTheBloke/notus-7B-v1-AWQTheBloke/Starling-LM-7B-alpha-AWQTheBloke/Yi-6B-AWQTheBloke/dragon-yi-6B-v0-AWQUnquantizedmicrosoft/phi-2stabilityai/stablelm-zephyr-3bEmbeddings for RAGsentence-transformers/all-MiniLM-L6-v2CUDA and Torch in WINDOWSIf you are running on Windows on a machine with a GPU, and you get a message about not being able to find
your gpu, you need to configure CUDA for Windows.Go toCUDA installation webpage.Select your Windows version and specifics.Download and installUninstall torch (pip uninstall torch)
5a. Go to requirements.txt, comment the line after LINUX for torch and uncomment the line after WINDOWS
5b. Alternatively you can just run this command:pip3 install torch -i https://download.pytorch.org/whl/cu121You torch on windows CUDA should be working. To test it:importtorchif__name__=="__main__":print(torch.cuda.is_available())vLLMvLLMhas been included for Fast Inference, in local, remote installations and Docker.Local Fast inference (Paged Attention)To use fast-inference, just run addfast=Trueto yourWorldobject:world=World(world_name="Lord of the Rings",embeddings=EmbeddingsTypes.MINILM,store_type=StoresTypes.CHROMA,llm_type=LLMType.YI_6B_AWQ,fast=True)# <---- HERERemote Fast InferenceTo the previousfastparameter, add alsoremote=Trueworld=World(world_name="Lord of the Rings",embeddings=EmbeddingsTypes.MINILM,store_type=StoresTypes.CHROMA,llm_type=LLMType.YI_6B_AWQ,fast=True,# <---- HEREremote=True)# <---- HEREStreaming (only if remote!)To the previousfastandremoteparameters, add alsostreaming=Trueworld=World(world_name="Lord of the Rings",embeddings=EmbeddingsTypes.MINILM,store_type=StoresTypes.CHROMA,llm_type=LLMType.YI_6B_AWQ,fast=True,# <---- HEREremote=True,streaming=True)# <---- HEREExample dataLord of the RingsNotebooksYou can find notebooks in thenotebooksfolder of this project.Demo 1: Creating a World and an NPCVideoArchitectureTestspython -m pytest tests/*Header