aistv 1.3.4__py3-none-any.whl → 1.3.6__py3-none-any.whl

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.
aistv/core.py CHANGED
@@ -1,9 +1,10 @@
1
+ # aistv.py
1
2
  import subprocess
2
3
  import sys
3
4
  import sqlite3
4
5
  import time
5
6
 
6
- # Tự cài groq requests nếu chưa có
7
+ # Tự cài thư viện nếu thiếu
7
8
  def install_package(pkg):
8
9
  try:
9
10
  __import__(pkg)
@@ -13,58 +14,69 @@ def install_package(pkg):
13
14
  install_package("groq")
14
15
  install_package("requests")
15
16
 
16
- import requests
17
17
  from groq import Groq
18
18
 
19
+ API_KEYS = [
20
+ "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",
21
+ # Thêm key phụ tại đây nếu muốn:
22
+ # "your_second_key",
23
+ ]
24
+
19
25
  DB_FILE = "usage.db"
20
26
  SPAM_DELAY_SECONDS = 5
21
27
 
22
- # Giới hạn theo loại token
23
28
  FREE_MAX_REQUESTS = 20
24
29
  NORMAL_MAX_REQUESTS = 30
25
- VIP_MAX_REQUESTS = None # Không giới hạn
30
+ VIP_MAX_REQUESTS = None
26
31
 
27
- # Ví dụ token thường và token VIP bạn quản lý
28
32
  TOKEN_VIP_SET = {
29
33
  "aistv",
30
34
  "phuc",
31
35
  }
32
36
 
33
37
  TOKEN_NORMAL_SET = {
34
- "aistvgsk_wr9rnhdGCQYCaeAEFQusWGdyb3FYF4LVKrxM0I9JDSGkZIVIymwPgsk_wr9rnhdGCQYCaeAEFQusWGdyb3FYF4LVKrxM0I9JDSGkZIVIymwPgsk_wr9rnhdGCQYCaeAEFQusWGdyb3FYF4LVKrxM0I9JDSGkZIVIymwPgsk_wr9rnhdGCQYCaeAEFQusWGdyb3FYF4LVKrxM0I9JDSGkZIVIymwP",
35
- "tokengsk_wr9rnhdGCQYCaeAEFQusWGdyb3FYF4LVKrxM0I9JDSGkZIVIymwP",
38
+ "tokengsk_...",
36
39
  }
37
40
 
38
- API_KEY = "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"
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
+ }
39
48
 
40
49
  class STVBot:
41
- def __init__(self, token: str = None, system_prompt: str = None):
42
- """
43
- token: None (free user), or token string (normal or VIP)
44
- """
45
- if not system_prompt:
46
- system_prompt = "Tôi là AI STV, được phát triển bởi Trọng Phúc."
47
- self.client = Groq(api_key=API_KEY)
48
- self.system_prompt = system_prompt
49
- self.model = "meta-llama/llama-4-maverick-17b-128e-instruct"
50
- self.history = [{"role": "system", "content": self.system_prompt}]
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())}")
51
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."
52
63
  self.token = token
53
- if token is None:
54
- self.max_requests = FREE_MAX_REQUESTS
55
- self.user_id = "free_user"
56
- elif token in TOKEN_VIP_SET:
64
+
65
+ self.api_key_index = 0
66
+ self.client = Groq(api_key=API_KEYS[self.api_key_index])
67
+
68
+ self.history = [{"role": "system", "content": self.system_prompt}]
69
+
70
+ if token in TOKEN_VIP_SET:
57
71
  self.max_requests = VIP_MAX_REQUESTS
58
72
  self.user_id = token
59
73
  elif token in TOKEN_NORMAL_SET:
60
74
  self.max_requests = NORMAL_MAX_REQUESTS
61
75
  self.user_id = token
62
76
  else:
63
- # Token không hợp lệ tính free
64
77
  self.max_requests = FREE_MAX_REQUESTS
65
78
  self.user_id = "free_user"
66
79
 
67
- # Kết nối DB SQLite
68
80
  self.conn = sqlite3.connect(DB_FILE, check_same_thread=False)
69
81
  self.cursor = self.conn.cursor()
70
82
  self.cursor.execute('''
@@ -76,15 +88,12 @@ class STVBot:
76
88
  )
77
89
  ''')
78
90
  self.conn.commit()
79
-
80
- # Khởi tạo dữ liệu user nếu chưa có
81
91
  self._init_user()
82
92
 
83
93
  def _init_user(self):
84
94
  today = time.strftime("%Y-%m-%d")
85
95
  self.cursor.execute("SELECT * FROM usage WHERE user_id = ?", (self.user_id,))
86
- row = self.cursor.fetchone()
87
- if not row:
96
+ if not self.cursor.fetchone():
88
97
  self.cursor.execute(
89
98
  "INSERT INTO usage (user_id, count, last_time, last_date) VALUES (?, ?, ?, ?)",
90
99
  (self.user_id, 0, 0, today)
@@ -94,10 +103,7 @@ class STVBot:
94
103
  def _get_usage(self):
95
104
  self.cursor.execute("SELECT count, last_time, last_date FROM usage WHERE user_id = ?", (self.user_id,))
96
105
  row = self.cursor.fetchone()
97
- if row:
98
- return {"count": row[0], "last_time": row[1], "last_date": row[2]}
99
- else:
100
- return {"count": 0, "last_time": 0, "last_date": time.strftime("%Y-%m-%d")}
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")}
101
107
 
102
108
  def _save_usage(self, count, last_time, last_date):
103
109
  self.cursor.execute(
@@ -106,43 +112,51 @@ class STVBot:
106
112
  )
107
113
  self.conn.commit()
108
114
 
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
+
109
122
  def chat(self, prompt: str) -> str:
110
123
  usage = self._get_usage()
111
124
  now = time.time()
112
125
  today = time.strftime("%Y-%m-%d")
113
126
 
114
- # Reset count nếu khác ngày
115
127
  if usage["last_date"] != today:
116
128
  usage["count"] = 0
117
129
  usage["last_date"] = today
118
130
 
119
131
  if self.max_requests is not None and usage["count"] >= self.max_requests:
120
- return (
121
- f"⚠️ Bạn đã sử dụng hết giới hạn {self.max_requests} câu hỏi trong ngày.\n"
122
- "Vui lòng thử lại vào ngày mai hoặc liên hệ để được cấp thêm quyền.https://discord.gg/Ze7RTExgdv"
123
- )
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"
124
133
 
125
134
  if now - usage["last_time"] < SPAM_DELAY_SECONDS:
126
135
  wait_time = SPAM_DELAY_SECONDS - int(now - usage["last_time"])
127
- return f"⚠️ Vui lòng chờ thêm {wait_time} giây giữa mỗi câu hỏi."
136
+ return f"⚠️ Vui lòng chờ thêm {wait_time} giây giữa các câu hỏi."
128
137
 
129
138
  self.history.append({"role": "user", "content": prompt})
130
139
 
131
- try:
132
- chat_completion = self.client.chat.completions.create(
133
- messages=self.history,
134
- model=self.model,
135
- stream=False,
136
- )
137
- reply = chat_completion.choices[0].message.content
138
- self.history.append({"role": "assistant", "content": reply})
139
-
140
- if self.max_requests is not None:
141
- usage["count"] += 1
142
- usage["last_time"] = now
143
- self._save_usage(usage["count"], usage["last_time"], usage["last_date"])
144
-
145
- return reply.strip()
146
-
147
- except Exception as e:
148
- return f"⚠️ Lỗi khi gọi API: {e}"
140
+ while True:
141
+ try:
142
+ chat_completion = self.client.chat.completions.create(
143
+ messages=self.history,
144
+ model=self.model,
145
+ stream=False,
146
+ )
147
+ reply = chat_completion.choices[0].message.content
148
+ self.history.append({"role": "assistant", "content": reply})
149
+
150
+ if self.max_requests is not None:
151
+ usage["count"] += 1
152
+ usage["last_time"] = now
153
+ self._save_usage(usage["count"], usage["last_time"], usage["last_date"])
154
+
155
+ return reply.strip()
156
+
157
+ 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}"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: aistv
3
- Version: 1.3.4
3
+ Version: 1.3.6
4
4
  Summary: STV AI Chatbot Library for Python
5
5
  Home-page: https://github.com/phuctrong1tuv
6
6
  Author: Trọng Phúc
@@ -0,0 +1,8 @@
1
+ aistv/__init__.py,sha256=G1ZBskWNkKKEK9s51RrBuK-S2_d3Fw8UaLk0RKsbuKw,33
2
+ aistv/aistv.py,sha256=8OClk7fg1g41NtW7ELj83-g0JF5fV1fQy0dJh2qlV8s,190
3
+ aistv/core.py,sha256=eEbA_L_DB2ZVzn1SQ_cXtK1ZIxT2lJ0TbB6FejJp1U4,5821
4
+ aistv/trainer.py,sha256=oqIHGAJAJgIdw3P6_BrnXt3JMXYwsGHfGVaJ_e635YU,1125
5
+ aistv-1.3.6.dist-info/METADATA,sha256=eXAXFGLBGnxzNtunmb_HxQ9w4oRGy7qqzn90ZnPUNb0,4244
6
+ aistv-1.3.6.dist-info/WHEEL,sha256=zaaOINJESkSfm_4HQVc5ssNzHCPXhJm0kEUakpsEHaU,91
7
+ aistv-1.3.6.dist-info/top_level.txt,sha256=P6gyYzXFvBPESQyFZQSi6Rs8v5ALhw-d4UN2Jo9Pkgw,6
8
+ aistv-1.3.6.dist-info/RECORD,,
@@ -1,8 +0,0 @@
1
- aistv/__init__.py,sha256=G1ZBskWNkKKEK9s51RrBuK-S2_d3Fw8UaLk0RKsbuKw,33
2
- aistv/aistv.py,sha256=8OClk7fg1g41NtW7ELj83-g0JF5fV1fQy0dJh2qlV8s,190
3
- aistv/core.py,sha256=U76vDxmN2310PCGV5YE8DXhkyutKN-6Dmk8ISHWeLsY,5437
4
- aistv/trainer.py,sha256=oqIHGAJAJgIdw3P6_BrnXt3JMXYwsGHfGVaJ_e635YU,1125
5
- aistv-1.3.4.dist-info/METADATA,sha256=xk-PfzOHD5PhC_GUDsa1EpM0IXHR_Gn2JgJ6MtXRCg8,4244
6
- aistv-1.3.4.dist-info/WHEEL,sha256=zaaOINJESkSfm_4HQVc5ssNzHCPXhJm0kEUakpsEHaU,91
7
- aistv-1.3.4.dist-info/top_level.txt,sha256=P6gyYzXFvBPESQyFZQSi6Rs8v5ALhw-d4UN2Jo9Pkgw,6
8
- aistv-1.3.4.dist-info/RECORD,,
File without changes