Use base64 encoded image instead of source URL for persistence

The source URL returned by OpenAI would expire soon. This would make
the chat sessions contain non-accessible images/messages if using
OpenaI image URL

Get base64 encoded image from OpenAI and store directly in
conversation logs. This resolves the image link expiring issue
This commit is contained in:
Debanjum Singh Solanky
2023-12-04 22:55:22 -05:00
parent 52c5f4170a
commit 6e3f66c0f1
5 changed files with 18 additions and 36 deletions

View File

@@ -109,7 +109,7 @@ export class KhojChatModal extends Modal {
if (!message) {
return;
} else if (intentType === "text-to-image") {
let imageMarkdown = `![](${message})`;
let imageMarkdown = `![](data:image/png;base64,${message})`;
this.renderMessage(chatEl, imageMarkdown, sender, dt);
return;
} else if (!context) {
@@ -317,8 +317,8 @@ export class KhojChatModal extends Modal {
if (responseText.startsWith("{") && responseText.endsWith("}")) {
try {
const responseAsJson = JSON.parse(responseText);
if (responseAsJson.imageUrl) {
responseText = `![${query}](${responseAsJson.imageUrl})`;
if (responseAsJson.image) {
responseText = `![${query}](data:image/png;base64,${responseAsJson.image})`;
} else if (responseAsJson.detail) {
responseText = responseAsJson.detail;
}