由于 CORS 问题,尽管邮递员使用 bard api 给出了正确答案,但 google BARD API 未集成在接口上?

问题描述 投票:0回答:0

提问界面:-

enter image description here

enter image description here

带有 api 响应的邮递员

后端代码:-

import { createRequire } from 'module';
const require = createRequire(import.meta.url);
var express = require('express');
var bodyParser = require('body-parser');
var cors = require('cors');
import {Bard} from "googlebard";
var port = process.env.PORT || 5000;

var app = express();
app.use(express.json());
app.use(cors({
    origin: "*",
    methods: ["GET", "POST"]
  }));

app.post ("/bard",async (req,res)=>{
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

    try{
        let cookies = `__Secure-1PSID=ZQjuqQs64mhZLUGRh9Ma61gcl-hfONOEfUZNQeuehLXK5syPKzJL9sfkMbCv-i-zZJKS-A.`
        const bot = new Bard(cookies);        
        let response = await  bot.ask(req.body.prompt);
        res.status(200).json({"message":response});        
        }catch(err){
        console.log(err);
        }
});

app.listen(port, ()=>{
    console.log("Server running at port "+port);
})

前端代码

import logo from './logo.svg';
import './App.css';
import React, { useState, useEffect } from "react";

function App() {
  const [prompt, setPrompt] = useState("");
  const [message, setMessage] = useState("");
  const requestOption = {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ prompt: prompt })
  }
  const fetchdata = () => {
    fetch("http://localhost:5000/bard", requestOption)
      .then((res) => res.json())
      .then((data) => {
        
        console.log(data.message);
        setMessage(data.message);
      });
  }
  const handleSubmit = (e) => {
    fetchdata();
  }
  useEffect((e) => {
    handleSubmit(e);
  }, []);

  return (
    <div className="App">
      <form >
        <input name="prompt" className="form-control input-inline" type="text" placeholder='Ask anything' onChange={(e) => setPrompt(e.target.value)} />
        <button type="submit" className="ask-button btn btn-primary" onClick={handleSubmit}>Ask</button>
      </form>
      <div id="answer" >{message}</div>
    </div>
  );

}
export default App;

请帮助我解决 CORS 问题,以使用 Fetch 进行集成以做出反应?控制台日志上出现以下错误

Error parsing response: make sure you are using the correct cookie, copy the value of "__Secure-1PSID" cookie and set it like this:

new Bard("__Secure-1PSID=<COOKIE_VALUE>")

Also using a US proxy is recommended.

If this error persists, please open an issue on github.
https://github.com/PawanOsman/GoogleBard
TypeError: Cannot read properties of undefined (reading '3')
    at Bard.ask (file:///C:/Users/User1/google-bard-backend/node_modules/googlebard/dist/classes/bard.js:174:23)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async file:///C:/Users/User1/google-bard-backend/app.js:27:24
Error parsing response: make sure you are using the correct cookie, copy the value of "__Secure-1PSID" cookie and set it like this:

new Bard("__Secure-1PSID=<COOKIE_VALUE>")

Also using a US proxy is recommended.

If this error persists, please open an issue on github.
https://github.com/PawanOsman/GoogleBard
TypeError: Cannot read properties of undefined (reading '3')
    at Bard.ask (file:///C:/Users/User1/google-bard-backend/node_modules/googlebard/dist/classes/bard.js:174:23)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async file:///C:/Users/User1/google-bard-backend/app.js:27:24

我尝试消除 COR 问题并根据上述错误建议生成 cookie,但它无法解决。请帮助我。

reactjs cookies artificial-intelligence integration
© www.soinside.com 2019 - 2024. All rights reserved.