feat(image-search): handle chat history

This commit is contained in:
ItzCrazyKns 2024-04-28 11:15:28 +05:30
parent f14050840b
commit 5df3c5ad8c
No known key found for this signature in database
GPG Key ID: 8162927C7CCE3065
4 changed files with 24 additions and 5 deletions

View File

@ -3,12 +3,21 @@ import handleImageSearch from '../agents/imageSearchAgent';
import { BaseChatModel } from '@langchain/core/language_models/chat_models'; import { BaseChatModel } from '@langchain/core/language_models/chat_models';
import { getAvailableProviders } from '../lib/providers'; import { getAvailableProviders } from '../lib/providers';
import { getChatModel, getChatModelProvider } from '../config'; import { getChatModel, getChatModelProvider } from '../config';
import { HumanMessage, AIMessage } from '@langchain/core/messages';
const router = express.Router(); const router = express.Router();
router.post('/', async (req, res) => { router.post('/', async (req, res) => {
try { try {
const { query, chat_history } = req.body; let { query, chat_history } = req.body;
chat_history = chat_history.map((msg: any) => {
if (msg.role === 'user') {
return new HumanMessage(msg.content);
} else if (msg.role === 'assistant') {
return new AIMessage(msg.content);
}
});
const models = await getAvailableProviders(); const models = await getAvailableProviders();
const provider = getChatModelProvider(); const provider = getChatModelProvider();

View File

@ -116,7 +116,10 @@ const MessageBox = ({
</div> </div>
</div> </div>
<div className="lg:sticky lg:top-20 flex flex-col items-center space-y-3 w-full lg:w-3/12 z-30 h-full pb-4"> <div className="lg:sticky lg:top-20 flex flex-col items-center space-y-3 w-full lg:w-3/12 z-30 h-full pb-4">
<SearchImages query={history[messageIndex - 1].content} /> <SearchImages
query={history[messageIndex - 1].content}
chat_history={history.slice(0, messageIndex - 1)}
/>
<div className="border border-dashed border-[#1C1C1C] px-4 py-2 flex flex-row items-center justify-between rounded-lg text-white text-sm w-full"> <div className="border border-dashed border-[#1C1C1C] px-4 py-2 flex flex-row items-center justify-between rounded-lg text-white text-sm w-full">
<div className="flex flex-row items-center space-x-2"> <div className="flex flex-row items-center space-x-2">
<VideoIcon size={17} /> <VideoIcon size={17} />

View File

@ -3,6 +3,7 @@ import { ImagesIcon, PlusIcon } from 'lucide-react';
import { useState } from 'react'; import { useState } from 'react';
import Lightbox from 'yet-another-react-lightbox'; import Lightbox from 'yet-another-react-lightbox';
import 'yet-another-react-lightbox/styles.css'; import 'yet-another-react-lightbox/styles.css';
import { Message } from './ChatWindow';
type Image = { type Image = {
url: string; url: string;
@ -10,7 +11,13 @@ type Image = {
title: string; title: string;
}; };
const SearchImages = ({ query }: { query: string }) => { const SearchImages = ({
query,
chat_history,
}: {
query: string;
chat_history: Message[];
}) => {
const [images, setImages] = useState<Image[] | null>(null); const [images, setImages] = useState<Image[] | null>(null);
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [open, setOpen] = useState(false); const [open, setOpen] = useState(false);
@ -31,7 +38,7 @@ const SearchImages = ({ query }: { query: string }) => {
}, },
body: JSON.stringify({ body: JSON.stringify({
query: query, query: query,
chat_history: [], chat_history: chat_history,
}), }),
}, },
); );