Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: resolve web links in messages to embed text into prompt #371

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/hooks/useSubmit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useTranslation } from 'react-i18next';
import { ChatInterface, MessageInterface } from '@type/chat';
import { getChatCompletion, getChatCompletionStream } from '@api/api';
import { parseEventSource } from '@api/helper';
import { limitMessageTokens, updateTotalTokenUsed } from '@utils/messageUtils';
import { limitMessageTokens, resolveWebLinks, updateTotalTokenUsed } from '@utils/messageUtils';
import { _defaultChatConfig } from '@constants/chat';
import { officialAPIEndpoint } from '@constants/auth';

Expand Down Expand Up @@ -70,13 +70,15 @@ const useSubmit = () => {
if (chats[currentChatIndex].messages.length === 0)
throw new Error('No messages submitted!');

const messages = limitMessageTokens(
let messages = limitMessageTokens(
chats[currentChatIndex].messages,
chats[currentChatIndex].config.max_tokens,
chats[currentChatIndex].config.model
);
if (messages.length === 0) throw new Error('Message exceed max token!');

messages = await resolveWebLinks(messages);

// no api key (free)
if (!apiKey || apiKey.length === 0) {
// official endpoint
Expand Down
43 changes: 43 additions & 0 deletions src/utils/messageUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,49 @@ export const limitMessageTokens = (
return limitedMessages;
};


export const resolveWebLinks = async (messages: MessageInterface[]): Promise<MessageInterface[]> => {
const resolvedMessages: MessageInterface[] = [];

const fetchWebContent = async (link: string): Promise<string> => {
try {
const response = await fetch(link);
const resolvedContent = await response.text();
return resolvedContent;
} catch (error) {
console.error(`Failed to fetch web content from ${link}:`, error);
return '';
}
};

const replaceWebLinks = async (content: string): Promise<string> => {
const linkRegex = /(http[s]?:\/\/[^\s]+)/g;
const matches = content.match(linkRegex);

if (!matches) {
return content;
}

for (const match of matches) {
try {
const resolvedContent = await fetchWebContent(match);
content = content.replace(match, resolvedContent);
} catch (error) {
console.error(`Failed to fetch web content from ${match}:`, error);
}
}

return content;
};

for (const message of messages) {
const resolvedContent = await replaceWebLinks(message.content);
resolvedMessages.push({ ...message, content: resolvedContent });
}

return resolvedMessages;
};

export const updateTotalTokenUsed = (
model: ModelOptions,
promptMessages: MessageInterface[],
Expand Down