|
| 1 | +import openai from "../../openai.app.mjs"; |
| 2 | +import common from "../common/common.mjs"; |
| 3 | +import constants from "../../common/constants.mjs"; |
| 4 | + |
| 5 | +export default { |
| 6 | + ...common, |
| 7 | + name: "Chat using File Search", |
| 8 | + version: "0.0.1", |
| 9 | + key: "openai-chat-using-file-search", |
| 10 | + description: "Chat with your files knowledge base (vector stores). [See the documentation](https://platform.openai.com/docs/guides/tools-file-search)", |
| 11 | + type: "action", |
| 12 | + props: { |
| 13 | + openai, |
| 14 | + alert: { |
| 15 | + type: "alert", |
| 16 | + alertType: "info", |
| 17 | + content: "To use this action, you need to have set up a knowledge base in a vector store and uploaded files to it. [More infomation here](https://platform.openai.com/docs/guides/tools-file-search?lang=javascript#overview).", |
| 18 | + }, |
| 19 | + modelId: { |
| 20 | + propDefinition: [ |
| 21 | + openai, |
| 22 | + "chatCompletionModelId", |
| 23 | + ], |
| 24 | + }, |
| 25 | + vectorStoreId: { |
| 26 | + propDefinition: [ |
| 27 | + openai, |
| 28 | + "vectorStoreId", |
| 29 | + ], |
| 30 | + description: "The identifier of a vector store. Currently supports only one vector store at a time", |
| 31 | + }, |
| 32 | + input: { |
| 33 | + type: "string", |
| 34 | + label: "Chat Input", |
| 35 | + description: "Text, image, or file inputs to the model, used to generate a response", |
| 36 | + }, |
| 37 | + instructions: { |
| 38 | + type: "string", |
| 39 | + label: "Instructions", |
| 40 | + description: "Inserts a system (or developer) message as the first item in the model's context", |
| 41 | + optional: true, |
| 42 | + }, |
| 43 | + includeSearchResults: { |
| 44 | + type: "boolean", |
| 45 | + label: "Include Search Results", |
| 46 | + description: "Include the search results in the response", |
| 47 | + default: false, |
| 48 | + optional: true, |
| 49 | + }, |
| 50 | + maxNumResults: { |
| 51 | + type: "integer", |
| 52 | + label: "Max Number of Results", |
| 53 | + description: "Customize the number of results you want to retrieve from the vector store", |
| 54 | + optional: true, |
| 55 | + }, |
| 56 | + metadataFiltering: { |
| 57 | + type: "boolean", |
| 58 | + label: "Metadata Filtering", |
| 59 | + description: "Configure how the search results are filtered based on file metadata", |
| 60 | + optional: true, |
| 61 | + reloadProps: true, |
| 62 | + }, |
| 63 | + previousResponseId: { |
| 64 | + type: "string", |
| 65 | + label: "Previous Response ID", |
| 66 | + description: "The unique ID of the previous response to the model. Use this to create multi-turn conversations", |
| 67 | + optional: true, |
| 68 | + }, |
| 69 | + truncation: { |
| 70 | + type: "string", |
| 71 | + label: "Truncation", |
| 72 | + description: "Specifies the truncation mode for the response if it's larger than the context window size", |
| 73 | + optional: true, |
| 74 | + default: "auto", |
| 75 | + options: [ |
| 76 | + "auto", |
| 77 | + "disabled", |
| 78 | + ], |
| 79 | + }, |
| 80 | + responseFormat: { |
| 81 | + type: "string", |
| 82 | + label: "Response Format", |
| 83 | + description: "Specify the format that the model must output. \n- **Text**: Returns unstructured text output.\n- **JSON Schema**: Enables you to define a [specific structure for the model's output using a JSON schema](https://platform.openai.com/docs/guides/structured-outputs?api-mode=responses).", |
| 84 | + options: [ |
| 85 | + "text", |
| 86 | + "json_schema", |
| 87 | + ], |
| 88 | + default: "text", |
| 89 | + optional: true, |
| 90 | + reloadProps: true, |
| 91 | + }, |
| 92 | + skipThisStep: { |
| 93 | + type: "boolean", |
| 94 | + label: "Skip This Step", |
| 95 | + description: "Pass in a boolean custom expression to skip this step's execution at runtime", |
| 96 | + optional: true, |
| 97 | + default: false, |
| 98 | + }, |
| 99 | + }, |
| 100 | + additionalProps() { |
| 101 | + const { |
| 102 | + modelId, |
| 103 | + metadataFiltering, |
| 104 | + responseFormat, |
| 105 | + } = this; |
| 106 | + const props = {}; |
| 107 | + |
| 108 | + if (this.openai.isReasoningModel(modelId)) { |
| 109 | + props.reasoningEffort = { |
| 110 | + type: "string", |
| 111 | + label: "Reasoning Effort", |
| 112 | + description: "Constrains effort on reasoning for reasoning models", |
| 113 | + optional: true, |
| 114 | + options: [ |
| 115 | + "low", |
| 116 | + "medium", |
| 117 | + "high", |
| 118 | + ], |
| 119 | + }; |
| 120 | + |
| 121 | + // aparrently not supported yet as of 12/march/2025 |
| 122 | + // props.generateSummary = { |
| 123 | + // type: "string", |
| 124 | + // label: "Generate Reasoning Summary", |
| 125 | + // description: "A summary of the reasoning performed by the model", |
| 126 | + // optional: true, |
| 127 | + // options: [ |
| 128 | + // "concise", |
| 129 | + // "detailed", |
| 130 | + // ], |
| 131 | + // }; |
| 132 | + } |
| 133 | + |
| 134 | + // TODO: make this configuration user-friendly |
| 135 | + // https://platform.openai.com/docs/guides/retrieval?attributes-filter-example=region#attribute-filtering |
| 136 | + if (metadataFiltering) { |
| 137 | + props.filters = { |
| 138 | + type: "object", |
| 139 | + label: "Filters", |
| 140 | + description: "Filter the search results based on file metadata. [See the documentation here](https://platform.openai.com/docs/guides/retrieval#attribute-filtering)", |
| 141 | + }; |
| 142 | + } |
| 143 | + |
| 144 | + if (responseFormat === constants.CHAT_RESPONSE_FORMAT.JSON_SCHEMA.value) { |
| 145 | + props.jsonSchema = { |
| 146 | + type: "string", |
| 147 | + label: "JSON Schema", |
| 148 | + description: "Define the schema that the model's output must adhere to. [Generate one here](https://platform.openai.com/docs/guides/structured-outputs/supported-schemas).", |
| 149 | + }; |
| 150 | + } |
| 151 | + |
| 152 | + return props; |
| 153 | + }, |
| 154 | + methods: { |
| 155 | + ...common.methods, |
| 156 | + }, |
| 157 | + async run({ $ }) { |
| 158 | + if (this.skipThisStep) { |
| 159 | + $.export("$summary", "Step execution skipped"); |
| 160 | + return; |
| 161 | + } |
| 162 | + |
| 163 | + const data = { |
| 164 | + model: this.modelId, |
| 165 | + input: this.input, |
| 166 | + instructions: this.instructions, |
| 167 | + previous_response_id: this.previousResponseId, |
| 168 | + truncation: this.truncation, |
| 169 | + tools: [ |
| 170 | + { |
| 171 | + type: "file_search", |
| 172 | + vector_store_ids: [ |
| 173 | + this.vectorStoreId, |
| 174 | + ], |
| 175 | + max_num_results: this.maxNumResults, |
| 176 | + }, |
| 177 | + ], |
| 178 | + }; |
| 179 | + |
| 180 | + if (this.includeSearchResults) { |
| 181 | + data.include = [ |
| 182 | + "output[*].file_search_call.search_results", |
| 183 | + ]; |
| 184 | + } |
| 185 | + |
| 186 | + if (this.filters) { |
| 187 | + data.tools[0].filters = this.filters; |
| 188 | + } |
| 189 | + |
| 190 | + if (this.openai.isReasoningModel(this.modelId) && this.reasoningEffort) { |
| 191 | + data.reasoning = { |
| 192 | + ...data.reasoning, |
| 193 | + effort: this.reasoningEffort, |
| 194 | + }; |
| 195 | + } |
| 196 | + |
| 197 | + if (this.openai.isReasoningModel(this.modelId) && this.generateSummary) { |
| 198 | + data.reasoning = { |
| 199 | + ...data.reasoning, |
| 200 | + generate_summary: this.generateSummary, |
| 201 | + }; |
| 202 | + } |
| 203 | + |
| 204 | + if (this.responseFormat === constants.CHAT_RESPONSE_FORMAT.JSON_SCHEMA.value) { |
| 205 | + try { |
| 206 | + data.text = { |
| 207 | + format: { |
| 208 | + type: this.responseFormat, |
| 209 | + ...JSON.parse(this.jsonSchema), |
| 210 | + }, |
| 211 | + }; |
| 212 | + } catch (error) { |
| 213 | + throw new Error("Invalid JSON format in the provided JSON Schema"); |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + const response = await this.openai.responses({ |
| 218 | + $, |
| 219 | + data, |
| 220 | + }); |
| 221 | + |
| 222 | + if (response) { |
| 223 | + $.export("$summary", `Successfully sent chat with id ${response.id}`); |
| 224 | + $.export("chat_responses", response.output); |
| 225 | + } |
| 226 | + |
| 227 | + return response; |
| 228 | + }, |
| 229 | +}; |
0 commit comments