Bypassing token limit in chatgpt

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
labrint
Posts: 379
Joined: 14 Jun 2017, 05:06
Location: Malta

Bypassing token limit in chatgpt

14 Feb 2023, 07:06

Hi I need to know how I can bypass the token limit in chatgpt api by batching my request.

There is some documentation how this is done in python:
https://marco-gonzalez.medium.com/breaking-the-token-limit-how-to-work-with-large-amounts-of-text-in-chatgpt-da18c798d882

Has anyone batched anything in AHK? Perhaps someone can share a script (no need to share API key).
Logitope
Posts: 19
Joined: 13 Feb 2021, 14:44

Re: Bypassing token limit in chatgpt

15 Feb 2023, 20:15

No, sorry, I don't know.

However, I was also planning to use the ChatGPT API. I haven't gotten around yet to looking into that, so if you would be willing to give a basic introduction or paste some basic code showing how a request is made and the answer received using AHK, it would be great.
Logitope
Posts: 19
Joined: 13 Feb 2021, 14:44

Re: Bypassing token limit in chatgpt

16 Feb 2023, 00:54

malcev wrote:
16 Feb 2023, 00:22
You can do it by Yourself.
Thanks. I'm really an absolute beginner at this, never made an API call of any kind before, so some things mentioned there are not totally obvious to me and I was hoping for a shortcut. But I should be able to figure it out...
User avatar
labrint
Posts: 379
Joined: 14 Jun 2017, 05:06
Location: Malta

Re: Bypassing token limit in chatgpt

16 Feb 2023, 04:03

Logitope wrote:
16 Feb 2023, 00:54
malcev wrote:
16 Feb 2023, 00:22
You can do it by Yourself.
Thanks. I'm really an absolute beginner at this, never made an API call of any kind before, so some things mentioned there are not totally obvious to me and I was hoping for a shortcut. But I should be able to figure it out...

I am copying my code here ("ChatGPT Doctor") without the API Key (replace INSERT_API_KEY_HERE) hope it helps (edit out the things you don't need and don't forget to include the JSON library at the bottom of my code):

Code: Select all

#singleinstance force

Gui, Add, Text,, Age
Gui, Add, Edit, vAge w300 h20, 
Gui, Add, Text,, Symptoms
Gui, Add, Edit, vSymptoms w300 h60, 
Gui, Add, Text,, Number of Advices
Gui, Add, Edit, vAdvices w300 h20, 3 
Gui, Add, Text,, Classes of Medicines
Gui, Add, Edit, vCoM w300 h20, 2
Gui, Add, Text,, Number of Alternatives each
Gui, Add, Edit, vNoA w300 h20, 2


Gui, Add, Button, gButtonClick w200 h30, Ask

Gui, Add, Text,, Answer
Gui, Add, Edit, vAnswerEdit r20 w300, 
Gui, Show
Return

ButtonClick:
Gui, Submit, NoHide
Prompt := "A patient who is " Age " years old is suffering from " Symptoms ". Give me " CoM " suitable classes of medicines with " NoA " alternatives each that are available Malta and the European Union as well as dosage instructions for each. Also provide me with " Advices " advices to give to the patient."

;MsgBox, % Prompt

AttemptNo := 1

GoSub, AskGPT
Return


AskGPT:
Gui, PleaseWait: +LastFound +AlwaysOnTop +Disabled -Caption +ToolWindow
Gui, PleaseWait: Color, FFFFFF
Gui, PleaseWait: Add, Text, w200 Center, Please wait...
Gui, PleaseWait: Add, Text,, Contacting Chat GPT`n`nServer may be overloaded/slow.`n`nAttempt No. %AttemptNo%
Gui, PleaseWait: Show, Center, PleaseWait

Prompt := RegExReplace(Prompt, "\""", """""")
Prompt := RegExReplace(Prompt, "\n", "``n")

;MsgBox, % Prompt


; Your OpenAI API key
api_key := "INSERT_API_KEY_HERE"

; The endpoint URL for the GPT-3 API
api_url := "https://api.openai.com/v1/engines/text-davinci-003/completions"

; The prompt to use as the context for the text generation
;prompt := "What is the capital of France?"


Try{
; Set up the cURL session
curl := ComObjCreate("WinHttp.WinHttpRequest.5.1")
curl.Open("POST", api_url)
curl.SetRequestHeader("Content-Type", "application/json")
curl.SetRequestHeader("Authorization", "Bearer " api_key)



; Build the JSON payload
jsonY=
(
{
    "prompt": "%Prompt%",
    "max_tokens": 4000,
    "temperature": 0.1
}
)

; Send the request and get the response
curl.Send(jsonY)
result := curl.ResponseText

test := result
}

If test contains error
{
AttemptNo++
GuiControl, -Redraw, PleaseWait
Gui, PleaseWait: Destroy
GoTo, AskGPT
}

If test =
{
AttemptNo++
GuiControl, -Redraw, PleaseWait
Gui, PleaseWait: Destroy
GoTo, AskGPT
}

Array := []

Array := JSON.Load(test)

Number := 1
While (Number < 100){
Answer := Array["choices"][Number]["text"]

If Answer !=
Break

Number++
}


Answer := RegExReplace(Answer, "^[\s\r\n]+|[\s\r\n]+$", "")  ; remove leading and trailing whitespaces
;Answer := RegExReplace(Answer, "(?:\r\n|\r|\n)", " ")       ; replaces line breaks with space

GuiControl,, AnswerEdit, %Answer%


GuiControl, -Redraw, PleaseWait
Gui, PleaseWait: Destroy
Return


GuiClose:
ExitApp



/**
 * Lib: JSON.ahk
 *     JSON lib for AutoHotkey.
 * Version:
 *     v2.1.3 [updated 04/18/2016 (MM/DD/YYYY)]
 * License:
 *     WTFPL [http://wtfpl.net/]
 * Requirements:
 *     Latest version of AutoHotkey (v1.1+ or v2.0-a+)
 * Installation:
 *     Use #Include JSON.ahk or copy into a function library folder and then
 *     use #Include <JSON>
 * Links:
 *     GitHub:     - https://github.com/cocobelgica/AutoHotkey-JSON
 *     Forum Topic - http://goo.gl/r0zI8t
 *     Email:      - cocobelgica <at> gmail <dot> com
 */


/**
 * Class: JSON
 *     The JSON object contains methods for parsing JSON and converting values
 *     to JSON. Callable - NO; Instantiable - YES; Subclassable - YES;
 *     Nestable(via #Include) - NO.
 * Methods:
 *     Load() - see relevant documentation before method definition header
 *     Dump() - see relevant documentation before method definition header
 */
class JSON
{
	/**
	 * Method: Load
	 *     Parses a JSON string into an AHK value
	 * Syntax:
	 *     value := JSON.Load( text [, reviver ] )
	 * Parameter(s):
	 *     value      [retval] - parsed value
	 *     text    [in, ByRef] - JSON formatted string
	 *     reviver   [in, opt] - function object, similar to JavaScript's
	 *                           JSON.parse() 'reviver' parameter
	 */
	class Load extends JSON.Functor
	{
		Call(self, ByRef text, reviver:="")
		{
			this.rev := IsObject(reviver) ? reviver : false
		; Object keys(and array indices) are temporarily stored in arrays so that
		; we can enumerate them in the order they appear in the document/text instead
		; of alphabetically. Skip if no reviver function is specified.
			this.keys := this.rev ? {} : false

			static quot := Chr(34), bashq := "\" . quot
			     , json_value := quot . "{[01234567890-tfn"
			     , json_value_or_array_closing := quot . "{[]01234567890-tfn"
			     , object_key_or_object_closing := quot . "}"

			key := ""
			is_key := false
			root := {}
			stack := [root]
			next := json_value
			pos := 0

			while ((ch := SubStr(text, ++pos, 1)) != "") {
				if InStr(" `t`r`n", ch)
					continue
				if !InStr(next, ch, 1)
					this.ParseError(next, text, pos)

				holder := stack[1]
				is_array := holder.IsArray

				if InStr(",:", ch) {
					next := (is_key := !is_array && ch == ",") ? quot : json_value

				} else if InStr("}]", ch) {
					ObjRemoveAt(stack, 1)
					next := stack[1]==root ? "" : stack[1].IsArray ? ",]" : ",}"

				} else {
					if InStr("{[", ch) {
					; Check if Array() is overridden and if its return value has
					; the 'IsArray' property. If so, Array() will be called normally,
					; otherwise, use a custom base object for arrays
						static json_array := Func("Array").IsBuiltIn || ![].IsArray ? {IsArray: true} : 0
					
					; sacrifice readability for minor(actually negligible) performance gain
						(ch == "{")
							? ( is_key := true
							  , value := {}
							  , next := object_key_or_object_closing )
						; ch == "["
							: ( value := json_array ? new json_array : []
							  , next := json_value_or_array_closing )
						
						ObjInsertAt(stack, 1, value)

						if (this.keys)
							this.keys[value] := []
					
					} else {
						if (ch == quot) {
							i := pos
							while (i := InStr(text, quot,, i+1)) {
								value := StrReplace(SubStr(text, pos+1, i-pos-1), "\\", "\u005c")

								static tail := A_AhkVersion<"2" ? 0 : -1
								if (SubStr(value, tail) != "\")
									break
							}

							if (!i)
								this.ParseError("'", text, pos)

							  value := StrReplace(value,  "\/",  "/")
							, value := StrReplace(value, bashq, quot)
							, value := StrReplace(value,  "\b", "`b")
							, value := StrReplace(value,  "\f", "`f")
							, value := StrReplace(value,  "\n", "`n")
							, value := StrReplace(value,  "\r", "`r")
							, value := StrReplace(value,  "\t", "`t")

							pos := i ; update pos
							
							i := 0
							while (i := InStr(value, "\",, i+1)) {
								if !(SubStr(value, i+1, 1) == "u")
									this.ParseError("\", text, pos - StrLen(SubStr(value, i+1)))

								uffff := Abs("0x" . SubStr(value, i+2, 4))
								if (A_IsUnicode || uffff < 0x100)
									value := SubStr(value, 1, i-1) . Chr(uffff) . SubStr(value, i+6)
							}

							if (is_key) {
								key := value, next := ":"
								continue
							}
						
						} else {
							value := SubStr(text, pos, i := RegExMatch(text, "[\]\},\s]|$",, pos)-pos)

							static number := "number", integer :="integer"
							if value is %number%
							{
								if value is %integer%
									value += 0
							}
							else if (value == "true" || value == "false")
								value := %value% + 0
							else if (value == "null")
								value := ""
							else
							; we can do more here to pinpoint the actual culprit
							; but that's just too much extra work.
								this.ParseError(next, text, pos, i)

							pos += i-1
						}

						next := holder==root ? "" : is_array ? ",]" : ",}"
					} ; If InStr("{[", ch) { ... } else

					is_array? key := ObjPush(holder, value) : holder[key] := value

					if (this.keys && this.keys.HasKey(holder))
						this.keys[holder].Push(key)
				}
			
			} ; while ( ... )

			return this.rev ? this.Walk(root, "") : root[""]
		}

		ParseError(expect, ByRef text, pos, len:=1)
		{
			static quot := Chr(34), qurly := quot . "}"
			
			line := StrSplit(SubStr(text, 1, pos), "`n", "`r").Length()
			col := pos - InStr(text, "`n",, -(StrLen(text)-pos+1))
			msg := Format("{1}`n`nLine:`t{2}`nCol:`t{3}`nChar:`t{4}"
			,     (expect == "")     ? "Extra data"
			    : (expect == "'")    ? "Unterminated string starting at"
			    : (expect == "\")    ? "Invalid \escape"
			    : (expect == ":")    ? "Expecting ':' delimiter"
			    : (expect == quot)   ? "Expecting object key enclosed in double quotes"
			    : (expect == qurly)  ? "Expecting object key enclosed in double quotes or object closing '}'"
			    : (expect == ",}")   ? "Expecting ',' delimiter or object closing '}'"
			    : (expect == ",]")   ? "Expecting ',' delimiter or array closing ']'"
			    : InStr(expect, "]") ? "Expecting JSON value or array closing ']'"
			    :                      "Expecting JSON value(string, number, true, false, null, object or array)"
			, line, col, pos)

			static offset := A_AhkVersion<"2" ? -3 : -4
			throw Exception(msg, offset, SubStr(text, pos, len))
		}

		Walk(holder, key)
		{
			value := holder[key]
			if IsObject(value) {
				for i, k in this.keys[value] {
					; check if ObjHasKey(value, k) ??
					v := this.Walk(value, k)
					if (v != JSON.Undefined)
						value[k] := v
					else
						ObjDelete(value, k)
				}
			}
			
			return this.rev.Call(holder, key, value)
		}
	}

	/**
	 * Method: Dump
	 *     Converts an AHK value into a JSON string
	 * Syntax:
	 *     str := JSON.Dump( value [, replacer, space ] )
	 * Parameter(s):
	 *     str        [retval] - JSON representation of an AHK value
	 *     value          [in] - any value(object, string, number)
	 *     replacer  [in, opt] - function object, similar to JavaScript's
	 *                           JSON.stringify() 'replacer' parameter
	 *     space     [in, opt] - similar to JavaScript's JSON.stringify()
	 *                           'space' parameter
	 */
	class Dump extends JSON.Functor
	{
		Call(self, value, replacer:="", space:="")
		{
			this.rep := IsObject(replacer) ? replacer : ""

			this.gap := ""
			if (space) {
				static integer := "integer"
				if space is %integer%
					Loop, % ((n := Abs(space))>10 ? 10 : n)
						this.gap .= " "
				else
					this.gap := SubStr(space, 1, 10)

				this.indent := "`n"
			}

			return this.Str({"": value}, "")
		}

		Str(holder, key)
		{
			value := holder[key]

			if (this.rep)
				value := this.rep.Call(holder, key, ObjHasKey(holder, key) ? value : JSON.Undefined)

			if IsObject(value) {
			; Check object type, skip serialization for other object types such as
			; ComObject, Func, BoundFunc, FileObject, RegExMatchObject, Property, etc.
				static type := A_AhkVersion<"2" ? "" : Func("Type")
				if (type ? type.Call(value) == "Object" : ObjGetCapacity(value) != "") {
					if (this.gap) {
						stepback := this.indent
						this.indent .= this.gap
					}

					is_array := value.IsArray
				; Array() is not overridden, rollback to old method of
				; identifying array-like objects. Due to the use of a for-loop
				; sparse arrays such as '[1,,3]' are detected as objects({}). 
					if (!is_array) {
						for i in value
							is_array := i == A_Index
						until !is_array
					}

					str := ""
					if (is_array) {
						Loop, % value.Length() {
							if (this.gap)
								str .= this.indent
							
							v := this.Str(value, A_Index)
							str .= (v != "") ? v . "," : "null,"
						}
					} else {
						colon := this.gap ? ": " : ":"
						for k in value {
							v := this.Str(value, k)
							if (v != "") {
								if (this.gap)
									str .= this.indent

								str .= this.Quote(k) . colon . v . ","
							}
						}
					}

					if (str != "") {
						str := RTrim(str, ",")
						if (this.gap)
							str .= stepback
					}

					if (this.gap)
						this.indent := stepback

					return is_array ? "[" . str . "]" : "{" . str . "}"
				}
			
			} else ; is_number ? value : "value"
				return ObjGetCapacity([value], 1)=="" ? value : this.Quote(value)
		}

		Quote(string)
		{
			static quot := Chr(34), bashq := "\" . quot

			if (string != "") {
				  string := StrReplace(string,  "\",  "\\")
				; , string := StrReplace(string,  "/",  "\/") ; optional in ECMAScript
				, string := StrReplace(string, quot, bashq)
				, string := StrReplace(string, "`b",  "\b")
				, string := StrReplace(string, "`f",  "\f")
				, string := StrReplace(string, "`n",  "\n")
				, string := StrReplace(string, "`r",  "\r")
				, string := StrReplace(string, "`t",  "\t")

				static rx_escapable := A_AhkVersion<"2" ? "O)[^\x20-\x7e]" : "[^\x20-\x7e]"
				while RegExMatch(string, rx_escapable, m)
					string := StrReplace(string, m.Value, Format("\u{1:04x}", Ord(m.Value)))
			}

			return quot . string . quot
		}
	}

	/**
	 * Property: Undefined
	 *     Proxy for 'undefined' type
	 * Syntax:
	 *     undefined := JSON.Undefined
	 * Remarks:
	 *     For use with reviver and replacer functions since AutoHotkey does not
	 *     have an 'undefined' type. Returning blank("") or 0 won't work since these
	 *     can't be distnguished from actual JSON values. This leaves us with objects.
	 *     Replacer() - the caller may return a non-serializable AHK objects such as
	 *     ComObject, Func, BoundFunc, FileObject, RegExMatchObject, and Property to
	 *     mimic the behavior of returning 'undefined' in JavaScript but for the sake
	 *     of code readability and convenience, it's better to do 'return JSON.Undefined'.
	 *     Internally, the property returns a ComObject with the variant type of VT_EMPTY.
	 */
	Undefined[]
	{
		get {
			static empty := {}, vt_empty := ComObject(0, &empty, 1)
			return vt_empty
		}
	}

	class Functor
	{
		__Call(method, ByRef arg, args*)
		{
		; When casting to Call(), use a new instance of the "function object"
		; so as to avoid directly storing the properties(used across sub-methods)
		; into the "function object" itself.
			if IsObject(method)
				return (new this).Call(method, arg, args*)
			else if (method == "")
				return (new this).Call(arg, args*)
		}
	}
}
Image
Attachments
image.png
image.png (28.85 KiB) Viewed 3016 times
User avatar
Frosti
Posts: 426
Joined: 27 Oct 2017, 14:30
Contact:

Re: Bypassing token limit in chatgpt

16 Feb 2023, 08:46

Don't ask for medicine if you don't have a diagnosis. Don't ask for diagnosis if you cant understand the answers. This machine is able to follow you on a wrong path and allways gives you answers that feels well. And this because its a Chatbot not a Docbot.
User avatar
labrint
Posts: 379
Joined: 14 Jun 2017, 05:06
Location: Malta

Re: Bypassing token limit in chatgpt

16 Feb 2023, 09:39

@Frosti you are 100% correct. I am a pharmacist, hence a medical professional. This bot should not be used to replace medical advice from a health care professional. It was simply a tool I had built to test the capabilities of ChatGPT and aid my work, since I am able to vet the results.

It was the only script I've built with ChatGPT so I am unable to share another example.

While we are at it, it was quite impressive, but I've also seen some serious errors.
Logitope
Posts: 19
Joined: 13 Feb 2021, 14:44

Re: Bypassing token limit in chatgpt

17 Feb 2023, 20:27

labrint wrote:
16 Feb 2023, 04:03
I am copying my code here ("ChatGPT Doctor") without the API Key (replace INSERT_API_KEY_HERE) hope it helps (edit out the things you don't need and don't forget to include the JSON library at the bottom of my code):
Nice - thank you, I will have a look at your code!
User avatar
labrint
Posts: 379
Joined: 14 Jun 2017, 05:06
Location: Malta

Re: Bypassing token limit in chatgpt

18 Feb 2023, 03:34

Has anyone managed to automate this website https://chat.openai.com/chat via Rufaydium or chrome.ahk so we can bypass the token limit of the API and make use of Chatgpt plus to get answers?
ShiningCraft
Posts: 3
Joined: 10 May 2023, 11:11

Re: Bypassing token limit in chatgpt

11 May 2023, 16:38

Hello, thank you for sharing your code. I was able to use it for a while, but then it suddenly stopped working, and now I keep getting the "Please wait contacting chat GPT.. attempt " and i let it try 300 or so times and still nothing. This keeps happening now. I haven't modified the code you posted at all, besides adding my own API key.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 85 guests