Skip to content

Commit

Permalink
refactor: method to get expected response format
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanorosanelli committed Oct 3, 2024
1 parent e2eb1bb commit 8961eb6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
17 changes: 13 additions & 4 deletions composables/response-formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,24 @@ import showdown from 'showdown';
const converter = new showdown.Converter();

export const useResponseFormat = () => {
const formatResponse = (textToFormat: string, completionllm: any) => {
if (!completionllm || completionllm._type === 'openai-chat') {
const formatResponse = (textToFormat: string, format: string = 'text') => {
if (format === 'markdown') {
return converter.makeHtml(textToFormat);
} else {
return textToFormat;
}

return textToFormat;
};

const llmResponseFormat = (llmConf: any) => {
if (!llmConf || llmConf._type === 'openai-chat') {
return 'markdown';
}

return 'text';
};

return {
llmResponseFormat,
formatResponse,
};
};
6 changes: 4 additions & 2 deletions pages/chatbot/[id].vue
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<p class="text-xs">{{ item.who }}</p>
<div class="chat-balloon-status" :class="{ busy: isBusy && i === dialog.length - 1 }"></div>
</div>
<p class="whitespace-break-spaces" v-html="formatResponse(item.message, collection?.cmetadata?.qa_completion_llm)"></p>
<p class="whitespace-break-spaces" v-html="formatResponse(item.message, responseFormat)"></p>
<!--MENU CONTESTUALE-->
<div
v-if="canSeeDocs && i === dialog.length - 1 && showResponseMenu && hovered === i"
Expand Down Expand Up @@ -95,7 +95,7 @@
<script lang="ts" setup>
const config = useRuntimeConfig();
const store = useStatesStore();
const { formatResponse } = useResponseFormat();
const { formatResponse, llmResponseFormat } = useResponseFormat();
useHead({ title: `Chatbot | ${config.public.appName}` });
interface DialogItem {
Expand Down Expand Up @@ -126,6 +126,7 @@ const showResponseMenu = ref(true);
let sessionId = '';
let collectionName = '';
let editLevel = ItemEditLevel.None;
const responseFormat = ref('text');
onBeforeMount(async () => {
const route = useRoute();
Expand All @@ -149,6 +150,7 @@ onBeforeMount(async () => {
});
}
responseFormat.value = llmResponseFormat(collection.value.cmetadata?.qa_completion_llm);
sessionId = crypto.randomUUID();
isBusy.value = false;
updateLeftMessages();
Expand Down
8 changes: 5 additions & 3 deletions pages/chatbot/history-[id].vue
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
class="w-96 px-1 border rounded border-primary bg-white hover:bg-sky-100 focus:outline-primary text-primary hover:cursor-default"
>
<div class="flex flex-row justify-between" @click="openSelect = !openSelect">
<span v-html="formatResponse(selectedChat.title, collection?.cmetadata?.qa_completion_llm)"></span>
<span v-html="formatResponse(selectedChat.title, responseFormat)"></span>
<Icon class="text-xs self-center" name="ph:caret-down-bold" />
</div>
<div v-if="openSelect" class="w-96 -mx-1 max-h-96 absolute z-50 bg-white border border-primary rounded shadow-md overflow-y-scroll">
Expand All @@ -86,7 +86,7 @@
openSelect = !openSelect;
"
>
<span v-html="formatResponse(item.sessionTitle, collection?.cmetadata?.qa_completion_llm)"></span>
<span v-html="formatResponse(item.sessionTitle, responseFormat)"></span>
<span class="self-center text-xs italic">{{ getLocalCreationDate(item.sessionStart) }}</span>
</div>
</div>
Expand Down Expand Up @@ -135,7 +135,7 @@ import { utils, writeFile } from 'xlsx';
import moment from 'moment';
const config = useRuntimeConfig();
const { formatResponse } = useResponseFormat();
const { formatResponse, llmResponseFormat } = useResponseFormat();
useHead({ title: `Chat history | ${config.public.appName}` });
interface DialogItem {
Expand Down Expand Up @@ -177,6 +177,8 @@ if (!collection.value?.uuid) {
});
}
const responseFormat = llmResponseFormat(collection.value.cmetadata?.qa_completion_llm);
const csvKeys = ['question', 'answer', 'session_id', 'created', 'user_evaluation', 'user_feedback', 'chat_source'];
onBeforeMount(async () => showHistory());
Expand Down

0 comments on commit 8961eb6

Please sign in to comment.