mirror of
https://github.com/ikechan8370/chatgpt-plugin.git
synced 2025-12-17 05:47:11 +00:00
Api3 (#149)
* feat: api3 WIP * feat: add support for proxy of openai * fix: if no proxy
This commit is contained in:
parent
ab021d0caa
commit
62c8d56e76
4 changed files with 115 additions and 4 deletions
84
utils/message.js
Normal file
84
utils/message.js
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
import { v4 as uuidv4 } from 'uuid'
|
||||
import { Config } from '../config/index.js'
|
||||
import HttpsProxyAgent from 'https-proxy-agent'
|
||||
import _ from 'lodash'
|
||||
import fetch from 'node-fetch'
|
||||
export class OfficialChatGPTClient {
|
||||
constructor (opts = {}) {
|
||||
const {
|
||||
accessToken,
|
||||
apiReverseUrl,
|
||||
timeoutMs
|
||||
} = opts
|
||||
this._accessToken = accessToken
|
||||
this._apiReverseUrl = apiReverseUrl
|
||||
this._timeoutMs = timeoutMs
|
||||
}
|
||||
|
||||
async sendMessage (prompt, opts = {}) {
|
||||
let {
|
||||
timeoutMs = this._timeoutMs,
|
||||
conversationId = uuidv4(),
|
||||
parentMessageId = uuidv4(),
|
||||
messageId = uuidv4(),
|
||||
action = 'next'
|
||||
} = opts
|
||||
let abortController = null
|
||||
if (timeoutMs) {
|
||||
abortController = new AbortController()
|
||||
}
|
||||
const url = this._apiReverseUrl || 'https://chat.openai.com/backend-api/conversation'
|
||||
const body = {
|
||||
action,
|
||||
messages: [
|
||||
{
|
||||
id: messageId,
|
||||
role: 'user',
|
||||
content: {
|
||||
content_type: 'text',
|
||||
parts: [prompt]
|
||||
}
|
||||
}
|
||||
],
|
||||
conversationId,
|
||||
model: Config.plus ? 'text-davinci-002-render-sha' : 'text-davinci-002-render-sha',
|
||||
parent_message_id: parentMessageId
|
||||
}
|
||||
let option = {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(body),
|
||||
signal: abortController?.signal,
|
||||
headers: {
|
||||
accept: 'text/event-stream',
|
||||
'x-openai-assistant-app-id': '',
|
||||
authorization: `Bearer ${this._accessToken}`,
|
||||
'content-type': 'application/json',
|
||||
referer: 'https://chat.openai.com/chat'
|
||||
},
|
||||
referrer: 'https://chat.openai.com/chat'
|
||||
}
|
||||
if (Config.proxy) {
|
||||
option.agent = new HttpsProxyAgent(Config.proxy)
|
||||
}
|
||||
const res = await fetch(url, option)
|
||||
const decoder = new TextDecoder('utf-8')
|
||||
const bodyBytes = await res.arrayBuffer()
|
||||
const bodyText = decoder.decode(bodyBytes)
|
||||
const events = bodyText.split('\n\n').filter(item => !_.isEmpty(item))
|
||||
let fullResponse = events[events.length - 2]
|
||||
fullResponse = _.trimStart(fullResponse, 'data: ')
|
||||
if (Config.debug) {
|
||||
logger.debug(fullResponse)
|
||||
}
|
||||
fullResponse = JSON.parse(fullResponse)
|
||||
if (!fullResponse.message) {
|
||||
throw new Error(fullResponse.detail || 'unkown error')
|
||||
}
|
||||
return {
|
||||
text: fullResponse.message.content.parts[0],
|
||||
conversationId: fullResponse.conversation_id,
|
||||
id: fullResponse.message.id,
|
||||
parentMessageId
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue