ใช้ AirOps API ในแอปพลิเคชันไคลเอนต์ของคุณด้วย JavaScript SDK ของเรา
npm i @airops/airops-js
หรือใช้ CDN:
< script src = “https://cdn.jsdelivr.net/npm/@airops/airops-js/dist/index.umd.min.js” > </ script >
ในการตรวจสอบสิทธิ์กับ API ของเราโดยใช้ SDK คุณจะต้องมีข้อมูลสามส่วน: รหัสพื้นที่ทำงานของคุณ คีย์ API และรหัสผู้ใช้ (ตัวระบุสำหรับผู้ใช้ในแอปพลิเคชันของคุณ) ขั้นแรก ให้ใช้คีย์ API เพื่อแฮช ID ผู้ใช้ของคุณบนเซิร์ฟเวอร์แบ็คเอนด์ของคุณ ซึ่งจะส่งผลให้มีรหัสผู้ใช้ที่ถูกแฮชซึ่งเป็นเอกลักษณ์ของคีย์ API และรหัสผู้ใช้ของคุณ รหัสพื้นที่ทำงานและคีย์ 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 ( ) ;
การตอบสนอง
การตอบสนองจากวิธีการดำเนินการจะมีรหัสการดำเนินการที่สามารถใช้เพื่อดึงผลลัพธ์ในภายหลังพร้อมกับสองวิธีในการรอผลลัพธ์หรือยกเลิกการดำเนินการ:
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 ;
}
วิธีผลลัพธ์ใช้ตรรกะการดึงสำหรับผลลัพธ์โดยมีการหมดเวลา 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 ของเรา