sensor:
- platform: command_line
name: GPT Response
command: "curl -XPOST https://api.openai.com/v1/chat/completions -H 'Content-Type: application/json' -H 'Authorization: Bearer YOUR_API_SECRET_KEY' -d '{\"model\": \"gpt-3.5-turbo\", \"messages\": [{\"role\": \"user\", \"content\": \"Act as a personal assistant for the male head of a household. You are witty and talk conversationally. The current time is {{ now() }}, and the current day is {{ now().weekday() }}. The weather is {{ states('weather.tomorrow_io_acehouse_control_nowcast') }}. Tottenham Hotspur play their next game {{ state_attr('sensor.thfc_tracker', 'kickoff_in') }} against {{ state_attr('sensor.thfc_tracker', 'opponent_name') }}. Greet me in a friendly but respectful way, commenting on the weather, football, and sometimes making a joke, in one or two sentences.\"}], \"top_p\": 1, \"temperature\": 0.7, \"max_tokens\": 320, \"presence_penalty\": 1}'"
value_template: "{{ value_json.choices[0].message.content }}"
scan_interval: 900
However I sometimes find it running into the character limit where the response exceeds 255 characters and thus cannot be stored in the sensor’s value_template. I read somewhere that attributes do not have this limit but I can’t figure out how to write that data into an attribute. Can you guide me here?
This is an example response from ChatGPT:
{
'id': 'chatcmpl-6p9XYPYSTTRi0xEviKjjilqrWU2Ve',
'object': 'chat.completion',
'created': 1677649420,
'model': 'gpt-3.5-turbo',
'usage': {'prompt_tokens': 56, 'completion_tokens': 31, 'total_tokens': 87},
'choices': [
{
'message': {
'role': 'assistant',
'content': 'The 2020 World Series was played in Arlington, Texas at the Globe Life Field, which was the new home stadium for the Texas Rangers.'},
'finish_reason': 'stop',
'index': 0
}
]
}
I tend to use option (a) as it gives me complete control to rewrite the JSON to what I want. But if you do not know JQ, then option (b) can probably get you what you want in an attribute and you can parse downward from there in Lovelace or whatever you wish.
As an example, your JSON with proper quoting and JQ could be like this:
Then the JSON attributes would be “created” and “response”.
Side note: The JSON you post is totally invalid. It uses single quotes. I have a hard time believing that is what you are getting back. Strings in JSON are specified using double quotes, i.e., " . If the strings are enclosed using single quotes, then the JSON is invalid unless processed by something that allows ALLOW_SINGLE_QUOTES
Ok, that solution with JQ sounds really neat. How would I append that command to my prompt? I have trouble forming it with the right quotes as the command is currently wrapped in double quotes.
It’s not a concern of mine. In fact if you want to laugh … scroll up a bit on that page and look at what they submit TO their application … it uses double quotes. And they return data in single quotes.
Specifically the text … A value can be a string in double quotes, or a number , or true or false or null, or an object or an array . These structures can be nested.
It does NOT say a value can be a string in ANY quotes or SINGLE quotes or anything else other than DOUBLE quotes. It is invalid JSON (I would not expect anything less from ChatGPT since it normally gives invalid answers).
This exact discussion is discussed here on another recent post in which another person (like me) says whomever is creating the JSON response does not understand JSON.
Then again, maybe you can just ask ChatGPT why it believes it’s JSON response is correct???