|
| 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 Web Search", |
| 8 | + version: "0.0.1", |
| 9 | + key: "openai-chat-web-search", |
| 10 | + description: "Chat using the web search tool. [See the documentation](https://platform.openai.com/docs/guides/tools-web-search)", |
| 11 | + type: "action", |
| 12 | + props: { |
| 13 | + openai, |
| 14 | + modelId: { |
| 15 | + type: "string", |
| 16 | + label: "Model", |
| 17 | + description: "Model used to generate the response", |
| 18 | + default: "gpt-4o", |
| 19 | + options: [ |
| 20 | + "gpt-4o", |
| 21 | + "gpt-4o-mini", |
| 22 | + ], |
| 23 | + }, |
| 24 | + input: { |
| 25 | + type: "string", |
| 26 | + label: "Chat Input", |
| 27 | + description: "Text, image, or file inputs to the model, used to generate a response", |
| 28 | + }, |
| 29 | + instructions: { |
| 30 | + type: "string", |
| 31 | + label: "Instructions", |
| 32 | + description: "Inserts a system (or developer) message as the first item in the model's context", |
| 33 | + optional: true, |
| 34 | + }, |
| 35 | + previousResponseId: { |
| 36 | + type: "string", |
| 37 | + label: "Previous Response ID", |
| 38 | + description: "The unique ID of the previous response to the model. Use this to create multi-turn conversations", |
| 39 | + optional: true, |
| 40 | + }, |
| 41 | + truncation: { |
| 42 | + type: "string", |
| 43 | + label: "Truncation", |
| 44 | + description: "Specifies the truncation mode for the response if it's larger than the context window size", |
| 45 | + optional: true, |
| 46 | + default: "auto", |
| 47 | + options: [ |
| 48 | + "auto", |
| 49 | + "disabled", |
| 50 | + ], |
| 51 | + }, |
| 52 | + userLocation: { |
| 53 | + type: "string[]", |
| 54 | + label: "User Location", |
| 55 | + description: "Additional configuration for approximate location parameters for the search", |
| 56 | + optional: true, |
| 57 | + options: [ |
| 58 | + "City", |
| 59 | + "Region", |
| 60 | + "Country", |
| 61 | + "Timezone", |
| 62 | + ], |
| 63 | + }, |
| 64 | + searchContextSize: { |
| 65 | + type: "string", |
| 66 | + label: "Search Context Size", |
| 67 | + description: "Determines the amount of context to use during the web search", |
| 68 | + optional: true, |
| 69 | + default: "medium", |
| 70 | + options: [ |
| 71 | + "low", |
| 72 | + "medium", |
| 73 | + "high", |
| 74 | + ], |
| 75 | + }, |
| 76 | + responseFormat: { |
| 77 | + type: "string", |
| 78 | + label: "Response Format", |
| 79 | + 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).", |
| 80 | + options: [ |
| 81 | + "text", |
| 82 | + "json_schema", |
| 83 | + ], |
| 84 | + default: "text", |
| 85 | + optional: true, |
| 86 | + reloadProps: true, |
| 87 | + }, |
| 88 | + }, |
| 89 | + additionalProps() { |
| 90 | + const { |
| 91 | + responseFormat, |
| 92 | + userLocation, |
| 93 | + } = this; |
| 94 | + const props = {}; |
| 95 | + |
| 96 | + if (userLocation?.includes("City")) { |
| 97 | + props.city = { |
| 98 | + type: "string", |
| 99 | + label: "City", |
| 100 | + description: "Free text input for the city of the user, e.g. `San Francisco`", |
| 101 | + }; |
| 102 | + } |
| 103 | + |
| 104 | + if (userLocation?.includes("Region")) { |
| 105 | + props.region = { |
| 106 | + type: "string", |
| 107 | + label: "Region", |
| 108 | + description: "Free text input for the region of the user, e.g. `California`", |
| 109 | + }; |
| 110 | + } |
| 111 | + |
| 112 | + if (userLocation?.includes("Country")) { |
| 113 | + props.country = { |
| 114 | + type: "string", |
| 115 | + label: "Country", |
| 116 | + description: "The two-letter [ISO country code](https://en.wikipedia.org/wiki/ISO_3166-1) of the user, e.g. `US`", |
| 117 | + }; |
| 118 | + } |
| 119 | + |
| 120 | + if (userLocation?.includes("Timezone")) { |
| 121 | + props.timezone = { |
| 122 | + type: "string", |
| 123 | + label: "Timezone", |
| 124 | + description: "The [IANA timezone](https://timeapi.io/documentation/iana-timezones) of the user, e.g. `America/Los_Angeles`", |
| 125 | + }; |
| 126 | + } |
| 127 | + |
| 128 | + if (responseFormat === constants.CHAT_RESPONSE_FORMAT.JSON_SCHEMA.value) { |
| 129 | + props.jsonSchema = { |
| 130 | + type: "string", |
| 131 | + label: "JSON Schema", |
| 132 | + description: "Define the schema that the model's output must adhere to. [See the documentation here](https://platform.openai.com/docs/guides/structured-outputs/supported-schemas).", |
| 133 | + }; |
| 134 | + } |
| 135 | + |
| 136 | + return props; |
| 137 | + }, |
| 138 | + methods: { |
| 139 | + ...common.methods, |
| 140 | + }, |
| 141 | + async run({ $ }) { |
| 142 | + const data = { |
| 143 | + model: this.modelId, |
| 144 | + input: this.input, |
| 145 | + instructions: this.instructions, |
| 146 | + previous_response_id: this.previousResponseId, |
| 147 | + truncation: this.truncation, |
| 148 | + tools: [ |
| 149 | + { |
| 150 | + type: "web_search_preview", |
| 151 | + user_location: { |
| 152 | + type: "approximate", |
| 153 | + city: this.city, |
| 154 | + country: this.country, |
| 155 | + region: this.region, |
| 156 | + timezone: this.timezone, |
| 157 | + }, |
| 158 | + search_context_size: this.searchContextSize, |
| 159 | + }, |
| 160 | + ], |
| 161 | + }; |
| 162 | + |
| 163 | + const response = await this.openai.responses({ |
| 164 | + $, |
| 165 | + data, |
| 166 | + }); |
| 167 | + |
| 168 | + if (response) { |
| 169 | + $.export("$summary", `Successfully sent chat with id ${response.id}`); |
| 170 | + } |
| 171 | + |
| 172 | + return response; |
| 173 | + }, |
| 174 | +}; |
0 commit comments