From 41b8bd8cc97b0802ca8e06b712dd309a1277661b Mon Sep 17 00:00:00 2001 From: broken-admin Date: Sat, 12 Apr 2025 08:20:44 +0000 Subject: [PATCH 1/4] Implement a patch for o1 model functionality * OpenAI intends to implement the "system" role in future updates --- terminalgpt/config.py | 34 ++++++++++++++++++++++++++++++++++ terminalgpt/main.py | 13 ++++++++++--- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/terminalgpt/config.py b/terminalgpt/config.py index 08d71bd..7caacc1 100644 --- a/terminalgpt/config.py +++ b/terminalgpt/config.py @@ -50,6 +50,17 @@ def get_default_config() -> dict: """, } +INIT_SYSTEM_MESSAGE_O1 = { + "role": "user", + "content": f""" +- Your name is "TerminalGPT". +- You are a helpful personal assistant for programers. +- You are running on {machine_info()} machine. +- Please note that your answers will be displayed on the terminal. +- So keep answers short as possible and use a suitable format for printing on a terminal. +""", +} + INIT_WELCOME_MESSAGE = { "role": "system", @@ -60,6 +71,15 @@ def get_default_config() -> dict: """, } +INIT_WELCOME_MESSAGE_O1 = { + "role": "user", + "content": """ +- Please start the conversation with a random and short greeting message starts with 'Welcome to terminalGPT'. +- Add a ton of self humor. +- Keep it short as possible, one line. +""", +} + INIT_WELCOME_BACK_MESSAGE = { "role": "system", "content": """ @@ -74,6 +94,20 @@ def get_default_config() -> dict: """, } +INIT_WELCOME_BACK_MESSAGE_O1 = { + "role": "user", + "content": """ +The conversation you remember was a while ago, now we are continuing it. +Please start the conversation with a random and short welcome back message. +- Start with 'Welcome back to terminalGPT'. +- Add a ton of self humor. +- Keep it short as possible, one line. + +After the welcome back message, please summarize the last conversation. (e.g. "Last time we talked about ...") +- End with a something that invites the user to continue the conversation. +""", +} + TITLE_MESSAGE = """ Please give this conversation a short title. I'm going to use this title as a file name for the conversation. diff --git a/terminalgpt/main.py b/terminalgpt/main.py index b3e0309..2bebf2c 100644 --- a/terminalgpt/main.py +++ b/terminalgpt/main.py @@ -212,8 +212,12 @@ def new(ctx): f"{Style.RESET_ALL}{Style.BRIGHT}Style: {Style.RESET_ALL}{ctx.obj['STYLE']}" ) - messages = [config.INIT_SYSTEM_MESSAGE] - chat_manager.welcome_message(messages + [config.INIT_WELCOME_MESSAGE]) + if ctx.obj['MODEL'].startswith('o1'): + messages = [config.INIT_SYSTEM_MESSAGE_O1] + chat_manager.welcome_message(messages + [config.INIT_WELCOME_MESSAGE_O1]) + else: + messages = [config.INIT_SYSTEM_MESSAGE] + chat_manager.welcome_message(messages + [config.INIT_WELCOME_MESSAGE]) chat_manager.messages = messages chat_manager.chat_loop() @@ -238,7 +242,10 @@ def one_shot(ctx, question): chat_manager.client = client conversation_manager.client = client - messages = [config.INIT_SYSTEM_MESSAGE] + if ctx.obj["MODEL"].startswith("o1"): + messages = [config.INIT_SYSTEM_MESSAGE_O1] + else: + messages = [config.INIT_SYSTEM_MESSAGE] messages.append({"role": "user", "content": question}) From a6f0670f00838b7a0943a43aa66d8b00d372efdd Mon Sep 17 00:00:00 2001 From: broken-admin Date: Sat, 12 Apr 2025 20:43:50 +0000 Subject: [PATCH 2/4] Implement behavior for conversation loading --- terminalgpt/main.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/terminalgpt/main.py b/terminalgpt/main.py index 2bebf2c..76428f4 100644 --- a/terminalgpt/main.py +++ b/terminalgpt/main.py @@ -315,7 +315,10 @@ def load(ctx): while not messages: messages = conversation_manager.load_conversation() - messages.append(config.INIT_WELCOME_BACK_MESSAGE) + if ctx.obj["MODEL"].startswith('o1'): + messages.append(config.INIT_WELCOME_BACK_MESSAGE_O1) + else: + messages.append(config.INIT_WELCOME_BACK_MESSAGE) chat_manager.messages = messages chat_manager.total_usage = chat_manager.num_tokens_from_messages() From 1c363117170331e4d90b5e274b4d168afa9684e1 Mon Sep 17 00:00:00 2001 From: broken-admin Date: Mon, 14 Apr 2025 18:44:03 +0000 Subject: [PATCH 3/4] Reduce duplicated prompts by introducing additional logic * Modify logic to only change role of messages when necessary rather than duplicate * Change logic to only o1-mini model --- terminalgpt/config.py | 34 ---------------------------------- terminalgpt/main.py | 27 +++++++++++++-------------- 2 files changed, 13 insertions(+), 48 deletions(-) diff --git a/terminalgpt/config.py b/terminalgpt/config.py index 7caacc1..08d71bd 100644 --- a/terminalgpt/config.py +++ b/terminalgpt/config.py @@ -50,17 +50,6 @@ def get_default_config() -> dict: """, } -INIT_SYSTEM_MESSAGE_O1 = { - "role": "user", - "content": f""" -- Your name is "TerminalGPT". -- You are a helpful personal assistant for programers. -- You are running on {machine_info()} machine. -- Please note that your answers will be displayed on the terminal. -- So keep answers short as possible and use a suitable format for printing on a terminal. -""", -} - INIT_WELCOME_MESSAGE = { "role": "system", @@ -71,15 +60,6 @@ def get_default_config() -> dict: """, } -INIT_WELCOME_MESSAGE_O1 = { - "role": "user", - "content": """ -- Please start the conversation with a random and short greeting message starts with 'Welcome to terminalGPT'. -- Add a ton of self humor. -- Keep it short as possible, one line. -""", -} - INIT_WELCOME_BACK_MESSAGE = { "role": "system", "content": """ @@ -94,20 +74,6 @@ def get_default_config() -> dict: """, } -INIT_WELCOME_BACK_MESSAGE_O1 = { - "role": "user", - "content": """ -The conversation you remember was a while ago, now we are continuing it. -Please start the conversation with a random and short welcome back message. -- Start with 'Welcome back to terminalGPT'. -- Add a ton of self humor. -- Keep it short as possible, one line. - -After the welcome back message, please summarize the last conversation. (e.g. "Last time we talked about ...") -- End with a something that invites the user to continue the conversation. -""", -} - TITLE_MESSAGE = """ Please give this conversation a short title. I'm going to use this title as a file name for the conversation. diff --git a/terminalgpt/main.py b/terminalgpt/main.py index 76428f4..8c75a88 100644 --- a/terminalgpt/main.py +++ b/terminalgpt/main.py @@ -212,12 +212,12 @@ def new(ctx): f"{Style.RESET_ALL}{Style.BRIGHT}Style: {Style.RESET_ALL}{ctx.obj['STYLE']}" ) - if ctx.obj['MODEL'].startswith('o1'): - messages = [config.INIT_SYSTEM_MESSAGE_O1] - chat_manager.welcome_message(messages + [config.INIT_WELCOME_MESSAGE_O1]) - else: - messages = [config.INIT_SYSTEM_MESSAGE] - chat_manager.welcome_message(messages + [config.INIT_WELCOME_MESSAGE]) + messages = [config.INIT_SYSTEM_MESSAGE] + init_welcome = config.INIT_WELCOME_MESSAGE + if ctx.obj['MODEL'] == ('o1-mini'): + messages[0]["role"] = "user" + init_welcome["role"] = "user" + chat_manager.welcome_message(messages + [init_welcome]) chat_manager.messages = messages chat_manager.chat_loop() @@ -242,10 +242,9 @@ def one_shot(ctx, question): chat_manager.client = client conversation_manager.client = client - if ctx.obj["MODEL"].startswith("o1"): - messages = [config.INIT_SYSTEM_MESSAGE_O1] - else: - messages = [config.INIT_SYSTEM_MESSAGE] + messages = [config.INIT_SYSTEM_MESSAGE] + if ctx.obj['MODEL'] == ('o1-mini'): + messages[0]["role"] = "user" messages.append({"role": "user", "content": question}) @@ -315,10 +314,10 @@ def load(ctx): while not messages: messages = conversation_manager.load_conversation() - if ctx.obj["MODEL"].startswith('o1'): - messages.append(config.INIT_WELCOME_BACK_MESSAGE_O1) - else: - messages.append(config.INIT_WELCOME_BACK_MESSAGE) + messages = [config.INIT_WELCOME_BACK_MESSAGE] + if ctx.obj['MODEL'] == ('o1-mini'): + messages[0]["role"] = "user" + chat_manager.messages = messages chat_manager.total_usage = chat_manager.num_tokens_from_messages() From 9b95032dd964a9395cf5f21c27d690254c165e15 Mon Sep 17 00:00:00 2001 From: broken-admin Date: Mon, 14 Apr 2025 18:53:00 +0000 Subject: [PATCH 4/4] Further reduce logic into cli method * Only check the model type once * Modify the necessary prompts to run the command with the o1-model --- terminalgpt/main.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/terminalgpt/main.py b/terminalgpt/main.py index 8c75a88..442d7c8 100644 --- a/terminalgpt/main.py +++ b/terminalgpt/main.py @@ -115,6 +115,10 @@ def cli(ctx, model, style: str, token_limit: int = 0): printer=ctx.obj["PRINTER"], ) + if ctx.obj['MODEL'] == ('o1-mini'): + config.INIT_SYSTEM_MESSAGE["role"] = "user" + config.INIT_WELCOME_MESSAGE["role"] = "user" + config.INIT_WELCOME_BACK_MESSAGE["role"] = "user" @click.command(help="Installing the OpenAI API key and setup some default settings.") def install(): @@ -213,11 +217,7 @@ def new(ctx): ) messages = [config.INIT_SYSTEM_MESSAGE] - init_welcome = config.INIT_WELCOME_MESSAGE - if ctx.obj['MODEL'] == ('o1-mini'): - messages[0]["role"] = "user" - init_welcome["role"] = "user" - chat_manager.welcome_message(messages + [init_welcome]) + chat_manager.welcome_message(messages + [config.INIT_WELCOME_MESSAGE]) chat_manager.messages = messages chat_manager.chat_loop() @@ -243,8 +243,6 @@ def one_shot(ctx, question): conversation_manager.client = client messages = [config.INIT_SYSTEM_MESSAGE] - if ctx.obj['MODEL'] == ('o1-mini'): - messages[0]["role"] = "user" messages.append({"role": "user", "content": question})