Skip to content
This repository has been archived by the owner on Aug 11, 2023. It is now read-only.

Commit

Permalink
fix: fix timeout by custom settting timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Crazyokd committed Jun 3, 2023
1 parent 35ae241 commit 18fe593
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/config/openai.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ export const OPENAI_API_KEY =
"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";
export const OPENAI_MAKING_REQUEST =
"https://api.openai.com/v1/chat/completions";

export const OPENAI_TIMEOUT = 60000;
3 changes: 2 additions & 1 deletion src/services/api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
GROUP_SEARCH_URL,
COMMON_PUBLIC_KEY_URL,
} from "@/config/api";
import { OPENAI_TIMEOUT } from "@/config/openai";
import encrypt from "@/utils/crypto";

export default {
Expand Down Expand Up @@ -93,7 +94,7 @@ export default {
console.log("Waiting for chatgpt answer...");
const response = await asyncPost(OPENAI_MAKING_REQUEST, {}, data, {
headers,
});
}, OPENAI_TIMEOUT);
console.log("show chatgpt response...");
return response == null ? "request timeout!" : response.data.choices[0].message.content;
} catch (err) {
Expand Down
7 changes: 4 additions & 3 deletions src/services/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ function post(url, params, data) {
return request("post", url, params, data);
}

function asyncPost(url, params, data, { headers }) {
return asyncRequest("post", url, params, data, { headers })
function asyncPost(url, params, data, { headers }, timeout = 0) {
return asyncRequest("post", url, params, data, { headers }, timeout)
}

async function asyncRequest(method, url, params, data, { headers }) {
async function asyncRequest(method, url, params, data, { headers }, timeout) {
return await instance({
method,
url,
params,
data,
headers,
responseType: "json",
timeout,
});
}

Expand Down

0 comments on commit 18fe593

Please sign in to comment.