Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4636 from kishanprmr/openai-extract-data
feat(openai): Extract Data from Text Action
- Loading branch information
Showing
3 changed files
with
222 additions
and
60 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"name": "@activepieces/piece-openai", | ||
"version": "0.3.22" | ||
"version": "0.3.23" | ||
} |
This file contains 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
153 changes: 153 additions & 0 deletions
153
packages/pieces/community/openai/src/lib/actions/extract-structure-data.action.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
import { openaiAuth } from '../../'; | ||
import { createAction, Property } from '@activepieces/pieces-framework'; | ||
import OpenAI from 'openai'; | ||
import { notLLMs } from '../common/common'; | ||
|
||
export const extractStructuredDataAction = createAction({ | ||
auth: openaiAuth, | ||
name: 'extract-structured-data', | ||
displayName: 'Extract Structured Data from Text', | ||
description: 'Returns structured data from provided unstructured text.', | ||
props: { | ||
model: Property.Dropdown({ | ||
displayName: 'Model', | ||
required: true, | ||
refreshers: [], | ||
defaultValue: 'gpt-3.5-turbo', | ||
options: async ({ auth }) => { | ||
if (!auth) { | ||
return { | ||
disabled: true, | ||
placeholder: 'Enter your API key first', | ||
options: [], | ||
}; | ||
} | ||
try { | ||
const openai = new OpenAI({ | ||
apiKey: auth as string, | ||
}); | ||
const response = await openai.models.list(); | ||
// We need to get only LLM models | ||
const models = response.data.filter((model) => !notLLMs.includes(model.id)); | ||
return { | ||
disabled: false, | ||
options: models.map((model) => { | ||
return { | ||
label: model.id, | ||
value: model.id, | ||
}; | ||
}), | ||
}; | ||
} catch (error) { | ||
return { | ||
disabled: true, | ||
options: [], | ||
placeholder: "Couldn't load models, API key is invalid", | ||
}; | ||
} | ||
}, | ||
}), | ||
text: Property.LongText({ | ||
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', | ||
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. ', | ||
required: true, | ||
}), | ||
propDescription: Property.LongText({ | ||
displayName: 'Description', | ||
description: | ||
'Brief description of the parameter, defining what data will be extracted to this parameter.', | ||
required: false, | ||
}), | ||
propDataType: Property.StaticDropdown({ | ||
displayName: 'Data Type', | ||
description: 'Type of parameter.', | ||
required: true, | ||
defaultValue: 'string', | ||
options: { | ||
disabled: false, | ||
options: [ | ||
{ label: 'Text', value: 'string' }, | ||
{ label: 'Number', value: 'number' }, | ||
{ label: 'Boolean', value: 'boolean' }, | ||
], | ||
}, | ||
}), | ||
propIsRequired: Property.Checkbox({ | ||
displayName: 'Is Property Required?', | ||
description: 'If the property must be present, the action will fail if it is not found.', | ||
required: true, | ||
defaultValue: true, | ||
}), | ||
}, | ||
}), | ||
}, | ||
async run(context) { | ||
const { model, text, prompt } = 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, | ||
}; | ||
if (param.propIsRequired) { | ||
requiredFunctionParams.push(param.propName); | ||
} | ||
} | ||
|
||
const openai = new OpenAI({ | ||
apiKey: context.auth, | ||
}); | ||
|
||
const response = await openai.chat.completions.create({ | ||
model: model, | ||
messages: [{ role: 'user', content: text }], | ||
tools: [ | ||
{ | ||
type: 'function', | ||
function: { | ||
name: 'extract_structured_data', | ||
description: prompt, | ||
parameters: { | ||
type: 'object', | ||
properties: functionParams, | ||
required: requiredFunctionParams, | ||
}, | ||
}, | ||
}, | ||
], | ||
}); | ||
|
||
const toolCallsResponse = response.choices[0].message.tool_calls; | ||
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.' | ||
})); | ||
} | ||
}, | ||
}); | ||
|
||
interface ParamInput { | ||
propName: string; | ||
propDescription: string; | ||
propDataType: string; | ||
propIsRequired: boolean; | ||
} |