airops js
1.0.0
透過我們的 JavaScript SDK 在您的客戶端應用程式中使用 AirOps API。
npm i @airops/airops-js
或使用CDN:
< script src = “https://cdn.jsdelivr.net/npm/@airops/airops-js/dist/index.umd.min.js” > </ script >
要使用 SDK 對我們的 API 進行身份驗證,您需要三個部分資訊:您的工作區 ID、API 金鑰和使用者 ID(應用程式中使用者的識別碼)。首先,使用 API 金鑰在後端伺服器上對您的使用者 ID 進行雜湊處理。這將產生一個雜湊用戶 ID,該用戶 ID 對於您的 API 金鑰和用戶 ID 組合來說是唯一的。工作區 ID 和 API 金鑰可以在您的工作區設定中找到。
const crypto = require ( 'crypto' ) ;
const userIdHash = ( ) => {
const apiKey = 'YOUR_API_KEY' ;
const userId = 'YOUR_USER_ID' ;
// Convert your api key to a buffer
const key = Buffer . from ( apiKey , 'utf-8' ) ;
// Hash the message using HMAC-SHA256 and the key
const hash = crypto . createHmac ( 'sha256' , key ) . update ( userId ) . digest ( 'hex' ) ;
return hash ;
} ;
const airopsInstance = AirOps . identify ( {
userId : 'YOUR_USER_ID' ,
workspaceId : 'YOUR_WORKSPACE_ID' ,
hashedUserId : 'YOUR_USER_ID_HASH' ,
} ) ;
請注意,執行公共應用程式不需要授權。如果您正在使用不帶參數的公共初始化:
const airopsInstance = new AirOps ( ) ;
成功初始化 SDK 後,您就可以開始使用可用的方法與我們的 API 互動。請注意,這些方法將返回承諾。
SDK提供了執行應用程式的方法。為了串流應用程式結果,您需要啟用流並將回調函數傳遞給執行方法。您可以選擇傳遞額外的回調函數,以便在應用程式完成時收到通知。
// Execute an app
const response = await airopsInstance . apps . execute ( {
appId : 1 ,
version : 1 ,
payload : {
inputs : {
name : 'XXXXYYYYZZZZ' ,
} ,
} ,
stream : true , // Optional - Default false
streamCallback : ( data : { content : string } ) => {
// Do something with the data
} , // Optional, required if stream is true
streamCompletedCallback : ( data : { content : string } ) => {
// Do something with the data
} , // Optional
} ) ;
// Wait for result
const result = await response . result ( ) ;
// Do something with result.output
// Cancel execution
await response . cancel ( ) ;
回覆
執行方法的回應將包含可用於稍後檢索結果的執行 ID,以及等待結果或取消執行的兩個方法:
interface ExecuteResponse {
executionId : number ;
result : ( ) => Promise < AppExecution > ;
cancel : ( ) => Promise < void > ;
}
interface AppExecution {
airops_app_id : number ;
id : number ;
status : string ;
output : string ;
stream_channel_id : string ;
}
result 方法實現了結果的拉取邏輯,逾時時間為10 分鐘。如果您想要實作自己的拉取邏輯,可以使用下面所述的 getResults 方法。
您可以使用 getResults 方法實作自己的拉取邏輯。
const result = await airopsInstance . apps . getResults ( {
executionId : response . executionId ,
} ) ;
對於聊天應用程序,您可以使用chatStream
方法,該方法允許您向聊天應用程式發送訊息。
const response = await airopsInstance . apps . chatStream ( {
appId : 2 ,
message ,
inputs : {
name : 'XXXXYYYYZZZZ' ,
} ,
streamCallback : ( { action : ChatAction , ... data ) => {
// ChatAction can either be 'agent-response', 'agent-action' or 'agent-action-error'
// data will have different values depending on the action:
// - agent-response: { token: string, stream_finished: boolean, result :string }
// - agent-action: { tool: string, tool_input: Record<string, string> }
// - agent-action-error: { tool: string, tool_error: string }
} ,
streamCompletedCallback : ( data : { result : string } ) => {
// do something with data.result
} ,
... ( sessionId && { sessionId } ) , // optionally pass sessionId to continue chat.
} ) ;
// Wait for result
const result = await response . result ( ) ;
// Do something with result.result
// Cancel execution
await response . cancel ( ) ;
// Use session id to continue chat
response . sessionId ;
回覆
chatStream 方法的回應將包含 sessionId 和等待回應的結果方法。為了繼續聊天,請傳遞 sessionId 和訊息。
export interface ChatStreamResponse {
sessionId : string ;
result : Promise < { result : string } > ; // result is a promise that resolves when the execution is completed.
}
try {
await airopsInstance . apps . execute ( {
appId : 1 ,
version : 4 ,
payload : {
inputs : { name : 'XXXXYYYYZZZZ' } ,
} ,
} ) ;
} catch ( e ) {
// Do something with error.message
}
加入我們的 AirOps 建構者 Slack 社群。