OvidijusParsiunas / deep-chat

Fully customizable AI chatbot component for your website
https://deepchat.dev
MIT License
1.27k stars 175 forks source link

next.js example is not accessible in regions where OpenAI prohibits access #103

Closed buzhou9 closed 5 months ago

buzhou9 commented 5 months ago

OpenAI/chat.ts in the next.js example is not accessible in regions where OpenAI prohibits access, even if I have the proxy turned on. But I execute JS scripts in the terminal and it can be accessed normally。

Here's what I tested the script。

const axios = require('axios');

const axiosConfig = {
  headers: {
    'Content-Type': 'application/json',
    Authorization: 'Bearer openAIKey',
  }
};

axios.post(
  'https://api.openai.com/v1/chat/completions',
  {
    model: 'gpt-3.5-turbo',
    messages: [{ role: 'user', content: 'Say this is a test!' }],
  },
  axiosConfig
).then(res => {
  console.log(res.data)
}).catch(e => {
  console.error(e)
})

This is the “chat.ts ” file I rewrote and it works fine

import axios from 'axios'
import type { NextApiRequest, NextApiResponse } from 'next'
import {createReqChatBody} from "../../../utils/openAIChatBody";
import {DeepChatOpenAITextRequestBody} from "../../../types/deepChatTextRequestBody";

export default async function handle (req: NextApiRequest, res: NextApiResponse) {
  try {
    const axiosConfig = {
      headers: {
        'Content-Type': 'application/json',
        Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
      }
    };
    const textRequestBody = (req.body) as DeepChatOpenAITextRequestBody;
    const chatBody = createReqChatBody(textRequestBody)
    console.log(chatBody)
    const response = await axios.post(
      'https://api.openai.com/v1/chat/completions',
      chatBody,
      axiosConfig
    )
    console.log(response.data);
    console.log(response.data.choices[0].message.content)
    res.json({text: response.data.choices[0].message.content})
  } catch (error) {
    res.status(500).json({ message: error.toString() })
  }
}

Why can't the agent I configure in the terminal not take effect in the fetch method of next.js?

OvidijusParsiunas commented 5 months ago

Hi @buzhou9. I'm sorry to hear you are having proxy issues.

First I want to clarify your problem statement; the first example works fine if you run it in the terminal, but the second next.js example doesn't work?

May I ask what error are you getting in the second example and are you sure it is a proxy error?

If it is a proxy error, then the problem will largely depend on your pc/proxy setup and is unfortunately difficult for me to diagnose. However I can perhaps point you to areas to look at:

This is unfortunately as much advise I can give you as proxy issues are largely dependent on the setup of the proxy itself. Furthermore, the scope of the problem is outside of the topic of Deep Chat, hence the information I can provide is limited.

buzhou9 commented 5 months ago

Thank you for the suggestion. I'd like to try debugging locally。Both of the snippets I provided work fine, which is enough to prove that the agent is valid. The fetch method of next.js does not have server access through a proxy, which should be a problem with the next.js framework itself, which is indeed beyond the topic of deep-chat.