Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Attach the user's name to the user message payload if known #28

Merged
merged 5 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Derived from [OpenAI Conversation](https://www.home-assistant.io/integrations/op
- Ability to call service of Home Assistant
- Ability to create automation
- Ability to get data from API or web page
- Option to pass the current user's name to OpenAI via the user message context

## How it works
Extended OpenAI Conversation uses OpenAI API's feature of [function calling](https://platform.openai.com/docs/guides/function-calling) to call service of Home Assistant.
Expand Down Expand Up @@ -55,6 +56,7 @@ https://github.com/jekalmin/extended_openai_conversation/assets/2917984/64ba656e
By clicking a button from Edit Assist, Options can be customized.<br/>
Options include [OpenAI Conversation](https://www.home-assistant.io/integrations/openai_conversation/) options and two new options.

- `Attach Username`: Pass the active user's name (if applicaple) to OpenAI via rhe message payload. Currently, this only applies to conversations through the UI or REST API.
Copy link
Owner

@jekalmin jekalmin Nov 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious of what rhe stands for. Can it be a typo?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typeo

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved


- `Maximum Function Calls Per Conversation`: limit the number of function calls in a single conversation.
(Sometimes function is called over and over again, possibly running into infinite loop)
Expand Down
11 changes: 9 additions & 2 deletions custom_components/extended_openai_conversation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from homeassistant.components import conversation
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_API_KEY, MATCH_ALL
from homeassistant.const import CONF_API_KEY, MATCH_ALL, ATTR_NAME
from homeassistant.core import HomeAssistant, ServiceCall, SupportsResponse
from homeassistant.util import ulid
from homeassistant.components.homeassistant.exposed_entities import async_should_expose
Expand All @@ -30,6 +30,7 @@
)

from .const import (
CONF_ATTACH_USERNAME,
CONF_CHAT_MODEL,
CONF_MAX_TOKENS,
CONF_PROMPT,
Expand All @@ -38,6 +39,7 @@
CONF_MAX_FUNCTION_CALLS_PER_CONVERSATION,
CONF_FUNCTIONS,
CONF_BASE_URL,
DEFAULT_ATTACH_USERNAME,
DEFAULT_CHAT_MODEL,
DEFAULT_MAX_TOKENS,
DEFAULT_PROMPT,
Expand Down Expand Up @@ -150,8 +152,13 @@ async def async_process(
response=intent_response, conversation_id=conversation_id
)
messages = [{"role": "system", "content": prompt}]
user_message = {"role": "user", "content": user_input.text}
if self.entry.options.get(CONF_ATTACH_USERNAME, DEFAULT_ATTACH_USERNAME):
user = await self.hass.auth.async_get_user(user_input.context.user_id)
if user is not None and user.name is not None:
user_message[ATTR_NAME] = user.name

messages.append({"role": "user", "content": user_input.text})
messages.append(user_message)

try:
response = await self.query(user_input, messages, exposed_entities, 0)
Expand Down
11 changes: 11 additions & 0 deletions custom_components/extended_openai_conversation/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.selector import (
BooleanSelector,
NumberSelector,
NumberSelectorConfig,
TemplateSelector,
Expand All @@ -24,6 +25,7 @@
from .helpers import validate_authentication

from .const import (
CONF_ATTACH_USERNAME,
CONF_CHAT_MODEL,
CONF_MAX_TOKENS,
CONF_PROMPT,
Expand All @@ -32,6 +34,7 @@
CONF_MAX_FUNCTION_CALLS_PER_CONVERSATION,
CONF_FUNCTIONS,
CONF_BASE_URL,
DEFAULT_ATTACH_USERNAME,
DEFAULT_CHAT_MODEL,
DEFAULT_MAX_TOKENS,
DEFAULT_PROMPT,
Expand Down Expand Up @@ -65,6 +68,7 @@
CONF_TOP_P: DEFAULT_TOP_P,
CONF_TEMPERATURE: DEFAULT_TEMPERATURE,
CONF_FUNCTIONS: DEFAULT_CONF_FUNCTIONS_STR,
CONF_ATTACH_USERNAME: DEFAULT_ATTACH_USERNAME,
}
)

Expand Down Expand Up @@ -194,4 +198,11 @@ def openai_config_option_schema(options: MappingProxyType[str, Any]) -> dict:
description={"suggested_value": options.get(CONF_FUNCTIONS)},
default=DEFAULT_CONF_FUNCTIONS_STR,
): TemplateSelector(),
vol.Optional(
CONF_ATTACH_USERNAME,
description={
"suggested_value": options.get(CONF_ATTACH_USERNAME)
},
default=DEFAULT_ATTACH_USERNAME,
): BooleanSelector(),
}
2 changes: 2 additions & 0 deletions custom_components/extended_openai_conversation/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,5 @@
]
CONF_BASE_URL = "base_url"
DEFAULT_CONF_BASE_URL = "https://api.openai.com/v1"
CONF_ATTACH_USERNAME = "attach_username"
DEFAULT_ATTACH_USERNAME = False
3 changes: 2 additions & 1 deletion custom_components/extended_openai_conversation/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"temperature": "Temperature",
"top_p": "Top P",
"max_function_calls_per_conversation": "Maximum function calls per conversation",
"functions": "Functions"
"functions": "Functions",
"attach_username": "Attach Username to Message"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"temperature": "Temperature",
"top_p": "Top P",
"max_function_calls_per_conversation": "Maximum function calls per conversation",
"functions": "Functions"
"functions": "Functions",
"attach_username": "Attach Username to Message"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"temperature": "Temperature",
"top_p": "Top P",
"max_function_calls_per_conversation": "Maximum function calls per conversation",
"functions": "Functions"
"functions": "Functions",
"attach_username": "Attach Username to Message"
}
}
}
Expand Down