Skip to content

Commit

Permalink
done with nl
Browse files Browse the repository at this point in the history
  • Loading branch information
pratyush1712 committed May 12, 2024
1 parent e28d813 commit 4afc9d7
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 26 deletions.
38 changes: 21 additions & 17 deletions CleverHug-Frontend/src/pages/Affirmation.tsx
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -189,7 +193,7 @@ function Affirmation() {
</div>
</strong>
<br />
{rruleToText(processedTime)}
{processedTime && processedTime.result}
</p>
<p>
<strong className="bg-gray-900">Please Confirm the CRON:</strong>
Expand Down
50 changes: 41 additions & 9 deletions CleverHug-Frontend/src/utils/rrule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> => {
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<string> => {
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<string> => {
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}`;
}
};

0 comments on commit 4afc9d7

Please sign in to comment.