Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import json
- import re
- # 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
- pattern = 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."
- itemPrompt = '''Please generate the name and description of a magical item from a fantasy setting.
- It can be any sort of item, from a personal possession to a piece of furniture or even a structure or location.
- Not all of the items have positive effects, a few might be cursed. The power of the enchantment can range from a trivial trinket to a world-shaking artifact.
- Do not include any effects in the description that are specific to any RPG system, such as a bonus or advantage to an ability score (Charisma, Wisdom, Strength, Dexterity, Constitution, Intelligence), armor class, hit points, saving throws, attack rolls, skill checks, or other game-specific mechanics.
- The description can include effects described in ways that are not specific to a particular roleplaying ruleset.'''
- initialPrompt = "{{[INPUT]}}\n" + itemPrompt+"\n"+itemSchemaDescription + "\n{{[OUTPUT]}}"
- def getPromptJson(prompt, grammar):
- data = {
- "prompt": prompt,
- "max_length": 1024
- #"stop_sequence": "</s>"
- }
- if grammar:
- data["grammar"] = grammar
- return json.dumps(data)
- def getResult(prompt, grammar):
- # Send the request and get the response
- response = requests.post(generateUrl, headers=headers, data=getPromptJson(prompt, 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 getItem():
- result = getResult(initialPrompt, itemGBNF)
- match = re.search(pattern, result)
- return {"name":match.group(1), "description":match.group(2)}
- toFile = True
- number = 100
- items = {}
- for x in range(number):
- item = getItem()
- items[item["name"]] = item["description"]
- if toFile:
- with open("itemsout.txt", "w") as f:
- f.write(json.dumps(items))
- else:
- print(items)
Advertisement
Add Comment
Please, Sign In to add comment