File size: 6,773 Bytes
5f32fbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php

namespace App\Http\Controllers\Chatbot;

use App\Helpers\Classes\Helper;
use App\Http\Controllers\Controller;
use App\Http\Requests\Chatbot\ChatbotRequest;
use App\Http\Requests\Chatbot\ChatbotSettingRequest;
use App\Models\Chatbot\Chatbot;
use App\Models\Chatbot\ChatbotData;
use App\Models\Chatbot\ChatbotDataVector;
use App\Models\SettingTwo;
use Illuminate\Contracts\View\View;

class ChatbotController extends Controller
{
    public function setting()
    {
        return view('panel.admin.chatbot.setting', [
            'title'       => trans('Floating Chat Settings'),
            'method'      => 'post',
            'action'      => route('dashboard.admin.chatbot.setting'),
            'chatbotData' => Chatbot::query()->get(),
            'chatbot'     => Chatbot::query()->find(Helper::settingTwo('chatbot_template')),
        ]);
    }

    public function externalChatSettings(): View
    {
        return view('panel.admin.chatbot.external-settings', [
            'title'       => trans('External Chat Settings'),
            'method'      => 'post',
            'action'      => route('dashboard.admin.chatbot.external_settings'),
            'chatbotData' => Chatbot::query()->get(),
            'chatbot'     => Chatbot::query()->find(Helper::settingTwo('chatbot_template')),
        ]);
    }

    public function putSetting(ChatbotSettingRequest $request)
    {
        if (Helper::appIsDemo()) {
            return back()->with([
                'type'    => 'error',
                'message' => trans('This feature is disabled in demo mode.'),
            ]);
        }

        $setting = SettingTwo::first();
        $setting->chatbot_status = $request->chatbot_status;
        $setting->chatbot_template = $request->chatbot_template;
        $setting->chatbot_position = $request->chatbot_position;
        $setting->chatbot_login_require = (bool) $request->chatbot_login_require;
        $setting->chatbot_rate_limit = $request->chatbot_rate_limit;
        $setting->chatbot_show_timestamp = (bool) $request->chatbot_show_timestamp;
        $setting->save();

        if ($request->chatbot_template && $request->first_message && $request->instructions) {
            $template = Chatbot::query()->find($request->chatbot_template);

            $template->update([
                'user_id'       => auth()->id(),
                'first_message' => $request->first_message,
                'instructions'  => $request->instructions,
            ]);
        }

        return back()->with([
            'type'    => 'success',
            'message' => trans('Chatbot settings updated successfully'),
        ]);
    }

    public function index()
    {
        return view('panel.admin.chatbot.index', [
            'title' => trans('Chatbot Training'),
            'items' => Chatbot::query()->paginate(10),
        ]);
    }

    public function create()
    {
        $default_engine = setting('default_ai_engine', 'openai');
        switch ($default_engine) {
            case 'openai':
                $default_model = Helper::setting('openai_default_model');

                break;
            case 'anthropic':
                $default_model = setting('anthropic_default_model', 'claude-3-opus-20240229');

                break;
            case 'gemini':
                $default_model = setting('gemini_default_model', 'gemini-pro');

                break;
            default:
                $default_model = Helper::setting('openai_default_model');

                break;
        }

        $item = Chatbot::query()->create([
            'title' => 'Untitled chatbot',
            'model' => $default_model,
        ]);

        return to_route('dashboard.admin.chatbot.show', $item->getAttribute('id'));
    }

    public function store(ChatbotRequest $request)
    {
        if (Helper::appIsDemo()) {
            return back()->with([
                'type'    => 'error',
                'message' => trans('This feature is disabled in demo mode.'),
            ]);
        }

        $data = $request->validated();

        if ($request->hasFile('logo')) {
            $data['image'] = $request->file('logo')->store('chatbot');
        }

        Chatbot::query()->create($data);

        return to_route('dashboard.admin.chatbot.index')->with('success', trans('Chatbot Template Created Successfully'));
    }

    public function show(Chatbot $chatbot)
    {
        return view('panel.admin.chatbot.training', [
            'title'  => trans('Chatbot Training'),
            'item'   => $chatbot,
            'data'   => $chatbot->data()->get(),
            'action' => route('dashboard.admin.chatbot.update', $chatbot),
        ]);
    }

    public function edit(Chatbot $chatbot)
    {
        return view('panel.admin.chatbot.form', [
            'title'  => trans('Edit Chatbot'),
            'method' => 'put',
            'action' => route('dashboard.admin.chatbot.update', $chatbot),
            'item'   => $chatbot,
        ]);
    }

    public function update(ChatbotRequest $request, Chatbot $chatbot)
    {
        if (Helper::appIsDemo()) {
            if ($request->ajax()) {
                return response()->json([
                    'status'  => 'error',
                    'message' => trans('This feature is disabled in demo mode.'),
                ]);
            }

            return back()->with([
                'type'    => 'error',
                'message' => trans('This feature is disabled in demo mode.'),
            ]);
        }

        $data = $request->validated();

        if ($request->hasFile('logo')) {
            $data['image'] = $request->file('logo')->store('chatbot');
        }

        $chatbot->update($data);

        if ($request->ajax()) {
            return response()->json([
                'message' => trans('Chatbot Updated Successfully'),
                'status'  => 'success',
            ]);
        }

        return back()->with([
            'type'    => 'success',
            'message' => trans('Chatbot Updated Successfully'),
        ]);

    }

    public function destroy(Chatbot $chatbot)
    {
        if (Helper::appIsDemo()) {
            return response()->json([
                'status'  => 'error',
                'message' => trans('This feature is disabled in demo mode.'),
            ]);
        }

        $chatbotId = $chatbot->getAttribute('id');

        ChatbotData::query()->where('chatbot_id', $chatbotId)->delete();

        ChatbotDataVector::query()->where('chatbot_id', $chatbotId)->delete();

        $chatbot->delete();

        return response()->json([
            'message'    => trans('Chatbot Deleted Successfully'),
            'reload'     => true,
            'setTimeOut' => 1000,
        ]);
    }
}