{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import os\n", "import json\n", "from pathlib import Path\n", "from langchain.text_splitter import TokenTextSplitter, CharacterTextSplitter\n", "from langchain.embeddings.openai import OpenAIEmbeddings\n", "from langchain.vectorstores import Chroma, Qdrant\n", "from langchain.document_loaders import TextLoader" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{}" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "json.loads(\"{}\")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[{\"1\": \"a\"},{\"2\": \"b\"}]\n" ] } ], "source": [ "a = [{1: \"a\"}, {2: \"b\"}]\n", "x = \"[\"+\",\".join([json.dumps(i) for i in a]) + \"]\"\n", "print(x)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[{'1': 'a'}, {'2': 'b'}]\n" ] } ], "source": [ "y = json.loads(x)\n", "print(y)" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "D:\\00Repos\\GPT-Swarm\\keys.json\n" ] } ], "source": [ "keys_file = Path(\".\").resolve().parent / \"keys.json\"\n", "print(keys_file)\n", "with open(keys_file) as f:\n", " keys = json.load(f)\n", "os.environ[\"OPENAI_API_KEY\"] = keys[\"OPENAI_API_KEY\"]" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Created a chunk of size 1361, which is longer than the specified 1000\n", "Created a chunk of size 1259, which is longer than the specified 1000\n", "Created a chunk of size 1008, which is longer than the specified 1000\n", "Created a chunk of size 1382, which is longer than the specified 1000\n", "Created a chunk of size 1039, which is longer than the specified 1000\n", "Created a chunk of size 1106, which is longer than the specified 1000\n", "Created a chunk of size 1026, which is longer than the specified 1000\n", "Created a chunk of size 1001, which is longer than the specified 1000\n", "Created a chunk of size 1079, which is longer than the specified 1000\n", "Created a chunk of size 1627, which is longer than the specified 1000\n", "Created a chunk of size 1149, which is longer than the specified 1000\n", "Created a chunk of size 1207, which is longer than the specified 1000\n" ] }, { "data": { "text/plain": [ "75" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "text_file_test = Path(\"D:\\\\00Repos\\\\GPT-Swarm\\\\runs\\\\run_2023-04-28_14-12-56\\\\shared_memory.json\")\n", "text_file_test = Path(\"./test_text.txt\")\n", "loader = TextLoader(text_file_test)\n", "documents = loader.load()\n", "# # improt json as text\n", "# text_dump = \"\"\n", "# with open(text_file_test) as f:\n", "# text_test = json.load(f)\n", "# for key, val in text_test.items():\n", "# text_dump += val[\"content\"]\n", "\n", "text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=150)\n", "texts = text_splitter.split_documents(documents)\n", "len(texts)" ] }, { "cell_type": "code", "execution_count": 182, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "93" ] }, "execution_count": 182, "metadata": {}, "output_type": "execute_result" } ], "source": [ "text_file_test = Path(\"D:\\\\00Repos\\\\GPT-Swarm\\\\runs\\\\run_2023-04-28_14-12-56\\\\shared_memory.json\")\n", "# improt json as text\n", "text_dump = \"\"\n", "with open(text_file_test) as f:\n", " text_test = json.load(f)\n", " for key, val in text_test.items():\n", " text_dump += val[\"content\"]\n", "\n", "text_splitter = CharacterTextSplitter(chunk_size=1500, chunk_overlap=150, separator=\" \")\n", "texts = text_splitter.split_text(text_dump)\n", "len(texts)" ] }, { "cell_type": "code", "execution_count": 121, "metadata": {}, "outputs": [], "source": [ "embeddings = OpenAIEmbeddings()" ] }, { "cell_type": "code", "execution_count": 165, "metadata": {}, "outputs": [], "source": [ "texts = [\"init\"]" ] }, { "cell_type": "code", "execution_count": 166, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Using embedded DuckDB with persistence: data will be stored in: ./test_qdrant2\n" ] } ], "source": [ "chroma_db = Chroma.from_texts(\n", " texts=texts,\n", " embedding=embeddings,\n", " persist_directory=\"./test_qdrant2\",\n", ")" ] }, { "cell_type": "code", "execution_count": 167, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 167, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chroma_db._collection.count()" ] }, { "cell_type": "code", "execution_count": 183, "metadata": {}, "outputs": [], "source": [ "_ = chroma_db.add_texts(\n", " texts\n", ")" ] }, { "cell_type": "code", "execution_count": 184, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "95" ] }, "execution_count": 184, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chroma_db._collection.count()" ] }, { "cell_type": "code", "execution_count": 193, "metadata": {}, "outputs": [], "source": [ "query = \"What is the destruction mechanic?\"\n", "docs = chroma_db.similarity_search_with_score(query, k=1)\n", "docs = chroma_db.max_marginal_relevance_search(query, k=4)" ] }, { "cell_type": "code", "execution_count": 198, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Document(page_content='sessions\\n4. High replayability\\n5. Minimalistic design\\n6. Addictive gameplay\\n7. High score-based gameplay\\n\\nDestruction-Based Game Mechanic Ideas:\\n1. Smash and destroy buildings\\n2. Explode objects\\n3. Break through walls\\n4. Crush cars\\n5. Demolish structures\\n6. Destroy planets\\n7. Wreck havoc on cities\\n8. Obliterate objects with a hammer\\n\\nBreaking Down Destruction-Based Game Mechanic Ideas into Smaller Components:\\n1. Smash and destroy buildings: Players can use different tools to knock down buildings such as a wrecking ball, bulldozer, or explosives.\\n2. Explode objects: Players can set off bombs, grenades, or other explosive devices to destroy objects.\\n3. Break through walls: Players can use different tools to break through walls such as a sledgehammer, drill, or pickaxe.\\n4. Crush cars: Players can use different tools to crush cars such as a monster truck or a hydraulic press.\\n5. Demolish structures: Players can use different tools to demolish structures such as a wrecking ball or explosives.\\n6. Destroy planets: Players can use a spaceship to destroy planets by shooting lasers or other weapons.\\n7. Wreck havoc on cities: Players can cause chaos in a city by destroying buildings, cars, and other objects.\\n8. Obliterate objects with a hammer: Players can use a giant hammer to smash objects into pieces.\\n\\nCombining and Mixing Game Mechanics in Crazy Ways:\\n1. Players control a giant monster that destroys buildings with its fists while avoiding attacks from military forces.\\n2. Players', metadata={})" ] }, "execution_count": 198, "metadata": {}, "output_type": "execute_result" } ], "source": [ "docs[0][0]" ] }, { "cell_type": "code", "execution_count": 192, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 192, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(docs[0].page_content)" ] }, { "cell_type": "code", "execution_count": 155, "metadata": {}, "outputs": [], "source": [ "retriever_chroma = chroma_db.as_retriever(search_type=\"mmr\")" ] }, { "cell_type": "code", "execution_count": 156, "metadata": {}, "outputs": [], "source": [ "from langchain.chat_models import ChatOpenAI\n", "from langchain.chains import ConversationalRetrievalChain, RetrievalQA\n", "from langchain.chains.question_answering import load_qa_chain" ] }, { "cell_type": "code", "execution_count": 157, "metadata": {}, "outputs": [], "source": [ "model = ChatOpenAI(model='gpt-3.5-turbo', temperature=0) # 'ada' 'gpt-3.5-turbo' 'gpt-4',\n", "qa_chain = load_qa_chain(model, chain_type=\"stuff\")\n", "qa = RetrievalQA(combine_documents_chain=qa_chain, retriever=retriever_chroma)" ] }, { "cell_type": "code", "execution_count": 158, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-> **Question**: What are the principles of hypercasual game design? \n", "\n", "**Answer**: The principles of hypercasual game design include the following:\n", "1. Easy to Learn\n", "2. Quick Gameplay\n", "3. Addictive\n", "4. Minimalistic\n", "5. High Replayability\n", "6. Short gameplay sessions\n", "7. Focus on one core mechanic\n", "8. Easy to share and socialize. \n", "\n", "-> **Question**: what are the main destruction based machanics? \n", "\n", "**Answer**: Some main destruction-based game mechanics are:\n", "\n", "1. Smash and destroy buildings\n", "2. Explode objects\n", "3. Break through walls\n", "4. Crush cars\n", "5. Demolish structures\n", "6. Destroy planets\n", "7. Wreck havoc on cities\n", "8. Obliterate objects with a hammer\n", "\n", "In addition, game mechanics like Chain Reaction, Avalanche, Meteor Strike, Robot Uprising, and Nuclear Fallout also involve destruction-based gameplay objectives. \n", "\n" ] } ], "source": [ "questions = [\n", " \"What are the principles of hypercasual game design?\",\n", " \"what are the main destruction based machanics?\"\n", "]\n", "\n", "for question in questions: \n", " result = qa.run(question)\n", " print(f\"-> **Question**: {question} \\n\")\n", " print(f\"**Answer**: {result} \\n\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": ".venv_gptswarm", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.6" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }