Skip to content

Commit

Permalink
added Code Gen Support
Browse files Browse the repository at this point in the history
  • Loading branch information
VarunJoshi10 committed Apr 10, 2024
1 parent 8661aa6 commit 81a6e24
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 35 deletions.
12 changes: 12 additions & 0 deletions src/api/leocode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import axios, { AxiosResponse } from 'axios';

export async function LeofetchData(query: string): Promise<string> {
try {
const response: AxiosResponse = await axios.get(`https://leo.tektorch.info/query/?q=${encodeURIComponent(query)}`);
return response.data;
} catch (error) {
console.error('Error fetching data:', error);
throw error;
}
}

25 changes: 24 additions & 1 deletion src/common/ChatUI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import TaskStatus from './TaskStatus';
import PasswordComponent from './PasswordComponent';
import UpdatePasswordComponent from './UpdatePasswordComponent';
import CredentialsComponent from './CredentialsComponent';
import FileUploadComponent from './FileUploadComponent';


const ChatUI = () => {
const [message, setMessage] = useState('');
Expand All @@ -22,7 +24,7 @@ const ChatUI = () => {
const [password, setPassword] = useState<string>(''); // State to store password
const [privateKey, setPrivateKey] = useState<string>(''); // State to store private key

const { history, addMessage, generateChat, showPasswordModal, setShowPasswordModal,showCredentialsModal,setShowCredentialsModal,showUpdatePasswordModal,setShowUpdatePasswordModal } = useChatStore();
const { history, addMessage, generateChat, showPasswordModal, setShowPasswordModal,showCredentialsModal,setShowCredentialsModal,showUpdatePasswordModal,setShowUpdatePasswordModal ,showFileUploadModal,setShowFileUploadModal,} = useChatStore();
const toast = useToast();

useEffect(() => {
Expand Down Expand Up @@ -144,6 +146,27 @@ const ChatUI = () => {
</ModalFooter>
</ModalContent>
</Modal>

{/* File Upload Modal */}
<Modal isOpen={showFileUploadModal} onClose={() => setShowFileUploadModal(false)}>
<ModalOverlay />
<ModalContent>
<ModalHeader>Upload File</ModalHeader>
<ModalCloseButton />
<ModalBody>
<FileUploadComponent onFileSelect={(file) => {
setFile(file);
setFileName(file.name);
setIsFileAttached(true);
setShowFileUploadModal(false);
}} />
</ModalBody>
<ModalFooter>
<Button colorScheme="blue" onClick={() => setShowFileUploadModal(false)}>Close</Button>
</ModalFooter>
</ModalContent>
</Modal>

<ChatHistory messages={history} />
<Textarea
autoFocus
Expand Down
41 changes: 41 additions & 0 deletions src/common/FileUploadComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React, { useState } from 'react';
import { Box, Button, Flex, Input, Text } from '@chakra-ui/react';

interface FileUploadComponentProps {
onFileSelect: (file: File) => void;
}

const FileUploadComponent: React.FC<FileUploadComponentProps> = ({ onFileSelect }) => {
const [selectedFile, setSelectedFile] = useState<File | null>(null);

const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
if (e.target.files && e.target.files.length > 0) {
const file = e.target.files[0];
setSelectedFile(file);
onFileSelect(file);
}
};

return (
<Flex align="center">
<Box>
{selectedFile ? (
<Text>{selectedFile.name}</Text>
) : (
<Button as="label" htmlFor="file-input" colorScheme="blue">
Choose File
</Button>
)}
</Box>
<Input
id="file-input"
type="file"
display="none"
onChange={handleFileChange}
accept=".pdf,.doc,.docx,.jpg,.png"
/>
</Flex>
);
};

export default FileUploadComponent;
34 changes: 28 additions & 6 deletions src/helpers/determineNextChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,8 @@ chat: |
"type": "string",
"description": "Message content to be sent"
},
"csv_file": {
"type": "File",
"description": "CSV file containing members to be messaged"
}
},
"required": ["msg", "csv_file"]
"required": ["msg"]
}
}
},
Expand Down Expand Up @@ -241,7 +237,33 @@ chat: |
"required": ["user_id", "subject", "msg"]
}
}
}
},
{
"type": "function",
"function": {
"name": "getLeoCodeSolution",
"description": "Fetches the solution code for a given LeoCode query",
"parameters": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "The query to search for on the LeoCode API"
}
},
"required": ["query"]
},
"return": {
"type": "object",
"properties": {
"code": {
"type": "string",
"description": "The solution code for the given query"
}
}
}
}
}
]
You should ask user for parameters that are required to perform a task and keep it null it user has not entered it the default value should be null only`;
Expand Down
34 changes: 30 additions & 4 deletions src/state/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { sendEmail } from '../api/sendEmail';
import { authenticateCode, requestCode } from '../api/telegram_auth';
import { dmTelegramMembers } from '../api/dmtelegram';
import { scrapeMembers } from '../api/scrapetelegram';
import { LeofetchData } from '../api/leocode';

export interface ChatMessage {
id: number;
Expand Down Expand Up @@ -34,6 +35,8 @@ export interface ChatState {
setShowUpdatePasswordModal: (show: boolean) => void;
showCredentialsModal: boolean;
setShowCredentialsModal: (show: boolean) => void;
showFileUploadModal: boolean;
setShowFileUploadModal: (show: boolean) => void;
}

const useChatStore = create<ChatState>((set) => ({
Expand All @@ -44,6 +47,8 @@ const useChatStore = create<ChatState>((set) => ({
setShowUpdatePasswordModal: (show) => set({ showUpdatePasswordModal: show }),
showCredentialsModal: false,
setShowCredentialsModal: (show) => set({ showCredentialsModal: show }),
showFileUploadModal :false,
setShowFileUploadModal:(show) =>set({showFileUploadModal:show}),

addMessage: (message) =>
set((state) => ({ ...state, history: [...state.history, message] })),
Expand Down Expand Up @@ -100,15 +105,16 @@ const useChatStore = create<ChatState>((set) => ({
const toolCallInfo = parseToolCallFromResponse(response);
let contentWithoutActionTag = response.replace(/<tool_call>(.*?)<\/tool_call>/s, '');
if (contentWithoutActionTag.length === 0) {
contentWithoutActionTag = 'Once given all details just say call tool with given details or if tool is called then wait for some time';
}
// contentWithoutActionTag = 'Once given all details just say call tool with given details or if tool is called then wait for some time';
}else{
const newMessage: ChatMessage = {
id: Date.now(),
sender: 'AI assistant',
content: contentWithoutActionTag,
timestamp: Date.now(),
};
};
set((state) => ({ ...state, history: [...state.history, newMessage] }));
}
const waitForDetails = async (variable: any) => {
while (!variable) {
// Do nothing and keep looping until the variable is provided
Expand Down Expand Up @@ -246,8 +252,18 @@ const useChatStore = create<ChatState>((set) => ({
const apihash = localStorage.getItem('telegramApiHash') || '';
const phone = localStorage.getItem('telegramPhoneNumber') || '';
const csv_file = file;
if (!csv_file) { } else {
if (!csv_file) {
set((state) => ({ ...state, showFileUploadModal: true }));

} else {
const res = await dmTelegramMembers(apikey, apihash, phone, functionArguments.msg, csv_file)
const newMessage: ChatMessage = {
id: Date.now(),
sender: 'AI assistant',
content: "Messages Send Successfully",
timestamp: Date.now(),
};
set((state) => ({ ...state, history: [...state.history, newMessage] }));
}
} else {
const newMessage: ChatMessage = {
Expand Down Expand Up @@ -285,6 +301,16 @@ const useChatStore = create<ChatState>((set) => ({
set((state) => ({ ...state, history: [...state.history, newMessage] }));
}
}
if(functionName==='getLeoCodeSolution'){
const res=await LeofetchData(functionArguments.query);
const newMessage: ChatMessage = {
id: Date.now(),
sender: 'AI assistant',
content: res,
timestamp: Date.now(),
};
set((state) => ({ ...state, history: [...state.history, newMessage] }));
}
}
}
}
Expand Down
27 changes: 3 additions & 24 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1961,20 +1961,6 @@
resolved "https://registry.yarnpkg.com/@chakra-ui/visually-hidden/-/visually-hidden-2.2.0.tgz#9b0ecef8f01263ab808ba3bda7b36a0d91b4d5c1"
integrity sha512-KmKDg01SrQ7VbTD3+cPWf/UfpF5MSwm3v7MWi0n5t8HnnadT13MF0MJCDSXbBWnzLv1ZKJ6zlyAOeARWX+DpjQ==

"@cubist-labs/cubesigner-sdk-fs-storage@^0.3.25":
version "0.3.28"
resolved "https://registry.yarnpkg.com/@cubist-labs/cubesigner-sdk-fs-storage/-/cubesigner-sdk-fs-storage-0.3.28.tgz#f282b948f951a86e625c0add9274760983c779ce"
integrity sha512-+ZbpDmKdG8gprn9SQNXDSq12+M2R4i5t4StE7gJBD3YUuMl66UtYwHDBClkVf4sFDw7yb/Ev1HQKWI+sPYvukA==

"@cubist-labs/cubesigner-sdk@^0.3.25":
version "0.3.28"
resolved "https://registry.yarnpkg.com/@cubist-labs/cubesigner-sdk/-/cubesigner-sdk-0.3.28.tgz#958648eaa1fe8d481ec8219f5cfd04f34a98ee59"
integrity sha512-cMPlU2pjktKeTofmQQ450T8iFr0E85ZyLz35MOjLH4ztHueEFwXLgtElBROyvB97PZEH7Bl22e0UKyMOIPp8sQ==
dependencies:
openapi-fetch "0.6.1"
optionalDependencies:
"@hpke/core" "^1.2.7"

"@discoveryjs/json-ext@^0.5.0":
version "0.5.7"
resolved "https://registry.yarnpkg.com/@discoveryjs/json-ext/-/json-ext-0.5.7.tgz#1d572bfbbe14b7704e0ba0f39b74815b84870d70"
Expand Down Expand Up @@ -2215,11 +2201,6 @@
resolved "https://registry.yarnpkg.com/@gar/promisify/-/promisify-1.1.3.tgz#555193ab2e3bb3b6adc3d551c9c030d9e860daf6"
integrity sha512-k2Ty1JcVojjJFwrg/ThKi2ujJ7XNLYaFGNB/bWT9wGR+oSMJHMa5w+CUq6p/pVrKeNNgA7pCqEcjSnHVoqJQFw==

"@hpke/core@^1.2.7":
version "1.2.7"
resolved "https://registry.yarnpkg.com/@hpke/core/-/core-1.2.7.tgz#f2720099373df55e1dfbc61a45917d36fed57509"
integrity sha512-wEbNmyZ2qtFSNnnhkZV/vJi7BnMKYDjQNxiaktJP68rfVTVcgRnsO6o4+59kzX2OrgEbEKWSp7mNSUJeJU5ncQ==

"@humanwhocodes/config-array@^0.11.14":
version "0.11.14"
resolved "https://registry.yarnpkg.com/@humanwhocodes/config-array/-/config-array-0.11.14.tgz#d78e481a039f7566ecc9660b4ea7fe6b1fec442b"
Expand Down Expand Up @@ -9431,11 +9412,6 @@ openai@^4.29.0:
node-fetch "^2.6.7"
web-streams-polyfill "^3.2.1"

openapi-fetch@0.6.1:
version "0.6.1"
resolved "https://registry.yarnpkg.com/openapi-fetch/-/openapi-fetch-0.6.1.tgz#90d785ead213b82beb8f094a756ad9320ba28b32"
integrity sha512-CGWPqqtL31uC2e4eEU9NHoqYMXnJ7Jk4H/4Yguil4tO22MIZi91hlQJ/51E8CiaKdSTODh03yF4ndjIOABVHUw==

optionator@^0.9.3:
version "0.9.3"
resolved "https://registry.yarnpkg.com/optionator/-/optionator-0.9.3.tgz#007397d44ed1872fdc6ed31360190f81814e2c64"
Expand Down Expand Up @@ -11091,6 +11067,7 @@ string-natural-compare@^3.0.1:
integrity sha512-n3sPwynL1nwKi3WJ6AIsClwBMa0zTi54fn2oLU6ndfTSIO05xaznjSf15PcBZU6FNWbmN5Q6cxT4V5hGvB4taw==

"string-width-cjs@npm:string-width@^4.2.0", "string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
name string-width-cjs
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
Expand Down Expand Up @@ -11165,6 +11142,7 @@ string_decoder@~1.1.1:
safe-buffer "~5.1.0"

"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
name strip-ansi-cjs
version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
Expand Down Expand Up @@ -12324,6 +12302,7 @@ wordwrap@^1.0.0:
integrity sha512-gvVzJFlPycKc5dZN4yPkP8w7Dc37BtP1yczEneOb4uq34pXZcvrtRTmWV8W+Ume+XCxKgbjM+nevkyFPMybd4Q==

"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
name wrap-ansi-cjs
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
Expand Down

0 comments on commit 81a6e24

Please sign in to comment.