PawanOsman / GoogleBard

GoogleBard - A reverse engineered API for Google Bard chatbot for NodeJS
https://bard.google.com
MIT License
415 stars 58 forks source link

Google Bard with langchainjs #13

Closed ansarizafar closed 1 year ago

ansarizafar commented 1 year ago

How can we use this module with Langchainjs js.langchain.com/docs/api/llms_base/classes/LLM

PawanOsman commented 1 year ago

unfortunately, you can't use this with langchain

ansarizafar commented 1 year ago

I was experimenting with GoogleBard and Langchainjs and I am able to integrate this module with Langchainjs. Here is the code.

import { LLM } from "langchain/llms/base"
import { LLMChain } from 'langchain/chains'
import { PromptTemplate } from 'langchain/prompts'
import { Bard } from "googlebard";

class BardAI extends LLM {
  streaming = true

  constructor(config) {
    super(config);
    this.streaming = config.streaming
    let cookies =  <cookie>
     this.model = new Bard(cookies, {
      inMemory: true, 
      protocol: "http"
    })
    this.controller = new AbortController();
  }

  async _call(prompt, options = { signal: controller.signal }, runManager) {
    //  let completion = ''

    const res = await this.model.askStream(
      (token) => {
         if (!token) return
        if (this.streaming) {
          this.callbacks.handleLLMNewToken(token)
          }
      },
      prompt
    );

   return res
  }

  _llmType() {
    return 'bard.chat'
  }

  cancel() {
    this.controller.abort();
  }
}

async function main() {
  let model = new BardAI({
    cache: false, maxConcurrency: 4, streaming: false, 
   callbacks: 
      {
        handleLLMNewToken(token) {
          process.stdout.write(token);
        },
      },

  })

  const template = 'You are a computer science teacher. I want you to create flash cards for learning about Computer software for class {grade}. You will only return a Javascript object and nothing else. No explination or introduction,'
  const prompt = new PromptTemplate({
    template: template,
    inputVariables: ['grade'],
  })
  const chain = new LLMChain({ llm: model, prompt: prompt })
  const response = await chain.call({ grade: '7' })
console.log(`response`, response, JSON.stringify(response))

}

main()