FaceDeer

Random magic item descriptions using Koboldcpp

Nov 7th, 2023 (edited)
936
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.00 KB | Source Code | 0 0
  1. import requests
  2. import json
  3. import re
  4.  
  5. # Define the URL
  6. generateUrl = "http://localhost:5001/api/v1/generate"
  7. tokenCountUrl = "http://localhost:5001/api/extra/tokencount"
  8.  
  9. # Define the headers for the request
  10. headers = {
  11.     'Content-Type': 'application/json'
  12. }
  13.  
  14. itemGBNF = 'root ::= "name = " [^\n]* "\ndescription = " [^~]*' #not-tilde to allow for newlines, since the "." doesn't include those
  15. pattern = r"name = ([^\n]*)\ndescription = ([^~]*)"
  16. 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."
  17. itemPrompt = '''Please generate the name and description of a magical item from a fantasy setting.
  18. It can be any sort of item, from a personal possession to a piece of furniture or even a structure or location.
  19. 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.
  20. 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.
  21. The description can include effects described in ways that are not specific to a particular roleplaying ruleset.'''
  22. initialPrompt = "{{[INPUT]}}\n" + itemPrompt+"\n"+itemSchemaDescription + "\n{{[OUTPUT]}}"
  23.  
  24. def getPromptJson(prompt, grammar):
  25.     data = {
  26.         "prompt": prompt,
  27.         "max_length": 1024
  28.         #"stop_sequence": "</s>"
  29.     }
  30.     if grammar:
  31.         data["grammar"] = grammar
  32.     return json.dumps(data)
  33.  
  34. def getResult(prompt, grammar):
  35.     # Send the request and get the response
  36.     response = requests.post(generateUrl, headers=headers, data=getPromptJson(prompt, grammar))
  37.     # Check if the request was successful
  38.     if response.status_code == 200:
  39.         # Parse the response JSON into a Python dictionary
  40.         response_data = json.loads(response.text)
  41.         #print(response_data)
  42.         return response_data["results"][0]["text"]
  43.     else:
  44.         print(f"Request failed with status code {response.status_code}")
  45.         return False
  46.  
  47. def countTokens(prompt):
  48.     response = requests.post(tokenCountUrl, headers=headers, data=json.dumps({"prompt":prompt}))
  49.     if response.status_code == 200:
  50.         # Parse the response JSON into a Python dictionary
  51.         response_data = json.loads(response.text)
  52.         print(response_data)
  53.  
  54. def getItem():
  55.     result = getResult(initialPrompt, itemGBNF)
  56.     match = re.search(pattern, result)
  57.     return {"name":match.group(1), "description":match.group(2)}
  58.  
  59. toFile = True
  60. number = 100
  61.  
  62. items = {}
  63.  
  64. for x in range(number):
  65.     item = getItem()
  66.     items[item["name"]] = item["description"]
  67. if toFile:
  68.     with open("itemsout.txt", "w") as f:
  69.         f.write(json.dumps(items))
  70. else:
  71.     print(items)
  72.  
Advertisement
Add Comment
Please, Sign In to add comment