Skip to content

Commit

Permalink
some parameter tweaking
Browse files Browse the repository at this point in the history
  • Loading branch information
neph1 committed Aug 8, 2023
1 parent 472bc7c commit 8b83643
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 24 deletions.
8 changes: 8 additions & 0 deletions stories/prancingllama/npcs/npcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from tale.base import Item, Living, ParseResult
from tale.errors import ParseError, ActionRefused
from tale.lang import capital
from tale.llm_ext import LivingNpc
from tale.player import Player
from tale.util import call_periodically, Context
Expand Down Expand Up @@ -48,6 +49,10 @@ def do_random_move(self, ctx: Context) -> None:
if direction:
self.move(direction.target, self, direction_names=direction.names)

@call_periodically(30, 60)
def do_pick_up_dishes(self, ctx: Context) -> None:
self.location.tell("%s wipes a table and picks up dishes." % capital(self.title), evoke=False)

class Patron(LivingNpc):

def __init__(self, name: str, gender: str, *,
Expand Down Expand Up @@ -118,4 +123,7 @@ def do_idle_action(self, ctx: Context) -> None:
urta = InnKeeper("Urta", "f", age=44, descr="A gruff, curvy woman with a long brown coat and bushy hair that reaches her waist. When not serving, she keeps polishing jugs with a dirty rag.", personality="She's the owner of The Prancing Llama, and of few words. But the words she speak are kind. She knows a little about all the patrons in her establishment.", short_descr="A curvy woman with long brown coat standing behind the bar.")
urta.aliases = {"bartender", "inn keeper", "curvy woman"}

brim = Maid("Brim", "f", age=22, descr="A timid girl with long dark blonde hair in a braid down her back. She carries trays and dishes back and forth.", personality="She's shy and rarely looks anyone in the eye. When she speaks, it is merely a whisper. She traveled up the mountain looking for work, and ended up serving at the Inn. She dreams of big adventures.", short_descr="A timid maid with a braid wearing a tunic and dirty apron.")
brim.aliases = {"maid", "timid girl", "serving girl"}


2 changes: 1 addition & 1 deletion stories/prancingllama/zones/prancingllama.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def spawn_rat(self, ctx: Context) -> None:

bar.init_inventory([urta, norhardt])

hearth.init_inventory([count_karta])
hearth.init_inventory([count_karta, brim])

drink = Item("ale", "jug of ale", descr="Looks and smells like strong ale.")

Expand Down
22 changes: 12 additions & 10 deletions tale/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,8 @@ def tell(self, room_msg: str, exclude_living: 'Living'=None, specific_targets: S
that based on this message string. That will make it quite hard because you need to
parse the string again to figure out what happened... Use handle_verb / notify_action instead.
"""
#alt_prompt ="### Instruction: Location: [" + str(self.look(short=True)) + "]. Rewrite the following text in your own words using vivid language, use 'location' for context. Text:\n\n [{input_text}] \n\nEnd of text.\n\n### Response:\n"

targets = specific_targets or set()
assert isinstance(targets, (frozenset, set, list, tuple))
assert exclude_living is None or isinstance(exclude_living, Living)
Expand Down Expand Up @@ -669,7 +671,7 @@ def message_nearby_locations(self, message: str) -> None:
if exit.target in yelled_locations:
continue # skip double locations (possible because there can be multiple exits to the same location)
if exit.target is not self:
exit.target.tell(message, evoke=True, max_length=True)
exit.target.tell(message, evoke=False, max_length=True)
yelled_locations.add(exit.target)
for direction, return_exit in exit.target.exits.items():
if return_exit.target is self:
Expand All @@ -684,7 +686,7 @@ def message_nearby_locations(self, message: str) -> None:
direction = "below"
else:
continue # no direction description possible for this exit
exit.target.tell("The sound is coming from %s." % direction, evoke=True, max_length=True)
exit.target.tell("The sound is coming from %s." % direction, evoke=False, max_length=True)
break

def nearby(self, no_traps: bool=True) -> Iterable['Location']:
Expand Down Expand Up @@ -1034,7 +1036,7 @@ def wiz_clone(self, actor: 'Living', make_clone: bool=True) -> 'Living':
actor.tell("Cloned into: " + repr(duplicate) + " (spawned in current location)")
actor.tell_others("{Actor} summons %s..." % lang.a(duplicate.title))
actor.location.insert(duplicate, actor)
actor.location.tell("%s appears." % lang.capital(duplicate.title), evoke=True, max_length=True)
actor.location.tell("%s appears." % lang.capital(duplicate.title), evoke=False, max_length=True)
return duplicate

@util.authorized("wizard")
Expand Down Expand Up @@ -1274,7 +1276,7 @@ def display_direction(directions: Sequence[str]) -> str:
message = "%s leaves %s." % (lang.capital(self.title), direction_txt)
else:
message = "%s leaves." % lang.capital(self.title)
original_location.tell(message, exclude_living=self, evoke=True, max_length=True)
original_location.tell(message, exclude_living=self, evoke=False, max_length=True)
# queue event
if is_player:
pending_actions.send(lambda who=self, where=target: original_location.notify_player_left(who, where))
Expand All @@ -1283,7 +1285,7 @@ def display_direction(directions: Sequence[str]) -> str:
else:
target.insert(self, actor)
if not silent:
target.tell(f"{lang.capital(self.title)} arrives from {original_location}." , exclude_living=self, evoke=True, max_length=True)
target.tell(f"{lang.capital(self.title)} arrives from {original_location}." , exclude_living=self, evoke=False, max_length=True)
# queue event
if is_player:
pending_actions.send(lambda who=self, where=original_location: target.notify_player_arrived(who, where))
Expand Down Expand Up @@ -1683,7 +1685,7 @@ def open(self, actor: Living, item: Item=None) -> None:
actor.tell_others("{Actor} opens the %s." % self.name, evoke=True, max_length=True)
if self.linked_door:
self.linked_door.opened = True
self.target.tell("The %s is opened from the other side." % self.linked_door.name, evoke=True, max_length=True)
self.target.tell("The %s is opened from the other side." % self.linked_door.name, evoke=False, max_length=True)

def close(self, actor: Living, item: Item=None) -> None:
"""Close the door with optional item. Notifies actor and room of this event."""
Expand All @@ -1694,7 +1696,7 @@ def close(self, actor: Living, item: Item=None) -> None:
actor.tell_others("{Actor} closes the %s." % self.name, evoke=True, max_length=True)
if self.linked_door:
self.linked_door.opened = False
self.target.tell("The %s is closed from the other side." % self.linked_door.name, evoke=True, max_length=True)
self.target.tell("The %s is closed from the other side." % self.linked_door.name, evoke=False, max_length=True)

def lock(self, actor: Living, item: Item=None) -> None:
"""Lock the door with the proper key (optional)."""
Expand All @@ -1714,11 +1716,11 @@ def lock(self, actor: Living, item: Item=None) -> None:
if not key:
raise ActionRefused("You don't seem to have the means to lock it.")
self.locked = True
actor.tell("Your %s fits, the %s is now locked." % (key.title, self.name), evoke=True, max_length=True)
actor.tell_others("{Actor} locks the %s with %s." % (self.name, lang.a(key.title)), evoke=True, max_length=True)
actor.tell("Your %s fits, the %s is now locked." % (key.title, self.name), evoke=False, max_length=True)
actor.tell_others("{Actor} locks the %s with %s." % (self.name, lang.a(key.title)), evoke=False, max_length=True)
if self.linked_door:
self.linked_door.locked = True
self.target.tell("The %s is locked from the other side." % self.linked_door.name, evoke=True, max_length=True)
self.target.tell("The %s is locked from the other side." % self.linked_door.name, evoke=False, max_length=True)

def unlock(self, actor: Living, item: Item=None) -> None:
"""Unlock the door with the proper key (optional)."""
Expand Down
1 change: 1 addition & 0 deletions tale/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,7 @@ def load_character(self, player: player.Player, path: str):
age = character.age,
personality = character.personality,
occupation = character.occupation)
npc.following = player
player.location.insert(npc, None)


Expand Down
9 changes: 5 additions & 4 deletions tale/llm_config.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
URL: "http://localhost:5001"
ENDPOINT: "/api/v1/generate"
STREAM: False
STREAM_ENDPOINT: "/api/extra/generate/stream"
DATA_ENDPOINT: "/api/extra/generate/check"
WORD_LIMIT: 500
DEFAULT_BODY: '{"stop_sequence": "", "max_length":300, "max_context_length":4096, "temperature":1.0, "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, "mirostat":2, "mirostat_tau":5.0, "mirostat_eta":0.1, "sampler_order":[6,0,1,3,4,2,5], "seed":-1}'
ANALYSIS_BODY: '{"stop_sequence": "\n\n", "max_length":300, "max_context_length":4096, "temperature":0.15, "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, "mirostat":2, "mirostat_tau":5.0, "mirostat_eta":0.1, "sampler_order":[6,0,1,3,4,2,5], "seed":-1}'
MEMORY_SIZE: 1024
DEFAULT_BODY: '{"stop_sequence": "", "max_length":500, "max_context_length":4096, "temperature":1.0, "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, "mirostat":2, "mirostat_tau":5.0, "mirostat_eta":0.1, "sampler_order":[6,0,1,3,4,2,5], "seed":-1}'
ANALYSIS_BODY: '{"banned_tokens":"\n\n", "stop_sequence": "", "max_length":500, "max_context_length":4096, "temperature":0.15, "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, "mirostat":2, "mirostat_tau":5.0, "mirostat_eta":0.1, "sampler_order":[6,0,1,3,4,2,5], "seed":-1}'
MEMORY_SIZE: 512
PRE_PROMPT: 'Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.\n\n'
BASE_PROMPT: '### Instruction: Rewrite the following text in your own words using vivid language. Text:\n\n {input_text} \n\nEnd of text.\n\n### Response:\n'
BASE_PROMPT: "History: [{history}]. ### Instruction: Rewrite the following text in your own words using vivid language. 'History' can be used to create a context for what you write. Text:\n\n [{input_text}] \n\nEnd of text.\n\n### Response:\n"
DIALOGUE_PROMPT: 'The following is a conversation between {character1} and {character2}. {character2_description}. Chat history: {previous_conversation}\n\n {character2}s sentiment towards {character1}: {sentiment}. ### Instruction: Write a single response as {character2}, using {character2} description.\n\n### Response:\n'
ITEM_PROMPT: '### Instruction: Items:[{items}];Characters:[{character1},{character2}] Text:[{text}] \n\nIn the supplied text, was an item explicitly given, taken, dropped or put somewhere? Insert your thoughts about it in [my thoughts], and the results in "item", "from" and "to". Insert {character1}s sentiment towards {character2} in a single word in [sentiment assessment]. Write your response in JSON format. Example: {{ "thoughts":"[my thoughts]", "result": {{ "item":"", "from":"", "to":""}}, {{"sentiment":"[sentiment assessment]"}} }} End of example. \n\n Make sure the response is valid JSON\n\n### Response:\n'

Expand Down
2 changes: 1 addition & 1 deletion tale/llm_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def _do_process_result(self, url, player_io: TextBuffer, io) -> str:
""" Process the result from the stream endpoint """
tries = 0
old_text = ''
while tries < 2:
while tries < 4:
time.sleep(0.5)
data = requests.post(url)
text = json.loads(data.text)['results'][0]['text']
Expand Down
12 changes: 4 additions & 8 deletions tale/llm_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def __init__(self):
self.item_prompt = config_file['ITEM_PROMPT']
self._story_background = ''
self.io_util = IoUtil()
self.stream = True
self.stream = config_file['STREAM']
self.connection = None

def evoke(self, player_io: TextBuffer, message: str, max_length : bool=False, rolling_prompt='', alt_prompt=''):
Expand All @@ -39,8 +39,7 @@ def evoke(self, player_io: TextBuffer, message: str, max_length : bool=False, ro
trimmed_message = parse_utils.remove_special_chars(str(message))
base_prompt = alt_prompt if alt_prompt else self.base_prompt
amount = int(len(trimmed_message) * 2.5)
prompt = rolling_prompt if not alt_prompt else ''
prompt += base_prompt.format(input_text=str(trimmed_message))
prompt = base_prompt.format(history=rolling_prompt if not alt_prompt else '', input_text=str(trimmed_message))

rolling_prompt = self.update_memory(rolling_prompt, trimmed_message)

Expand Down Expand Up @@ -71,8 +70,7 @@ def generate_dialogue(self, conversation: str, character_card: str, character_na

request_body = self.default_body
request_body['prompt'] = prompt
response = requests.post(self.url, data=json.dumps(request_body))
text = parse_utils.trim_response(json.loads(response.text)['results'][0]['text'])
text = parse_utils.trim_response(self.io_util.synchronous_request(self.url + self.endpoint, request_body))

item_handling_result, new_sentiment = self.dialogue_analysis(text, character_card, character_name, target)

Expand All @@ -83,9 +81,7 @@ def dialogue_analysis(self, text: str, character_card: str, character_name: str,
prompt = self.generate_item_prompt(text, items, character_name, target)
request_body = self.analysis_body
request_body['prompt'] = prompt
response = requests.post(self.url, data=json.dumps(request_body))

text = parse_utils.trim_response(json.loads(response.text)['results'][0]['text'])
text = parse_utils.trim_response(self.io_util.synchronous_request(self.url + self.endpoint, request_body))
try:
json_result = json.loads(text.replace('\n', ''))
except JSONDecodeError as exc:
Expand Down

0 comments on commit 8b83643

Please sign in to comment.