Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Output Processing #35

Open
hexthedev opened this issue Apr 28, 2021 · 6 comments
Open

Output Processing #35

hexthedev opened this issue Apr 28, 2021 · 6 comments
Labels
enhancement New feature or request

Comments

@hexthedev
Copy link
Owner

OpenAI spits out some funny outputs with lots of // and stuff. It would be nice to add a processing layer to the output with an interface so that output processing can be customized.

Adding this functionality, or at least the interfaces, would facilitate the output of the REST adapter layer to the consuming layer.

@hexthedev hexthedev added the enhancement New feature or request label Apr 28, 2021
@hexthedev
Copy link
Owner Author

@syedbdusa

@abethke
Copy link

abethke commented Sep 2, 2021

In case its useful to anyone this is what I did to compensate for the junk in the responses.

        string raw = comp.Result.choices[0].text;
        raw = raw.Replace("\\\\", "\\");
        raw = raw.Replace("\\", "\\u");
        raw = System.Text.RegularExpressions.Regex.Unescape(raw);

@hexthedev
Copy link
Owner Author

Awesome, thanks for this.

I was actually thinking of starting up a new repo called the "Unity OpenAI Toolkit", or something along those lines, as a Unity package that that will depend on the API wrapper, but provide some of these higher level tools.

If you have any other ideas, please let me know.

@abethke
Copy link

abethke commented Sep 8, 2021

I revised my code after extensive testing showed some Unicode parsing errors triggering an Argument exception when trying to regrex decode the unicode. Here is the revised parsing to 'clean' the GPT response string.

        result = result.Replace("\\\\00a0", "");
        result = result.Replace("\\\\2019", "'");
        result = result.Replace("\\\"", "\"");
        result = result.Replace("\\\\", "\"");

@abethke
Copy link

abethke commented Oct 5, 2021

Ok here's what I've figured out... It's UTF16/32 that's been escaped some how.

All of the \\2019 (and similar) are actually supposed to be \u2019 which Unity then reads and displays correctly; in this case as an apostrophe.

Having said that I've been struggling on and off for weeks trying to get this to reparse on my side properly and am close to giving up and just adding a ton of Replace commands for specific characters that are showing up commonly.

@abethke
Copy link

abethke commented Oct 5, 2021

Here's the full and ultimate fix...

    string ConvertHexIntToAsciiString(string in_string)
    {
        try
        {
            int testValue = int.Parse(in_string);
            int intAscii = Convert.ToInt32(testValue.ToString(), 16);
            char resultChar = (char)intAscii;
            return resultChar.ToString();
        }
        catch (Exception e)
        {
            return in_string;
        }
    }
    string CleanUnicodeJunk(string in_string)
    {
        // clean the leading junk which I've never identified? Capitalization unicode?
        in_string = in_string.Replace("\\\\00a0", "");

        string junkTag = "\\\\";
        while (in_string.Contains(junkTag))
        {
            int index = in_string.IndexOf(junkTag);
            string hexString = in_string.Substring(index + 2, 4);
            string unicodeAscii = ConvertHexIntToAsciiString(hexString);
            in_string = in_string.Replace(junkTag + hexString, unicodeAscii);
        }
        return in_string;
    }

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants