Skip to content

Commit

Permalink
feat: extract structured data
Browse files Browse the repository at this point in the history
  • Loading branch information
abuaboud committed May 9, 2024
1 parent 30ac6a1 commit 614a172
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/pieces/community/openai/package.json
@@ -1,4 +1,4 @@
{
"name": "@activepieces/piece-openai",
"version": "0.3.23"
"version": "0.3.24"
}
Expand Up @@ -51,26 +51,20 @@ export const extractStructuredDataAction = createAction({
displayName: 'Unstructured Text',
required: true,
}),
prompt: Property.LongText({
displayName: 'Prompt',
description:
'Provide a brief description of what sort of data you want extracted from the unstructured text.',
required: true,
}),
params: Property.Array({
displayName: 'Structured Data Definition',
displayName: 'Data Definition',
required: true,
properties: {
propName: Property.ShortText({
displayName: 'Name',
description:
'Provide the name of the values you want to extract from the unstructured text. Name should be unique and short. ',
'Provide the name of the value you want to extract from the unstructured text. The name should be unique and short. ',
required: true,
}),
propDescription: Property.LongText({
displayName: 'Description',
description:
'Brief description of the parameter, defining what data will be extracted to this parameter.',
'Brief description of the data, this hints for the AI on what to look for',
required: false,
}),
propDataType: Property.StaticDropdown({
Expand All @@ -88,29 +82,28 @@ export const extractStructuredDataAction = createAction({
},
}),
propIsRequired: Property.Checkbox({
displayName: 'Is Property Required?',
description: 'If the property must be present, the action will fail if it is not found.',
displayName: 'Fail if Not present?',
required: true,
defaultValue: true,
defaultValue: false,
}),
},
}),
},
async run(context) {
const { model, text, prompt } = context.propsValue;
const { model, text } = context.propsValue;
const paramInputArray = context.propsValue.params as ParamInput[];
const functionParams: Record<string, unknown> = {};
const requiredFunctionParams: string[] = [];
for (const param of paramInputArray) {
functionParams[param.propName] = {
type: param.propDataType,
description: param.propDescription,
description: param.propDescription ?? param.propName,
};
if (param.propIsRequired) {
requiredFunctionParams.push(param.propName);
}
}

const prompt = 'Extract the following data from the provided text'
const openai = new OpenAI({
apiKey: context.auth,
});
Expand Down Expand Up @@ -138,8 +131,8 @@ export const extractStructuredDataAction = createAction({
if (toolCallsResponse) {
return JSON.parse(toolCallsResponse[0].function.arguments);
} else {
throw Error(JSON.stringify({
message: 'Unable to extract data. Please provide valid params and text.'
throw new Error(JSON.stringify({
message: "OpenAI couldn't extract the fields from the above text."
}));
}
},
Expand Down

0 comments on commit 614a172

Please sign in to comment.