Closed ansarizafar closed 1 year ago
unfortunately, you can't use this with langchain
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()
How can we use this module with Langchainjs js.langchain.com/docs/api/llms_base/classes/LLM