Skip to content

Commit

Permalink
Merge pull request #1 from bananaml/feature/v4
Browse files Browse the repository at this point in the history
bumped to v4 endpoints, removed feedback, and added /start return pos…
  • Loading branch information
erik-dunteman authored Aug 10, 2022
2 parents ff022ed + f985262 commit 8ddc58d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 53 deletions.
69 changes: 22 additions & 47 deletions module/genericsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,24 @@ if ("BANANA_URL" in process.env){
}


export async function runMain(apiKey: string, modelKey: string, modelInputs: object = {}): Promise<object>{
const callID = await startAPI(apiKey, modelKey, modelInputs)
export async function runMain(apiKey: string, modelKey: string, modelInputs: object = {}): Promise<any>{
const startOut = await startAPI(apiKey, modelKey, modelInputs)
if (startOut["finished"] == true){
const res = {
"id": startOut["id"],
"message": startOut["message"],
"created": startOut["created"],
"apiVersion": startOut["apiVersion"],
"modelOutputs": startOut["modelOutputs"]
}
return res
}

// else it's long running, so poll for result
while (true) {
const jsonOut = await checkAPI(apiKey, callID)
const jsonOut = await checkAPI(apiKey, startOut["callID"])
if (jsonOut !== undefined){
if (jsonOut.message.toLowerCase() === "success"){
jsonOut['callID'] = callID
return jsonOut
}
}
Expand All @@ -27,28 +38,25 @@ export async function runMain(apiKey: string, modelKey: string, modelInputs: obj
}

export async function startMain(apiKey: string, modelKey: string, modelInputs: object = {}): Promise<string>{
const callID = await startAPI(apiKey, modelKey, modelInputs)
return callID
const jsonOut = await startAPI(apiKey, modelKey, modelInputs, true)
return jsonOut["callID"]
}

export async function feedback(apiKey: string, callID: string, feedback: object = {}): Promise<object>{
const jsonOut = await feedbackAPI(apiKey, callID, feedback)
return jsonOut
}

export async function checkMain(apiKey: string, callID: string): Promise<object>{
const jsonOut = await checkAPI(apiKey, callID)
return jsonOut
}

const startAPI = async (apiKey: string, modelKey: string, modelInputs: object): Promise<string> => {
const urlStart = endpoint.concat("start/v2/")
const startAPI = async (apiKey: string, modelKey: string, modelInputs: object, startOnly: boolean = false): Promise<any> => {
const urlStart = endpoint.concat("start/v4/")
const payload = {
"id": uuidv4(),
"created": Math.floor(new Date().getTime() / 1000),
"apiKey" : apiKey,
"modelKey" : modelKey,
"modelInputs" : modelInputs,
"startOnly": startOnly,
}

const response = await axios.post(urlStart, payload).catch(err => {
Expand All @@ -67,44 +75,11 @@ const startAPI = async (apiKey: string, modelKey: string, modelInputs: object):
throw jsonOut.message
}

const callID = jsonOut.callID
if (callID === undefined){
throw `server error: start call failed without a message or callID`
}

return callID
}

const feedbackAPI = async (apiKey: string, callID: string, feedback: object): Promise<any> => {
const url = endpoint.concat("feedback/v2/")

const payload = {
"id": uuidv4(),
"created": Math.floor(new Date().getTime() / 1000),
"apiKey" : apiKey,
"callID" : callID,
"feedback": feedback
}

const response = await axios.post(url, payload).catch(err => {
if (err.response) {
throw `server error: status code ${err.response.status}`
} else if (err.request) {
throw 'server error: endpoint busy or not available.'
} else {
console.log(err)
throw "Misc axios error. Please email erik@banana.dev with above error"
}
})
const jsonOut = response.data

if (jsonOut.message.toLowerCase().includes("error")){
throw jsonOut.message
}
return jsonOut
}

const checkAPI = async (apiKey: string, callID: string): Promise<any> => {
const urlCheck = endpoint.concat("check/v2/")
const urlCheck = endpoint.concat("check/v4/")

const payload = {
"id": uuidv4(),
Expand Down
5 changes: 0 additions & 5 deletions module/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ export async function start(apiKey: string, modelKey: string, modelInputs: objec
return callID
}

export async function feedback(apiKey: string, callID: string, feedback: object = {}): Promise<object>{
const jsonOut = await genericsUtils.feedback(apiKey, callID, feedback)
return jsonOut
}

export async function check(apiKey: string, callID: string): Promise<object>{
const jsonOut = await genericsUtils.checkMain(apiKey, callID)
return jsonOut
Expand Down
2 changes: 1 addition & 1 deletion module/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@banana-dev/banana-dev",
"version": "3.0.0",
"version": "4.0.0",
"description": "A typescript-friendly node client to interact with Banana's machine learning inference APIs",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down

0 comments on commit 8ddc58d

Please sign in to comment.