!pip install openai
from openai import OpenAI
client = OpenAI(api_key=API_KEY)
from pathlib import Path

JSONPATH = Path("/Users/joregan/Playing/hsi/annotations/final_resolved")
SKIPPABLES = ["room"]

def process_item(item):
    utt_key = "utterance_type"
    if not utt_key in item["high_level"]:
        utt_key = "topic_name"
    if item["high_level"][utt_key] == "conversation_generic":
        return {}
    current = item["high_level"]["current_topic"]
    if current in SKIPPABLES:
        return {}
    snippet = item["snippet"]
    return {
        "topic:": current,
        "snippet": snippet
    }
import json

alldata = []
filepointer = []

for jsonfile in JSONPATH.glob("*.json"):
    stem = jsonfile.stem
    parts = stem.split("_")
    person = parts[1]
    room = parts[3]

    with open(jsonfile) as f:
        data = json.load(f)

    output = {
        "person": person,
        "room": room,
        "items": []
    }
    for item in data.keys():
        processed = process_item(data[item])
        if processed and processed != {}:
            output["items"].append(processed)
    alldata.append(output)
    filepointer.append(stem)
with open("/tmp/hsi.json", "w") as f:
    json.dump(alldata, f, indent=4)
test = alldata[0]
PROMPT = f"""
Below is some data from a conversation. The data is in JSON format.
The data contains a list of items, each with a topic and a snippet. The topic is the subject of the conversation, and the snippet is a short excerpt from the conversation.
Continue the conversation by writing a new snippet that is relevant to the topic. The new snippet should contain a reference to the topic and should be a natural continuation of the conversation.
The new snippet should be in the same format as the original snippets, and should be a short paragraph of text, of between 30 and 80 words.
There should only be one reference to the topic in the new snippet. It should be a natural part of the conversation, and should not be forced or awkward. It should be marked in boldface using markdown syntax. Only the determiner should be marked; boldface is marked by a single asterisk. The reference should be to the item itself, not to an abstract property related to the item: for example, in "the style of that painting", only "that painting" is a valid reference to the item: "the style" is not a valid reference to the item.
The reference to the topic should be suitable for instructing a person to point at it in a conversation. It can be a noun phrase, or a pronoun, but it should be clear and unambiguous. If the reference is a noun phrase, it should include a determiner, such as "this" or "that". Do not count articles ("the", "a", "an") as determiners, we are only interested in demonstrative determiners. If the reference is a pronoun, it should be clear what it refers to. Be sure to vary the references so there are results with each type of reference.
If another topic is mentioned in the new snippet and the spatial relationship is very clear, it is ok to reuse that spatial reference.
'If you want, I can stop by the kiosk and get *a newspaper* for tomorrow.' is a bad example for a snippet, because it does not contain to the item itself, but to another item of that category; also it includes an article "a" instead of a demonstrative determiner.
Create as many new snippets as there are items in the data.
```json
{test}
```
"""
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {
            "role": "user",
            "content": [
                { "type": "text", "text": PROMPT },
            ]
        }
    ]
)
response
ChatCompletion(id='chatcmpl-BRGKBf83FizBn2cuY40CfosbbuwGm', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='```json\n{\'topic:\': \'Book_28\', \'snippet\': "Yeah, I noticed **that novel** when I came in, and it seemed interesting. It must belong to someone who really enjoys detective stories, don\'t you think?"}\n```', refusal=None, role='assistant', annotations=[], audio=None, function_call=None, tool_calls=None))], created=1745837307, model='gpt-4o-2024-08-06', object='chat.completion', service_tier='default', system_fingerprint='fp_90122d973c', usage=CompletionUsage(completion_tokens=52, prompt_tokens=1029, total_tokens=1081, completion_tokens_details=CompletionTokensDetails(accepted_prediction_tokens=0, audio_tokens=0, reasoning_tokens=0, rejected_prediction_tokens=0), prompt_tokens_details=PromptTokensDetails(audio_tokens=0, cached_tokens=0)))
response_text = response.choices[0].message.content
response_text
'```json\n{\'topic:\': \'Book_28\', \'snippet\': "Yeah, I noticed **that novel** when I came in, and it seemed interesting. It must belong to someone who really enjoys detective stories, don\'t you think?"}\n```'
def clean_response(response_text):
    response_text = response_text.replace("```json", "")
    response_text = response_text.replace("```", "")
    response_text = response_text.strip()
    # response_text = response_text.replace("'", '"')
    # try:
    #     return json.loads(response_text)
    # except json.JSONDecodeError:
    #     print(response_text)
    data = eval(response_text)
    return data
clean_response(response_text)
{'topic:': 'Book_28',
 'snippet': "Yeah, I noticed **that novel** when I came in, and it seemed interesting. It must belong to someone who really enjoys detective stories, don't you think?"}
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {
            "role": "user",
            "content": [
                { "type": "text", "text": PROMPT },
            ]
        }
    ]
)
response
ChatCompletion(id='chatcmpl-BRGSLwcAubUqXJVqMYgh5T77PwpTP', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='```json\n[\n    {"topic:": "furniture", "snippet": "I\'ll rearrange the chairs today, so **this setup** feels more welcoming."},\n    {"topic:": "furniture", "snippet": "Perhaps moving **that table** near the window would brighten up the room."},\n    {"topic:": "furniture", "snippet": "You know, **those stools** would be perfect at the kitchen counter."},\n    {"topic:": "furniture", "snippet": "Let\'s think about where **this bench** could go; maybe the hallway?"},\n    {"topic:": "Television_f9d000b1", "snippet": "Maybe we could move **the TV** to the opposite wall for a cozier setup."},\n    {"topic:": "Dog_Statue_1", "snippet": "I checked around, and **the statue** seems perfectly fine now."},\n    {"topic:": "Dog_Statue_1", "snippet": "I\'d love to know how **that piece** first caught your eye."},\n    {"topic:": "Dog_Statue_1", "snippet": "And I was thinking, maybe **this statue** deserves a spot in the garden."},\n    {"topic:": "Tennis_Racquet_4", "snippet": "I think **this racquet** needs a new grip before the next match."},\n    {"topic:": "Laptop_4", "snippet": "I found **the charger**, so now you can use it wherever you like."},\n    {"topic:": "Laptop_4", "snippet": "Sure, just let me know if **this laptop** starts acting up again."},\n    {"topic:": "Laptop_4", "snippet": "I\'m happy holding onto **this one** until you\'re sure you need it."},\n    {"topic:": "KeyChain_76c05fe6", "snippet": "Can you show me how **these keys** fit into everything?"},\n    {"topic:": "Newspaper_990192bf", "snippet": "If you want, we could swap **this newspaper** for a new one."},\n    {"topic:": "Newspaper_990192bf", "snippet": "I heard they changed the layout in **the papers**, right?"},\n    {"topic:": "Newspaper_990192bf", "snippet": "I\'ve been thinking of subscribing to **them** for a while now."},\n    {"topic:": "Box_7d6b3e2c", "snippet": "I wonder if **that box** has any old photos we can look through."},\n    {"topic:": "Book_17", "snippet": "I might borrow **this novel** once you\'re done with it."},\n    {"topic:": "Book_17", "snippet": "Noticed that **another book** fits well with the decor too."},\n    {"topic:": "Book_17", "snippet": "I can’t wait to hear your thoughts on **the story** when you finish."},\n    {"topic:": "furniture", "snippet": "After settling **this room**, moving the furniture outside may be next."},\n    {"topic:": "painting", "snippet": "I think that moving **these paintings** has changed the room\'s atmosphere."},\n    {"topic:": "painting", "snippet": "And if we frame **that piece**, the entire collection will truly shine."},\n    {"topic:": "garbage bin", "snippet": "It might be a good idea to move **the bin** outside, near the sidewalk."},\n    {"topic:": "Book_28", "snippet": "Or perhaps **that novel** could be a fun read during the weekend."}\n]\n```', refusal=None, role='assistant', annotations=[], audio=None, function_call=None, tool_calls=None))], created=1745837813, model='gpt-4o-2024-08-06', object='chat.completion', service_tier='default', system_fingerprint='fp_90122d973c', usage=CompletionUsage(completion_tokens=792, prompt_tokens=1060, total_tokens=1852, completion_tokens_details=CompletionTokensDetails(accepted_prediction_tokens=0, audio_tokens=0, reasoning_tokens=0, rejected_prediction_tokens=0), prompt_tokens_details=PromptTokensDetails(audio_tokens=0, cached_tokens=0)))
response_text = response.choices[0].message.content
clean_response(response_text)
[{'topic:': 'furniture',
  'snippet': "I'll rearrange the chairs today, so **this setup** feels more welcoming."},
 {'topic:': 'furniture',
  'snippet': 'Perhaps moving **that table** near the window would brighten up the room.'},
 {'topic:': 'furniture',
  'snippet': 'You know, **those stools** would be perfect at the kitchen counter.'},
 {'topic:': 'furniture',
  'snippet': "Let's think about where **this bench** could go; maybe the hallway?"},
 {'topic:': 'Television_f9d000b1',
  'snippet': 'Maybe we could move **the TV** to the opposite wall for a cozier setup.'},
 {'topic:': 'Dog_Statue_1',
  'snippet': 'I checked around, and **the statue** seems perfectly fine now.'},
 {'topic:': 'Dog_Statue_1',
  'snippet': "I'd love to know how **that piece** first caught your eye."},
 {'topic:': 'Dog_Statue_1',
  'snippet': 'And I was thinking, maybe **this statue** deserves a spot in the garden.'},
 {'topic:': 'Tennis_Racquet_4',
  'snippet': 'I think **this racquet** needs a new grip before the next match.'},
 {'topic:': 'Laptop_4',
  'snippet': 'I found **the charger**, so now you can use it wherever you like.'},
 {'topic:': 'Laptop_4',
  'snippet': 'Sure, just let me know if **this laptop** starts acting up again.'},
 {'topic:': 'Laptop_4',
  'snippet': "I'm happy holding onto **this one** until you're sure you need it."},
 {'topic:': 'KeyChain_76c05fe6',
  'snippet': 'Can you show me how **these keys** fit into everything?'},
 {'topic:': 'Newspaper_990192bf',
  'snippet': 'If you want, we could swap **this newspaper** for a new one.'},
 {'topic:': 'Newspaper_990192bf',
  'snippet': 'I heard they changed the layout in **the papers**, right?'},
 {'topic:': 'Newspaper_990192bf',
  'snippet': "I've been thinking of subscribing to **them** for a while now."},
 {'topic:': 'Box_7d6b3e2c',
  'snippet': 'I wonder if **that box** has any old photos we can look through.'},
 {'topic:': 'Book_17',
  'snippet': "I might borrow **this novel** once you're done with it."},
 {'topic:': 'Book_17',
  'snippet': 'Noticed that **another book** fits well with the decor too.'},
 {'topic:': 'Book_17',
  'snippet': 'I can’t wait to hear your thoughts on **the story** when you finish.'},
 {'topic:': 'furniture',
  'snippet': 'After settling **this room**, moving the furniture outside may be next.'},
 {'topic:': 'painting',
  'snippet': "I think that moving **these paintings** has changed the room's atmosphere."},
 {'topic:': 'painting',
  'snippet': 'And if we frame **that piece**, the entire collection will truly shine.'},
 {'topic:': 'garbage bin',
  'snippet': 'It might be a good idea to move **the bin** outside, near the sidewalk.'},
 {'topic:': 'Book_28',
  'snippet': 'Or perhaps **that novel** could be a fun read during the weekend.'}]
esponse = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {
            "role": "user",
            "content": [
                { "type": "text", "text": PROMPT },
            ]
        }
    ]
)
esponse
esponse_text = esponse.choices[0].message.content
clean_response(esponse_text)
{'person': '4',
 'room': '222',
 'items': [{'topic': 'furniture',
   'snippet': 'We can try rearranging *the* furniture into a more open layout, which might make the room feel bigger.'},
  {'topic': 'furniture',
   'snippet': 'Maybe moving *that* couch closer to the window will make the room more inviting.'},
  {'topic': 'furniture',
   'snippet': "Let's keep *that* chair near the entrance as it seems to welcome guests quite nicely."},
  {'topic': 'furniture',
   'snippet': 'Sure, I can help you with rearranging *the* furniture now so everything is ready for when guests arrive.'},
  {'topic': 'Television_f9d000b1',
   'snippet': 'Well, *the* TV is mostly just background noise; I prefer engaging conversations with friends.'},
  {'topic': 'Dog_Statue_1',
   'snippet': 'Oh, *the* dog statue is just missing a small piece, but it still looks great.'},
  {'topic': 'Dog_Statue_1',
   'snippet': 'I think *that* dog statue can be repaired easily; we should look into it.'},
  {'topic': 'Dog_Statue_1',
   'snippet': "Interestingly, *the* green one seems to be everyone's favorite, don't you think?"},
  {'topic': 'Tennis_Racquet_4',
   'snippet': 'We should place *the* racket back in its bag so it doesn’t get damaged.'},
  {'topic': 'Laptop_4',
   'snippet': "*The* computer has such a sleek design; it's definitely a great choice for work."},
  {'topic': 'Laptop_4',
   'snippet': "If you're unsure about *that* laptop, maybe it belongs to someone else around here."},
  {'topic': 'Laptop_4',
   'snippet': "Once you're sure it's safely packed, I'll help you put *the* laptop away."},
  {'topic': 'KeyChain_76c05fe6',
   'snippet': '*The* keys always seem to get misplaced; we need a dedicated spot for them.'},
  {'topic': 'Newspaper_990192bf',
   'snippet': 'I think picking up *that* newspaper could be a part of a nice morning ritual.'},
  {'topic': 'Newspaper_990192bf',
   'snippet': "Actually, I don't get *them* anymore, but I miss having a casual read now and then."},
  {'topic': 'Newspaper_990192bf',
   'snippet': "I'll check if I can get a subscription service for *the* newspaper to be delivered."},
  {'topic': 'Box_7d6b3e2c',
   'snippet': 'Looks like *the* old box could be full of memories; do you want to open it together?'},
  {'topic': 'Book_17',
   'snippet': 'And *this* thick book seems to be quite the engaging read; how are you finding it?'},
  {'topic': 'Book_17',
   'snippet': "Seeing how *this* book complements the aesthetic, I'm glad you liked it."},
  {'topic': 'Book_17',
   'snippet': 'Even with *the* book here, the whole setting feels so harmonious and cozy.'},
  {'topic': 'furniture',
   'snippet': "Before arranging *the* furniture back, let's see if these pillows can add some charm."},
  {'topic': 'painting',
   'snippet': "*The* painting gives off such a timeless vibe; I'm glad you chose this spot."},
  {'topic': 'painting',
   'snippet': "Finding a suitable place for *the* old paintings really enhances the room's character."},
  {'topic': 'garbage bin',
   'snippet': "Let's move *the* garbage bin a bit further from the door to keep the living room tidy."},
  {'topic': 'Book_28',
   'snippet': "You know, *this* Sherlock Holmes book looks intriguing; maybe it's worth a little peek."}]}
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {
            "role": "user",
            "content": [
                { "type": "text", "text": PROMPT },
            ]
        }
    ]
)
response
ChatCompletion(id='chatcmpl-BRGlWs58wDnCeyaEyxswix4IoJmmS', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='```json\n[\n    {\n        "topic": "furniture",\n        "snippet": "Let\'s rearrange the layout again. I think moving **this table** closer to the window might brighten up the space."\n    },\n    {\n        "topic": "Television_f9d000b1",\n        "snippet": "Speaking of TV, can we at least turn **it** on for some background noise while we chat?"\n    },\n    {\n        "topic": "Dog_Statue_1",\n        "snippet": "I\'m curious to know how **this statue** ended up here in the first place. It\'s quite a conversation piece!"\n    },\n    {\n        "topic": "Tennis_Racquet_4",\n        "snippet": "Oh, and I think **that racket** might come in handy if anyone wants to play later."\n    },\n    {\n        "topic": "Laptop_4",\n        "snippet": "By the way, I was wondering if **this laptop** has enough storage for my project files."\n    },\n    {\n        "topic": "KeyChain_76c05fe6",\n        "snippet": "I think **this keychain** would be easier to spot if we hung it by the door."\n    },\n    {\n        "topic": "Newspaper_990192bf",\n        "snippet": "Could you pass **that newspaper** over? I want to clip out the Sunday puzzle section."\n    },\n    {\n        "topic": "Box_7d6b3e2c",\n        "snippet": "The contents of **this old box** must hold a lot of history! Do you ever dig through it?"\n    },\n    {\n        "topic": "Book_17",\n        "snippet": "I heard someone describe **this book** as a masterpiece. Have you gotten to any of the good parts yet?"\n    },\n    {\n        "topic": "painting",\n        "snippet": "If you get closer, you\'ll see **this painting** has incredible details that you can\'t miss."\n    },\n    {\n        "topic": "garbage bin",\n        "snippet": "We should move **that garbage bin** out to the kitchen, so it doesn\'t clutter the space here."\n    },\n    {\n        "topic": "Book_28",\n        "snippet": "Well, if you\'re done with **that Sherlock Holmes book**, I\'d love to borrow it for the flight."\n    }\n]\n```', refusal=None, role='assistant', annotations=[], audio=None, function_call=None, tool_calls=None))], created=1745839002, model='gpt-4o-2024-08-06', object='chat.completion', service_tier='default', system_fingerprint='fp_90122d973c', usage=CompletionUsage(completion_tokens=500, prompt_tokens=1221, total_tokens=1721, completion_tokens_details=CompletionTokensDetails(accepted_prediction_tokens=0, audio_tokens=0, reasoning_tokens=0, rejected_prediction_tokens=0), prompt_tokens_details=PromptTokensDetails(audio_tokens=0, cached_tokens=0)))
response_text = response.choices[0].message.content
clean_response(response_text)
[{'topic:': 'furniture',
  'snippet': "Every time I walk in here, it's like a whole new space! Can you remind me why you wanted to move *this furniture* all the way to the other side?"},
 {'topic:': 'Television_f9d000b1',
  'snippet': 'I understand, but did you catch what happened with *that TV* last week? I heard there was some issue with the signal.'},
 {'topic:': 'Dog_Statue_1',
  'snippet': "I hope *that dog statue* isn't too fragile, especially considering how easily things get knocked over in here."},
 {'topic:': 'Tennis_Racquet_4',
  'snippet': "Maybe after we're done, we can go out and hit a few balls with *this racket*. It's been a while since I've played tennis."},
 {'topic:': 'Laptop_4',
  'snippet': "Even though this looks like my old one, I'm sure *that laptop* belongs to you. Maybe we can check the files to confirm?"},
 {'topic:': 'KeyChain_76c05fe6',
  'snippet': 'You must have a lot of responsibilities with all those keys. I wonder if *that keychain* has any special meaning to you.'},
 {'topic:': 'Newspaper_990192bf',
  'snippet': "I'm thinking of subscribing again. Does *that newspaper* have any interesting articles today?"},
 {'topic:': 'Box_7d6b3e2c',
  'snippet': "Looks like *this box* has been here forever. Maybe we should peek inside and see if there's anything worth keeping."},
 {'topic:': 'Book_17',
  'snippet': "I actually started reading something similar. If you're done, I'd love to borrow *that book* sometime."},
 {'topic:': 'painting',
  'snippet': "I'm glad *this painting* is still up—it really ties the room together. Did you notice how it complements the lighting?"},
 {'topic:': 'garbage bin',
  'snippet': "You're right; we should move *that garbage bin* somewhere it won’t be in the way, maybe closer to the kitchen."},
 {'topic:': 'Book_28',
  'snippet': "Even if it's not yours, *this book* really adds character to the shelf. Maybe we should leave it out for guests to peruse."}]
clean_response(response_text)
{'person': '4',
 'room': '222',
 'items': [{'topic:': 'furniture',
   'snippet': "The new setup actually makes *this room* feel more spacious, don't you think?"},
  {'topic:': 'furniture',
   'snippet': "I can't quite remember, but wasn't *that couch* part of a set? It looks distinct on its own now."},
  {'topic:': 'furniture',
   'snippet': "Could we move everything next weekend? I'd like to try a different layout for *the sofas* this time."},
  {'topic:': 'furniture',
   'snippet': "Let's make sure we don't forget the coasters when we adjust *those tables* around."},
  {'topic:': 'Television_f9d000b1',
   'snippet': "Maybe when we're done chatting, we can catch a new show on *that TV* together."},
  {'topic:': 'Dog_Statue_1',
   'snippet': 'Actually, *this dog statue* looks pretty charming. It adds character to the room.'},
  {'topic:': 'Dog_Statue_1',
   'snippet': "I heard you fixed it, so how's *the statue* holding up now?"},
  {'topic:': 'Dog_Statue_1',
   'snippet': '*This statue* looks quite regal on the mantelpiece. The green makes it pop!'},
  {'topic:': 'Tennis_Racquet_4',
   'snippet': 'Did you know the owner of *the tennis racquet*? It seems well-used.'},
  {'topic:': 'Laptop_4',
   'snippet': "Could you help me with something on *this laptop*? I'm having trouble logging in."},
  {'topic:': 'Laptop_4',
   'snippet': 'We could use *the laptop* to play some music while we reorganize this room.'},
  {'topic:': 'Laptop_4',
   'snippet': 'Are you sure you want to give it away? *That laptop* seems like a reliable companion.'},
  {'topic:': 'KeyChain_76c05fe6',
   'snippet': "Ah, I see you're using *this keychain* now. A practical and stylish choice."},
  {'topic:': 'Newspaper_990192bf',
   'snippet': 'I think you left *the newspaper* on the table, just by the window.'},
  {'topic:': 'Newspaper_990192bf',
   'snippet': '*This one* is packed with good articles; quite informative.'},
  {'topic:': 'Newspaper_990192bf',
   'snippet': 'If you want, I can stop by the kiosk and get *a newspaper* for tomorrow.'},
  {'topic:': 'Box_7d6b3e2c',
   'snippet': "Looks like you've been using *this box* for storage. Anything interesting inside?"},
  {'topic:': 'Book_17',
   'snippet': 'Have you finished reading *this thick book* yet? It seems like a serious read.'},
  {'topic:': 'Book_17',
   'snippet': '*The cover of this book* is so visually appealing; it caught my eye immediately.'},
  {'topic:': 'Book_17',
   'snippet': 'The colors of *the book* and the rug seem to go well together, don’t they?'},
  {'topic:': 'furniture',
   'snippet': "With *this furniture* rearrangement, the sunlight hits differently, enhancing the room's ambiance."},
  {'topic:': 'painting',
   'snippet': 'Did I mention how *this painting* perfectly complements the color scheme here?'},
  {'topic:': 'painting',
   'snippet': "I can't believe we found another classic to match *the old painting*. It’s simply remarkable."},
  {'topic:': 'garbage bin',
   'snippet': 'How about moving *the garbage bin* to the pantry to keep the living room uncluttered?'},
  {'topic:': 'Book_28',
   'snippet': '*This Sherlock Holmes book* must belong to someone else, given your taste in literature.'}]}
clean_response(response_text)
[{'topic': 'furniture',
  'snippet': "Let's rearrange the layout again. I think moving **this table** closer to the window might brighten up the space."},
 {'topic': 'Television_f9d000b1',
  'snippet': 'Speaking of TV, can we at least turn **it** on for some background noise while we chat?'},
 {'topic': 'Dog_Statue_1',
  'snippet': "I'm curious to know how **this statue** ended up here in the first place. It's quite a conversation piece!"},
 {'topic': 'Tennis_Racquet_4',
  'snippet': 'Oh, and I think **that racket** might come in handy if anyone wants to play later.'},
 {'topic': 'Laptop_4',
  'snippet': 'By the way, I was wondering if **this laptop** has enough storage for my project files.'},
 {'topic': 'KeyChain_76c05fe6',
  'snippet': 'I think **this keychain** would be easier to spot if we hung it by the door.'},
 {'topic': 'Newspaper_990192bf',
  'snippet': 'Could you pass **that newspaper** over? I want to clip out the Sunday puzzle section.'},
 {'topic': 'Box_7d6b3e2c',
  'snippet': 'The contents of **this old box** must hold a lot of history! Do you ever dig through it?'},
 {'topic': 'Book_17',
  'snippet': 'I heard someone describe **this book** as a masterpiece. Have you gotten to any of the good parts yet?'},
 {'topic': 'painting',
  'snippet': "If you get closer, you'll see **this painting** has incredible details that you can't miss."},
 {'topic': 'garbage bin',
  'snippet': "We should move **that garbage bin** out to the kitchen, so it doesn't clutter the space here."},
 {'topic': 'Book_28',
  'snippet': "Well, if you're done with **that Sherlock Holmes book**, I'd love to borrow it for the flight."}]
PROMPT = f"""
You are given a JSON list of conversation items. Each item has a "topic" and a "snippet" (a short excerpt).

Your task is to write a **new snippet** for each topic:
- The new snippet must naturally continue the conversation and be **30–80 words** long.
- It must include exactly **one reference** to the **specific topic**, using a demonstrative determiner ("this", "that", "these", "those") or a clear pronoun.
- **Only** the determiner should be marked in bold using markdown (e.g., *this painting*).
- Do not refer to a general category (e.g., "a newspaper"); refer directly to the topic.
- References must sound natural, not forced. Vary between noun phrases and pronouns where possible.
- If another topic is clearly mentioned in relation to the main one, it’s acceptable to reuse spatial references.
- Create **one new snippet per item**.

Bad example:  
'If you want, I can stop by the kiosk and get *a newspaper* for tomorrow.' → Incorrect: refers to another item, uses an article instead of a demonstrative determiner.

---

```json
{test}
```
"""
esponse = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {
            "role": "user",
            "content": [
                { "type": "text", "text": PROMPT },
            ]
        }
    ]
)
esponse_text = esponse.choices[0].message.content
clean_response(esponse_text)
[{'topic:': 'furniture',
  'snippet': "Can you rearrange *these pieces* a bit more toward the window? It might make the room feel more open and enhance the overall look. Otherwise, it seems a bit crowded in here, and we can't fully appreciate the space."},
 {'topic:': 'furniture',
  'snippet': "I see what you mean. *This couch* would look much better by the window, creating a cozy nook. How about rearranging it next weekend together? It'll give us a fresh perspective."},
 {'topic:': 'furniture',
  'snippet': 'You know, by moving *those sofas* around, we might find the perfect setup for comfortable conversations. It’s worth experimenting until we get it just right.'},
 {'topic:': 'furniture',
  'snippet': "I'd appreciate your help to shift *them* around. With your eye for detail and my muscle, we can transform the whole setting into something more inviting."},
 {'topic:': 'Television_f9d000b1',
  'snippet': 'I totally agree. Sometimes just sitting and chatting is far more fulfilling than any show we could watch on *this TV*.'},
 {'topic:': 'Dog_Statue_1',
  'snippet': "Hopefully, we can fix *that statue* soon. It adds such a unique touch to the room, and it'd be a shame to leave it in disrepair."},
 {'topic:': 'Dog_Statue_1',
  'snippet': "Oh no! I hope *it's* not too damaged. It was such a charming piece in the corner with its vibrant color."},
 {'topic:': 'Dog_Statue_1',
  'snippet': "That's good to hear. I always thought the subtle green hue on *that statue* was a wonderful addition to the decor."},
 {'topic:': 'Tennis_Racquet_4',
  'snippet': 'It seems like *this racket* is in the way. Maybe we should find a dedicated spot for it, perhaps in the sports corner of the room.'},
 {'topic:': 'Laptop_4',
  'snippet': "If you're unsure, we can check the label to see if *this laptop* belongs to you. It's better to confirm before we take any further steps."},
 {'topic:': 'Laptop_4',
  'snippet': "Indeed, *that's* possible, but if it is, you might find some of your old files on it, which could clarify things."},
 {'topic:': 'Laptop_4',
  'snippet': "If you’re positive it's not yours, *it* may serve as a useful backup. It’s always good to have a secondary device around."},
 {'topic:': 'KeyChain_76c05fe6',
  'snippet': "Ah yes, *this keychain* might help keep everything organized and in one place. With so many keys, it's easy to misplace one."},
 {'topic:': 'Newspaper_990192bf',
  'snippet': "I wonder if you'd like to discuss any interesting articles from *this newspaper*. It's always fun to share insights."},
 {'topic:': 'Newspaper_990192bf',
  'snippet': 'If you enjoy *them*, maybe you can consider a subscription. It can be quite convenient to have them directly delivered to your door.'},
 {'topic:': 'Newspaper_990192bf',
  'snippet': 'Luckily, you can pay digitally. *This subscription* can be renewed quickly online, saving you any hassle.'},
 {'topic:': 'Box_7d6b3e2c',
  'snippet': 'We should decide where *this box* belongs. Is it storage, or do you have plans to sort through it soon?'},
 {'topic:': 'Book_17',
  'snippet': "I'm curious whether *this hardcover* is a page-turner for you. If so, I'd love some recommendations from it."},
 {'topic:': 'Book_17',
  'snippet': 'Appreciate it! You know, I think *it* adds a scholarly charm to the room, complementing the overall aesthetic beautifully.'},
 {'topic:': 'Book_17',
  'snippet': "It's amazing how well *this edition* fits into the room's design. It brings a sense of sophistication to the space."},
 {'topic:': 'furniture',
  'snippet': 'Before we finish up, let’s make sure *these cushions* are neatly arranged. They bring a lot of warmth to the room when positioned correctly.'},
 {'topic:': 'painting',
  'snippet': "Rearranging *these artworks* might give a fresh outlook. I'm curious to see how the room would feel with the new placements."},
 {'topic:': 'painting',
  'snippet': "I adore how prominently *it* stands out. It's nice to see a piece of art immediately establish a room's character."},
 {'topic:': 'garbage bin',
  'snippet': 'I agree, *this garbage bin* needs a new spot. Perhaps moving it closer to the kitchen would be more convenient.'},
 {'topic:': 'Book_28',
  'snippet': "If you're not into this genre, maybe *this mystery novel* isn't your style, but it could be a nice gift for someone else who enjoys detective stories."}]
def get_prompt(item):
    test = json.dumps(item, indent=4)
    return f"""
You are given a JSON list of conversation items. Each item has a "topic" and a "snippet" (a short excerpt).

Your task is to write a **new snippet** for each topic:
- The new snippet must naturally continue the conversation and be **30–80 words** long.
- It must include exactly **one reference** to the **specific topic**, using a demonstrative determiner ("this", "that", "these", "those") or a clear pronoun.
- **Only** the determiner should be marked in bold using markdown (e.g., *this painting*).
- Do not refer to a general category (e.g., "a newspaper"); refer directly to the topic.
- References must sound natural, not forced. Vary between noun phrases and pronouns where possible.
- If another topic is clearly mentioned in relation to the main one, it’s acceptable to reuse spatial references.
- Create **one new snippet per item**.

Bad example:  
'If you want, I can stop by the kiosk and get *a newspaper* for tomorrow.' → Incorrect: refers to another item, uses an article instead of a demonstrative determiner.

---

```json
{test}
```
"""
all_results = {}
def process_item(itemid, max_retries=3):
    current = alldata[itemid]
    filename = filepointer[itemid]
    prompt = get_prompt(current)

    allreskey = f"{filename}__{itemid}"

    if not allreskey in all_results:
        all_results[allreskey] = {
            "person": current["person"],
            "room": current["room"],
            "filename": filename,
            "items": []
        }
    current_result = all_results[allreskey]

    for attempt in range(max_retries):
        try:
            response = client.chat.completions.create(
                model="gpt-4o",
                messages=[
                    {
                        "role": "user",
                        "content": [
                            { "type": "text", "text": prompt },
                        ]
                    }
                ]
            )

            response_text = response.choices[0].message.content
            response_data = clean_response(response_text)
            if type(response_data) == dict:
                current_result["items"].append(response_data["items"])
            elif type(response_data) == list:
                current_result["items"].append(response_data)

        except Exception as e:
            print(f"⚠️ Error on attempt {attempt + 1}: {e}")
            if attempt == max_retries - 1:
                print("Max retries reached. Skipping this item.")
                return
for itemid in range(len(alldata)):
    process_item(itemid)
with open("/tmp/allresults.json", "w") as f:
    json.dump(all_results, f, indent=4)
def make_single(listof_lists):
    result = []
    if len(listof_lists) == 0:
        return []
    if type(listof_lists[0]) is not list:
        return listof_lists
    for sublist in listof_lists:
        for item in sublist:
            result.append(item)
    return result
redo = []
numered_all = []
for key in all_results.keys():
    if all_results[key]["items"] == []:
        redo.append(key)
    sublist = make_single(all_results[key]["items"])
    if sublist == []:
        continue
    for count, item in enumerate(sublist):
        if not "snippet" in item:
            if "new_snippet" in item:
                item["snippet"] = item["new_snippet"]
        if not "topic" in item:
            if "topic:" in item:
                item["topic"] = item["topic:"]
        current = {
            "person": all_results[key]["person"],
            "room": all_results[key]["room"],
            "filename": all_results[key]["filename"],
            "id": f"{key}__{count}",
            "topic": item["topic"],
            "snippet": item["snippet"]
        }
        numered_all.append(current)
with open("/tmp/numered_all.json", "w") as f:
    json.dump(numered_all, f, indent=4)
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {
            "role": "user",
            "content": [
                { "type": "text", "text": prompt },
            ]
        }
    ]
)