Skip to content

Commit

Permalink
Add "context_threshold" and "context_truncation_strategy" in options
Browse files Browse the repository at this point in the history
  • Loading branch information
jekalmin authored and jekalmin committed Jan 27, 2024
1 parent d9c9811 commit e9b74dd
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 9 deletions.
27 changes: 27 additions & 0 deletions custom_components/extended_openai_conversation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
CONF_API_VERSION,
CONF_SKIP_AUTHENTICATION,
CONF_USE_TOOLS,
CONF_CONTEXT_THRESHOLD,
CONF_CONTEXT_TRUNCATE_STRATEGY,
DEFAULT_ATTACH_USERNAME,
DEFAULT_CHAT_MODEL,
DEFAULT_MAX_TOKENS,
Expand All @@ -57,6 +59,8 @@
DEFAULT_CONF_FUNCTIONS,
DEFAULT_SKIP_AUTHENTICATION,
DEFAULT_USE_TOOLS,
DEFAULT_CONTEXT_THRESHOLD,
DEFAULT_CONTEXT_TRUNCATE_STRATEGY,
DOMAIN,
)

Expand Down Expand Up @@ -271,6 +275,22 @@ def get_functions(self):
except:
raise FunctionLoadFailed()

async def truncate_message_history(self, messages):
"""Truncate message history."""
strategy = self.entry.options.get(
CONF_CONTEXT_TRUNCATE_STRATEGY, DEFAULT_CONTEXT_TRUNCATE_STRATEGY
)

if strategy == "clear":
last_user_message_index = None
for i in reversed(range(len(messages))):
if messages[i]["role"] == "user":
last_user_message_index = i
break

if last_user_message_index is not None:
del messages[1:last_user_message_index]

async def query(
self,
user_input: conversation.ConversationInput,
Expand All @@ -284,6 +304,9 @@ async def query(
top_p = self.entry.options.get(CONF_TOP_P, DEFAULT_TOP_P)
temperature = self.entry.options.get(CONF_TEMPERATURE, DEFAULT_TEMPERATURE)
use_tools = self.entry.options.get(CONF_USE_TOOLS, DEFAULT_USE_TOOLS)
context_threshold = self.entry.options.get(
CONF_CONTEXT_THRESHOLD, DEFAULT_CONTEXT_THRESHOLD
)
functions = list(map(lambda s: s["spec"], self.get_functions()))
function_call = "auto"
if n_requests == self.entry.options.get(
Expand Down Expand Up @@ -315,6 +338,10 @@ async def query(
)

_LOGGER.info("Response %s", response.model_dump(exclude_none=True))

if response.usage.total_tokens > context_threshold:
await self.truncate_message_history(messages)

choice: Choice = response.choices[0]
message = choice.message

Expand Down
28 changes: 27 additions & 1 deletion custom_components/extended_openai_conversation/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
NumberSelector,
NumberSelectorConfig,
TemplateSelector,
AttributeSelector,
SelectSelector,
SelectSelectorConfig,
SelectOptionDict,
Expand All @@ -41,6 +40,8 @@
CONF_API_VERSION,
CONF_SKIP_AUTHENTICATION,
CONF_USE_TOOLS,
CONF_CONTEXT_THRESHOLD,
CONF_CONTEXT_TRUNCATE_STRATEGY,
DEFAULT_ATTACH_USERNAME,
DEFAULT_CHAT_MODEL,
DEFAULT_MAX_TOKENS,
Expand All @@ -52,6 +53,9 @@
DEFAULT_CONF_BASE_URL,
DEFAULT_SKIP_AUTHENTICATION,
DEFAULT_USE_TOOLS,
DEFAULT_CONTEXT_THRESHOLD,
DEFAULT_CONTEXT_TRUNCATE_STRATEGY,
CONTEXT_TRUNCATE_STRATEGIES,
DOMAIN,
DEFAULT_NAME,
)
Expand Down Expand Up @@ -83,6 +87,8 @@
CONF_FUNCTIONS: DEFAULT_CONF_FUNCTIONS_STR,
CONF_ATTACH_USERNAME: DEFAULT_ATTACH_USERNAME,
CONF_USE_TOOLS: DEFAULT_USE_TOOLS,
CONF_CONTEXT_THRESHOLD: DEFAULT_CONTEXT_THRESHOLD,
CONF_CONTEXT_TRUNCATE_STRATEGY: DEFAULT_CONTEXT_TRUNCATE_STRATEGY,
}
)

Expand Down Expand Up @@ -230,4 +236,24 @@ def openai_config_option_schema(self, options: MappingProxyType[str, Any]) -> di
description={"suggested_value": options.get(CONF_USE_TOOLS)},
default=DEFAULT_USE_TOOLS,
): BooleanSelector(),
vol.Optional(
CONF_CONTEXT_THRESHOLD,
description={"suggested_value": options.get(CONF_CONTEXT_THRESHOLD)},
default=DEFAULT_CONTEXT_THRESHOLD,
): int,
vol.Optional(
CONF_CONTEXT_TRUNCATE_STRATEGY,
description={
"suggested_value": options.get(CONF_CONTEXT_TRUNCATE_STRATEGY)
},
default=DEFAULT_CONTEXT_TRUNCATE_STRATEGY,
): SelectSelector(
SelectSelectorConfig(
options=[
SelectOptionDict(value=strategy["key"], label=strategy["label"])
for strategy in CONTEXT_TRUNCATE_STRATEGIES
],
mode=SelectSelectorMode.DROPDOWN,
)
),
}
5 changes: 5 additions & 0 deletions custom_components/extended_openai_conversation/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,10 @@
DEFAULT_ATTACH_USERNAME = False
CONF_USE_TOOLS = "use_tools"
DEFAULT_USE_TOOLS = False
CONF_CONTEXT_THRESHOLD = "context_threshold"
DEFAULT_CONTEXT_THRESHOLD = 13000
CONTEXT_TRUNCATE_STRATEGIES = [{"key": "clear", "label": "Clear All Messages"}]
CONF_CONTEXT_TRUNCATE_STRATEGY = "context_truncate_strategy"
DEFAULT_CONTEXT_TRUNCATE_STRATEGY = CONTEXT_TRUNCATE_STRATEGIES[0]["key"]

SERVICE_QUERY_IMAGE = "query_image"
4 changes: 3 additions & 1 deletion custom_components/extended_openai_conversation/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
"max_function_calls_per_conversation": "Maximum function calls per conversation",
"functions": "Functions",
"attach_username": "Attach Username to Message",
"use_tools": "Use Tools"
"use_tools": "Use Tools",
"context_threshold": "Context Threshold",
"context_truncate_strategy": "Context truncation strategy when exceeded threshold"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
"max_function_calls_per_conversation": "Maximale Anzahl an Funktionsaufrufen pro Konversation",
"functions": "Funktionen",
"attach_username": "Benutzernamen mitgeben",
"use_tools": "Use Tools"
"use_tools": "Use Tools",
"context_threshold": "Context Threshold",
"context_truncate_strategy": "Context truncation strategy when exceeded threshold"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
"max_function_calls_per_conversation": "Maximum function calls per conversation",
"functions": "Functions",
"attach_username": "Attach Username to Message",
"use_tools": "Use Tools"
"use_tools": "Use Tools",
"context_threshold": "Context Threshold",
"context_truncate_strategy": "Context truncation strategy when exceeded threshold"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
"max_function_calls_per_conversation": "Nombre maximal d'appels de fonction par conversation",
"functions": "Fonctions",
"attach_username": "Joindre le nom d'utilisateur au message",
"use_tools": "Use Tools"
"use_tools": "Use Tools",
"context_threshold": "Context Threshold",
"context_truncate_strategy": "Context truncation strategy when exceeded threshold"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
"max_function_calls_per_conversation": "Beszélgetésenkénti maximum funkcióhívások száma",
"functions": "Funkciók",
"attach_username": "Felhasználónév hozzácsatolása az üzenethez",
"use_tools": "Use Tools"
"use_tools": "Use Tools",
"context_threshold": "Context Threshold",
"context_truncate_strategy": "Context truncation strategy when exceeded threshold"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
"max_function_calls_per_conversation": "Maximum function calls per conversation",
"functions": "Functions",
"attach_username": "Attach Username to Message",
"use_tools": "Use Tools"
"use_tools": "Use Tools",
"context_threshold": "Context Threshold",
"context_truncate_strategy": "Context truncation strategy when exceeded threshold"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
"max_function_calls_per_conversation": "Maximale keren functies mogen worden aangeroepen per conversatie",
"functions": "Functies",
"attach_username": "Gebruikersnaam aan bericht toevoegen",
"use_tools": "Use Tools"
"use_tools": "Use Tools",
"context_threshold": "Context Threshold",
"context_truncate_strategy": "Context truncation strategy when exceeded threshold"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
"max_function_calls_per_conversation": "Maksymalna ilość wywołań funkcji na rozmowę",
"functions": "Funkcje",
"attach_username": "Dodaj nazwę użytkownika do wiadomości",
"use_tools": "Use Tools"
"use_tools": "Use Tools",
"context_threshold": "Context Threshold",
"context_truncate_strategy": "Context truncation strategy when exceeded threshold"
}
}
}
Expand Down

0 comments on commit e9b74dd

Please sign in to comment.