Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bumped to v4 endpoints, removed feedback, and added /start return pos… #1

Merged
merged 2 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you check what happens if "finished" doesn't exist? will this crash?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i.e assuming runMain is called for older sdks too can this ever not return the key properly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if /start doesn't return it for some reason, the SDK falls back on our /check endpoint which doesn't use this "finished" logic as a break condition.

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