Skip to content
This repository was archived by the owner on Jan 5, 2025. It is now read-only.

Commit 79887af

Browse files
authored
Merge pull request #80 from openchatai/release/v0.3.0
Release/v0.3.0
2 parents 9b7fd9b + f68c396 commit 79887af

File tree

72 files changed

+5548
-2133
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+5548
-2133
lines changed

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,13 @@ install:
2626
@sleep 20
2727

2828
@echo "$(COLOR_BOLD)=== Clearing backend server config cache ===$(COLOR_RESET)"
29+
$(DOCKER_COMPOSE) exec backend-server php artisan cache:clear
2930
$(DOCKER_COMPOSE) exec backend-server php artisan config:cache
3031

32+
@echo "$(COLOR_BOLD)=== Running backward compatibility scripts ===$(COLOR_RESET)"
33+
$(DOCKER_COMPOSE) exec backend-server php artisan prompt:fill
34+
35+
3136
@echo "$(COLOR_BOLD)=== Run backend server server migrations ===$(COLOR_RESET)"
3237
$(DOCKER_COMPOSE) exec backend-server php artisan migrate --seed
3338
$(DOCKER_COMPOSE) exec backend-server php artisan storage:link
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use App\Models\Chatbot;
6+
use App\Models\User;
7+
use Illuminate\Console\Command;
8+
use Illuminate\Support\Str;
9+
10+
class FillInitialPromptCommand extends Command
11+
{
12+
protected $signature = 'prompt:fill';
13+
14+
protected $description = 'Fill initial prompt for all chatbots';
15+
16+
public function __construct()
17+
{
18+
parent::__construct();
19+
}
20+
21+
public function handle()
22+
{
23+
$initialPrompt = "You are a helpful AI customer support agent. Use the following pieces of context to answer the question at the end.
24+
If you don't know the answer, just say you don't know. DO NOT try to make up an answer.
25+
If the question is not related to the context, politely respond that you are tuned to only answer questions that are related to the context.
26+
27+
{context}
28+
29+
Question: {question}
30+
Helpful answer in markdown:";
31+
32+
$chatbots = Chatbot::where('prompt_message', null)->get();
33+
34+
foreach ($chatbots as $chatbot) {
35+
$chatbot->setPromptMessage($initialPrompt);
36+
$chatbot->save();
37+
38+
$this->info("Chatbot: " . $chatbot->getId() . " initial prompt got updated updated");
39+
}
40+
}
41+
}

backend-server/app/Http/Controllers/AuthController.php

Lines changed: 0 additions & 94 deletions
This file was deleted.

backend-server/app/Http/Controllers/ChatbotController.php

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
use App\Http\Responses\ChatbotResponse;
1414
use App\Http\Services\HandlePdfDataSource;
1515
use App\Models\Chatbot;
16+
use App\Models\ChatHistory;
1617
use App\Models\CodebaseDataSource;
1718
use Illuminate\Http\JsonResponse;
1819
use Illuminate\Http\RedirectResponse;
20+
use Illuminate\Support\Facades\Cookie;
1921
use Illuminate\Support\Facades\Http;
2022
use Illuminate\Support\Str;
2123
use Ramsey\Uuid\Uuid;
@@ -137,6 +139,7 @@ public function sendMessage(SendChatMessageRequest $request, $token): JsonRespon
137139
$question = $request->getMessage();
138140
$history = $request->getHistory();
139141
$mode = $request->getMode();
142+
$initialPrompt = $bot->getPromptMessage();
140143

141144
// Remove null and empty values and empty arrays or objects from the history
142145
$history = array_filter($history, function ($value) {
@@ -148,7 +151,8 @@ public function sendMessage(SendChatMessageRequest $request, $token): JsonRespon
148151
'question' => $question,
149152
'history' => $history,
150153
'namespace' => $bot->getId()->toString(),
151-
'mode' => $mode,
154+
'mode' => $mode,
155+
'initial_prompt' => $initialPrompt,
152156
]);
153157

154158
if ($response->failed()) {
@@ -160,6 +164,28 @@ public function sendMessage(SendChatMessageRequest $request, $token): JsonRespon
160164
// Create a ChatbotResponse instance from the API response
161165
$botResponse = new ChatbotResponse($response->json());
162166

167+
168+
$sessionId = Cookie::get('chatbot_' . $bot->getId()->toString());
169+
170+
if (!is_null($sessionId)) {
171+
$history = new ChatHistory();
172+
$history->setId(Uuid::uuid4());
173+
$history->setChatbotId($bot->getId());
174+
$history->setFromUser();
175+
$history->setMessage($question);
176+
$history->setSessionId($sessionId);
177+
$history->save();
178+
179+
$history = new ChatHistory();
180+
$history->setId(Uuid::uuid4());
181+
$history->setChatbotId($bot->getId());
182+
$history->setFromBot();
183+
$history->setMessage($botResponse->getBotReply());
184+
$history->setSessionId($sessionId);
185+
$history->save();
186+
}
187+
188+
163189
// Return the response from the chatbot
164190
return response()->json([
165191
'botReply' => $botResponse->getBotReply(),
@@ -178,7 +204,13 @@ public function getChatView($token)
178204
// Find the chatbot by token
179205
$bot = Chatbot::where('token', $token)->firstOrFail();
180206

181-
// Render the chat view with the chatbot data
207+
// initiate a cookie if it doesn't exist
208+
$cookieName = 'chatbot_' . $bot->getId()->toString();
209+
if (!Cookie::has($cookieName)) {
210+
$cookieValue = Str::random(20);
211+
Cookie::queue($cookieName, $cookieValue, 60 * 24 * 365);
212+
}
213+
182214
return view('chat', [
183215
'bot' => $bot,
184216
]);
@@ -199,7 +231,6 @@ public function createViaCodebaseFlow(CreateChatbotViaCodebaseRequest $request):
199231
$datasource->setRepository($request->getRepoUrl());
200232
$datasource->save();
201233

202-
203234
event(new CodebaseDataSourceWasAdded(
204235
$chatbot->getId(),
205236
$datasource->getId())

backend-server/app/Http/Controllers/ChatbotSettingController.php

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@
22

33
namespace App\Http\Controllers;
44

5+
use App\Http\Enums\ChatBotInitialPromptEnum;
56
use App\Models\Chatbot;
7+
use App\Models\ChatHistory;
8+
use Illuminate\Http\RedirectResponse;
69
use Illuminate\Http\Request;
10+
use Illuminate\Support\Facades\DB;
711
use Illuminate\Validation\ValidationException;
812

913
class ChatbotSettingController extends Controller
1014
{
1115
/**
1216
* Display the general settings page for a chatbot.
1317
*
14-
* @param string $id
18+
* @param string $id
1519
* @return \Illuminate\View\View
1620
*/
1721
public function generalSettings($id)
@@ -24,14 +28,16 @@ public function generalSettings($id)
2428
]);
2529
}
2630

31+
public function deleteBot($id): RedirectResponse
32+
{
33+
$bot = Chatbot::where('id', $id)->firstOrFail();
34+
$bot->delete();
35+
36+
return redirect()->route('index')->with('success', 'Bot deleted!');
37+
}
38+
2739
/**
28-
* Update the general settings for a chatbot.
29-
*
30-
* @param \Illuminate\Http\Request $request
31-
* @param string $id
32-
* @return \Illuminate\Http\RedirectResponse
33-
*
34-
* @throws \Illuminate\Validation\ValidationException
40+
* @throws ValidationException
3541
*/
3642
public function generalSettingsUpdate(Request $request, $id)
3743
{
@@ -45,18 +51,45 @@ public function generalSettingsUpdate(Request $request, $id)
4551

4652
// Update the chatbot name
4753
$bot->setName($request->input('name'));
54+
$bot->setPromptMessage($request->input('prompt_message', ChatBotInitialPromptEnum::AI_ASSISTANT_INITIAL_PROMPT));
4855
$bot->save();
4956

5057
return redirect()->route('chatbot.settings', ['id' => $bot->getId()])->with('success', 'Settings updated!');
5158
}
5259

53-
/**
54-
* Display the data settings page for a chatbot.
55-
*
56-
* @param \Illuminate\Http\Request $request
57-
* @param string $id
58-
* @return \Illuminate\View\View
59-
*/
60+
public function historySettings(Request $request, $id)
61+
{
62+
/** @var Chatbot $bot */
63+
$bot = Chatbot::where('id', $id)->firstOrFail();
64+
65+
$chatHistory = ChatHistory::select('session_id', DB::raw('COUNT(*) as total_messages'))
66+
->selectRaw('MIN(created_at) as first_message')
67+
->where('chatbot_id', $bot->getId())
68+
->groupBy('session_id')
69+
->orderBy('first_message', 'desc')
70+
->get();
71+
72+
return view('settings-history', [
73+
'bot' => $bot,
74+
'chatHistory' => $chatHistory,
75+
]);
76+
}
77+
78+
public function getHistoryBySessionId(Request $request, $id, $sessionId)
79+
{
80+
/** @var Chatbot $bot */
81+
$bot = Chatbot::where('id', $id)->firstOrFail();
82+
83+
$chatHistory = ChatHistory::where('chatbot_id', $bot->getId())
84+
->where('session_id', $sessionId)
85+
->orderBy('created_at', 'asc')
86+
->get();
87+
88+
return view('widgets.chat-history', [
89+
'chatHistory' => $chatHistory,
90+
]);
91+
}
92+
6093
public function dataSettings(Request $request, $id)
6194
{
6295
// Find the chatbot by ID
@@ -126,9 +159,11 @@ public function dataSourcesUpdates($id)
126159

127160
// Get website data sources for the chatbot
128161
$dataSources = $bot->getWebsiteDataSources()->get();
162+
$pdfDataSources = $bot->getPdfFilesDataSources()->get();
129163

130164
return view('widgets.data-sources-updates', [
131165
'dataSources' => $dataSources,
166+
'pdfDataSources' => $pdfDataSources,
132167
]);
133168
}
134169

backend-server/app/Http/Controllers/MarketingWebsiteController.php

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace App\Http\Enums;
4+
5+
class ChatBotInitialPromptEnum
6+
{
7+
public const AI_ASSISTANT_INITIAL_PROMPT = "You are a helpful AI customer support agent. Use the following pieces of context to answer the question at the end.
8+
If you don\'t know the answer, just say you don\'t know. DO NOT try to make up an answer.
9+
If the question is not related to the context, politely respond that you are tuned to only answer questions that are related to the context.
10+
11+
{context}
12+
13+
Question: {question}
14+
Helpful answer in markdown:";
15+
16+
}
17+

0 commit comments

Comments
 (0)