(llm_model)
| 7 | from llmware.resources import PromptState |
| 8 | |
| 9 | def prompt_state(llm_model): |
| 10 | |
| 11 | # Create a new prompter with state persistence |
| 12 | prompter = Prompt(save_state=True) |
| 13 | |
| 14 | # Capture the prompt_id (which can be used later to reload state) |
| 15 | prompt_id = prompter.prompt_id |
| 16 | |
| 17 | # Load the model |
| 18 | prompter.load_model(llm_model, temperature=0.0, sample=False) |
| 19 | |
| 20 | # Define a list of prompts |
| 21 | prompts = [ |
| 22 | {"query": "How old is Bob?", "context": "John is 43 years old. Bob is 27 years old."}, |
| 23 | {"query": "When did COVID start?", "context": "COVID started in March of 2020 in most of the world."}, |
| 24 | {"query": "What is the current stock price?", "context": "The stock is trading at $26 today."}, |
| 25 | {"query": "When is the big game?", "context": "The big game will be played on November 14, 2023."}, |
| 26 | {"query": "What is the CFO's salary?", "context": "The CFO has a salary of $285,000."}, |
| 27 | {"query": "What grade is Michael in school?", "context": "Michael is starting 11th grade."} |
| 28 | ] |
| 29 | |
| 30 | # Iterate through the prompt which will save each response dict in in the prompt_state |
| 31 | print (f"> Sending a series of prompts to {llm_model}...") |
| 32 | |
| 33 | for i, prompt in enumerate(prompts): |
| 34 | print (" - " + prompt["query"]) |
| 35 | response = prompter.prompt_main(prompt["query"] ,context=prompt["context"] ,register_trx=True) |
| 36 | |
| 37 | print(f" - LLM Responses: {response}") |
| 38 | |
| 39 | # Print how many interactions are now in the prompt history |
| 40 | interaction_history = prompter.interaction_history |
| 41 | print (f"> Prompt Interaction History now contains {len(interaction_history)} interactions") |
| 42 | |
| 43 | # Use the dialog_tracker to regenerate the conversation with the LLM |
| 44 | print (f"> Reconstructed Dialog") |
| 45 | dialog_history = prompter.dialog_tracker |
| 46 | for i, conversation_turn in enumerate(dialog_history): |
| 47 | print(" - ", i, "[user]: ", conversation_turn["user"]) |
| 48 | print(" - ", i, "[ bot]: ", conversation_turn["bot"]) |
| 49 | |
| 50 | # Saving and clean the prompt state |
| 51 | prompter.save_state() |
| 52 | prompter.clear_history() |
| 53 | |
| 54 | # Print the number of interactions |
| 55 | interaction_history = prompter.interaction_history |
| 56 | print (f"> Prompt history has been cleared") |
| 57 | print (f"> Prompt Interaction History now contains {len(interaction_history)} interactions") |
| 58 | |
| 59 | # Reload the prompt state using the prompt_id and print again the number of interactions |
| 60 | prompter.load_state(prompt_id) |
| 61 | interaction_history = prompter.interaction_history |
| 62 | print (f"> The previous prompt state has been re-loaded") |
| 63 | print (f"> Prompt Interaction History now contains {len(interaction_history)} interactions") |
| 64 | |
| 65 | # Generate a Prompt transaction report |
| 66 | prompt_transaction_report = PromptState().generate_interaction_report([prompt_id]) |
no test coverage detected