From 6bf8b9a90b1c8e661484da56100188f319c0eeba Mon Sep 17 00:00:00 2001 From: Tim Ermilov Date: Thu, 7 Nov 2024 12:47:38 +0100 Subject: [PATCH] Add agent function to generate pipeline from user request Addresses #21 --- app/components/agent/Chat.tsx | 18 ++++++++++++++++-- app/components/agent/agent.ts | 36 ++++++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/app/components/agent/Chat.tsx b/app/components/agent/Chat.tsx index 854e53e..a4bcb2b 100644 --- a/app/components/agent/Chat.tsx +++ b/app/components/agent/Chat.tsx @@ -1,6 +1,6 @@ import { PaperAirplaneIcon } from '@heroicons/react/24/solid'; import { useAtomValue, useSetAtom } from 'jotai'; -import { useState } from 'react'; +import { useEffect, useRef, useState } from 'react'; import { litlyticsAtom, pipelineAtom } from '~/store/store'; import { Button } from '../catalyst/button'; import { Input } from '../catalyst/input'; @@ -33,6 +33,7 @@ function MessageRender({ message }: { message: Message }) { } export function Chat() { + const messageBoxRef = useRef(null); const litlytics = useAtomValue(litlyticsAtom); const setPipeline = useSetAtom(pipelineAtom); const [input, setInput] = useState(''); @@ -44,6 +45,16 @@ export function Chat() { }, ]); + useEffect(() => { + // scroll to bottom + if (messageBoxRef.current) { + messageBoxRef.current.scrollTo({ + top: messageBoxRef.current.scrollHeight, + behavior: 'smooth', + }); + } + }, [messages]); + const sendMessage = async () => { const inputMessage = input; // reset input @@ -72,7 +83,10 @@ export function Chat() { return (
-
+
{messages.map((m) => ( ))} diff --git a/app/components/agent/agent.ts b/app/components/agent/agent.ts index 576582b..b8ca577 100644 --- a/app/components/agent/agent.ts +++ b/app/components/agent/agent.ts @@ -10,7 +10,7 @@ LitLytics allows creating custom text document processing pipelines using custom You have access to following LitLytics functions: - Suggest a list of possible pipelines that can be applied to user's documents -- Generate a suggested pipeline for processing documents +- Generate a new pipeline for processing documents for given task from user - Refine suggested pipeline for processing documents - Add a new step to pipeline - Edit a step in the pipeline @@ -87,6 +87,40 @@ Generate a text description for user.`, ); }, }), + generatePipeline: tool({ + description: `Generate a new pipeline for processing documents for given task from user.`, + parameters: z.object({ + task: z.string(), + }), + execute: async ({ task }) => { + litlytics.setPipeline({ + pipelineDescription: task, + }); + // run task + const newPipeline = await litlytics.generatePipeline(); + setPipeline(newPipeline); + // generate a response + const agentMessagesWithResult = agentMessages.concat([ + { + content: `Suggested pipeline from function execution: +${newPipeline.pipelinePlan} + +Ask a user if that look fine.`, + role: 'system', + }, + ]); + const result = await litlytics.runPromptFromMessages({ + messages: agentMessagesWithResult, + }); + resolve( + messages.concat({ + id: String(messages.length), + from: 'assistant', + text: result.result, + }) + ); + }, + }), }; // execute request