├── README.md └── script.py /README.md: -------------------------------------------------------------------------------- 1 | # dynamic_context 2 | 3 | Extension for oobabooga's [Text Generation WebUI](https://github.com/oobabooga/text-generation-webui) that replaces the tags {{time}}, {{date}} and {{weekday}} in the character's context with the current time, and date, respectively. 4 | Additionally it also adds the current time as extra information at the end of the prompt (and optionally the date too). 5 | 6 | This is a simple extension, and this additional context doesn't get saved in the chat history, as it's purpose is only to bias the AI response while keeping in mind the current time, or date. 7 | -------------------------------------------------------------------------------- /script.py: -------------------------------------------------------------------------------- 1 | import os 2 | import gradio as gr 3 | from datetime import datetime 4 | 5 | params = { 6 | 'timecontext': True, 7 | 'datecontext': False, 8 | } 9 | 10 | def custom_generate_chat_prompt(user_input, state, **kwargs): 11 | ''' 12 | This extension will replace all instances of 13 | {{time}} and {{date}} with the current 14 | time and date, respectively 15 | Additionall replaces {{weekday}} with the day of the week. 16 | ''' 17 | state['context'] = state['context'].replace('{{time}}',datetime.now().strftime("%I:%M %p")) 18 | state['context'] = state['context'].replace('{{date}}',datetime.now().strftime("%B %d, %Y")) 19 | state['context'] = state['context'].replace('{{weekday}}',("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")[datetime.now().weekday()]) 20 | # print(f'<>') 21 | # print(f'{state["context"]}') 22 | return 23 | 24 | 25 | def input_modifier(string): 26 | """ 27 | This function adds time or date context to each prompt. 28 | """ 29 | if params['timecontext'] : string += f' [Current time: {datetime.now().strftime("%I:%M %p")}]' 30 | if params['datecontext'] : string += f' [Current date: {datetime.now().strftime("%B %d, %Y")}]' 31 | return string 32 | 33 | 34 | def ui(): 35 | with gr.Accordion("Prompt context"): 36 | timecontext = gr.Checkbox(value=params['timecontext'], label='Add current time context to the end of the prompt') 37 | datecontext = gr.Checkbox(value=params['datecontext'], label='Add current date context to the end of the prompt') 38 | timecontext.change(lambda x: params.update({"timecontext": x}), timecontext, None) 39 | datecontext.change(lambda x: params.update({"datecontext": x}), datecontext, None) 40 | --------------------------------------------------------------------------------