|
| 1 | +const fetchSSE = require('./fetchSSE.js'); |
| 2 | +const fetch = require('node-fetch'); |
| 3 | + |
| 4 | +module.exports = class Api2d { |
| 5 | + // 设置key和apiBaseUrl |
| 6 | + constructor(key = null, apiBaseUrl = null, timeout = 60000) { |
| 7 | + this.key = key; |
| 8 | + this.apiBaseUrl = apiBaseUrl || (key && key.startsWith('fk') ? 'https://stream.api2d.net' : 'https://api.openai.com'); |
| 9 | + this.timeout = timeout; |
| 10 | + this.controller = new AbortController(); |
| 11 | + } |
| 12 | + |
| 13 | + // set key |
| 14 | + setKey(key) { |
| 15 | + this.key = key; |
| 16 | + } |
| 17 | + |
| 18 | + // set apiBaseUrl |
| 19 | + setApiBaseUrl(apiBaseUrl) { |
| 20 | + this.apiBaseUrl = apiBaseUrl; |
| 21 | + } |
| 22 | + |
| 23 | + setTimeout(timeout) { |
| 24 | + this.timeout = parseInt(timeout) || 60 * 1000; |
| 25 | + } |
| 26 | + |
| 27 | + abort() { |
| 28 | + this.controller.abort(); |
| 29 | + } |
| 30 | + |
| 31 | + // Completion |
| 32 | + async completion(options) { |
| 33 | + // 拼接目标URL |
| 34 | + const url = this.apiBaseUrl + '/v1/chat/completions'; |
| 35 | + // 拼接headers |
| 36 | + const headers = { |
| 37 | + 'Content-Type': 'application/json', |
| 38 | + Authorization: 'Bearer ' + this.key |
| 39 | + }; |
| 40 | + |
| 41 | + const { onMessage, onEnd, model, ...restOptions } = options; |
| 42 | + |
| 43 | + // 如果是流式返回,且有回调函数 |
| 44 | + if (restOptions.stream && onMessage) { |
| 45 | + // 返回一个 Promise |
| 46 | + return new Promise(async (resolve, reject) => { |
| 47 | + try { |
| 48 | + let chars = ''; |
| 49 | + console.log('in stream'); |
| 50 | + // 使用 fetchEventSource 发送请求 |
| 51 | + const timeout_handle = setTimeout(() => { |
| 52 | + this.controller.abort(); |
| 53 | + // throw new Error( "Timeout "+ this.timeout ); |
| 54 | + reject(new Error(`[408]:Timeout by ${this.timeout} ms`)); |
| 55 | + }, this.timeout); |
| 56 | + const response = await fetchSSE(url, { |
| 57 | + signal: this.controller.signal, |
| 58 | + method: 'POST', |
| 59 | + openWhenHidden: true, |
| 60 | + fetch: fetch, |
| 61 | + headers: { ...headers, Accept: 'text/event-stream' }, |
| 62 | + body: JSON.stringify({ ...restOptions, model: model || 'gpt-3.5-turbo' }), |
| 63 | + async onopen(response) { |
| 64 | + if (response.status != 200) { |
| 65 | + throw new Error(`[${response.status}]:${response.statusText}`); |
| 66 | + } |
| 67 | + }, |
| 68 | + onmessage: (data) => { |
| 69 | + if (timeout_handle) { |
| 70 | + clearTimeout(timeout_handle); |
| 71 | + } |
| 72 | + if (data == '[DONE]') { |
| 73 | + // console.log( 'DONE' ); |
| 74 | + if (onEnd) onEnd(chars); |
| 75 | + resolve(chars); |
| 76 | + } else { |
| 77 | + const event = JSON.parse(data); |
| 78 | + if (event.choices[0].delta.content) chars += event.choices[0].delta.content; |
| 79 | + if (onMessage) onMessage(chars); |
| 80 | + } |
| 81 | + }, |
| 82 | + onerror: (error) => { |
| 83 | + console.log(error); |
| 84 | + throw new Error(String(error)?.match(/\[(\d+)\]/)?.[1] ? error : `[500]:${error}`); |
| 85 | + } |
| 86 | + }, global.fetch || fetch); |
| 87 | + |
| 88 | + // const ret = await response.json(); |
| 89 | + } catch (error) { |
| 90 | + console.log(error); |
| 91 | + reject(error); |
| 92 | + } |
| 93 | + }); |
| 94 | + } else { |
| 95 | + // 使用 fetch 发送请求 |
| 96 | + const response = await fetch(url, { |
| 97 | + signal: this.controller.signal, |
| 98 | + method: 'POST', |
| 99 | + headers: headers, |
| 100 | + body: JSON.stringify({ ...restOptions, model: model || 'gpt-3.5-turbo' }) |
| 101 | + }); |
| 102 | + const timeout_handle = setTimeout(() => { |
| 103 | + this.controller.abort(); |
| 104 | + }, this.timeout); |
| 105 | + const ret = await response.json(); |
| 106 | + clearTimeout(timeout_handle); |
| 107 | + return ret; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + async embeddings(options) { |
| 112 | + // 拼接目标URL |
| 113 | + const url = this.apiBaseUrl + '/v1/embeddings'; |
| 114 | + // 拼接headers |
| 115 | + const headers = { |
| 116 | + 'Content-Type': 'application/json', |
| 117 | + Authorization: 'Bearer ' + this.key |
| 118 | + }; |
| 119 | + const { model, ...restOptions } = options; |
| 120 | + // 使用 fetch 发送请求 |
| 121 | + const response = await fetch(url, { |
| 122 | + signal: this.controller.signal, |
| 123 | + method: 'POST', |
| 124 | + headers: headers, |
| 125 | + body: JSON.stringify({ ...restOptions, model: model || 'text-embedding-ada-002' }) |
| 126 | + }); |
| 127 | + const timeout_handle = setTimeout(() => { |
| 128 | + this.controller.abort(); |
| 129 | + }, this.timeout); |
| 130 | + const ret = await response.json(); |
| 131 | + clearTimeout(timeout_handle); |
| 132 | + return ret; |
| 133 | + } |
| 134 | + |
| 135 | + async billing() { |
| 136 | + const url = this.apiBaseUrl + '/dashboard/billing/credit_grants'; |
| 137 | + const headers = { |
| 138 | + 'Content-Type': 'application/json', |
| 139 | + Authorization: 'Bearer ' + this.key |
| 140 | + }; |
| 141 | + const response = await fetch(url, { |
| 142 | + signal: this.controller.signal, |
| 143 | + method: 'GET', |
| 144 | + headers: headers |
| 145 | + }); |
| 146 | + const timeout_handle = setTimeout(() => { |
| 147 | + this.controller.abort(); |
| 148 | + }, this.timeout); |
| 149 | + const ret = await response.json(); |
| 150 | + clearTimeout(timeout_handle); |
| 151 | + return ret; |
| 152 | + } |
| 153 | +}; |
0 commit comments