jekalmin / extended_openai_conversation

Home Assistant custom component of conversation agent. It uses OpenAI to control your devices.
916 stars 129 forks source link

[Feature][Advice request] About conversation_id without possibility to define it in #78

Closed kamilk91 closed 9 months ago

kamilk91 commented 9 months ago

Hi.

Since conversation process service has no option to provide very important conversation_id which is ULID format (probably?) i wanted to do something to allow do that by HASS variable, or another way.

My plan is to specify where current conversation_id is stored and define events to recreate this id (like time pattern, or message count patter, whatever).

I wanted to make try with constant variable in Python, in your code @jekalmin

I modified __init__.py but to be honest it is a little bit confusing for me.

 async def async_process(
     self, user_input: conversation.ConversationInput
 ) -> conversation.ConversationResult:
     raw_prompt = self.entry.options.get(CONF_PROMPT, DEFAULT_PROMPT)
     exposed_entities = self.get_exposed_entities()

     conversation_id = ulid.from_uuid(uuid.UUID('a061d69e-bde0-4c35-9448-198d3a58d904'));

     if user_input.conversation_id in self.history:
         conversation_id = user_input.conversation_id
         messages = self.history[conversation_id]

But it produces exception:

invalid agent ID for dictionary value @ data['agent_id']

Do you have any ideas where exactly should i start ? Which place will be best to pass this Id without making new problems?

jekalmin commented 9 months ago

I'm quite not sure if I can distinguish whether the request is from service call or assist pipeline. If it's possible to distinguish and okay to hard code, creating an input_text and storing id in there might be one way.

You can save and modify input_text whenever you want, and let conversation use state of input_text as conversation_id.


async def async_process(
     self, user_input: conversation.ConversationInput
 ) -> conversation.ConversationResult:

     if is_from_service_call(): # I don't know if it's possible to distinguish
         conversation_id = self.hass.states.get("input_text.xxxx").state
     else:
         conversation_id = ulid.ulid()

However, if you don't need it right away, then pray for https://github.com/home-assistant/core/pull/106078 to be merged soon.

hasbean commented 9 months ago

Looks like it just got merged!

kamilk91 commented 9 months ago

Thank you, you are helpful as always @jekalmin!