From 4afc9d72219dc90926d344fb19df96aedef3e3ae Mon Sep 17 00:00:00 2001 From: Pratyush Sudhakar Date: Sat, 11 May 2024 23:23:30 -0400 Subject: [PATCH] done with nl --- CleverHug-Frontend/src/pages/Affirmation.tsx | 38 ++++++++------- CleverHug-Frontend/src/utils/rrule.ts | 50 ++++++++++++++++---- 2 files changed, 62 insertions(+), 26 deletions(-) diff --git a/CleverHug-Frontend/src/pages/Affirmation.tsx b/CleverHug-Frontend/src/pages/Affirmation.tsx index 6a315a1..07c297c 100644 --- a/CleverHug-Frontend/src/pages/Affirmation.tsx +++ b/CleverHug-Frontend/src/pages/Affirmation.tsx @@ -1,5 +1,5 @@ import { useState } from "react"; -import { rruleToCron } from "../utils/rrule"; +import { rruleToCron, textToCron, cronToText } from "../utils/rrule"; import Loading from "../components/UI/Loading"; const SERVER_URL = process.env.REACT_APP_SERVER_URL; @@ -18,21 +18,25 @@ function Affirmation() { const handleSubmit = async (e: any) => { e.preventDefault(); setLoading(true); - const response = await fetch(`${SERVER_URL}/scheduler/process-time`, { - method: "POST", - headers: { - "Content-Type": "application/json", - Authorization: `Bearer ${localStorage.getItem("token")}` - }, - body: JSON.stringify({ time }) - }); - const data = await response.json(); - if (!response.ok) { - setError(data.error); - setLoading(false); - return; - } - rruleToCron(data.rrule).then(cron => setCron(cron)); + // const response = await fetch(`${SERVER_URL}/scheduler/process-time`, { + // method: "POST", + // headers: { + // "Content-Type": "application/json", + // Authorization: `Bearer ${localStorage.getItem("token")}` + // }, + // body: JSON.stringify({ time }) + // }); + // const data = await response.json(); + // if (!response.ok) { + // setError(data.error); + // setLoading(false); + // return; + // } + const cron = await textToCron(time); + const text = await cronToText(cron); + const data = { result: text, type: "recurring" }; + // const cronResp = await rruleToCron(data.result); + setCron(cron); setProcessedTime(data); setLoading(false); setShowConfirmation(true); @@ -189,7 +193,7 @@ function Affirmation() {
- {rruleToText(processedTime)} + {processedTime && processedTime.result}

Please Confirm the CRON: diff --git a/CleverHug-Frontend/src/utils/rrule.ts b/CleverHug-Frontend/src/utils/rrule.ts index b63c968..bce0013 100644 --- a/CleverHug-Frontend/src/utils/rrule.ts +++ b/CleverHug-Frontend/src/utils/rrule.ts @@ -9,19 +9,51 @@ const chatModel = new ChatOllama({ }); const chatModelOpenAI = new ChatOpenAI({ - openAIApiKey: "sk-pratyush-LfIF12ZpdW3pL1E76jxyT3BlbkFJYYO9FgxQ8AaorKDbMLvQ", + openAIApiKey: process.env.REACT_APP_OPENAI_API_KEY, temperature: 0.9, - apiKey: "sk-pratyush-LfIF12ZpdW3pL1E76jxyT3BlbkFJYYO9FgxQ8AaorKDbMLvQ", + apiKey: process.env.REACT_APP_OPENAI_API_KEY, model: "gpt-3.5-turbo", cache: true }); +export const cronToText = async (cron: string): Promise => { + console.log(`Converting CRON string ${cron} to text.`); + try { + const openai_response = await chatModelOpenAI.invoke( + `I want to schedule multiple events based on the provided CRON string: "${cron}". Could you please convert this CRON string to a text schedule? Please encaspulate the text in a code block.` + ); + const response = openai_response.content as string; + const cronString = response.split("```")[1]; + return cronString; + } catch (error) { + console.error(error); + return `Error converting CRON string ${cron} to text: ${error}`; + } +}; + +export const textToCron = async (text: string): Promise => { + console.log(`Converting text ${text} to CRON string.`); + try { + const openai_response = await chatModelOpenAI.invoke( + `I want to schedule multiple events based on the provided repeating schedule: "${text}". Could you please convert this text to a CRON string? Please encaspulate the CRON string in a code block.` + ); + const response = openai_response.content as string; + const cronString = response.split("```")[1]; + return cronString; + } catch (error) { + console.error(error); + return `Error converting text ${text} to CRON string: ${error}`; + } +}; + export const rruleToCron = async (rruleStr: string): Promise => { - const openai_response = await chatModel.invoke( - `Please convert the RRule string ${rruleStr} to CRON string. Please output the cron string in double quotes.` - ); - const response = openai_response.content as string; - const startIndex = response.indexOf('"'); - const endIndex = response.lastIndexOf('"'); - return response.slice(startIndex + 1, endIndex); + console.log(`Converting RRule string ${rruleStr} to CRON string.`); + try { + const openai_response = await chatModelOpenAI.invoke(`Could you please translate the RRule string "${rruleStr}" to a CRON string?`); + const response = openai_response.content as string; + return response; + } catch (error) { + console.error(error); + return `Error converting RRule string ${rruleStr} to CRON string: ${error}`; + } };