@baiducloud/qianfan
Advanced tools
Comparing version 0.1.9 to 0.2.0
{ | ||
"name": "@baiducloud/qianfan", | ||
"version": "0.1.9", | ||
"version": "0.2.0", | ||
"publishConfig": { | ||
@@ -68,2 +68,2 @@ "access": "public", | ||
} | ||
} | ||
} |
@@ -29,2 +29,3 @@ // Copyright (c) 2024 Baidu, Inc. All Rights Reserved. | ||
QIANFAN_CONSOLE_API_BASE_URL: 'https://qianfan.baidubce.com', | ||
QIANFAN_BEAR_TOKEN_URL: 'http://iam.bj.baidubce.com/v1/BCE-BEARER/token', | ||
QIANFAN_LLM_API_RETRY_TIMEOUT: '600000', | ||
@@ -50,2 +51,2 @@ QIANFAN_LLM_API_RETRY_BACKOFF_FACTOR: '0', | ||
export const DYNAMIC_INVALID = ['reranker']; | ||
export const DYNAMIC_INVALID = ['reranker']; |
@@ -21,3 +21,3 @@ // Copyright (c) 2024 Baidu, Inc. All Rights Reserved. | ||
import Reranker from './Reranker'; | ||
import {setEnvVariable, setBrowserVariable, consoleAction} from './utils'; | ||
import {setEnvVariable, setBrowserVariable, consoleAction, getBearToken} from './utils'; | ||
@@ -35,2 +35,3 @@ export { | ||
consoleAction, | ||
getBearToken | ||
}; |
@@ -22,2 +22,3 @@ // Copyright (c) 2024 Baidu, Inc. All Rights Reserved. | ||
QIANFAN_CONSOLE_API_BASE_URL: string; | ||
QIANFAN_BEAR_TOKEN_URL: string; | ||
QIANFAN_LLM_API_RETRY_TIMEOUT: string; | ||
@@ -24,0 +25,0 @@ QIANFAN_LLM_API_RETRY_BACKOFF_FACTOR: string; |
@@ -17,2 +17,3 @@ // Copyright (c) 2024 Baidu, Inc. All Rights Reserved. | ||
import Fetch from './Fetch'; | ||
import {Headers} from './Fetch/nodeFetch'; | ||
import {BASE_PATH, DEFAULT_CONFIG, DEFAULT_HEADERS} from './constant'; | ||
@@ -360,1 +361,65 @@ import {IAMConfig, QfLLMInfoMap, ReqBody, DefaultConfig} from './interface'; | ||
} | ||
interface GetTokenProps { | ||
expireInSeconds?: number; | ||
} | ||
interface TokenResp { | ||
headers?: Headers, | ||
userId?: string, | ||
status?: string, | ||
createTime?: string, | ||
token: string, | ||
expireTime: string | ||
} | ||
async function fetchBearToken(props?: GetTokenProps): Promise<TokenResp> { | ||
const {expireInSeconds: expireInSecondsInProps} = props || {}; | ||
const config = getDefaultConfig(); | ||
const {QIANFAN_BEAR_TOKEN_URL} = config; | ||
try { | ||
// 鉴权 | ||
const httpClientConfig = getIAMConfig( | ||
config.QIANFAN_ACCESS_KEY, | ||
config.QIANFAN_SECRET_KEY, | ||
QIANFAN_BEAR_TOKEN_URL | ||
); | ||
const client = new HttpClient(httpClientConfig); | ||
const expireInSeconds = typeof expireInSecondsInProps === 'number' ? expireInSecondsInProps : 100000 | ||
const fetchOptions = await client.getSignature({ | ||
httpMethod: 'GET', | ||
path: QIANFAN_BEAR_TOKEN_URL, | ||
params: {expireInSeconds}, | ||
headers: { | ||
...DEFAULT_HEADERS, | ||
} | ||
}); | ||
const fetchInstance = new Fetch(); | ||
const {url, ...rest} = fetchOptions; | ||
const resp = await fetchInstance.makeRequest(`${QIANFAN_BEAR_TOKEN_URL}?expireInSeconds=${expireInSeconds}`, rest); | ||
return resp; | ||
} | ||
catch (error) { | ||
const error_msg = `Failed to get access token: ${error && error.message}`; | ||
throw new Error(error_msg); | ||
} | ||
} | ||
function _getBearToken() { | ||
let expire_time: string | number = 0, data; | ||
return async function getToken(props?: GetTokenProps): Promise<TokenResp> { | ||
try{ | ||
if(!expire_time || new Date(expire_time) <= new Date()){ | ||
const resp = await fetchBearToken(props); | ||
const {expireTime} = resp || {}; | ||
expire_time = expireTime; | ||
data = resp; | ||
} | ||
return data; | ||
} catch(error) { | ||
throw new Error(error?.message); | ||
} | ||
} | ||
} | ||
export const getBearToken: (props?: GetTokenProps) => Promise<TokenResp> = _getBearToken(); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2998549
101864