Last active
May 22, 2023 12:05
-
-
Save houshuang/cafda00d5104fdcc73004c3288514c55 to your computer and use it in GitHub Desktop.
Process flight confirmation email and send to Tana
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require("express"); | |
var http = require("http"); | |
var app = express(); | |
const prompt = (context) => ` | |
TASK: Extract information from CONTEXT | |
OUTPUT FORMAT: as a JSON structure following the TYPESCRIPT DEFINITION. Output only a valid JSON structure. | |
type Node = { | |
name: string; | |
description?: string; | |
supertags: [{ | |
/* flight */ | |
id: '-rbfHN6vLO' | |
}]; | |
children: [ | |
{ | |
/* From airport */ | |
type: 'field'; | |
attributeId: '9qvRZHUu4x'; | |
children: Node[]; | |
}, | |
{ | |
/* To airport */ | |
type: 'field'; | |
attributeId: 'rnGO8-Y_X8'; | |
children: Node[]; | |
}, | |
{ | |
/* Departure time */ | |
type: 'field'; | |
attributeId: 'PMJLTUNy5u'; | |
children: Node[]; | |
}, | |
{ | |
/* Arrival time */ | |
type: 'field'; | |
attributeId: 'wlEsLmTUyk'; | |
children: Node[]; | |
}, | |
{ | |
/* Passenger */ | |
type: 'field'; | |
attributeId: 'O6GmuxZGFM'; | |
children: Node[]; | |
}, | |
{ | |
/* Price */ | |
type: 'field'; | |
attributeId: 'dyO-DGjBEC'; | |
children: Node[]; | |
}, | |
]; | |
}; | |
CONTEXT: ${context} | |
OUTPUT: | |
{`; | |
app.use(express.json({ limit: "200mb" })); | |
//listener | |
app.post("/", async function(req, res) { | |
const body = req.body.plain.replace(/http(.+?)\s/g, "").slice(0, 6000); | |
const promptString = prompt(body); | |
console.log("SENDING OPENAI REQUEST", promptString); | |
const result = await fetch("https://api.openai.com/v1/chat/completions", { | |
method: "POST", | |
headers: { | |
"Content-Type": "application/json", | |
Authorization: `Bearer OPENAI API TOKEN | |
`, | |
}, | |
body: JSON.stringify({ | |
messages:[ {role: 'user', content: promptString}], | |
temperature: 0, | |
max_tokens: 1000, | |
model: "gpt-3.5-turbo", | |
}), | |
}).then(res => { | |
if (res.ok) { | |
return res.json() | |
} | |
return res.text().then(text => { throw new Error(text) }) | |
}) | |
console.log(result) | |
const jsonstring = '{' + result.choices[0].message.content | |
console.log(jsonstring) | |
const payload = JSON.parse(jsonstring) | |
console.log(payload) | |
const tanares = await fetch("https://europe-west1-tagr-prod.cloudfunctions.net/addToNodeV2", { | |
method: "POST", | |
headers: { | |
"Content-Type": "application/json", | |
Authorization: `Bearer TANA API TOKEN`, | |
}, | |
body: JSON.stringify({nodes: [payload]}) | |
}).then(res => { | |
if (res.ok) { | |
return res.json() | |
} | |
return res.text().then(text => { throw new Error(text) }) | |
}) | |
res.writeHead(200); | |
res.end(); | |
}); | |
app.get("/", function(req, res) { | |
res.writeHead(200); | |
res.write("Hi"); | |
res.end(); | |
}); | |
http.createServer(app).listen(3000); | |
console.log("AI email processor ready"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment