|
1 |
| -import { IChatGptService } from "@nestia/agent"; |
2 |
| -import { IHttpConnection } from "@samchon/openapi"; |
| 1 | +import { |
| 2 | + Button, |
| 3 | + FormControl, |
| 4 | + FormControlLabel, |
| 5 | + FormLabel, |
| 6 | + Radio, |
| 7 | + RadioGroup, |
| 8 | + TextField, |
| 9 | + Typography, |
| 10 | +} from "@mui/material"; |
| 11 | +import { INestiaAgentProvider, NestiaAgent } from "@nestia/agent"; |
| 12 | +import { IHttpLlmApplication } from "@samchon/openapi"; |
| 13 | +import OpenAI from "openai"; |
| 14 | +import React, { useState } from "react"; |
| 15 | +import JsonInput from "react-json-editor-ajrm"; |
| 16 | +// @ts-ignore |
| 17 | +import locale from "react-json-editor-ajrm/locale/en"; |
3 | 18 |
|
4 |
| -export const NestiaChatUploader = () => {}; |
| 19 | +import { NestiaChatApplication } from "./NestiaChatApplication"; |
| 20 | +import { NestiaChatFileUploadMovie } from "./movies/uploader/NestiaChatFileUploadMovie"; |
| 21 | + |
| 22 | +export const NestiaChatUploader = (props: NestiaChatUploader.IProps) => { |
| 23 | + // PARAMETERS |
| 24 | + const [host, setHost] = useState("http://localhost:37001"); |
| 25 | + const [headers, setHeaders] = useState<Record<string, string>>({ |
| 26 | + Authorization: "YOUR_SERVER_API_KEY", |
| 27 | + }); |
| 28 | + const [model, setModel] = useState("gpt-4o"); |
| 29 | + const [apiKey, setApiKey] = useState("YOUR-OPENAI-API-KEY"); |
| 30 | + |
| 31 | + // RESULT |
| 32 | + const [application, setApplication] = |
| 33 | + useState<IHttpLlmApplication<"chatgpt"> | null>(null); |
| 34 | + const [progress, setProgress] = useState(false); |
| 35 | + |
| 36 | + // HANDLERS |
| 37 | + const handleApplication = ( |
| 38 | + application: IHttpLlmApplication<"chatgpt"> | null, |
| 39 | + error: string | null, |
| 40 | + ) => { |
| 41 | + setApplication(application); |
| 42 | + if (error !== null) handleError(error); |
| 43 | + }; |
| 44 | + const handleError = (error: string) => { |
| 45 | + if (props.onError) props.onError(error); |
| 46 | + else alert(error); |
| 47 | + }; |
| 48 | + |
| 49 | + const open = async () => { |
| 50 | + if (application === null) return; |
| 51 | + setProgress(true); |
| 52 | + try { |
| 53 | + const provider: INestiaAgentProvider = { |
| 54 | + type: "chatgpt", |
| 55 | + api: new OpenAI({ |
| 56 | + apiKey, |
| 57 | + dangerouslyAllowBrowser: true, |
| 58 | + }), |
| 59 | + model: model as "gpt-4o", |
| 60 | + }; |
| 61 | + const agent: NestiaAgent = new NestiaAgent({ |
| 62 | + provider, |
| 63 | + controllers: [ |
| 64 | + { |
| 65 | + protocol: "http", |
| 66 | + name: "main", |
| 67 | + application, |
| 68 | + connection: { |
| 69 | + host, |
| 70 | + headers, |
| 71 | + }, |
| 72 | + }, |
| 73 | + ], |
| 74 | + }); |
| 75 | + props.onSuccess(<NestiaChatApplication agent={agent} />); |
| 76 | + } catch (error) { |
| 77 | + handleError(error instanceof Error ? error.message : "unknown error"); |
| 78 | + setProgress(false); |
| 79 | + } |
| 80 | + }; |
| 81 | + |
| 82 | + return ( |
| 83 | + <React.Fragment> |
| 84 | + <NestiaChatFileUploadMovie onChange={handleApplication} /> |
| 85 | + <br /> |
| 86 | + <FormControl fullWidth style={{ paddingLeft: 15 }}> |
| 87 | + <Typography variant="h6">HTTP Connection</Typography> |
| 88 | + <br /> |
| 89 | + <TextField |
| 90 | + onChange={(e) => setHost(e.target.value)} |
| 91 | + defaultValue={host} |
| 92 | + label="Host URL" |
| 93 | + variant="outlined" |
| 94 | + /> |
| 95 | + <br /> |
| 96 | + <FormLabel> Headers </FormLabel> |
| 97 | + <JsonInput |
| 98 | + locale={locale} |
| 99 | + theme="dark_vscode_tribute" |
| 100 | + placeholder={headers} |
| 101 | + onChange={setHeaders} |
| 102 | + height="100px" |
| 103 | + /> |
| 104 | + <br /> |
| 105 | + <br /> |
| 106 | + <Typography variant="h6">LLM Arguments</Typography> |
| 107 | + <br /> |
| 108 | + <FormLabel> LLM Provider </FormLabel> |
| 109 | + <RadioGroup defaultValue={"chatgpt"} style={{ paddingLeft: 15 }}> |
| 110 | + <FormControlLabel |
| 111 | + value="chatgpt" |
| 112 | + control={<Radio />} |
| 113 | + label="OpenAI (ChatGPT)" |
| 114 | + /> |
| 115 | + </RadioGroup> |
| 116 | + <FormLabel style={{ paddingTop: 20 }}> OpenAI Model </FormLabel> |
| 117 | + <RadioGroup |
| 118 | + defaultValue={model} |
| 119 | + onChange={(_e, value) => setModel(value)} |
| 120 | + style={{ paddingLeft: 15 }} |
| 121 | + > |
| 122 | + <FormControlLabel value="gpt-4o" control={<Radio />} label="GPT-4o" /> |
| 123 | + <FormControlLabel |
| 124 | + value="gpt-4o-mini" |
| 125 | + control={<Radio />} |
| 126 | + label="GPT-4o Mini" |
| 127 | + /> |
| 128 | + </RadioGroup> |
| 129 | + <br /> |
| 130 | + <TextField |
| 131 | + onChange={(e) => setApiKey(e.target.value)} |
| 132 | + defaultValue={apiKey} |
| 133 | + label="OpenAI API Key" |
| 134 | + variant="outlined" |
| 135 | + /> |
| 136 | + </FormControl> |
| 137 | + <br /> |
| 138 | + <br /> |
| 139 | + <Button |
| 140 | + component="a" |
| 141 | + fullWidth |
| 142 | + variant="contained" |
| 143 | + color={"info"} |
| 144 | + size="large" |
| 145 | + disabled={progress === true || document === null} |
| 146 | + onClick={() => open()} |
| 147 | + > |
| 148 | + {progress ? "Generating..." : "Generate Editor"} |
| 149 | + </Button> |
| 150 | + </React.Fragment> |
| 151 | + ); |
| 152 | +}; |
5 | 153 | export namespace NestiaChatUploader {
|
6 | 154 | export interface IProps {
|
7 |
| - connection?: IHttpConnection; |
8 |
| - service?: IChatGptService; |
| 155 | + onError?: (error: string) => void; |
| 156 | + onSuccess: (element: JSX.Element) => void; |
9 | 157 | }
|
10 | 158 | }
|
0 commit comments