@@ -22,6 +22,43 @@ function openai_autoload_info() {
2222 ];
2323}
2424
25+ /**
26+ * Implements hook_init().
27+ */
28+ function openai_init() {
29+ // Check if the current path is an admin path.
30+ if (path_is_admin(current_path())) {
31+ // Safely retrieve the API key configuration.
32+ $api_key_config = config('openai.settings')->get('api_key');
33+
34+ // Check if the Key module is enabled.
35+ if (module_exists('key')) {
36+ // Validate the API key if a configuration value exists.
37+ $api_key = !empty($api_key_config) ? key_get_key_value($api_key_config) : NULL;
38+
39+ if (empty($api_key)) {
40+ $message = t('You have not provided an OpenAI API key yet. This is required for its functionality to work. Please obtain an API key from <a href="@account" target="_blank">your OpenAI account</a> and add it to the <a href="@settings">OpenAI settings configuration here</a>.',
41+ [
42+ '@account' => 'https://platform.openai.com/',
43+ '@settings' => url('admin/config/openai/settings'),
44+ ]
45+ );
46+ backdrop_set_message($message, 'error');
47+ }
48+ }
49+ else {
50+ // Key module is not enabled, so the OpenAI module cannot work.
51+ $message = t('The Key module is not enabled. The OpenAI module requires the Key module to securely manage API keys. Please enable it from the <a href="@modules">modules page</a>.',
52+ [
53+ '@modules' => url('admin/modules'),
54+ ]
55+ );
56+ backdrop_set_message($message, 'error');
57+ }
58+ }
59+ }
60+
61+
2562/**
2663 * Implements hook_menu().
2764 */
@@ -65,6 +102,13 @@ function openai_settings_form($form, &$form_state) {
65102 $form = [];
66103 $form['#config'] = 'openai.settings';
67104
105+ // Fetch available keys from the Key module.
106+ $available_keys = key_get_key_names_as_options();
107+
108+ // Default to all keys if no specific key is set.
109+ $default_api_key = config_get('openai.settings', 'api_key') ?: '';
110+ $default_api_org = config_get('openai.settings', 'api_org') ?: '';
111+
68112 // Collapsible fieldset for API settings.
69113 $form['api_settings'] = [
70114 '#type' => 'fieldset',
@@ -76,21 +120,21 @@ function openai_settings_form($form, &$form_state) {
76120 $form['api_settings']['api_key'] = [
77121 '#type' => 'key_select',
78122 '#title' => t('OpenAI API Key'),
79- '#default_value' => config_get('openai.settings', 'api_key') ,
80- '#options' => key_get_key_names_as_options() ,
123+ '#default_value' => $default_api_key ,
124+ '#options' => $available_keys ,
81125 '#key_filters' => ['type' => 'authentication'],
82126 '#description' => t(
83127 'Select the API key to use for accessing OpenAI services. Keys are managed through the <a href="/admin/config/system/keys">Key module</a>.'
84128 ),
85- '#required' => TRUE ,
129+ '#required' => FALSE ,
86130 ];
87131
88132 // Organization ID field (optional).
89133 $form['api_settings']['api_org'] = [
90134 '#type' => 'key_select',
91135 '#title' => t('Organization ID'),
92- '#default_value' => config_get('openai.settings', 'api_org') ,
93- '#options' => key_get_key_names_as_options() ,
136+ '#default_value' => $default_api_org ,
137+ '#options' => $available_keys ,
94138 '#key_filters' => ['type' => 'authentication'],
95139 '#description' => t(
96140 'The organization ID on your OpenAI account. This is required for some OpenAI services to work correctly.'
@@ -104,24 +148,32 @@ function openai_settings_form($form, &$form_state) {
104148 '#submit' => ['openai_settings_form_submit'],
105149 ];
106150
107- // Fetch available models from OpenAI.
108- $models = openai_fetch_available_models();
109-
110- if (!empty($models)) {
111- $model_list = '<ul>';
112- foreach ($models as $model) {
113- $model_list .= '<li>' . check_plain($model) . '</li>';
151+ // Fetch available models only if the API key is configured.
152+ if (!empty($default_api_key)) {
153+ $models = openai_fetch_available_models($default_api_key);
154+
155+ if (!empty($models)) {
156+ $model_list = '<ul>';
157+ foreach ($models as $model) {
158+ $model_list .= '<li>' . check_plain($model) . '</li>';
159+ }
160+ $model_list .= '</ul>';
161+ $form['models'] = [
162+ '#type' => 'markup',
163+ '#markup' => t('<h3>Available Models</h3>' . $model_list),
164+ ];
165+ }
166+ else {
167+ $form['models'] = [
168+ '#type' => 'markup',
169+ '#markup' => t('Failed to fetch models or no models available.'),
170+ ];
114171 }
115- $model_list .= '</ul>';
116- $form['models'] = [
117- '#type' => 'markup',
118- '#markup' => t('<h3>Available Models</h3>' . $model_list),
119- ];
120172 }
121173 else {
122174 $form['models'] = [
123175 '#type' => 'markup',
124- '#markup' => t('Failed to fetch models or no models available.'),
176+ '#markup' => t('Configure your API key to view available models .'),
125177 ];
126178 }
127179
@@ -179,4 +231,3 @@ function openai_fetch_available_models() {
179231 return [];
180232 }
181233}
182-
0 commit comments