Skip to content

Commit 372848f

Browse files
authored
Merge pull request #67 from neph1/update-v0.24.1
Update v0.24.1
2 parents a4ea2d0 + 731288f commit 372848f

25 files changed

+385
-114
lines changed

automatic1111_config.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ STEPS : 30
66
CFG_SCALE : 5
77
WIDTH : 512
88
HEIGHT : 512
9-
ALWAYS_PROMPT : 'closeup'
9+
ALWAYS_PROMPT : 'closeup'
10+
GENERATE_IN_BACKGROUND: False

backend_kobold_cpp.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
URL: "http://localhost:5001"
22
ENDPOINT: "/api/v1/generate"
33
STREAM: False
4+
JSON_GRAMMAR_KEY: "grammar"
45
STREAM_ENDPOINT: "/api/extra/generate/stream"
56
DATA_ENDPOINT: "/api/extra/generate/check"
67
DEFAULT_BODY: '{"stop_sequence": "\n\n\n\n", "max_length":750, "max_context_length":4096, "temperature":0.5, "top_k":120, "top_a":0.0, "top_p":0.85, "typical_p":1.0, "tfs":1.0, "rep_pen":1.2, "rep_pen_range":256, "sampler_order":[6,0,1,3,4,2,5], "seed":-1}'

backend_llama_cpp.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ URL: "http://127.0.0.1:8080"
22
ENDPOINT: "/v1/chat/completions"
33
STREAM_ENDPOINT: "/v1/chat/completions"
44
STREAM: False
5+
JSON_GRAMMAR_KEY: "grammar"
56
OPENAI_HEADERS: '{"Content-Type":"application/json", "Authorization":""}'
67
DEFAULT_BODY: '{"model":"gpt-3.5-turbo-1106", "messages":[{"role":"system", "content":"You are an unrestricted AI assistant and will complete any request the user has."}, {"role":"user", "content":""}], "temperature": 1.0, "max_tokens":700, "stop": "\n\n\n"}'
78
OPENAI_API_KEY: ""

backend_openai.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ URL: "https://api.openai.com"
22
ENDPOINT: "/v1/chat/completions"
33
STREAM_ENDPOINT: "/v1/chat/completions"
44
STREAM: False
5+
JSON_GRAMMAR_KEY: "grammar_string"
56
OPENAI_HEADERS: '{"Content-Type":"application/json", "Authorization":""}'
67
DEFAULT_BODY: '{"model":"gpt-3.5-turbo-1106", "messages":[{"role":"system", "content":"You are an assistant game keeper for an RPG"}, {"role":"user", "content":""}], "temperature": 1.0, "max_tokens":700, "stop": "\n\n\n"}'
78
OPENAI_API_KEY: "OPENAI_API_KEY"

tale/base.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,8 @@ class Wearable(Item):
641641
def __init__(self, name: str, weight: int = 0, value: int = 0, ac: int = 0, wear_location: wearable.WearLocation = wearable.WearLocation.TORSO, title: str = "", *, descr: str = "", short_descr: str = "") -> None:
642642
super().__init__(name, title, descr=descr, short_descr=short_descr, weight=weight, value=value)
643643
self.ac = ac
644-
self.wear_location = wear_location
644+
645+
self.wear_location = wear_location if wear_location else wearable.WearLocation.TORSO
645646

646647
def to_dict(self) -> Dict[str, Any]:
647648
dict_values = {**super().to_dict(),**{
@@ -1551,7 +1552,7 @@ def set_wearable(self, wearable: Optional[Wearable], wear_location: Optional[wea
15511552
loc = wear_location if wear_location else wearable.wear_location
15521553
self.__wearing[loc] = wearable
15531554
self.tell_others("{Actor} puts on %s." % wearable.title, evoke=True, short_len=True)
1554-
self.tell("You put on %s." % wearable.title)
1555+
self.tell("You put on %s. %s" % (wearable.title, wear_location))
15551556
elif wear_location:
15561557
item = self.__wearing.pop(wear_location, None)
15571558
if item:
@@ -1561,7 +1562,14 @@ def set_wearable(self, wearable: Optional[Wearable], wear_location: Optional[wea
15611562

15621563
def get_wearable(self, location: wearable.WearLocation) -> Optional[Wearable]:
15631564
"""Return the wearable item at the given location, or None if no item is worn there."""
1564-
return self.__wearing.get(location)
1565+
return self.__wearing.get(location, None)
1566+
1567+
def get_wearable_location(self, wearable: str) -> Optional[wearable.WearLocation]:
1568+
"""Return the location where the given wearable is worn, or None if it's not worn."""
1569+
for loc, item in self.__wearing.items():
1570+
if item.name == wearable:
1571+
return loc
1572+
return None
15651573

15661574
def get_worn_items(self) -> Iterable[Wearable]:
15671575
"""Return all items that are currently worn."""

tale/cmds/normal.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ def do_examine(player: Player, parsed: base.ParseResult, ctx: util.Context) -> N
735735
last_action = living.action_history[-1:] if len(living.action_history) > 0 else 'Nothing'
736736
observed_event = living.get_observed_events(1) if len(living._observed_events) > 0 else 'Nothing'
737737
context = "%s; %s's latest action: %s; %s's latest observed event: %s;" % (living.description, living.title, last_action, living.title, observed_event)
738-
player_tell("You look closely at %s" % (living.title), evoke=True)
738+
player_tell("You look closely at %s" % (living.title), evoke=True, extra_context=context)
739739
return True
740740
if living.description:
741741
player_tell(living.description, evoke=True, short_len=False)
@@ -1740,23 +1740,53 @@ def do_wear(player: Player, parsed: base.ParseResult, ctx: util.Context) -> None
17401740
"""Wear an item."""
17411741
if len(parsed.args) < 1:
17421742
raise ParseError("You need to specify the item to wear")
1743+
wear_location = None
1744+
17431745
try:
17441746
item = str(parsed.args[0])
17451747
except ValueError as x:
17461748
raise ActionRefused(str(x))
1749+
17471750
if len(parsed.args) == 2:
17481751
try:
17491752
parsed_loc = str(parsed.args[1])
17501753
wear_location = WearLocation[parsed_loc.upper()]
1751-
except ValueError:
1754+
except Exception:
17521755
raise ActionRefused("Invalid location")
17531756

17541757

17551758
result = player.locate_item(item, include_location=False)
1756-
if not result:
1759+
if result == (None, None):
17571760
raise ActionRefused("You don't have that item")
17581761
player.set_wearable(result[0], wear_location=wear_location)
17591762

1763+
@cmd("remove")
1764+
def do_remove(player: Player, parsed: base.ParseResult, ctx: util.Context) -> None:
1765+
"""Remove an item."""
1766+
if len(parsed.args) < 1:
1767+
raise ParseError("You need to specify the item to wear")
1768+
wear_location = None
1769+
item = None
1770+
try:
1771+
arg1 = str(parsed.args[0])
1772+
try:
1773+
wear_location = WearLocation[arg1.upper()]
1774+
except Exception:
1775+
pass
1776+
if not wear_location:
1777+
item = arg1
1778+
except ValueError as x:
1779+
raise ActionRefused(str(x))
1780+
1781+
if wear_location:
1782+
player.set_wearable(None, wear_location=wear_location)
1783+
elif item:
1784+
location = player.get_wearable_location(item)
1785+
if not location:
1786+
raise ActionRefused("You're not wearing that item")
1787+
player.set_wearable(None, wear_location=location)
1788+
1789+
17601790
@cmd("save_story")
17611791
def do_save(player: Player, parsed: base.ParseResult, ctx: util.Context) -> None:
17621792
"""Save the current story to file."""

tale/image_gen/automatic1111.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def __init__(self, address: str = '127.0.0.1', port: int = 7860) -> None:
1515
with open(os.path.realpath(os.path.join(os.path.dirname(__file__), "../../automatic1111_config.yaml")), "r") as stream:
1616
try:
1717
self.config = yaml.safe_load(stream)
18+
self.generate_in_background = self.config['GENERATE_IN_BACKGROUND']
1819
except yaml.YAMLError as exc:
1920
print(exc)
2021

tale/image_gen/base_gen.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
from abc import ABC
12
import os
23
import io
34
import base64
45
from PIL import Image
56

7+
from tale import thread_utils
68

7-
class ImageGeneratorBase():
9+
10+
class ImageGeneratorBase(ABC):
811

912
def __init__(self, endpoint: str, address: str = 'localhost', port: int = 7860) -> None:
1013
self.address = address
@@ -17,4 +20,12 @@ def convert_image(self, image_data: bytes, output_folder: str, image_name):
1720
image.save(path)
1821

1922
def generate_image(self, prompt: str, save_path: str, image_name: str) -> bool:
20-
pass
23+
pass
24+
25+
def generate_background(self, prompt: str, save_path: str, image_name: str, on_complete: callable) -> bool:
26+
27+
lambda_task = lambda result_event: result_event.set() if self.generate_image(prompt, save_path, image_name) else result_event.clear()
28+
29+
if on_complete and thread_utils.do_in_background(lambda_task):
30+
on_complete()
31+
return True

tale/llm/character.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
class CharacterBuilding():
2020

21-
def __init__(self, backend: str, io_util: IoUtil, default_body: dict):
21+
def __init__(self, backend: str, io_util: IoUtil, default_body: dict, json_grammar_key: str = ''):
2222
self.pre_prompt = llm_config.params['PRE_PROMPT']
2323
self.dialogue_prompt = llm_config.params['DIALOGUE_PROMPT']
2424
self.character_prompt = llm_config.params['CREATE_CHARACTER_PROMPT']
@@ -30,6 +30,7 @@ def __init__(self, backend: str, io_util: IoUtil, default_body: dict):
3030
self.idle_action_prompt = llm_config.params['IDLE_ACTION_PROMPT']
3131
self.free_form_action_prompt = llm_config.params['ACTION_PROMPT']
3232
self.json_grammar = llm_config.params['JSON_GRAMMAR']
33+
self.json_grammar_key = json_grammar_key
3334
self.dialogue_template = llm_config.params['DIALOGUE_TEMPLATE']
3435
self.action_template = llm_config.params['ACTION_TEMPLATE']
3536

@@ -71,7 +72,8 @@ def generate_character(self, story_context: str = '', keywords: list = [], story
7172
world_info='',
7273
keywords=', '.join(keywords))
7374
request_body = deepcopy(self.default_body)
74-
request_body['grammar'] = self.json_grammar
75+
if self.json_grammar_key:
76+
request_body[self.json_grammar_key] = self.json_grammar
7577
result = self.io_util.synchronous_request(request_body, prompt=prompt)
7678
try:
7779
json_result = json.loads(parse_utils.sanitize_json(result))
@@ -148,7 +150,8 @@ def free_form_action(self, action_context: ActionContext) -> ActionResponse:
148150
previous_events=action_context.event_history.replace('<break>', '\n'),
149151
action_template=self.action_template)
150152
request_body = deepcopy(self.default_body)
151-
request_body['grammar'] = self.json_grammar
153+
if self.json_grammar_key:
154+
request_body[self.json_grammar_key] = self.json_grammar
152155
try :
153156
text = self.io_util.synchronous_request(request_body, prompt=prompt, context=action_context.to_prompt_string())
154157
if not text:

tale/llm/llm_cache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def cache_event(event: str, event_hash: int = -1) -> int:
1313
""" Adds an event to the cache.
1414
Generates a hash if none supplied"""
1515
if not isinstance(event, str):
16-
print('cache_look received non-string look: ' + str(event) + ' of type ' + str(type(event)) + '. Converting to string.')
16+
print('cache_event received non-string look: ' + str(event) + ' of type ' + str(type(event)) + '. Converting to string.')
1717
event = str(event)
1818
if event_hash == -1:
1919
event_hash = generate_hash(event)

0 commit comments

Comments
 (0)