Skip to content

Commit

Permalink
feat: adjust interface for report chatting
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmtslmngcl committed Dec 22, 2024
1 parent d25365b commit 37c55e6
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 79 deletions.
11 changes: 8 additions & 3 deletions src/components/Chatbot/Chatbot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,21 @@ export default function HungerMapChatbot() {
const previousMessages = chats[currentChatIndex].messages;
let aiResponse = '';
try {
if (chats[currentChatIndex].isReportStarter && chats[currentChatIndex].context) {
if (chats[currentChatIndex].isReportStarter && chats[currentChatIndex].reports_country_name) {
aiResponse = (
await chatbot.sendMessage(text, {
previous_messages: previousMessages,
context: chats[currentChatIndex].context,
reports_country_name: chats[currentChatIndex].reports_country_name,
})
).response;
chats[currentChatIndex].isReportStarter = false;
} else {
aiResponse = (await chatbot.sendMessage(text, { previous_messages: previousMessages })).response;
aiResponse = (
await chatbot.sendMessage(text, {
previous_messages: previousMessages,
reports_country_name: chats[currentChatIndex].reports_country_name,
})
).response;
}
} catch (err) {
if (err instanceof APIError) {
Expand Down
17 changes: 3 additions & 14 deletions src/domain/contexts/ChatbotContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,8 @@ export function ChatbotProvider({ children }: { children: ReactNode }) {
* Initiates a chat with a report based on the provided country name and report content.
* If a chat for that specific already exists, it opens that chat; otherwise, it creates a new one.
* @param countryName - The name of the country related to the report.
* @param reportURL - The URL of the report to be processed.
*/
const initiateChatAboutReport = async (countryName: string, reportURL: string) => {
const initiateChatAboutReport = async (countryName: string) => {
const reportChatIndex = chats.findIndex((chat) => chat.title === `Report ${countryName}`);
const reportChatExists = reportChatIndex !== -1;

Expand All @@ -94,26 +93,16 @@ export function ChatbotProvider({ children }: { children: ReactNode }) {
return;
}

const { extractPdfText } = await import('@/operations/chatbot/ExtractPdfText');
let reportText;
try {
reportText = await extractPdfText(reportURL, showSnackBar);
} catch {
reportText = '';
}

const assistantMessage = {
id: crypto.randomUUID(),
content: reportText
? `Hey, how can I help you with this report about ${countryName}?`
: `Hey, unfortunately I'm currently unable to answer questions about this report. You can try it later or chat with me about other things!`,
content: `Hey, how can I help you with this report about ${countryName}?`,
role: SenderRole.ASSISTANT,
};

const newChat: IChat = {
id: chats.length + 1,
title: `Report ${countryName}`,
context: reportText,
reports_country_name: countryName,
isReportStarter: true,
messages: [assistantMessage],
isTyping: false,
Expand Down
2 changes: 1 addition & 1 deletion src/domain/entities/chatbot/BackendCommunication.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IMessage } from './Chatbot';

export interface QueryRequest {
context?: string;
reports_country_name?: string;
query: string;
version?: number;
chatbot_type?: string;
Expand Down
2 changes: 1 addition & 1 deletion src/domain/entities/chatbot/Chatbot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface IMessage {
export interface IChat {
id: number;
title: string;
context?: string;
reports_country_name?: string;
isReportStarter?: boolean;
messages: IMessage[];
isTyping: boolean;
Expand Down
2 changes: 1 addition & 1 deletion src/infrastructure/repositories/ChatbotRepositoryImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default class ChatbotRepositoryImpl implements ChatbotRepository {
async sendMessage(message: string, options: Partial<Omit<QueryRequest, 'query'>>): Promise<QueryResponse> {
try {
const payload: QueryRequest = {
context: options.context || '',
reports_country_name: options.reports_country_name || '',
query: message,
version: options.version || 1,
chatbot_type: options.chatbot_type || 'gpt-4',
Expand Down
59 changes: 0 additions & 59 deletions src/operations/chatbot/ExtractPdfText.ts

This file was deleted.

0 comments on commit 37c55e6

Please sign in to comment.