voidpenguin-28 / Textractor-ExtraExtensions

Several useful Textractor extensions, which are not available by default in Textractor.
9 stars 1 forks source link

ChatGPT response won't show in Textractor #1

Closed niizam closed 5 months ago

niizam commented 5 months ago

I'm using this ChatGPT API Free Reverse Proxy as the API endpoint

here's gpt-response-log.txt

[2024-04-15 15:51:38] [INFO] {"model":"gpt-3.5-turbo","messages":[{"role":"system","content":"Translate novel script to natural fluent EN. Preserve numbering. Use all JP input lines as context (previous lines). However, only return the translation for the line that starts with '99:'."},{"role":"user","content":"\n97:  村を去った日と同じだ。俺を乗せて来たのがタイムマシンであったかのように、岬の眺めはあの日と変わらない。\n\n98:  滲んだ汗をぬぐい、荷物を背負い直した俺は、もう間近に迫った目的地へと足の向きを変える。\n\n99:  ガードレールに沿ってほんの30秒ほど。"}]}
{"id":"chatcmpl-49HSjSstJVSJF2YDDABz5i1e2Xft","created":1713171097423,"model":"gpt-3.5-turbo","object":"chat.completion","choices":[{"finish_reason":"stop","index":0,"message":{"content":"Along the guardrail, just for about 30 seconds.","role":"assistant"}}],"usage":{"prompt_tokens":226,"completion_tokens":12,"total_tokens":238}}

[2024-04-15 15:51:42] [INFO] {"model":"gpt-3.5-turbo","messages":[{"role":"system","content":"Translate novel script to natural fluent EN. Preserve numbering. Use all JP input lines as context (previous lines). However, only return the translation for the line that starts with '99:'."},{"role":"user","content":"\n96:  村を去った日と同じだ。俺を乗せて来たのがタイムマシンであったかのように、岬の眺めはあの日と変わらない。\n\n97:  滲んだ汗をぬぐい、荷物を背負い直した俺は、もう間近に迫った目的地へと足の向きを変える。\n\n98:  ガードレールに沿ってほんの30秒ほど。\n\n99:  国道を外れたとたん、青と緑の景色の中に鮮やかな黄色が咲き乱れた。"}]}
{"id":"chatcmpl-LcdrI8IYUPCvK2ZyhSpFjQwvSH4y","created":1713171101126,"model":"gpt-3.5-turbo","object":"chat.completion","choices":[{"finish_reason":"stop","index":0,"message":{"content":"99: As soon as I veered off the national highway, amidst the blue and green scenery, vibrant yellow blossoms erupted.","role":"assistant"}}],"usage":{"prompt_tokens":284,"completion_tokens":26,"total_tokens":310}}

Textractor ver 5.20 x86 Screenshot: image

Edit: changing the order of extensions also didn't fix the issue image

voidpenguin-28 commented 5 months ago

I think I've figured out the issue here. The proxy is returning the response in a slightly different key order and potentially without any line formatting, which was throwing off the extension's response parsing logic.

I've implemented some more flexible response parsing in this beta update, can you test this out when you get the chance? GptApiTranslate-v1.1.4-beta.zip

niizam commented 5 months ago

I think I've figured out the issue here. The proxy is returning the response in a slightly different key order and potentially without any line formatting, which was throwing off the extension's response parsing logic.

I've implemented some more flexible response parsing in this beta update, can you test this out when you get the chance? GptApiTranslate-v1.1.4-beta.zip

Thank you!, it definitely works image

niizam commented 4 months ago

@voidpenguin-28 I'm wondering if there's a plan to support Gemini AI, since they offer a free API. Here's an example of its usage.

curl https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=$GOOGLE_API_KEY \
    -H 'Content-Type: application/json' \
    -X POST \
    -d '{
        "contents": [
          {"role":"user",
          "parts":[{
            "text": "Translate novel script to natural fluent EN. Preserve numbering. Use all JP input lines as context (previous lines). However, only return the translation for the line that starts with '99:'."}]},
          {"role": "user",
          "parts":[{
            "text": "\n97:  村を去った日と同じだ。俺を乗せて来たのがタイムマシンであったかのように、岬の眺めはあの日と変わらない。\n\n98:  滲んだ汗をぬぐい、荷物を背負い直した俺は、もう間近に迫った目的地へと足の向きを変える。\n\n99:  ガードレールに沿ってほんの30秒ほど。"}]},
        ],
        "safetySettings": [
          {
            "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
            "threshold": "BLOCK_NONE"
          },
          {
            "category": "HARM_CATEGORY_HATE_SPEECH",
            "threshold": "BLOCK_NONE"
          },
          {
            "category": "HARM_CATEGORY_HARASSMENT",
            "threshold": "BLOCK_NONE"
          },
          {
            "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
            "threshold": "BLOCK_NONE"
          }
        ],
        "generationConfig": {
            "temperature": 1.0,
            "maxOutputTokens": 512,
            "topP": 0.95,
            "topK": 64
        }
    }'

Result

{
    "candidates": [{
        "content": {
            "parts": [{
                "text": "99: Along the guardrail for just about 30 seconds."
            }],
            "role": "model"
        },
        "finishReason": "STOP",
        "index": 0,
        "safetyRatings": [{
                "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
                "probability": "NEGLIGIBLE"
            },
            {
                "category": "HARM_CATEGORY_HATE_SPEECH",
                "probability": "NEGLIGIBLE"
            },
            {
                "category": "HARM_CATEGORY_HARASSMENT",
                "probability": "NEGLIGIBLE"
            },
            {
                "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
                "probability": "NEGLIGIBLE"
            }
        ]
    }],
    "usageMetadata": {
        "promptTokenCount": 125,
        "candidatesTokenCount": 15,
        "totalTokenCount": 140
    }
}
voidpenguin-28 commented 4 months ago

I just tested the API a bit, and its results aren't half bad. I'll add it to my list of tasks to work on, but it may be a bit of time before I take a look at it. I'll give an update in this thread if this gets implemented.

niizam commented 4 months ago

That's fine, take your time, and thanks for considering it. Looking forward to any updates on this.

voidpenguin-28 commented 3 months ago

Hi. I just updated the extension to v1.2.0, which will allow you to be able to integrate the Gemini API. https://github.com/voidpenguin-28/Textractor-ExtraExtensions/releases/tag/GptApiTranslate-v1.2.0

Rather than implementing the Gemini API directly in the extension itself, I've implemented a more flexible approach that should allow you to implement any API/endpoint you want.

v1.2 introduces 4 new config values which allows users to provide custom functionality for sending API requests and parsing API responses: CustomRequestTemplate, CustomResponseMsgRegex, CustomErrorMsgRegex, CustomHttpHeaders

More details on each config value can be found in the extension's documentation.

As for how to integrate the Gemini API with this, just simply add the following to your .ini config (fill in the Model & ApiKey), and you should be all set.

Url=https://generativelanguage.googleapis.com/v1beta/models/{1}:generateContent?key={0}
ApiKey=***GEMINI API KEY HERE***
Model=***DESIRED GEMINI MODEL HERE (ex: gemini-1.5-flash)***
CustomRequestTemplate={"systemInstruction":{"role":"user","parts":[{"text":"{1}"}]},"contents":[{"role":"user","parts":[{"text":"{2}"}]}],"safetySettings":[{"category":"HARM_CATEGORY_SEXUALLY_EXPLICIT","threshold":"BLOCK_NONE"},{"category":"HARM_CATEGORY_HATE_SPEECH","threshold":"BLOCK_NONE"},{"category":"HARM_CATEGORY_HARASSMENT","threshold":"BLOCK_NONE"},{"category":"HARM_CATEGORY_DANGEROUS_CONTENT","threshold":"BLOCK_NONE"}]}
CustomResponseMsgRegex="[Tt]ext":\\s{0,}"((?:\\\\"|[^"])*)"
CustomErrorMsgRegex=
CustomHttpHeaders=Content-Type: application/json
niizam commented 3 months ago

Hi. I just updated the extension to v1.2.0, which will allow you to be able to integrate the Gemini API. https://github.com/voidpenguin-28/Textractor-ExtraExtensions/releases/tag/GptApiTranslate-v1.2.0

Rather than implementing the Gemini API directly in the extension itself, I've implemented a more flexible approach that should allow you to implement any API/endpoint you want.

v1.2 introduces 4 new config values which allows users to provide custom functionality for sending API requests and parsing API responses: CustomRequestTemplate, CustomResponseMsgRegex, CustomErrorMsgRegex, CustomHttpHeaders

More details on each config value can be found in the extension's documentation.

As for how to integrate the Gemini API with this, just simply add the following to your .ini config (fill in the Model & ApiKey), and you should be all set.

Url=https://generativelanguage.googleapis.com/v1beta/models/{1}:generateContent?key={0}
ApiKey=***GEMINI API KEY HERE***
Model=***DESIRED GEMINI MODEL HERE (ex: gemini-1.5-flash)***
CustomRequestTemplate={"systemInstruction":{"role":"user","parts":[{"text":"{1}"}]},"contents":[{"role":"user","parts":[{"text":"{2}"}]}],"safetySettings":[{"category":"HARM_CATEGORY_SEXUALLY_EXPLICIT","threshold":"BLOCK_NONE"},{"category":"HARM_CATEGORY_HATE_SPEECH","threshold":"BLOCK_NONE"},{"category":"HARM_CATEGORY_HARASSMENT","threshold":"BLOCK_NONE"},{"category":"HARM_CATEGORY_DANGEROUS_CONTENT","threshold":"BLOCK_NONE"}]}
CustomResponseMsgRegex="[Tt]ext":\\s{0,}"((?:\\\\"|[^"])*)"
CustomErrorMsgRegex=
CustomHttpHeaders=Content-Type: application/json

You are the GOAT! Thank you for the update. I really appreciate you adding the ability to integrate custom APIs and makes it easy to support different APIs and is great for future-proofing the extension.

I'll test out the new version and try using the new config options. If I run into any issues or have any other feedback, I'll let you know in this thread.

voidpenguin-28 commented 3 months ago

Quick update here. A few issues were recently found with the custom api functionality, which now should be fixed.

The following 2 steps should fix this issue.

  1. Download the latest version of the GptApiTranslate extension (v1.2.1)
  2. When trying to use the Gemini API, use the following config values instead.
    Url=https://generativelanguage.googleapis.com/v1beta/models/{1}:generateContent?key={0}
    ApiKey=***GEMINI API KEY HERE***
    Model=***DESIRED GEMINI MODEL HERE (ex: gemini-1.5-flash)***
    CustomRequestTemplate={\"systemInstruction\":{\"role\":\"user\", \"parts\":[{\"text\":\"{1}\"}]}, \"contents\":[{\"role\":\"user\", \"parts\":[{\"text\":\"{2}\"}]}], \"safetySettings\":[{\"category\":\"HARM_CATEGORY_SEXUALLY_EXPLICIT\", \"threshold\":\"BLOCK_NONE\"}, {\"category\":\"HARM_CATEGORY_HATE_SPEECH\", \"threshold\":\"BLOCK_NONE\"}, {\"category\":\"HARM_CATEGORY_HARASSMENT\", \"threshold\":\"BLOCK_NONE\"}, {\"category\":\"HARM_CATEGORY_DANGEROUS_CONTENT\", \"threshold\":\"BLOCK_NONE\"}]}
    CustomResponseMsgRegex=\"[Tt]ext\":\\s{0,}\"((?:\\\\\"|[^\"])*)\"
    CustomErrorMsgRegex=
    CustomHttpHeaders=Content-Type: application/json


To explain why this issue was happening. Textractor has its own ini formatting logic that it applies every time the app is opened or closed. I was not aware that part of this logic was to auto-strip out all double quotes that aren't escaped.

So Textractor was auto-removing the double quotes from the CustomRequestTemplate & CustomResponseMsgRegex config values, which broke the extension.

This issue should be resolved now.