#json file not being able to parse through when having a emoji in it

130 messages · Page 1 of 1 (latest)

livid mirage
#

Hello.
My bot is running with a DB, which is using a .json file for her to learn. A few examples are the following: ```py
"\nUser: how old are you Rachel?": "\n\nRachel: As a AI, I don't have an exact age. But I was created on xxx, if that helps. :)\n",
"\nUser: hello rachel": "\n\nRachel: Hey there!\n",


And after days and days of troubleshooting, I found out emojis written like ":)" or ";)" and so on, completely prevents my bot to be able to parse through that json file (and obv. break it).

Is there any option to solve this issue? Any help is highly appreciated as I'm running out of ideas! ^^
timber fern
#

what are you using to parse the json?

#

is this the direct output of the API?

livid mirage
#

please also mention me next time. I don't have that nick for fun 😅
(have all my servers muted lol)

livid mirage
#

the rachel part is the direct output

#

or another call would be
"\nUser: Thank you very much Rachel!": "\n\nRachel: Nothing to thank for. :)\n"

This also breaks it

#

so yeah something which either converts those emojis to a discord emoji (like : smile :) would help.. or just something to prevent that from appearing in my DB

timber fern
#

@livid mirage here is the output of a request I just made directly to the API

{
    "id": "cmpl-6ImInNq2AmsoQrRkLm7uwMmXiVU6T",
    "object": "text_completion",
    "created": 1669933597,
    "model": "text-davinci-003",
    "choices": [
        {
            "text": "\n\nPerson 1: \"Life is like a box of chocolates, you never know what you're gonna get.\"\n\nPerson 2: \"That's a great quote! Who said it?\"\n\nPerson 1: \"Forrest Gump.\"",
            "index": 0,
            "logprobs": null,
            "finish_reason": "stop"
        }
    ],
    "usage": {
        "prompt_tokens": 11,
        "completion_tokens": 52,
        "total_tokens": 63
    }
}

it properly escapes the " the sample you posted it not the unaltered return of the API

#

do you have anything which is encoding or parsing the API return?

#

watever it is, it is not properly escaping the "

livid mirage
#

you don't get my problem @timber fern

#

My bot is running with a DB, which is using a .json file

which is ofc encoded in utf-8

#

but that shouldn't be a problem iirc.

#

my problem is that ":)" emojis, which get stored my DB, destroy my bots ability to iterate through it

timber fern
#

@livid mirage so, i got it right on the first time =P
what are you using to parse that json file?

livid mirage
#

and no, I have nothing which converts the answer. this is the code for my function ```py
async def ask(question, chat_log=None):
prompt_text = f'{chat_log}{restart_sequence}: {question}{start_sequence}'
response = openai.Completion.create(
model="text-davinci-003",
prompt=prompt_text,
temperature=1,
max_tokens=300,
top_p=1,
frequency_penalty=0.7,
presence_penalty=0.1,
stop=["\n"],
)
story = response['choices'][0]['text']
return str(story)

timber fern
#

the response of the API is completely fine, at some point in your code when you store or read it, it is not beeing properly encoded / parsed

livid mirage
timber fern
#

"parsing" is the process of interpreting the data
in this context, the parser is the code that takes the json formatted string and reads it

livid mirage
timber fern
#

if it is breaking, it is either because the json file is not correctly formated OR the parser is weirdly handling characters like :

livid mirage
timber fern
#

would you mind sending that json file?

livid mirage
#

well it's in german..

#

so idk if you can make much sense from it's content

timber fern
#

no problem, im going to check the formating, not the actual content

livid mirage
#

alright

#

1 sec

timber fern
#

oh, found your problem =P

livid mirage
#

do tell! @timber fern

timber fern
#

im typing it

livid mirage
#

oh lol. I didn't see any typing going on. my bad then

timber fern
#

the format is indeed wrong
{ and } mean it should be parsed as an object where the first parameter (the first string inside ") is the name of the property and the following string (the one after :) is the value of it
this is kind of valid json, but it is probabily not what you really want
here is an example of how it should look like

[
  {
    "question": "\nUser: Wer bist du?",
    "answer": "\n\nRachel: Ich bin Rachel, ein Chatbot, erstellt von Der Traudel.\n"
  },
  {
    "question": "\nUser: Bist du verliebt Rachel?",
    "answer": "\n\nRachel: Ich bin verliebt in @livid mirage.\n"
  }
]

note te difference where first, it starts with [ with means it is an array, which contains objects that all have the same property names

livid mirage
#

hm

#

ok I get what you mean

#

but that also means I have to change how I store my stuff.. ugh massive headache..

#

sorry I'm kinda on edge with json. idk but I can never really come clear with json lol

timber fern
livid mirage
#

omg

#

i love you

timber fern
livid mirage
# timber fern <:dalle_peace:1034389129720508466>

ok one more question.. I used to store stuff before you told me to replace stuff like this: ```py
async def save_knowledge(question, answer):
with open("assets/session_prompt.json", encoding='utf-8') as brain:
braincells = json.load(brain)
braincells[f'{restart_sequence} {question}'] = f'{start_sequence} {answer}\n'
with open("assets/session_prompt.json", "w", encoding='utf-8') as brain:
json.dump(braincells, brain, indent=2, ensure_ascii=False)

where:
 question = message content of the discord message
start_sequence = "\n\nRachel:"
restart_sequence = "\nUser:"
answer= the generated message
timber fern
#

yep, thats where it went wrong braincells[f'{restart_sequence} {question}'] = f'{start_sequence} {answer}\n'

livid mirage
#

ahhh

#

sorry for asking for so much assistance but how would I correct that?

timber fern
#

you appended a string, instead, you have to create the proper structure to a variable, then push this variable to braincells

#

and then, encode braincells and write it to the file

livid mirage
#

hmmmmmmmmm

livid mirage
#

how exactly would I create that structure? I get what you mean tho

timber fern
#

something like this

sync def save_knowledge(question, answer):
    with open("assets/session_prompt.json", encoding='utf-8') as brain:
        braincells = json.load(brain)

    data = {input: question, output: answer}
    braincells.append(data)

    with open("assets/session_prompt.json", "w", encoding='utf-8') as brain:
        json.dump(braincells, brain, indent=4)
livid mirage
#

ahh okay I gotcha

timber fern
#

maybe input was a poor choice of parameter name

livid mirage
#

yhea true..

timber fern
#

=P

livid mirage
#

it says output is a unresolved reference.. are you sure about that?

timber fern
#

no, I not familiar with python at all xD

#

im jsut guessing the syntax, you probaibly know it better than me

livid mirage
#

hm

timber fern
#

but I think you understood the problem already

livid mirage
#

yes. just trying to get it straight

timber fern
#

nice

livid mirage
# timber fern nice

Think I got it right now.. ```py
async def save_knowledge(question, answer):
with open("assets/session_prompt.json", encoding='utf-8') as brain:
braincells = json.load(brain)

data = {
    "input": f"{restart_sequence} {question}", 
    "output": f"{start_sequence} {answer}"
}
braincells.append(data)

with open("assets/session_prompt.json", "w", encoding='utf-8') as brain:
    json.dump(braincells, brain, indent=4)
timber fern
#

good, that seems right

livid mirage
#

okay

#

but now I have to change my ask function as well right?

timber fern
#

yep

livid mirage
#

bc I think prompt_text = f'{chat_log}{restart_sequence}: {question}{start_sequence}' now isn't right anymore

timber fern
#

yea, you'll have to adjust the rest of the code

livid mirage
#

well it's mainly just this

#

the rest is goot to go already

#

let me try something

#

@timber fern sorry to bother you again but do you think that would work as prompt? ```py
prompt_text = [
{chat_log},
{
"input": f"{restart_sequence} {question}",
"output": f"{start_sequence} "
}
]

timber fern
#

im not sure of what you mean

livid mirage
#

and I wanted to know if you, as clearly being smarter than me, would think this would work as the prompt

#

to load in the chat_log (the json file) and add the last bit from the new message to it, to serve a starting point for the AI to generate it text on

#

sorry for my bad english

timber fern
#

the text of the prompt for the API needs to be a plain string

livid mirage
#

oh

#

well

#

wait 1 sec

#

would that work? prompt_text = f"{chat_log}{restart_sequence} {question}, {start_sequence}"

timber fern
#

looks right

livid mirage
#

ok.. I pray to god it works

#

and I normally don't pray xD

#

no.. doesn't work..

#

it does load the json. that's for sure. something is still off with the ask function

#

yeah it must be the prompt

#
async def ask(question, chat_log=None):
    print("about to prompt")
    prompt_text = f"{chat_log}{restart_sequence} {question}, {start_sequence}"
    print("about to create")
    response = openai.Completion.create(
        model="text-davinci-003",
        prompt=prompt_text,
        temperature=1,
        max_tokens=300,
        top_p=1,
        frequency_penalty=0.7,
        presence_penalty=0.1,
        stop=["\n"],
    )
    print("done creating")
    story = response['choices'][0]['text']
    return str(story)

everything gets printed, except "done creating"

timber fern
#

i have no idea in this case, i don't know python =/

livid mirage
#

darn..

#

you know anybody on this server who could help..?

timber fern
#

just wait, someone might see this psot

livid mirage
#

I Hope so

#

I'm really thrilled to finish this as this is my last piece of work on that project in order to finish it

livid mirage
#

ah man idk how to fix this..

copper shore
#

Silly question, but... have you tried asking ChatGPT for help? 😂

#

@livid mirage

livid mirage
copper shore
livid mirage
#

I don't even know how to ask my problem

tawny hearth
copper shore
#

Apologies! I was being genuine ☺️

copper shore
#

Forgive me if I don't fully understand the scope of your question. I often find chatting with GPT to be a helpful way to refine my questions as well as find great solutions--sometimes unconventional! After chatting a bit with GPT-3 myself, I'm wondering if you need to include "await" in the function like 'response = await openaiCompletion.create'

livid mirage
#

Uh I think that could be a considerable fact

#

I will try that when I have time today

livid mirage
#

@copper shore that didn't work..

#
async def ask(question, chat_log):
    print("about to prompt")
    prompt_text = f'{chat_log}{restart_sequence}: {question}{start_sequence}'
    print("about to create")
    response = openai.Completion.create(
        print("poggers"),
        model="text-davinci-002",
        prompt=prompt_text,
        temperature=1,
        max_tokens=300,
        top_p=1,
        frequency_penalty=0.7,
        presence_penalty=0.1,
        stop=["\n"],
    )
    print("done creating")
    story = response['choices'][0]['text']
    return str(story)
``` that's the function right now

```py
thought = await message.channel.send(await ask(question=msg_content, chat_log=chat_log),
                                                     reference=message)
```and that's the call for the function
copper shore
livid mirage
#

So something is off with the prompt text

#

But idk how to deliver my whole DB content in one string to the ai call

copper shore
#

Does it output an error?

copper shore
livid mirage
livid mirage
#

Just around 80 lines I think

copper shore
#

🧐

livid mirage
livid mirage
#

so I guess you can't help?

narrow trench
#

cool

livid mirage
#

No

#

No it isn't cool