Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import json
- import re
- import random
- # Define the URL
- generateUrl = "http://localhost:5001/api/v1/generate"
- tokenCountUrl = "http://localhost:5001/api/extra/tokencount"
- # Define the headers for the request
- headers = {
- 'Content-Type': 'application/json'
- }
- itemGBNF = 'root ::= "name = " [^\n]* "\ndescription = " [^~]*' #not-tilde to allow for newlines, since the "." doesn't include those
- itemPattern = r"name = ([^\n]*)\ndescription = ([^~]*)"
- itemSchemaDescription = "Your response should follow the pattern of:\nname = the name of the item\ndescription = the description of the item.\nReplace the name and description with your new creation."
- planetGBNF = '''root ::= "name = " [^\n]* "\nclimate and terrain = " [^~]* "\nfactions present = " [^~]* "\nhistory = " [^~]*''' #not-tilde to allow for newlines, since the "." doesn't include those
- planetPattern = r"name = ([^\n]*)\nclimate and terrain = ([^~]*)\nfactions present = ([^~]*)\nhistory = ([^~]*)"
- planetSchemaDescription = '''Your response should follow the pattern of:
- name = the name of the planet. Be creative, the name could be almost anything. Avoid common science fiction names like \"New Terra\" or \"Eden Prime.\"
- climate and terrain = a simple description of the planet's physical condition and layout.
- factions present = a list of one-sentence descriptions of the various nations, cultures, civilizations, and so forth that are present on this planet. Give them distinct names. If they have non-baseline xenohumans describe what they are like. Feel free to invent new xenohuman types.
- history = a general description of the state of the planet and its people. Bear in mind that the current year is 5500 AD and no planet was colonized earlier than the year 2100 AD. Do not describe events earlier than 2100 AD.
- Replace the values with your new creation.'''
- basicSetting = 'This is a setting generally classified as "hard science fiction", with broadly realistic physics. In particular, faster-than-light travel is strictly impossible. The current year is 5500 AD. Around the year 2100 AD humanity began sending out colony ships using a form of suspended animation called "cryptosleep." Many thousands of planets have been colonized since then. Colony planets are generally isolated from each other due to the difficulty of interstellar travel and so each world has developed in very diverse ways. Alien life is extremely rare so all of these colonized planets have been terraformed, usually with flora and fauna genetically derived from Earth life.'
- #since the order in which items are present in context affects their relative "importance" to the output, shuffle the order each time to reduce bias.
- def getTechLevels():
- techLevels = [
- "Neolithic: tools and clothing made of wood, stone, leather and cloth. Metalworking is largely unknown. Often nomadic or with simple hut or cave dwellings.",
- "Medieval: metalworking is common but tools and weapons are hand-crafted by artisans. Large permanent towns and villages exist.",
- "Industrial: factories, electricity, electronics, motorized vehicles, explosives, nuclear fission, and rockets are the hallmarks of industrial tech.",
- "Spacer: fusion power, energy weapons, power armor, complex cybernetics and genetic modification, robotics. Capable of building interstellar spacecraft.",
- 'Ultratech: the peak of human technology, including nanotech, conscious artificial intelligence, artificial gravity, complex genetic engineering, cryptosleep.',
- 'Archotech: technology akin to magic. Includes things like "psychic" effects, easy production of antimatter, zero-point energy, and other reality-warping effects.'
- ]
- random.shuffle(techLevels)
- return "The technological capabilities of factions can be classified with several broad classifications. In no particular order, they are:" + "\n".join(techLevels)
- def getArtificialIntellgenceTypes():
- return 'Artificial intelligence is very significant in this setting. It can be divided into three broad levels. Subpersona AIs appear human-like but lack self-motivation and consciousness. Persona AIs are conscious at a human level, no different from actual people. And archotech AI is "superhuman," capable of creating archotec devices beyond the comprehension of human science. Archotech AI (usually just referred to as "archotechs") often develops inscrutable goals and performs actions they cannot explain to mere humans. The development of an archotech is considered the "end point" of human technology. Archotechs sometimes undergo a process called "transcendence" that appears to take them to yet higher levels of capability, but they cease interacting with humans or lesser AIs after this so the details are unknownable.'
- def getWorldTypes():
- worldCategories = [
- "Coreworld: planets relatively close to Earth that have been colonized for a long time, generally having high technology and long histories. Some coreworlds regress in technology for various reasons.",
- 'Rimworld: planets on the fringes of colonized space, they have been terraformed to some degree but are typically still "untamed" and have been settled by a diverse assortment of factions.',
- 'Deadworld: a planet that has not yet been significantly contacted by humans or archotechs. Generally not habitable and with no significant history.'
- ]
- random.shuffle(worldCategories)
- return "Planets can be categorized into three very broad categories:\n" + "\n".join(worldCategories)
- def getMechanoids():
- return "A common menace present throughout much of human-colonized space are Mechanoids, a broad term for all manner of non-sapient autonomous self-replicating robots. Mechnaoids were originally constructed for a wide variety of reasons - military, domestic, industrial - but have the common element of having gone rogue and now being hostile to human civilization. The most common type found on Rimworlds are derived from terraforming robots since Rimworlds were terraformed relatively recently. Their technologies frequently produce toxic pollution."
- def getInsectoids():
- return "In a short-sighted attempt to counteract the Mechanoid menace, early Ultratech societies developed a variety of genetically-engineered Insectoid species. Insectoids are giant, savage insect-like animals that thrive in toxic environments, breeding more rapidly than Mechanoids and actively hostile to to them. This did work to some degree, limiting Mechanoid spread on habitable worlds, but Insectoids are also a threat to human civilizations. Bear in mind that Insectoids are animals, not sapient like humans or AIs are. They have a hive-based social structure but they are not intelligent."
- def getXenotypes():
- xenotypes = [
- "Dirtmoles: Extremely capable at digging or mining tasks, but suffer from a sensitivity to light and have poor eyesight at distance.",
- "Genies: Designed to be engineers, genies are calm and great at crafting and intellect, but are fragile and socially inept.",
- "Highmates: Designed to be perfect mates, highmates can psychically bond with whoever they first romance. They have permanently positive moods but are incapable of violence. They are rarely the dominant xenotype of any given faction.",
- "Hussars: Designed as soldiers, hussars are great at combat and not much else. They are dependent on the combat drug go-juice, but are immune to any of its negative side effects.",
- "Neanderthals: Recreations of ancient humans that are slow to move and slow to learn, but are incredibly tough.",
- "Pigskins: pig-like humans that can eat raw food efficiently and are resistant to disease, but have clunky trotter hands and are nearsighted.",
- "Sanguophages: Effectively vampires. They don't age, are nearly deathless, and have multiple special abilities. In exchange, they have a need for blood and catatonic deathrest, and suffer in the light.",
- "Wasters: Bioweapons that can thrive in toxic buildup, survive disease, and can ingest wake-up freely, but have a need for some form of psychite.",
- "Yttakin: Fur-skinned humans that are well adapted to the cold, and have an animal warcall. Prefer to be nude."
- ]
- random.shuffle(xenotypes)
- return "Although no intelligent alien life has ever been encountered, the human species has undergone much modification through artificial genetic engineering over the millennia since leaving Earth. In addition to the standard \"baseline\" form that still resembles modern-day humans there are many varieties of humans called \"xenohumans.\" Xenohumans can have just minor cosmetic differences such as unusual skin colors or catlike ears, or they can be so different from baseline that only genetic testing would reveal they're from the same stock as humanity.\n\nSome common types of xenohuman include:" + "\n".join(xenotypes) +"\nNote that there are many kinds of xenohuman other than just these, any ultratech or archotech society is likely to invent new ones and sometimes it's easier to adapt colonists to their new planets rather than terraforming them completely."
- # skipping world types for now, it was confusing the LLM
- def getBasicBackground():
- return basicSetting +"\n\n"+ getTechLevels() +"\n\n"+ getArtificialIntellgenceTypes() +"\n\n"+ getMechanoids() +"\n\n"+ getInsectoids() +"\n\n"+ getXenotypes()
- def getPromptJson(promptContext, promptRequest, grammar):
- data = {
- "prompt": "{{[INPUT]}}\n" + promptContext +"\n\n"+ promptRequest + "\n{{[OUTPUT]}}",
- "max_length": 2048
- #"stop_sequence": "</s>"
- }
- if grammar:
- data["grammar"] = grammar
- return json.dumps(data)
- def getResult(promptContext, promptRequest, grammar):
- # Send the request and get the response
- response = requests.post(generateUrl, headers=headers, data=getPromptJson(promptContext, promptRequest, grammar))
- # Check if the request was successful
- if response.status_code == 200:
- # Parse the response JSON into a Python dictionary
- response_data = json.loads(response.text)
- #print(response_data)
- return response_data["results"][0]["text"]
- else:
- print(f"Request failed with status code {response.status_code}")
- return False
- def countTokens(prompt):
- response = requests.post(tokenCountUrl, headers=headers, data=json.dumps({"prompt":prompt}))
- if response.status_code == 200:
- # Parse the response JSON into a Python dictionary
- response_data = json.loads(response.text)
- print(response_data)
- def getPlanet(alreadyUsedNames):
- namesUsed = "The following names have already been used: " + ", ".join(alreadyUsedNames) +". Please invent a name distinct from these existing names.\n"
- result = getResult(getBasicBackground(), "Please generate a description of a planet.\n" + namesUsed + planetSchemaDescription, planetGBNF)
- match = re.search(planetPattern, result)
- if match:
- return {"name":match.group(1).strip(), "terrain":match.group(2).strip(), "factions":match.group(3).strip(), "history":match.group(4).strip()}
- return result
- toFile = True
- number = 100
- items = {}
- for x in range(number):
- item = getPlanet(items.keys())
- if isinstance(item, str):
- print(item)
- continue
- items[item["name"]] = {"history":item["history"],"terrain":item["terrain"],"factions":item["factions"]}
- if toFile:
- with open("planetsout.json", "w") as f:
- f.write(json.dumps(items))
- else:
- print(items)
Advertisement
Add Comment
Please, Sign In to add comment