nuxt-chatgpt 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Oliver Trajceski
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Oliver Trajceski
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,182 @@
1
+ [![Contributors][contributors-shield]][contributors-url]
2
+ [![Downloads][downloads-shield]][downloads-url]
3
+ [![Issues][issues-shield]][issues-url]
4
+ [![MIT License][license-shield]][license-url]
5
+ [![LinkedIn][linkedin-shield]][linkedin-url]
6
+
7
+ <!-- PROJECT LOGO -->
8
+ <br />
9
+ <div>
10
+ <h1>Nuxt Chatgpt</h3>
11
+
12
+ <img src="images/chatgpt-logo.png" alt="Logo">
13
+
14
+ > [ChatGPT](https://openai.com/) integration for [Nuxt 3](https://nuxt.com).
15
+ <p align="center">
16
+ <br />
17
+ <a href="https://github.com/schnapsterdog/nuxt-chatgpt/issues target="_blank"">Report Bug</a>
18
+ ยท
19
+ <a href="https://github.com/schnapsterdog/nuxt-chatgpt/issues target="_blank"">Request Feature</a>
20
+ </p>
21
+ </div>
22
+
23
+ ## About the module
24
+
25
+ This user-friendly module boasts of an easy integration process that enables seamless implementation into any [Nuxt 3](https://nuxt.com) project. With type-safe integration, you can integrate [ChatGPT](https://openai.com/) into your [Nuxt 3](https://nuxt.com) project without breaking a <b>sweat</b>. Enjoy easy access to the `send` method through the `useChatgpt()` composable. Additionally, the module guarantees <b><i>security</i></b> as requests are routed through a [Nitro Server](https://nuxt.com/docs/guide/concepts/server-engine), thus preventing the exposure of your <b>API Key</b>.
26
+
27
+ ## Features
28
+
29
+ - ๐Ÿ’ช &nbsp; Easy implementation into any [Nuxt 3](https://nuxt.com) project.
30
+ - ๐Ÿ‘‰ &nbsp; Type-safe integration of Chatgpt into your [Nuxt 3](https://nuxt.com) project.
31
+ - ๐Ÿ•น๏ธ &nbsp; Provides a `useChatgpt()` composable that grants easy access to the `send` method.
32
+ - ๐Ÿ”ฅ &nbsp; Ensures security by routing requests through a [Nitro Server](https://nuxt.com/docs/guide/concepts/server-engine), preventing the <b>API Key</b> from being exposed.
33
+ - ๐Ÿงฑ &nbsp; It is lightweight and performs well.
34
+
35
+ ## Getting Started
36
+
37
+ 1. Add nuxt-chatgpt dependency to your project
38
+ * npm
39
+ ```sh
40
+ npm install --save-dev nuxt-chatgpt
41
+ ```
42
+ * pnpm
43
+ ```sh
44
+ pnpm add -D nuxt-chatgpt
45
+ ```
46
+ * yarn
47
+ ```sh
48
+ yarn add --dev nuxt-chatgpt
49
+ ```
50
+ 2. Add nuxt-chatgpt to the modules section of nuxt.config.ts
51
+
52
+ ```js
53
+ export default defineNuxtConfig({
54
+ modules: ["nuxt-chatgpt"],
55
+
56
+ // entirely optional
57
+ chatgpt: {
58
+ apiKey: 'Your apiKey here goes here'
59
+ },
60
+ })
61
+ ```
62
+ That's it! You can now use Nuxt Chatgpt in your Nuxt app ๐Ÿ”ฅ
63
+
64
+ ## Example & Usage
65
+
66
+ To access the `send` method in the nuxt-chatgpt module, you can use the `useChatgpt()` composable, which provides easy access to the method. The send method requires two parameters:
67
+
68
+ - `message`: a string representing the text message that you want to send to the GPT-3 model for processing.
69
+ - `options`: an optional object that specifies any additional options you want to pass to the API request, such as the GPT-3 model ID, the number of responses to generate, and the maximum length of each response.
70
+
71
+ ```js
72
+ const { send } = useChatgpt()
73
+
74
+ const data = ref('')
75
+ const message = ref('')
76
+
77
+ async function sendMessage() {
78
+ const response = await send(message.value)
79
+ data.value = response
80
+ }
81
+
82
+ ```
83
+
84
+ ```html
85
+ <template>
86
+ <div>
87
+ <input v-model="message">
88
+ <button
89
+ @click="sendMessage"
90
+ v-text="'Send'"
91
+ />
92
+ <div>{{ data }}</div>
93
+ </div>
94
+ </template>
95
+ ```
96
+
97
+ ## Module Options
98
+
99
+ | Name | Type | Default | Description |
100
+ |--|--|--|--|
101
+ |**apiKey**|`String`|`xxxxxx`|Your apiKey here goes here
102
+ |**isEnabled**|`Boolean`|`true`| Enable or disable the module. `True` by default.
103
+
104
+ <!-- CONTRIBUTING -->
105
+ ## Contributing
106
+
107
+ Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**.
108
+
109
+ If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement".
110
+ Don't forget to give the project a star! Thanks again!
111
+
112
+ 1. Fork the Project
113
+ 2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`)
114
+ 3. Commit your Changes (`git commit -m 'Add some AmazingFeature'`)
115
+ 4. Push to the Branch (`git push origin feature/AmazingFeature`)
116
+ 5. Open a Pull Request
117
+
118
+
119
+ <!-- LICENSE -->
120
+ ## License
121
+
122
+ Distributed under the MIT License. See `LICENSE.txt` for more information.
123
+
124
+ <!-- CONTACT -->
125
+ ## Contact
126
+
127
+ Oliver Trajceski - [LinkedIn](https://mk.linkedin.com/in/oliver-trajceski-8a28b070) - oliver@akrinum.com
128
+
129
+ Project Link: [https://github.com/schnapsterdog/nuxt-chatgpt](https://github.com/schnapsterdog/nuxt-chatgpt)
130
+
131
+ ## Development
132
+
133
+ ```bash
134
+ # Install dependencies
135
+ npm install
136
+
137
+ # Generate type stubs
138
+ npm run dev:prepare
139
+
140
+ # Develop with the playground
141
+ npm run dev
142
+
143
+ # Build the playground
144
+ npm run dev:build
145
+
146
+ # Run ESLint
147
+ npm run lint
148
+
149
+ # Run Vitest
150
+ npm run test
151
+ npm run test:watch
152
+
153
+ # Release new version
154
+ npm run release
155
+ ```
156
+
157
+
158
+ <!-- ACKNOWLEDGMENTS -->
159
+ ## Acknowledgments
160
+
161
+ Use this space to list resources you find helpful and would like to give credit to. I've included a few of my favorites to kick things off!
162
+
163
+ * [Choose an Open Source License](https://choosealicense.com)
164
+ * [Img Shields](https://shields.io)
165
+ * [GitHub Pages](https://pages.github.com)
166
+
167
+ <!-- MARKDOWN LINKS & IMAGES -->
168
+ <!-- https://www.markdownguide.org/basic-syntax/#reference-style-links -->
169
+ [contributors-shield]: https://img.shields.io/github/contributors/schnapsterdog/nuxt-chatgpt.svg?style=for-the-badge
170
+ [contributors-url]: https://github.com/schnapsterdog/nuxt-chatgpt/graphs/contributors
171
+ [downloads-shield]: https://img.shields.io/npm/dw/nuxt-chatgpt.svg?style=for-the-badge
172
+ [downloads-url]: https://www.npmjs.com/package/nuxt-chatgpt
173
+ [stars-shield]: https://img.shields.io/github/stars/nuxt-chatgpt.svg?style=for-the-badge
174
+ [stars-url]: https://github.com/schnapsterdog/nuxt-chatgpt/stargazers
175
+ [issues-shield]: https://img.shields.io/github/issues/schnapsterdog/nuxt-chatgpt.svg?style=for-the-badge
176
+ [issues-url]: https://github.com/schnapsterdog/nuxt-chatgpt/issues
177
+ [license-shield]: https://img.shields.io/github/license/schnapsterdog/nuxt-chatgpt.svg?style=for-the-badge
178
+ [license-url]: https://github.com/schnapsterdog/nuxt-chatgpt/blob/master/LICENSE.txt
179
+ [linkedin-shield]: https://img.shields.io/badge/-LinkedIn-black.svg?style=for-the-badge&logo=linkedin&colorB=555
180
+ [linkedin-url]: https://mk.linkedin.com/in/oliver-trajceski-8a28b070
181
+ [Vue.js]: https://img.shields.io/badge/Vue.js-35495E?style=for-the-badge&logo=vuedotjs&logoColor=4FC08D
182
+ [Vue-url]: https://vuejs.org/
@@ -0,0 +1,5 @@
1
+ module.exports = function(...args) {
2
+ return import('./module.mjs').then(m => m.default.call(this, ...args))
3
+ }
4
+ const _meta = module.exports.meta = require('./module.json')
5
+ module.exports.getMeta = () => Promise.resolve(_meta)
@@ -0,0 +1,19 @@
1
+ import * as _nuxt_schema from '@nuxt/schema';
2
+
3
+ interface ModuleOptions {
4
+ /**
5
+ * Set chatGPT apiKey
6
+ * @default undefined
7
+ * @description Set your chatGPT apiKey
8
+ */
9
+ apiKey?: string;
10
+ /**
11
+ * Setting to `false` disables the module.
12
+ * @default true
13
+ * @description Use this setting to disable the module.
14
+ */
15
+ isEnabled?: boolean;
16
+ }
17
+ declare const _default: _nuxt_schema.NuxtModule<ModuleOptions>;
18
+
19
+ export { ModuleOptions, _default as default };
@@ -0,0 +1,8 @@
1
+ {
2
+ "name": "nuxt-chatgpt",
3
+ "configKey": "chatgpt",
4
+ "compatibility": {
5
+ "nuxt": "^3.0.0"
6
+ },
7
+ "version": "0.1.0"
8
+ }
@@ -0,0 +1,42 @@
1
+ import { fileURLToPath } from 'url';
2
+ import { defineNuxtModule, createResolver, addImportsDir, addServerHandler } from '@nuxt/kit';
3
+
4
+ const configKey = "chatgpt";
5
+ const moduleName = "nuxt-chatgpt";
6
+ const nuxtVersion = "^3.0.0";
7
+
8
+ const module = defineNuxtModule({
9
+ meta: {
10
+ name: moduleName,
11
+ configKey,
12
+ compatibility: {
13
+ nuxt: nuxtVersion
14
+ }
15
+ },
16
+ // Default configuration options of the Nuxt module
17
+ defaults: {
18
+ isEnabled: true
19
+ },
20
+ setup(options, nuxt) {
21
+ const { resolve } = createResolver(import.meta.url);
22
+ const runtimeDir = fileURLToPath(new URL("./runtime", import.meta.url));
23
+ if (!options.apiKey) {
24
+ return console.warn(`[${moduleName}]: No '${configKey}.apiKey' option provided!`);
25
+ }
26
+ if (!options.isEnabled) {
27
+ return console.warn(`[${moduleName}] module is disabled and will not be loaded.`);
28
+ }
29
+ nuxt.options.runtimeConfig.public[configKey] = {
30
+ apiKey: options.apiKey
31
+ };
32
+ addImportsDir(resolve("./runtime/composables"));
33
+ addServerHandler({
34
+ route: "/api/chatgpt",
35
+ method: "post",
36
+ handler: resolve(runtimeDir, "server/api/chatgpt")
37
+ });
38
+ nuxt.options.build.transpile.push(runtimeDir);
39
+ }
40
+ });
41
+
42
+ export { module as default };
@@ -0,0 +1,2 @@
1
+ import type { IChatgptClient } from "../types";
2
+ export declare const useChatgpt: () => IChatgptClient;
@@ -0,0 +1,19 @@
1
+ import { createError } from "h3";
2
+ import { getBody, getHeaders } from "../utils/index.mjs";
3
+ export const useChatgpt = () => {
4
+ const send = async (message) => {
5
+ try {
6
+ return await $fetch("/api/chatgpt", {
7
+ method: "POST",
8
+ headers: getHeaders(),
9
+ body: getBody(message)
10
+ });
11
+ } catch (error) {
12
+ throw createError({
13
+ statusCode: 500,
14
+ message: "Failed to forward request to server"
15
+ });
16
+ }
17
+ };
18
+ return { send };
19
+ };
@@ -0,0 +1,2 @@
1
+ declare const _default: any;
2
+ export default _default;
@@ -0,0 +1,27 @@
1
+ import { createError } from "h3";
2
+ import { Configuration, OpenAIApi } from "openai";
3
+ const config = useRuntimeConfig();
4
+ export default defineEventHandler(async (event) => {
5
+ const { prompt } = await readBody(event);
6
+ const configuration = new Configuration({
7
+ apiKey: config.public.chatgpt.apiKey
8
+ });
9
+ const openai = new OpenAIApi(configuration);
10
+ try {
11
+ const response = await openai.createCompletion({
12
+ model: "text-davinci-003",
13
+ prompt,
14
+ temperature: 0.5,
15
+ max_tokens: 2048,
16
+ top_p: 1,
17
+ frequency_penalty: 0,
18
+ presence_penalty: 0
19
+ });
20
+ return response.data.choices[0].text?.slice(2);
21
+ } catch (error) {
22
+ throw createError({
23
+ statusCode: 500,
24
+ message: "Failed to forward request to ChatGPT API"
25
+ });
26
+ }
27
+ });
@@ -0,0 +1,12 @@
1
+ export interface IHeaders {
2
+ [key: string]: string;
3
+ }
4
+
5
+ export interface IChatgptClient {
6
+ send ( IMessage ) : Promise
7
+ }
8
+
9
+ export interface IMessage {
10
+ message: string
11
+ }
12
+
@@ -0,0 +1,3 @@
1
+ import type { IHeaders, IMessage } from "../types";
2
+ export declare const getBody: (message: IMessage) => string;
3
+ export declare const getHeaders: () => IHeaders;
@@ -0,0 +1,8 @@
1
+ export const getBody = (message) => {
2
+ return JSON.stringify({ prompt: message });
3
+ };
4
+ export const getHeaders = () => {
5
+ return {
6
+ "Content-Type": "application/json"
7
+ };
8
+ };
@@ -0,0 +1,10 @@
1
+
2
+ import { ModuleOptions } from './module'
3
+
4
+ declare module '@nuxt/schema' {
5
+ interface NuxtConfig { ['chatgpt']?: Partial<ModuleOptions> }
6
+ interface NuxtOptions { ['chatgpt']?: ModuleOptions }
7
+ }
8
+
9
+
10
+ export { ModuleOptions, default } from './module'
package/package.json ADDED
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "nuxt-chatgpt",
3
+ "version": "0.1.0",
4
+ "description": "ChatGPT integration for Nuxt 3",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "homepage": "https://vuemadness.com.com/nuxt-chatgpt",
8
+ "bugs": {
9
+ "url": "https://github.com/schnapsterdog/nuxt-chatgpt/issues"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/schnapsterdog/nuxt-chatgpt"
14
+ },
15
+ "contributors": [
16
+ {
17
+ "name": "Oliver Trajceski (@schnapsterdog)"
18
+ }
19
+ ],
20
+ "author": {
21
+ "name": "Oliver Trajceski",
22
+ "email": "oliver@akrinum.com"
23
+ },
24
+ "keywords": [
25
+ "vue3",
26
+ "nuxt3",
27
+ "nuxt",
28
+ "nuxt.js",
29
+ "nuxt-chatgpt"
30
+ ],
31
+ "exports": {
32
+ ".": {
33
+ "types": "./dist/types.d.ts",
34
+ "import": "./dist/module.mjs",
35
+ "require": "./dist/module.cjs"
36
+ }
37
+ },
38
+ "main": "./dist/module.cjs",
39
+ "types": "./dist/types.d.ts",
40
+ "files": [
41
+ "dist"
42
+ ],
43
+ "scripts": {
44
+ "prepack": "nuxt-module-build",
45
+ "dev": "nuxi dev playground",
46
+ "dev:build": "nuxi build playground",
47
+ "dev:generate": "nuxi generate playground",
48
+ "dev:prepare": "nuxt-module-build --stub && nuxi prepare playground",
49
+ "dev:preview": "nuxi preview playground",
50
+ "release": "npm run lint && npm run test && npm run prepack && changelogen --release && npm publish && git push --follow-tags",
51
+ "lint": "eslint .",
52
+ "test": "vitest run",
53
+ "test:watch": "vitest watch"
54
+ },
55
+ "dependencies": {
56
+ "@nuxt/kit": "^3.2.3",
57
+ "openai": "^3.2.1"
58
+ },
59
+ "devDependencies": {
60
+ "@nuxt/eslint-config": "^0.1.1",
61
+ "@nuxt/module-builder": "^0.2.1",
62
+ "@nuxt/schema": "^3.2.3",
63
+ "@nuxt/test-utils": "^3.2.3",
64
+ "changelogen": "^0.5.1",
65
+ "eslint": "^8.36.0",
66
+ "nuxt": "^3.2.3",
67
+ "vitest": "^0.29.2"
68
+ }
69
+ }