File

src/chatbot/chatbot.service.ts

Index

Properties
Methods

Constructor

constructor(chatService: ChatService, messageService: MessageService)
Parameters :
Name Type Optional
chatService ChatService No
messageService MessageService No

Methods

Async askBot
askBot(question: string, history: any[], userId: string)
Parameters :
Name Type Optional
question string No
history any[] No
userId string No
Returns : unknown
Async getSummary
getSummary(chat: Chat, userMessage: Message)
Parameters :
Name Type Optional
chat Chat No
userMessage Message No
Returns : unknown
Async ingestUserMessage
ingestUserMessage(userMessage: UserMessage, userId: string)
Parameters :
Name Type Optional
userMessage UserMessage No
userId string No
Returns : unknown

Properties

Private Readonly chatbotApiUrl
Default value : process.env.CHATBOT_URL
import { Injectable } from "@nestjs/common";
import { UserMessage } from "./dto/userMessage.dto";
import { Message, MessageStatus, MessageType } from "./entities/message.entity";
import { ChatService } from "./chat.service";
import { MessageService } from "./message.service";
import { Chat } from "./entities/chat.entity";

@Injectable()
export class ChatBotService {
  private readonly chatbotApiUrl = process.env.CHATBOT_URL;

  constructor(
    private readonly chatService: ChatService,
    private readonly messageService: MessageService,
  ) {}

  async askBot(question: string, history: any[], userId: string) {
    const endpoint = `${this.chatbotApiUrl}/translate`;
    const response = await fetch(endpoint, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        question: question,
        history: history,
        user_id: userId,
      }),
    });
    return await response.json();
    // TODO: handle errors here
  }

  async getSummary(chat: Chat, userMessage: Message) {
    // TODO: Create new endpiont in chat api to summarise conversations
    const endpoint = `${this.chatbotApiUrl}/summarise`;
    const response = await fetch(endpoint, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        summary: chat.summary,
        message: userMessage.content,
      }),
    });
    return await response.json();
  }

  async ingestUserMessage(userMessage: UserMessage, userId: string) {
    let chat = null;
    if (!userMessage.chatId) {
      chat = await this.chatService.create(userMessage, userId);
      userMessage.chatId = chat.id;
    } else {
      chat = await this.chatService.ping(userMessage.chatId);
    }

    const history = await this.messageService.getChatContext(chat, 3);
    const message = await this.messageService.create(
      userMessage,
      userId,
      MessageType.USER,
      MessageStatus.SUCCESS,
    );
    const response = await this.askBot(message.content.text, history, userId);

    let aiMessage = null;
    if (response["error"]) {
      aiMessage = await this.messageService.create(
        {
          content: {
            text: JSON.stringify(response["error"]),
            data: {},
          },
          chatId: chat.id,
        },
        userId,
        MessageType.AI,
        MessageStatus.ERROR,
      );
    } else {
      aiMessage = await this.messageService.create(
        {
          content: {
            text: response["data"]["result"]["text"],
            data: response["data"]["result"]["data"],
          },
          chatId: chat.id,
        },
        userId,
        MessageType.AI,
        MessageStatus.SUCCESS,
      );
    }

    // const summary = await this.getSummary(chat, message)
    // chat = await this.chatService.update(chat.id, { summary: summary["result"] })

    return aiMessage;
  }
}

results matching ""

    No results matching ""