MCPcopy
hub / github.com/llmware-ai/llmware / PromptState

Class PromptState

llmware/resources.py:4517–4908  ·  view source on GitHub ↗

PromptState is the main class abstraction that handles persisting and lookup of Prompt interactions

Source from the content-addressed store, hash-verified

4515
4516
4517class PromptState:
4518
4519 """ PromptState is the main class abstraction that handles persisting and lookup of Prompt interactions"""
4520
4521 def __init__(self, prompt=None):
4522
4523 self.prompt = prompt
4524 self.prompt_state_base_name = "prompt_"
4525 self.prompt_state_format = ".jsonl"
4526
4527 # check for llmware path & create if not already set up
4528 if not os.path.exists(LLMWareConfig.get_llmware_path()):
4529
4530 # if not explicitly set up by user, then create folder directory structure
4531 LLMWareConfig.setup_llmware_workspace()
4532
4533 self.prompt_path = LLMWareConfig.get_prompt_path()
4534 self.output_path = LLMWareConfig.get_prompt_path()
4535
4536 # edge case - if llmware main path exists, but prompt path not created or deleted
4537 if not os.path.exists(self.prompt_path):
4538 os.mkdir(self.prompt_path)
4539 os.chmod(self.prompt_path, 0o777)
4540
4541 # prompt state written to files
4542 self.prompt_collection = None
4543 self.write_to_db = False
4544
4545 def get_prompt_state_fn_from_id(self, prompt_id):
4546
4547 """Generates the prompt state filename from prompt_id """
4548 fn = self.prompt_state_base_name + str(prompt_id) + self.prompt_state_format
4549 return fn
4550
4551 def get_prompt_id_from_prompt_state_fn(self, fn):
4552
4553 """ Gets the prompt id extracted from prompt state filename """
4554
4555 core_fn = fn.split(".")[0]
4556 id = core_fn.split("_")[-1]
4557 return id
4558
4559 def lookup_by_prompt_id(self, prompt_id):
4560
4561 """ Lookup by prompt id to retrieve a persisted prompt interaction """
4562
4563 ai_trx_list = self.lookup_by_prompt_id_from_file(prompt_id)
4564 return ai_trx_list
4565
4566 def register_interaction(self, ai_dict):
4567
4568 """ Registers a new prompt interaction into the interaction history in memory """
4569
4570 # by default, add to the interaction_history in memory
4571 self.prompt.interaction_history.append(ai_dict)
4572
4573 return ai_dict
4574

Callers 14

prompt_stateFunction · 0.90
__init__Method · 0.90
lookup_llm_trx_allMethod · 0.90
load_stateMethod · 0.90
save_stateMethod · 0.90
lookup_by_prompt_idMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected