Google BARD - Script to open bard - highlighted text

Share your favorite FastKeys commands
Post Reply
User avatar
GNS-Mississippi
Posts: 6
Joined: Oct 1st, ’23, 15:55
Location: Ridgeland, MS

Post by GNS-Mississippi » Oct 1st, ’23, 16:07

My first post!

Can someone share a script that might open Google BARD: https://bard.google.com/chat and start a new chat? I want to highlight a word in my document, and open the link to bard and have the highlighted word be pasted into Bard Chat Prompt.

I assume the script would be similar to the script in the default script list. Such as the Wikipedia script that allows the user to highlight a word, and command the script.

Code: Select all

Send, ^{vk43} ;Ctrl C 
Sleep 50
Run, http://en.wikipedia.org/wiki/Special:Search?search=%Clipboard%
It would be nice to have a similar script to open ChatGPT as well -

gns -
User avatar
Tom
Posts: 849
Joined: Nov 24th, ’15, 23:39

Post by Tom » Oct 3rd, ’23, 20:05

This works for me most of the time

Code: Select all

Send, ^c 
Sleep 100
run, https://bard.google.com/chat
sleep 3000
send, %clipboard%
User avatar
GNS-Mississippi
Posts: 6
Joined: Oct 1st, ’23, 15:55
Location: Ridgeland, MS

Post by GNS-Mississippi » Oct 8th, ’23, 15:34

Perfect! Worx likes new money! :D
justiceinthesystem
Posts: 2
Joined: Dec 9th, ’23, 01:40

Post by justiceinthesystem » Dec 9th, ’23, 05:14

Hello, I signed up so I could share this with you. So I apologize now if formatting of post is wrong or I don't do something correctly.

This .ahk script will use GPT on selected text anywhere on your computer if the text is also a user input box/area, command line, etc.
There is a second hotkey allowing you to use a popup dialog to modify/instruct or give context to selected text (if any) but not sure you will get much of a response if no text selected and only put something in the popup. Also, you probably need to go through to the api urls and update the endpoints from V1.

Bard should be about the same to implement but I'm not sure I have never used it. I finally downloaded FK again today because I was actually going to use it in conjunction with a tool called 'Bito' which is currently in alpha but has a pretty cool CLI chat and also non-interactive mode where you supply prompt, a context, and file to run prompt on as 3 different files to command line versus interactive chat in command line. I don't know rules for sharing links but it should be easy enough to find - it's also an extension in web store.

Anyway, here is GPT ahk

Code: Select all

; GPT for any input field on PC

; -- Configuration --
#SingleInstance  ; Allow only one instance of this script to be running.

; This is the hotkey used to autocomplete prompts (get an inline response if current window context allows text input))
HOTKEY_AUTOCOMPLETE = #o  ; Win+o
; This is the hotkey used to edit prompts (shows popup with or without preslected text, for instructions/context)
HOTKEY_INSTRUCT = #+o  ; Win+shift+o
; Models settings
MODEL_AUTOCOMPLETE_ID := "text-davinci-003" 
MODEL_AUTOCOMPLETE_MAX_TOKENS := 200
MODEL_INSTRUCT_ID := "text-davinci-edit-001" 

; -- Initialization --
; Dependencies
; WinHttpRequest: https://www.reddit.com/comments/mcjj4s
; cJSON.ahk: https://github.com/G33kDude/cJSON.ahk
#Include <JSON>
http := WinHttpRequest()

I_Icon = GPT-AHK.ico
IfExist, %I_Icon%
Menu, Tray, Icon, %I_Icon%

Hotkey, %HOTKEY_AUTOCOMPLETE%, AutocompleteFcn
Hotkey, %HOTKEY_INSTRUCT%, InstructFcn

IfNotExist, settings.ini     
{
  InputBox, API_KEY, Please insert your OpenAI API key, API key, , 270, 145
  IniWrite, %API_KEY%, settings.ini, OpenAI, API_KEY
} 
Else
{
  IniRead, API_KEY, settings.ini, OpenAI, API_KEY  
}
Return

; -- Main commands --
; Edit the phrase
InstructFcn: 
   GetText(CutText, "Cut")
   InputBox, UserInput, Text to edit "%CutText%", Enter an instruction, , 270, 145
   if ErrorLevel {
      PutText(CutText)
   }else{
      url := "https://api.openai.com/v1/edits"
      body := {}
      body.model := MODEL_INSTRUCT_ID ; ID of the model to use.
      body.input := CutText ; The prompt to edit.
      body.instruction := UserInput ; The instruction that tells how to edit the prompt
      headers := {"Content-Type": "application/json", "Authorization": "Bearer " . API_KEY}
      SetSystemCursor()
      response := http.POST(url, JSON.Dump(body), headers, {Object:true})
      obj := JSON.Load(response.Text)
      PutText(obj.choices[1].text, "")
      RestoreCursors()
   }
   Return   

; Auto-complete the phrase 
AutocompleteFcn:
   GetText(CopiedText, "Copy")
   url := "https://api.openai.com/v1/completions"
   body := {}
   body.model := MODEL_AUTOCOMPLETE_ID ; ID of the model to use.   
   body.prompt := CopiedText ; The prompt to generate completions for
   body.max_tokens := MODEL_AUTOCOMPLETE_MAX_TOKENS ; The maximum number of tokens to generate in the completion.
   headers := {"Content-Type": "application/json", "Authorization": "Bearer " . API_KEY}
   SetSystemCursor()
   response := http.POST(url, JSON.Dump(body), headers, {Object:true})
   obj := JSON.Load(response.Text)
   PutText(obj.choices[1].text, "AddSpace")
   RestoreCursors()   
   Return

; -- Auxiliar functions --
; Copies the selected text to a variable while preserving the clipboard.
GetText(ByRef MyText = "", Option = "Copy")
{
   SavedClip := ClipboardAll
   Clipboard =
   If (Option == "Copy")
   {
      Send ^c
   }
   Else If (Option == "Cut")
   {
      Send ^x
   }
   ClipWait 0.5
   If ERRORLEVEL
   {
      Clipboard := SavedClip
      MyText =
      Return
   }
   MyText := Clipboard
   Clipboard := SavedClip
   Return MyText
}

; Send text from a variable while preserving the clipboard.
PutText(MyText, Option = "")
{
   SavedClip := ClipboardAll 
   Clipboard = 
   Sleep 20
   Clipboard := MyText
   If (Option == "AddSpace")
   {
      Send {Right}
      Send {Space}
   }
   Send ^v
   Sleep 100
   Clipboard := SavedClip
   Return
}   

; Change system cursor (Uncomment for custom mouse cursor icon!)
;SetSystemCursor()
;{
;   Cursor = %A_ScriptDir%\GPT3-AHK.ani
;   CursorHandle := DllCall( "LoadCursorFromFile", Str,Cursor )

;   Cursors = 32512,32513,32514,32515,32516,32640,32641,32642,32643,32644,32645,32646,32648,32649,32650,32651
;   Loop, Parse, Cursors, `,
;   {
;      DllCall( "SetSystemCursor", Uint,CursorHandle, Int,A_Loopfield )
;   }
;}

;RestoreCursors() 
;{
;   DllCall( "SystemParametersInfo", UInt, 0x57, UInt,0, UInt,0, UInt,0 )
;}
justiceinthesystem
Posts: 2
Joined: Dec 9th, ’23, 01:40

Post by justiceinthesystem » Dec 9th, ’23, 05:44

Also, under dependencies, you would need to grab those and put them in a folder called "Lib" in the same folder beside .ahk script like in screenshot.
Image

Also in same folder .ahk script, you need a settings.ini like:

Code: Select all

[OpenAI]
API_KEY=sk-NOQUOTES
But I think it would be alright if you just use it directly in script, that's your call though.
Post Reply