-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
claude.php
218 lines (190 loc) · 7.62 KB
/
claude.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
/**
* Plugin Name: Claude 3.x Chat Interface
* Description: Adds a Claude AI chat interface to your WordPress site using a shortcode.
* Version: 1.0
* Author: Volkan Kücükbudak
*/
// Definiere die verfügbaren Modelle
define('CLAUDE_MODELS', [
'claude-3-haiku-20240307' => 'Claude 3 Haiku',
'claude-3-sonnet-20240229' => 'Claude 3 Sonnet',
'claude-3-opus-20240229' => 'Claude 3 Opus',
'claude-3-5-sonnet-20240620' => 'Claude 3.5 Sonnet'
]);
// Register settings
function claude_chat_register_settings() {
register_setting('claude_chat_options', 'claude_chat_api_key');
register_setting('claude_chat_options', 'claude_chat_model');
register_setting('claude_chat_options', 'claude_chat_temperature');
register_setting('claude_chat_options', 'claude_chat_max_tokens');
}
add_action('admin_init', 'claude_chat_register_settings');
// Enqueue necessary scripts and styles
function claude_chat_enqueue_scripts() {
wp_enqueue_style('claude-chat-style', plugin_dir_url(__FILE__) . 'css/claude-chat.css');
wp_enqueue_script('claude-chat-script', plugin_dir_url(__FILE__) . 'js/claude-chat.js', array('jquery'), '1.0', true);
wp_localize_script('claude-chat-script', 'claudeChat', array(
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('claude-chat-nonce')
));
}
add_action('wp_enqueue_scripts', 'claude_chat_enqueue_scripts');
// Shortcode to display the chat interface
function claude_chat_shortcode() {
ob_start();
?>
<div id="claude-chat-interface">
<div id="claude-chat-messages"></div>
<input type="text" id="claude-chat-input" placeholder="Ask Claude something...">
<button id="claude-chat-submit">Send</button>
</div>
<?php
return ob_get_clean();
}
add_shortcode('claude_chat', 'claude_chat_shortcode');
// AJAX handler for chat requests
function claude_chat_ajax_handler() {
check_ajax_referer('claude-chat-nonce', 'nonce');
$message = sanitize_text_field($_POST['message']);
$response = claude_chat_api_request($message);
if ($response) {
wp_send_json_success($response);
} else {
wp_send_json_error('Error: No response from API');
}
}
add_action('wp_ajax_claude_chat', 'claude_chat_ajax_handler');
add_action('wp_ajax_nopriv_claude_chat', 'claude_chat_ajax_handler');
// Claude API request function with logging
function claude_chat_api_request($message) {
$api_key = get_option('claude_chat_api_key');
$model = get_option('claude_chat_model');
$temperature = get_option('claude_chat_temperature');
$max_tokens = get_option('claude_chat_max_tokens');
// Verwende den richtigen API-Endpunkt
$url = 'https://api.anthropic.com/v1/messages';
$headers = array(
'Content-Type' => 'application/json',
'x-api-key' => $api_key,
'anthropic-version' => '2023-06-01', // Beispielhafte API-Version, an die tatsächliche Version anpassen
);
$body = array(
'model' => $model,
'max_tokens' => intval($max_tokens),
'messages' => array(
array(
'role' => 'user',
'content' => $message
)
)
);
$response = wp_remote_post($url, array(
'headers' => $headers,
'body' => json_encode($body),
'timeout' => 60,
));
if (is_wp_error($response)) {
claude_chat_log_error('HTTP Error', $response->get_error_message());
return 'Error: ' . $response->get_error_message();
}
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
if (isset($data['content'][0]['text'])) {
return $data['content'][0]['text'];
} elseif (isset($data['error'])) {
claude_chat_log_error('API Error', print_r($data, true));
return 'API Error: ' . $data['error']['message'];
} else {
claude_chat_log_error('Unknown Error', 'Unable to get a response from Claude API. Response: ' . print_r($data, true));
return 'Error: Unable to get a response from Claude API.';
}
}
// Logging function
function claude_chat_log_error($error_type, $error_message) {
$log_message = date('Y-m-d H:i:s') . " - $error_type: $error_message\n";
$log_file = plugin_dir_path(__FILE__) . 'claude-chat-error.log';
error_log($log_message, 3, $log_file);
}
// Add settings page
function claude_chat_settings_page() {
add_options_page('Claude Chat Settings', 'Claude Chat', 'manage_options', 'claude-chat-settings', 'claude_chat_settings_page_html');
}
add_action('admin_menu', 'claude_chat_settings_page');
// Settings page HTML
function claude_chat_settings_page_html() {
?>
<div class="wrap">
<h1><?php echo esc_html(get_admin_page_title()); ?></h1>
<form action="options.php" method="post">
<?php
settings_fields('claude_chat_options');
do_settings_sections('claude-chat-settings');
submit_button('Save Settings');
?>
</form>
</div>
<?php
}
// Initialize settings
function claude_chat_settings_init() {
add_settings_section(
'claude_chat_settings_section',
'Claude API Settings',
'claude_chat_settings_section_callback',
'claude-chat-settings'
);
add_settings_field(
'claude_chat_api_key',
'API Key',
'claude_chat_text_field_callback',
'claude-chat-settings',
'claude_chat_settings_section',
array('label_for' => 'claude_chat_api_key')
);
add_settings_field(
'claude_chat_model',
'Model',
'claude_chat_model_dropdown_callback',
'claude-chat-settings',
'claude_chat_settings_section',
array('label_for' => 'claude_chat_model')
);
add_settings_field(
'claude_chat_temperature',
'Temperature',
'claude_chat_number_field_callback',
'claude-chat-settings',
'claude_chat_settings_section',
array('label_for' => 'claude_chat_temperature', 'min' => 0, 'max' => 1, 'step' => 0.1)
);
add_settings_field(
'claude_chat_max_tokens',
'Max Tokens',
'claude_chat_number_field_callback',
'claude-chat-settings',
'claude_chat_settings_section',
array('label_for' => 'claude_chat_max_tokens', 'min' => 1, 'max' => 2048)
);
}
add_action('admin_init', 'claude_chat_settings_init');
function claude_chat_settings_section_callback($args) {
echo '<p>Enter your Claude API settings below:</p>';
}
function claude_chat_text_field_callback($args) {
$option = get_option($args['label_for']);
echo '<input type="text" id="' . esc_attr($args['label_for']) . '" name="' . esc_attr($args['label_for']) . '" value="' . esc_attr($option) . '" class="regular-text">';
}
function claude_chat_number_field_callback($args) {
$option = get_option($args['label_for']);
echo '<input type="number" id="' . esc_attr($args['label_for']) . '" name="' . esc_attr($args['label_for']) . '" value="' . esc_attr($option) . '" class="regular-text" min="' . esc_attr($args['min']) . '" max="' . esc_attr($args['max']) . '" step="' . (isset($args['step']) ? esc_attr($args['step']) : '1') . '">';
}
function claude_chat_model_dropdown_callback($args) {
$selected_model = get_option($args['label_for']);
echo '<select id="' . esc_attr($args['label_for']) . '" name="' . esc_attr($args['label_for']) . '" class="regular-text">';
foreach (CLAUDE_MODELS as $model_key => $model_name) {
$selected = ($selected_model == $model_key) ? 'selected="selected"' : '';
echo '<option value="' . esc_attr($model_key) . '" ' . $selected . '>' . esc_html($model_name) . '</option>';
}
echo '</select>';
}