bard

package module
v0.0.0-...-80a30fb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 29, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

README

GOBARD

Unofficial Golang API for Google BARD ChatBOT.

  1. Visit https://bard.google.com/ (login with your account).
  2. F12 for console.
  3. Session: Application → Cookies → __Secure-1PSID and __Secure-1PSIDTS cookie value.

ATTENTION: Do not share your auth cookie.

HOWTO

  • Create a GOBARD object.
  • .Ask("something")
  • .GetAnswer()
  • Did not like this answer ? .Next() and .GetAnswer() (or .NextAnswer())
  • Want to go back to previous answer ? .Prev() and .GetAnswer() (or .PrevAnswer())
  • Next question (.Ask()) will keep Chat reference until .Reset() is called.
  • .Ask() will use the "current answer" as a reference for the question.
  • Have Fun!

QUICK EXAMPLE

package main

import (
	"fmt"
	"os"

	"github.com/aquasecurity/gobard"
	"github.com/charmbracelet/glamour"
)

var render *glamour.TermRenderer

func init() {
	render, _ = glamour.NewTermRenderer(
		glamour.WithAutoStyle(),
		glamour.WithWordWrap(80),
	)
}

func main() {
	PSID := os.Getenv("__Secure-1PSID")
	if PSID == "" {
		mt.Fprintf(os.Stderr, "__Secure-1PSID is not set\n")
		os.Exit(1)
	}

	PSIDTS := os.Getenv("__Secure-1PSIDTS")
	if PSIDTS == "" {
		mt.Fprintf(os.Stderr, "__Secure-1PSIDTS is not set\n")
		os.Exit(1)
	}

	bard01 := gobard.New(PSID, PSIDTS)

	err := bard01.Ask("Act as a simple calculator and calculate 2 + 2. Give the result only, no more words.")
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error: %v\n", err)
		os.Exit(1)
	}
	for i := 0; i < bard01.GetNumOfAnswers(); i++ {
		answerMD, _ := render.Render(bard01.GetAnswer())
		fmt.Printf("%s\n", answerMD)
		bard01.Next()
	}

	bard01.Next() // will continue the conversation using the first answer as a base

	err = bard01.Ask("What if I add 3 to the result ?")
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error: %v\n", err)
		os.Exit(1)
	}
	for i := 0; i < bard01.GetNumOfAnswers(); i++ {
		answerMD, _ := render.Render(bard01.GetAnswer())
		fmt.Printf("%s\n", answerMD)
		bard01.Next()
	}
}

NOTES

  1. Each Bard object, from gobard.new(), will have its own context until Reset() is called:
Bard01: Act as a simple calculator and calculate 2 + 2. Give the result only, no more words.

  Sure, I can help you with that.                                             
                                                                              
  2 + 2 = 4                                                                   
                                                                              
  I hope this is helpful. Let me know if you have any other questions.        

Bard02: Act as a simple calculator and calculate 4 + 8. Give the result only, no more words.

  Sure, I can help you with that.                                             
                                                                              
  4 + 8 = 12                                                                  
                                                                              
  Is there anything else I can help you with?                                 

Bard01: What if I add 5 to the result ?

  If you add 5 to the result of 2 + 2, you get 4 + 5 = 9.                     
                                                                              
  Is there anything else I can help you with today?                           

Bard02: What if I add 5 to the result ?

  If you add 5 to the result of 4 + 8, you get 12 + 5 = 17.                   
                                                                              
  Is there anything else I can help you with?                   

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Answer

type Answer struct {
	// contains filtered or unexported fields
}

type Bard

type Bard struct {
	PSID   string
	PSIDTS string
	// contains filtered or unexported fields
}

Bard is the main struct for the bard.google.com API.

func New

func New(PSID, PSIDTS string) *Bard

New creates a new Bard instance.

func (*Bard) Ask

func (b *Bard) Ask(prompt string) error

Ask asks a question to bard.google.com.

func (*Bard) GetAnswer

func (b *Bard) GetAnswer() string

GetAnswer returns the current answer.

func (*Bard) GetNumOfAnswers

func (b *Bard) GetNumOfAnswers() int

GetNumOfAnswers returns the current number of answers.

func (*Bard) Next

func (b *Bard) Next()

Next moves to the next answer.

func (*Bard) NextAnswer

func (b *Bard) NextAnswer() string

NextAnswer moves to the next answer and returns it.

func (*Bard) Prev

func (b *Bard) Prev()

Prev moves to the previous answer.

func (*Bard) PrevAnswer

func (b *Bard) PrevAnswer() string

PrevAnswer moves to the previous answer and returns it.

func (*Bard) Reset

func (b *Bard) Reset()

Reset resets the bard instance (new conversation).

type Logger

type Logger struct{}

func (Logger) Debugf

func (l Logger) Debugf(msg string, args ...interface{})

func (Logger) Errorf

func (l Logger) Errorf(msg string, args ...interface{})

func (Logger) Fatalf

func (l Logger) Fatalf(msg string, args ...interface{})

func (Logger) Infof

func (l Logger) Infof(msg string, args ...interface{})

func (Logger) Warnf

func (l Logger) Warnf(msg string, args ...interface{})

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL