|
| 1 | +import './App.css'; |
| 2 | + |
| 3 | +import * as Form from '@radix-ui/react-form'; |
| 4 | +import { PlusIcon } from '@radix-ui/react-icons'; |
| 5 | +import { |
| 6 | + Box, |
| 7 | + Button, |
| 8 | + Container, |
| 9 | + Flex, |
| 10 | + Heading, |
| 11 | + Section, |
| 12 | + TextField, |
| 13 | +} from '@radix-ui/themes'; |
| 14 | +import OpenAI from 'openai'; |
| 15 | +import { useState } from 'react'; |
| 16 | + |
| 17 | +import { client as baseClient } from './client/client.gen'; |
| 18 | +import { OpenAi } from './client/sdk.gen'; |
| 19 | + |
| 20 | +const sdk = new OpenAI({ |
| 21 | + apiKey: import.meta.env.VITE_OPENAI_API_KEY, |
| 22 | + dangerouslyAllowBrowser: true, |
| 23 | +}); |
| 24 | + |
| 25 | +baseClient.setConfig({ |
| 26 | + auth() { |
| 27 | + return import.meta.env.VITE_OPENAI_API_KEY; |
| 28 | + }, |
| 29 | +}); |
| 30 | + |
| 31 | +const client = new OpenAi({ |
| 32 | + client: baseClient, |
| 33 | +}); |
| 34 | + |
| 35 | +function App() { |
| 36 | + const [isRequiredNameError] = useState(false); |
| 37 | + |
| 38 | + const onCreateResponse = async (values: FormData) => { |
| 39 | + const response = await sdk.responses.create({ |
| 40 | + input: values.get('input') as string, |
| 41 | + model: 'gpt-5-nano', |
| 42 | + }); |
| 43 | + |
| 44 | + console.log(response.output_text); |
| 45 | + const { data, error } = await client.createResponse({ |
| 46 | + body: { |
| 47 | + input: values.get('input') as string, |
| 48 | + model: 'gpt-5-nano', |
| 49 | + }, |
| 50 | + }); |
| 51 | + if (error) { |
| 52 | + console.log(error); |
| 53 | + return; |
| 54 | + } |
| 55 | + console.log(data?.output); |
| 56 | + }; |
| 57 | + |
| 58 | + return ( |
| 59 | + <Box |
| 60 | + style={{ background: 'var(--gray-a2)', borderRadius: 'var(--radius-3)' }} |
| 61 | + > |
| 62 | + <Container size="1"> |
| 63 | + <Section size="1" /> |
| 64 | + <Flex align="center"> |
| 65 | + <a className="shrink-0" href="https://heyapi.dev/" target="_blank"> |
| 66 | + <img |
| 67 | + src="https://heyapi.dev/logo.png" |
| 68 | + className="h-16 w-16 transition duration-300 will-change-auto" |
| 69 | + alt="Hey API logo" |
| 70 | + /> |
| 71 | + </a> |
| 72 | + <Heading>@hey-api/openapi-ts 🤝 OpenAI</Heading> |
| 73 | + </Flex> |
| 74 | + <Section size="1" /> |
| 75 | + <Flex direction="column" gapY="2"> |
| 76 | + <Form.Root |
| 77 | + className="w-[400px]" |
| 78 | + onSubmit={(event) => { |
| 79 | + event.preventDefault(); |
| 80 | + onCreateResponse(new FormData(event.currentTarget)); |
| 81 | + }} |
| 82 | + > |
| 83 | + <Form.Field className="grid mb-[10px]" name="input"> |
| 84 | + <div className="flex items-baseline justify-between"> |
| 85 | + <Form.Label className="text-[15px] font-medium leading-[35px] text-white"> |
| 86 | + Input |
| 87 | + </Form.Label> |
| 88 | + {isRequiredNameError && ( |
| 89 | + <Form.Message className="text-[13px] text-white opacity-[0.8]"> |
| 90 | + Please enter a name |
| 91 | + </Form.Message> |
| 92 | + )} |
| 93 | + <Form.Message |
| 94 | + className="text-[13px] text-white opacity-[0.8]" |
| 95 | + match="valueMissing" |
| 96 | + > |
| 97 | + Please enter an input |
| 98 | + </Form.Message> |
| 99 | + </div> |
| 100 | + <Form.Control asChild> |
| 101 | + <TextField.Root |
| 102 | + placeholder="Write a one-sentence bedtime story about a unicorn." |
| 103 | + name="input" |
| 104 | + type="text" |
| 105 | + required |
| 106 | + /> |
| 107 | + </Form.Control> |
| 108 | + </Form.Field> |
| 109 | + <Flex gapX="2"> |
| 110 | + <Form.Submit asChild> |
| 111 | + <Button type="submit"> |
| 112 | + <PlusIcon /> Create Response |
| 113 | + </Button> |
| 114 | + </Form.Submit> |
| 115 | + {/* <Button onClick={onUpdatePet} type="button"> |
| 116 | + <ReloadIcon /> Update Pet |
| 117 | + </Button> */} |
| 118 | + </Flex> |
| 119 | + </Form.Root> |
| 120 | + </Flex> |
| 121 | + <Section size="1" /> |
| 122 | + </Container> |
| 123 | + </Box> |
| 124 | + ); |
| 125 | +} |
| 126 | + |
| 127 | +export default App; |
0 commit comments