Skip to content

Instantly share code, notes, and snippets.

@szmeku
Last active September 21, 2023 14:45
Show Gist options
  • Save szmeku/dc763ad40fe6769ac6173112c6329ab4 to your computer and use it in GitHub Desktop.
Save szmeku/dc763ad40fe6769ac6173112c6329ab4 to your computer and use it in GitHub Desktop.
import OpenAI from 'openai';
import secrets from '../secrets.json' assert {type: 'json'};
import {map, path, pipe} from "ramda";
const openai = new OpenAI({
apiKey: secrets.openai,
});
const functions = [
{
"name": "extracted_data_callback",
"parameters": {
"type": "object",
"required": ["events"],
"properties": {
"events": {
"type": "array",
"items": {
"type": "object",
"required": ["date", "time", "city", "venue", "eventName"],
"properties": {
"date": {
"type": "string",
},
"time": {
"type": "string",
},
"city": {
"type": "string",
"description": "city where event happens"
},
"venue": {
"type": "string",
"description": "name of the venue where event happens"
},
"eventName": {
"type": "string"
},
}
}
},
},
},
}
]
export const extractEventsFromStrings = pipe(
map(v => `"${v}"\n`),
v => openai.chat.completions.create({
model: "gpt-3.5-turbo-0613",
messages: [
{role: "system", "content": "You are helpful events from text extractor"},
{role: "user", "content": `From following strings extract events and run proper callback function\n ${v}`},
],
functions,
function_call: {name: "extracted_data_callback"},
// could be function_call: "auto" to let ai decide which function to call
})
.then(path(['choices', 0, 'message', 'function_call', 'arguments']))
.then(v => JSON.parse(v))
.catch((error) => {
console.log(error);
}),
)
// TEST
//
// const eventsStrings = [
// "Katowice nowy dekameron, 10:00 3 Stycznia, Yoga class",
// "Katowice nowy dekameron, 10:00 10 Stycznia, Games night"
// ]
//
// extractEventsFromStrings(eventsStrings)
// .then(console.log)
//
// OUTPUT
// {
// events: [
// {
// date: '3 Stycznia',
// time: '10:00',
// city: 'Katowice',
// venue: 'nowy dekameron',
// eventName: 'Yoga class'
// },
// {
// date: '10 Stycznia',
// time: '10:00',
// city: 'Katowice',
// venue: 'nowy dekameron',
// eventName: 'Games night'
// }
// ]
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment