fix MiniMax api error (#1567)
Browse files### What problem does this PR solve?
#1353
### Type of change
- [x] Bug Fix (non-breaking change which fixes an issue)
---------
Co-authored-by: Zhedong Cen <[email protected]>
- api/db/db_models.py +7 -1
- conf/llm_factories.json +6 -6
- rag/llm/chat_model.py +78 -6
api/db/db_models.py
CHANGED
@@ -558,7 +558,7 @@ class TenantLLM(DataBaseModel):
|
|
558 |
null=True,
|
559 |
help_text="LLM name",
|
560 |
default="")
|
561 |
-
api_key = CharField(max_length=
|
562 |
api_base = CharField(max_length=255, null=True, help_text="API Base")
|
563 |
used_tokens = IntegerField(default=0)
|
564 |
|
@@ -885,3 +885,9 @@ def migrate_db():
|
|
885 |
)
|
886 |
except Exception as e:
|
887 |
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
558 |
null=True,
|
559 |
help_text="LLM name",
|
560 |
default="")
|
561 |
+
api_key = CharField(max_length=1024, null=True, help_text="API KEY")
|
562 |
api_base = CharField(max_length=255, null=True, help_text="API Base")
|
563 |
used_tokens = IntegerField(default=0)
|
564 |
|
|
|
885 |
)
|
886 |
except Exception as e:
|
887 |
pass
|
888 |
+
try:
|
889 |
+
migrate(
|
890 |
+
migrator.alter_column_type('tenant_llm', 'api_key', CharField(max_length=1024, null=True, help_text="API KEY"))
|
891 |
+
)
|
892 |
+
except Exception as e:
|
893 |
+
pass
|
conf/llm_factories.json
CHANGED
@@ -433,37 +433,37 @@
|
|
433 |
]
|
434 |
},
|
435 |
{
|
436 |
-
"name": "
|
437 |
"logo": "",
|
438 |
"tags": "LLM,TEXT EMBEDDING",
|
439 |
"status": "1",
|
440 |
"llm": [
|
441 |
{
|
442 |
-
"llm_name": "abab6.5",
|
443 |
"tags": "LLM,CHAT,8k",
|
444 |
"max_tokens": 8192,
|
445 |
"model_type": "chat"
|
446 |
},
|
447 |
{
|
448 |
-
"llm_name": "abab6.5s",
|
449 |
"tags": "LLM,CHAT,245k",
|
450 |
"max_tokens": 245760,
|
451 |
"model_type": "chat"
|
452 |
},
|
453 |
{
|
454 |
-
"llm_name": "abab6.5t",
|
455 |
"tags": "LLM,CHAT,8k",
|
456 |
"max_tokens": 8192,
|
457 |
"model_type": "chat"
|
458 |
},
|
459 |
{
|
460 |
-
"llm_name": "abab6.5g",
|
461 |
"tags": "LLM,CHAT,8k",
|
462 |
"max_tokens": 8192,
|
463 |
"model_type": "chat"
|
464 |
},
|
465 |
{
|
466 |
-
"llm_name": "abab5.5s",
|
467 |
"tags": "LLM,CHAT,8k",
|
468 |
"max_tokens": 8192,
|
469 |
"model_type": "chat"
|
|
|
433 |
]
|
434 |
},
|
435 |
{
|
436 |
+
"name": "MiniMax",
|
437 |
"logo": "",
|
438 |
"tags": "LLM,TEXT EMBEDDING",
|
439 |
"status": "1",
|
440 |
"llm": [
|
441 |
{
|
442 |
+
"llm_name": "abab6.5-chat",
|
443 |
"tags": "LLM,CHAT,8k",
|
444 |
"max_tokens": 8192,
|
445 |
"model_type": "chat"
|
446 |
},
|
447 |
{
|
448 |
+
"llm_name": "abab6.5s-chat",
|
449 |
"tags": "LLM,CHAT,245k",
|
450 |
"max_tokens": 245760,
|
451 |
"model_type": "chat"
|
452 |
},
|
453 |
{
|
454 |
+
"llm_name": "abab6.5t-chat",
|
455 |
"tags": "LLM,CHAT,8k",
|
456 |
"max_tokens": 8192,
|
457 |
"model_type": "chat"
|
458 |
},
|
459 |
{
|
460 |
+
"llm_name": "abab6.5g-chat",
|
461 |
"tags": "LLM,CHAT,8k",
|
462 |
"max_tokens": 8192,
|
463 |
"model_type": "chat"
|
464 |
},
|
465 |
{
|
466 |
+
"llm_name": "abab5.5s-chat",
|
467 |
"tags": "LLM,CHAT,8k",
|
468 |
"max_tokens": 8192,
|
469 |
"model_type": "chat"
|
rag/llm/chat_model.py
CHANGED
@@ -24,7 +24,8 @@ from volcengine.maas.v2 import MaasService
|
|
24 |
from rag.nlp import is_english
|
25 |
from rag.utils import num_tokens_from_string
|
26 |
from groq import Groq
|
27 |
-
|
|
|
28 |
|
29 |
class Base(ABC):
|
30 |
def __init__(self, key, model_name, base_url):
|
@@ -475,11 +476,83 @@ class VolcEngineChat(Base):
|
|
475 |
|
476 |
|
477 |
class MiniMaxChat(Base):
|
478 |
-
def __init__(
|
479 |
-
|
|
|
|
|
|
|
|
|
480 |
if not base_url:
|
481 |
-
base_url="https://api.minimax.chat/v1/text/chatcompletion_v2"
|
482 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
483 |
|
484 |
|
485 |
class MistralChat(Base):
|
@@ -748,4 +821,3 @@ class OpenRouterChat(Base):
|
|
748 |
self.base_url = "https://openrouter.ai/api/v1"
|
749 |
self.client = OpenAI(base_url=self.base_url, api_key=key)
|
750 |
self.model_name = model_name
|
751 |
-
|
|
|
24 |
from rag.nlp import is_english
|
25 |
from rag.utils import num_tokens_from_string
|
26 |
from groq import Groq
|
27 |
+
import json
|
28 |
+
import requests
|
29 |
|
30 |
class Base(ABC):
|
31 |
def __init__(self, key, model_name, base_url):
|
|
|
476 |
|
477 |
|
478 |
class MiniMaxChat(Base):
|
479 |
+
def __init__(
|
480 |
+
self,
|
481 |
+
key,
|
482 |
+
model_name,
|
483 |
+
base_url="https://api.minimax.chat/v1/text/chatcompletion_v2",
|
484 |
+
):
|
485 |
if not base_url:
|
486 |
+
base_url = "https://api.minimax.chat/v1/text/chatcompletion_v2"
|
487 |
+
self.base_url = base_url
|
488 |
+
self.model_name = model_name
|
489 |
+
self.api_key = key
|
490 |
+
|
491 |
+
def chat(self, system, history, gen_conf):
|
492 |
+
if system:
|
493 |
+
history.insert(0, {"role": "system", "content": system})
|
494 |
+
for k in list(gen_conf.keys()):
|
495 |
+
if k not in ["temperature", "top_p", "max_tokens"]:
|
496 |
+
del gen_conf[k]
|
497 |
+
headers = {
|
498 |
+
"Authorization": f"Bearer {self.api_key}",
|
499 |
+
"Content-Type": "application/json",
|
500 |
+
}
|
501 |
+
payload = json.dumps(
|
502 |
+
{"model": self.model_name, "messages": history, **gen_conf}
|
503 |
+
)
|
504 |
+
try:
|
505 |
+
response = requests.request(
|
506 |
+
"POST", url=self.base_url, headers=headers, data=payload
|
507 |
+
)
|
508 |
+
print(response, flush=True)
|
509 |
+
response = response.json()
|
510 |
+
ans = response["choices"][0]["message"]["content"].strip()
|
511 |
+
if response["choices"][0]["finish_reason"] == "length":
|
512 |
+
ans += "...\nFor the content length reason, it stopped, continue?" if is_english(
|
513 |
+
[ans]) else "路路路路路路\n鐢变簬闀垮害鐨勫師鍥狅紝鍥炵瓟琚埅鏂簡锛岃缁х画鍚楋紵"
|
514 |
+
return ans, response["usage"]["total_tokens"]
|
515 |
+
except Exception as e:
|
516 |
+
return "**ERROR**: " + str(e), 0
|
517 |
+
|
518 |
+
def chat_streamly(self, system, history, gen_conf):
|
519 |
+
if system:
|
520 |
+
history.insert(0, {"role": "system", "content": system})
|
521 |
+
ans = ""
|
522 |
+
total_tokens = 0
|
523 |
+
try:
|
524 |
+
headers = {
|
525 |
+
"Authorization": f"Bearer {self.api_key}",
|
526 |
+
"Content-Type": "application/json",
|
527 |
+
}
|
528 |
+
payload = json.dumps(
|
529 |
+
{
|
530 |
+
"model": self.model_name,
|
531 |
+
"messages": history,
|
532 |
+
"stream": True,
|
533 |
+
**gen_conf,
|
534 |
+
}
|
535 |
+
)
|
536 |
+
response = requests.request(
|
537 |
+
"POST",
|
538 |
+
url=self.base_url,
|
539 |
+
headers=headers,
|
540 |
+
data=payload,
|
541 |
+
)
|
542 |
+
for resp in response.text.split("\n\n")[:-1]:
|
543 |
+
resp = json.loads(resp[6:])
|
544 |
+
if "delta" in resp["choices"][0]:
|
545 |
+
text = resp["choices"][0]["delta"]["content"]
|
546 |
+
else:
|
547 |
+
continue
|
548 |
+
ans += text
|
549 |
+
total_tokens += num_tokens_from_string(text)
|
550 |
+
yield ans
|
551 |
+
|
552 |
+
except Exception as e:
|
553 |
+
yield ans + "\n**ERROR**: " + str(e)
|
554 |
+
|
555 |
+
yield total_tokens
|
556 |
|
557 |
|
558 |
class MistralChat(Base):
|
|
|
821 |
self.base_url = "https://openrouter.ai/api/v1"
|
822 |
self.client = OpenAI(base_url=self.base_url, api_key=key)
|
823 |
self.model_name = model_name
|
|