mehdi-akbari-ai-assistant-free 0.9.7 → 0.9.8
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/README.md +112 -6
- package/dist/react.js +1 -1
- package/dist/react.mjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -78,7 +78,7 @@ expand_less
|
|
|
78
78
|
import 'mehdi-akbari-ai-assistant-free/styles.css';
|
|
79
79
|
Step 2: Use the Component
|
|
80
80
|
|
|
81
|
-
Import the AiAssistant component and place it in your desired page.
|
|
81
|
+
Import the AiAssistant component and place it in your desired page.
|
|
82
82
|
|
|
83
83
|
code
|
|
84
84
|
JavaScript
|
|
@@ -97,6 +97,60 @@ export default function HomePage() {
|
|
|
97
97
|
</div>
|
|
98
98
|
);
|
|
99
99
|
}
|
|
100
|
+
Step 3: Create the API Endpoint
|
|
101
|
+
|
|
102
|
+
The AiAssistant component is a frontend-only tool. It needs a backend API route to securely communicate with the AI service. Here’s how to create the /api/chat endpoint using Next.js App Router.
|
|
103
|
+
|
|
104
|
+
Create the route file: In your Next.js project, create a new file at app/api/chat/route.ts (or .js).
|
|
105
|
+
|
|
106
|
+
Add the following code: This code uses the AiRobot class from this package to handle the server-side logic.
|
|
107
|
+
|
|
108
|
+
code
|
|
109
|
+
TypeScript
|
|
110
|
+
download
|
|
111
|
+
content_copy
|
|
112
|
+
expand_less
|
|
113
|
+
// app/api/chat/route.ts
|
|
114
|
+
import { NextResponse } from 'next/server';
|
|
115
|
+
import { AiRobot, Message } from 'mehdi-akbari-ai-assistant-free';
|
|
116
|
+
|
|
117
|
+
export async function POST(req: Request) {
|
|
118
|
+
try {
|
|
119
|
+
const { messages }: { messages: Message[] } = await req.json();
|
|
120
|
+
|
|
121
|
+
if (!messages) {
|
|
122
|
+
return NextResponse.json({ error: 'Messages are required' }, { status: 400 });
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Read the API key from environment variables for security
|
|
126
|
+
const apiKey = process.env.GROQ_API_KEY;
|
|
127
|
+
if (!apiKey) {
|
|
128
|
+
return NextResponse.json({ error: 'AI provider API key is not configured' }, { status: 500 });
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Initialize the robot with your API key
|
|
132
|
+
const robot = new AiRobot({ apiKey });
|
|
133
|
+
|
|
134
|
+
// Get the response from the AI
|
|
135
|
+
const content = await robot.chat(messages);
|
|
136
|
+
|
|
137
|
+
// Send the response back to the client
|
|
138
|
+
return NextResponse.json({ content });
|
|
139
|
+
|
|
140
|
+
} catch (error) {
|
|
141
|
+
console.error('API Route Error:', error);
|
|
142
|
+
return NextResponse.json({ error: 'An internal server error occurred' }, { status: 500 });
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
Set up Environment Variables: Create a file named .env.local in the root of your project and add your Groq API key:
|
|
147
|
+
|
|
148
|
+
code
|
|
149
|
+
Code
|
|
150
|
+
download
|
|
151
|
+
content_copy
|
|
152
|
+
expand_less
|
|
153
|
+
GROQ_API_KEY="YOUR_GROQ_API_KEY_HERE"
|
|
100
154
|
⚙️ Props (API Reference)
|
|
101
155
|
Prop Type Required Default Description
|
|
102
156
|
apiEndpoint string Yes - The URL of your API endpoint that handles chat requests.
|
|
@@ -105,7 +159,7 @@ welcomeMessage string No "Hello! How can I help you?" The initial message displa
|
|
|
105
159
|
lottieAnimationData any No A default robot animation The Lottie animation data. Can be a URL (string) or an imported JSON object.
|
|
106
160
|
🎨 Customization (Theming)
|
|
107
161
|
|
|
108
|
-
|
|
162
|
+
Override the following CSS variables in your project's main CSS file (e.g., globals.css) to match your brand identity.
|
|
109
163
|
|
|
110
164
|
code
|
|
111
165
|
CSS
|
|
@@ -186,7 +240,7 @@ expand_less
|
|
|
186
240
|
import 'mehdi-akbari-ai-assistant-free/styles.css';
|
|
187
241
|
مرحله ۲: استفاده از کامپوننت
|
|
188
242
|
|
|
189
|
-
کامپوننت AiAssistant را وارد کرده و آن را در صفحه مورد نظر خود قرار دهید.
|
|
243
|
+
کامپوننت AiAssistant را وارد کرده و آن را در صفحه مورد نظر خود قرار دهید.
|
|
190
244
|
|
|
191
245
|
code
|
|
192
246
|
JavaScript
|
|
@@ -205,6 +259,57 @@ export default function HomePage() {
|
|
|
205
259
|
</div>
|
|
206
260
|
);
|
|
207
261
|
}
|
|
262
|
+
مرحله ۳: ساخت API Endpoint
|
|
263
|
+
|
|
264
|
+
کامپوننت AiAssistant یک ابزار سمت کاربر (Frontend) است و برای ارتباط امن با سرویس هوش مصنوعی، به یک API در سمت سرور (Backend) نیاز دارد. در اینجا نحوه ساخت اندپوینت /api/chat با استفاده از Next.js App Router توضیح داده شده است.
|
|
265
|
+
|
|
266
|
+
۱. ایجاد فایل route: در پروژه Next.js خود، یک فایل جدید در مسیر app/api/chat/route.ts (یا .js) ایجاد کنید.
|
|
267
|
+
|
|
268
|
+
۲. اضافه کردن کد زیر: این کد از کلاس AiRobot که در همین پکیج قرار دارد برای مدیریت منطق سمت سرور استفاده میکند.
|
|
269
|
+
|
|
270
|
+
code
|
|
271
|
+
Code
|
|
272
|
+
download
|
|
273
|
+
content_copy
|
|
274
|
+
expand_less
|
|
275
|
+
```typescript
|
|
276
|
+
// app/api/chat/route.ts
|
|
277
|
+
import { NextResponse } from 'next/server';
|
|
278
|
+
import { AiRobot, Message } from 'mehdi-akbari-ai-assistant-free';
|
|
279
|
+
|
|
280
|
+
export async function POST(req: Request) {
|
|
281
|
+
try {
|
|
282
|
+
const { messages }: { messages: Message[] } = await req.json();
|
|
283
|
+
|
|
284
|
+
if (!messages) {
|
|
285
|
+
return NextResponse.json({ error: 'آرایه پیامها ضروری است' }, { status: 400 });
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// برای امنیت، کلید API را از متغیرهای محیطی بخوانید
|
|
289
|
+
const apiKey = process.env.GROQ_API_KEY;
|
|
290
|
+
if (!apiKey) {
|
|
291
|
+
return NextResponse.json({ error: 'کلید API برای سرویس هوش مصنوعی تنظیم نشده است' }, { status: 500 });
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// یک نمونه از ربات را با کلید API خود بسازید
|
|
295
|
+
const robot = new AiRobot({ apiKey });
|
|
296
|
+
|
|
297
|
+
// پاسخ را از هوش مصنوعی دریافت کنید
|
|
298
|
+
const content = await robot.chat(messages);
|
|
299
|
+
|
|
300
|
+
// پاسخ را به کلاینت برگردانید
|
|
301
|
+
return NextResponse.json({ content });
|
|
302
|
+
|
|
303
|
+
} catch (error) {
|
|
304
|
+
console.error('خطا در API Route:', error);
|
|
305
|
+
return NextResponse.json({ error: 'خطایی در سرور رخ داده است' }, { status: 500 });
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
۳. تنظیم متغیرهای محیطی: یک فایل با نام .env.local در ریشه اصلی پروژه خود ایجاد کرده و کلید API سرویس Groq را در آن قرار دهید:
|
|
311
|
+
GROQ_API_KEY="کلید_API_سرویس_GROQ_شما"
|
|
312
|
+
|
|
208
313
|
⚙️ پراپها (API Reference)
|
|
209
314
|
پراپ نوع ضروری پیشفرض توضیحات
|
|
210
315
|
apiEndpoint string بله - آدرس API Endpoint شما که درخواستهای چت به آن ارسال میشود.
|
|
@@ -233,7 +338,8 @@ expand_less
|
|
|
233
338
|
--ai-bot-bg: #F1F1F1; /* پسزمینه حباب پیام ربات */
|
|
234
339
|
--ai-user-bg: #E1F0FF; /* پسزمینه حباب پیام کاربر */
|
|
235
340
|
--ai-user-text: #0052D4; /* رنگ متن پیام کاربر */
|
|
236
|
-
}
|
|
237
|
-
|
|
341
|
+
}```
|
|
342
|
+
|
|
343
|
+
## 📜 لایسنس
|
|
238
344
|
|
|
239
|
-
این پروژه تحت لایسنس MIT منتشر شده است. برای اطلاعات بیشتر فایل LICENSE را مطالعه کنید.
|
|
345
|
+
این پروژه تحت لایسنس MIT منتشر شده است. برای اطلاعات بیشتر فایل `LICENSE` را مطالعه کنید.
|
package/dist/react.js
CHANGED
|
@@ -144,7 +144,7 @@ var ChatWindow = ({
|
|
|
144
144
|
}) => {
|
|
145
145
|
const [messages, setMessages] = (0, import_react3.useState)([]);
|
|
146
146
|
const [isLoading, setIsLoading] = (0, import_react3.useState)(false);
|
|
147
|
-
const version = "0.9.
|
|
147
|
+
const version = "0.9.8";
|
|
148
148
|
(0, import_react3.useEffect)(() => {
|
|
149
149
|
if (welcomeMessage && messages.length === 0) {
|
|
150
150
|
setMessages([{ role: "assistant", content: welcomeMessage }]);
|
package/dist/react.mjs
CHANGED
|
@@ -108,7 +108,7 @@ var ChatWindow = ({
|
|
|
108
108
|
}) => {
|
|
109
109
|
const [messages, setMessages] = useState2([]);
|
|
110
110
|
const [isLoading, setIsLoading] = useState2(false);
|
|
111
|
-
const version = "0.9.
|
|
111
|
+
const version = "0.9.8";
|
|
112
112
|
useEffect3(() => {
|
|
113
113
|
if (welcomeMessage && messages.length === 0) {
|
|
114
114
|
setMessages([{ role: "assistant", content: welcomeMessage }]);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mehdi-akbari-ai-assistant-free",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.8",
|
|
4
4
|
"description": "Professional AI Chatbot Library with self-contained CSS styles",
|
|
5
5
|
"homepage": "https://github.com/MehdiAkbariF/mehdi-akbari-ai-assistant-free#readme",
|
|
6
6
|
"repository": {
|