aistv 1.3.6__tar.gz → 1.3.8__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: aistv
3
- Version: 1.3.6
3
+ Version: 1.3.8
4
4
  Summary: STV AI Chatbot Library for Python
5
5
  Home-page: https://github.com/phuctrong1tuv
6
6
  Author: Trọng Phúc
@@ -85,12 +85,21 @@ while True:
85
85
  print("AI STV:", reply)
86
86
 
87
87
  ```
88
- ---
88
+ 🧪 Ví dụ
89
+ ```python
90
+ from aistv import aistv
89
91
 
90
- ⚙️ Tuỳ chỉnh nâng cao
92
+ Token=TOKEN_API
93
+ bot = aistv(token)
91
94
 
92
- custom_prompt = "Bạn là STV, một trợ lý AI thân thiện, giao tiếp bằng tiếng Việt."
93
- bot = aistv(token, system_prompt=custom_prompt)
95
+ while True:
96
+ user_input = input("Bạn: ")
97
+ if user_input.lower() == "exit":
98
+ break
99
+ reply = bot.chat(user_input)
100
+ print("AI STV:", reply)
101
+
102
+ ```
94
103
 
95
104
 
96
105
  ---
@@ -185,7 +194,21 @@ pip install aistv
185
194
  reply = bot.chat(user_input)
186
195
  print("AI STV:", reply)
187
196
  ```
197
+ 🧪 Example
198
+ ```python
199
+ from aistv import aistv
188
200
 
201
+ Token=TOKEN_API
202
+ bot = aistv(token)
203
+
204
+ while True:
205
+ user_input = input("Bạn: ")
206
+ if user_input.lower() == "exit":
207
+ break
208
+ reply = bot.chat(user_input)
209
+ print("AI STV:", reply)
210
+
211
+ ```
189
212
  ---
190
213
 
191
214
  ⚙️ Advanced Prompt Customization
@@ -55,12 +55,21 @@ while True:
55
55
  print("AI STV:", reply)
56
56
 
57
57
  ```
58
- ---
58
+ 🧪 Ví dụ
59
+ ```python
60
+ from aistv import aistv
59
61
 
60
- ⚙️ Tuỳ chỉnh nâng cao
62
+ Token=TOKEN_API
63
+ bot = aistv(token)
61
64
 
62
- custom_prompt = "Bạn là STV, một trợ lý AI thân thiện, giao tiếp bằng tiếng Việt."
63
- bot = aistv(token, system_prompt=custom_prompt)
65
+ while True:
66
+ user_input = input("Bạn: ")
67
+ if user_input.lower() == "exit":
68
+ break
69
+ reply = bot.chat(user_input)
70
+ print("AI STV:", reply)
71
+
72
+ ```
64
73
 
65
74
 
66
75
  ---
@@ -155,7 +164,21 @@ pip install aistv
155
164
  reply = bot.chat(user_input)
156
165
  print("AI STV:", reply)
157
166
  ```
167
+ 🧪 Example
168
+ ```python
169
+ from aistv import aistv
158
170
 
171
+ Token=TOKEN_API
172
+ bot = aistv(token)
173
+
174
+ while True:
175
+ user_input = input("Bạn: ")
176
+ if user_input.lower() == "exit":
177
+ break
178
+ reply = bot.chat(user_input)
179
+ print("AI STV:", reply)
180
+
181
+ ```
159
182
  ---
160
183
 
161
184
  ⚙️ Advanced Prompt Customization
@@ -1,10 +1,11 @@
1
- # aistv.py
2
1
  import subprocess
3
2
  import sys
4
3
  import sqlite3
5
4
  import time
5
+ import requests
6
+ from groq import Groq
6
7
 
7
- # Tự cài thư viện nếu thiếu
8
+ # Tự động cài thư viện nếu thiếu
8
9
  def install_package(pkg):
9
10
  try:
10
11
  __import__(pkg)
@@ -14,69 +15,47 @@ def install_package(pkg):
14
15
  install_package("groq")
15
16
  install_package("requests")
16
17
 
17
- from groq import Groq
18
+ # Cấu hình
19
+ DB_FILE = "usage.db"
20
+ SPAM_DELAY_SECONDS = 5
21
+ FREE_MAX_REQUESTS = 20
22
+ NORMAL_MAX_REQUESTS = 50
23
+ VIP_MAX_REQUESTS = None # Không giới hạn
18
24
 
19
25
  API_KEYS = [
20
26
  "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",
21
- # Thêm key phụ tại đây nếu muốn:
22
- # "your_second_key",
27
+ "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",
23
28
  ]
24
29
 
25
- DB_FILE = "usage.db"
26
- SPAM_DELAY_SECONDS = 5
27
-
28
- FREE_MAX_REQUESTS = 20
29
- NORMAL_MAX_REQUESTS = 30
30
- VIP_MAX_REQUESTS = None
31
-
32
- TOKEN_VIP_SET = {
33
- "aistv",
34
- "phuc",
35
- }
36
-
37
- TOKEN_NORMAL_SET = {
38
- "tokengsk_...",
39
- }
40
-
41
- MODEL_MAP = {
42
- "Chat-ai-stv-3.5": "meta-llama/llama-3-8b-instruct",
43
- "Chat-ai-stv-4.0": "meta-llama/llama-3-70b-instruct",
44
- "Chat-ai-stv-2.3": "meta-llama/llama-4-maverick-17b-128e-instruct",
45
- "Chat-ai-stv-4.5": "mistralai/mixtral-8x7b-instruct",
46
- "Chat-ai-stv-5.0": "google/gemma-7b-it",
47
- }
30
+ # Danh sách token
31
+ TOKEN_VIP_SET = {"aistv", "phuc"}
32
+ TOKEN_NORMAL_SET = {"token_sk-or-v1-56d24544a83100b354a57f82ea83fc31e6ae249749df44a912e35769123ea5d5", "another_token..."}
48
33
 
49
34
  class STVBot:
50
- def __init__(self, token, model, system_prompt=None):
51
- if token is None:
52
- raise ValueError("Bạn phải truyền token khi khởi tạo aistv")
53
- if model is None:
54
- raise ValueError("Bạn phải truyền model khi khởi tạo aistv")
55
-
56
- # Bắt buộc model phải nằm trong MODEL_MAP, dùng key nguyên gốc
57
- if model not in MODEL_MAP:
58
- raise ValueError(f"Mô hình không hợp lệ: {model}. Dùng: {list(MODEL_MAP.keys())}")
59
-
60
- self.model_key = model
61
- self.model = MODEL_MAP[self.model_key]
62
- self.system_prompt = system_prompt or "Tôi là AI STV, được phát triển bởi Trọng Phúc."
35
+ def __init__(self, token: str = None, system_prompt: str = None):
63
36
  self.token = token
64
-
65
- self.api_key_index = 0
66
- self.client = Groq(api_key=API_KEYS[self.api_key_index])
67
-
37
+ self.system_prompt = system_prompt or "Tôi là AI STV, được phát triển bởi Trọng Phúc."
38
+ self.api_keys = API_KEYS
39
+ self.api_index = 0
40
+ self.client = self._create_client()
41
+ self.model = "meta-llama/llama-4-maverick-17b-128e-instruct"
68
42
  self.history = [{"role": "system", "content": self.system_prompt}]
69
43
 
70
- if token in TOKEN_VIP_SET:
71
- self.max_requests = VIP_MAX_REQUESTS
44
+ # Xác định user & giới hạn
45
+ if not token:
46
+ self.user_id = "free_user"
47
+ self.max_requests = FREE_MAX_REQUESTS
48
+ elif token in TOKEN_VIP_SET:
72
49
  self.user_id = token
50
+ self.max_requests = VIP_MAX_REQUESTS
73
51
  elif token in TOKEN_NORMAL_SET:
74
- self.max_requests = NORMAL_MAX_REQUESTS
75
52
  self.user_id = token
53
+ self.max_requests = NORMAL_MAX_REQUESTS
76
54
  else:
77
- self.max_requests = FREE_MAX_REQUESTS
78
55
  self.user_id = "free_user"
56
+ self.max_requests = FREE_MAX_REQUESTS
79
57
 
58
+ # CSDL lưu lượt dùng
80
59
  self.conn = sqlite3.connect(DB_FILE, check_same_thread=False)
81
60
  self.cursor = self.conn.cursor()
82
61
  self.cursor.execute('''
@@ -90,6 +69,9 @@ class STVBot:
90
69
  self.conn.commit()
91
70
  self._init_user()
92
71
 
72
+ def _create_client(self):
73
+ return Groq(api_key=self.api_keys[self.api_index])
74
+
93
75
  def _init_user(self):
94
76
  today = time.strftime("%Y-%m-%d")
95
77
  self.cursor.execute("SELECT * FROM usage WHERE user_id = ?", (self.user_id,))
@@ -103,7 +85,11 @@ class STVBot:
103
85
  def _get_usage(self):
104
86
  self.cursor.execute("SELECT count, last_time, last_date FROM usage WHERE user_id = ?", (self.user_id,))
105
87
  row = self.cursor.fetchone()
106
- return {"count": row[0], "last_time": row[1], "last_date": row[2]} if row else {"count": 0, "last_time": 0, "last_date": time.strftime("%Y-%m-%d")}
88
+ return {
89
+ "count": row[0],
90
+ "last_time": row[1],
91
+ "last_date": row[2]
92
+ } if row else {"count": 0, "last_time": 0, "last_date": time.strftime("%Y-%m-%d")}
107
93
 
108
94
  def _save_usage(self, count, last_time, last_date):
109
95
  self.cursor.execute(
@@ -112,51 +98,53 @@ class STVBot:
112
98
  )
113
99
  self.conn.commit()
114
100
 
115
- def _switch_api_key(self):
116
- if self.api_key_index + 1 < len(API_KEYS):
117
- self.api_key_index += 1
118
- self.client = Groq(api_key=API_KEYS[self.api_key_index])
119
- return True
120
- return False
121
-
122
101
  def chat(self, prompt: str) -> str:
123
102
  usage = self._get_usage()
124
103
  now = time.time()
125
104
  today = time.strftime("%Y-%m-%d")
126
105
 
106
+ # Reset lượt mỗi ngày
127
107
  if usage["last_date"] != today:
128
108
  usage["count"] = 0
129
109
  usage["last_date"] = today
130
110
 
111
+ # Kiểm tra giới hạn lượt dùng
131
112
  if self.max_requests is not None and usage["count"] >= self.max_requests:
132
- return f"⚠️ Đã hết {self.max_requests} lượt/ngày. Thử lại mai hoặc xin thêm quyền. https://discord.gg/Ze7RTExgdv"
113
+ return (
114
+ f"⚠️ Bạn đã dùng hết {self.max_requests} lượt trong ngày.\n"
115
+ "Hãy thử lại vào ngày mai hoặc liên hệ để nâng cấp quyền. https://discord.gg/Ze7RTExgdv"
116
+ )
133
117
 
118
+ # Kiểm tra spam
134
119
  if now - usage["last_time"] < SPAM_DELAY_SECONDS:
135
- wait_time = SPAM_DELAY_SECONDS - int(now - usage["last_time"])
136
- return f"⚠️ Vui lòng chờ thêm {wait_time} giây giữa các câu hỏi."
120
+ wait = SPAM_DELAY_SECONDS - int(now - usage["last_time"])
121
+ return f"⚠️ Vui lòng đợi {wait} giây nữa trước khi gửi câu hỏi tiếp theo."
137
122
 
138
123
  self.history.append({"role": "user", "content": prompt})
124
+ tries = len(self.api_keys)
139
125
 
140
- while True:
126
+ for _ in range(tries):
141
127
  try:
142
- chat_completion = self.client.chat.completions.create(
128
+ self.client = self._create_client()
129
+ response = self.client.chat.completions.create(
143
130
  messages=self.history,
144
131
  model=self.model,
145
- stream=False,
132
+ stream=False
146
133
  )
147
- reply = chat_completion.choices[0].message.content
134
+ reply = response.choices[0].message.content.strip()
148
135
  self.history.append({"role": "assistant", "content": reply})
149
136
 
137
+ # Lưu lượt dùng (trừ VIP)
150
138
  if self.max_requests is not None:
151
139
  usage["count"] += 1
152
140
  usage["last_time"] = now
153
141
  self._save_usage(usage["count"], usage["last_time"], usage["last_date"])
154
142
 
155
- return reply.strip()
143
+ return reply
156
144
 
157
145
  except Exception as e:
158
- if "quota" in str(e).lower() or "token" in str(e).lower():
159
- if not self._switch_api_key():
160
- return f"⚠️ API hết hạn hoặc lỗi: {e}"
161
- else:
162
- return f"⚠️ Lỗi khi gọi API: {e}"
146
+ self.api_index += 1
147
+ if self.api_index >= len(self.api_keys):
148
+ return f"⚠️ Tất cả API key đều lỗi hoặc hết lượt.\nLỗi cuối cùng: {e}"
149
+
150
+ return "⚠️ Đã xảy ra lỗi. Vui lòng thử lại sau."
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: aistv
3
- Version: 1.3.6
3
+ Version: 1.3.8
4
4
  Summary: STV AI Chatbot Library for Python
5
5
  Home-page: https://github.com/phuctrong1tuv
6
6
  Author: Trọng Phúc
@@ -85,12 +85,21 @@ while True:
85
85
  print("AI STV:", reply)
86
86
 
87
87
  ```
88
- ---
88
+ 🧪 Ví dụ
89
+ ```python
90
+ from aistv import aistv
89
91
 
90
- ⚙️ Tuỳ chỉnh nâng cao
92
+ Token=TOKEN_API
93
+ bot = aistv(token)
91
94
 
92
- custom_prompt = "Bạn là STV, một trợ lý AI thân thiện, giao tiếp bằng tiếng Việt."
93
- bot = aistv(token, system_prompt=custom_prompt)
95
+ while True:
96
+ user_input = input("Bạn: ")
97
+ if user_input.lower() == "exit":
98
+ break
99
+ reply = bot.chat(user_input)
100
+ print("AI STV:", reply)
101
+
102
+ ```
94
103
 
95
104
 
96
105
  ---
@@ -185,7 +194,21 @@ pip install aistv
185
194
  reply = bot.chat(user_input)
186
195
  print("AI STV:", reply)
187
196
  ```
197
+ 🧪 Example
198
+ ```python
199
+ from aistv import aistv
188
200
 
201
+ Token=TOKEN_API
202
+ bot = aistv(token)
203
+
204
+ while True:
205
+ user_input = input("Bạn: ")
206
+ if user_input.lower() == "exit":
207
+ break
208
+ reply = bot.chat(user_input)
209
+ print("AI STV:", reply)
210
+
211
+ ```
189
212
  ---
190
213
 
191
214
  ⚙️ Advanced Prompt Customization
@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
2
2
 
3
3
  setup(
4
4
  name="aistv",
5
- version="1.3.6",
5
+ version="1.3.8",
6
6
  description="STV AI Chatbot Library for Python",
7
7
  long_description=open("README.md").read(),
8
8
  long_description_content_type="text/markdown",
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes