Skip to content

Commit

Permalink
fix MiniMax api error (#1567)
Browse files Browse the repository at this point in the history
### 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]>
  • Loading branch information
hangters and aopstudio authored Jul 17, 2024
1 parent fe5dd5b commit 9ae57eb
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 13 deletions.
8 changes: 7 additions & 1 deletion api/db/db_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ class TenantLLM(DataBaseModel):
null=True,
help_text="LLM name",
default="")
api_key = CharField(max_length=255, null=True, help_text="API KEY")
api_key = CharField(max_length=1024, null=True, help_text="API KEY")
api_base = CharField(max_length=255, null=True, help_text="API Base")
used_tokens = IntegerField(default=0)

Expand Down Expand Up @@ -885,3 +885,9 @@ def migrate_db():
)
except Exception as e:
pass
try:
migrate(
migrator.alter_column_type('tenant_llm', 'api_key', CharField(max_length=1024, null=True, help_text="API KEY"))
)
except Exception as e:
pass
12 changes: 6 additions & 6 deletions conf/llm_factories.json
Original file line number Diff line number Diff line change
Expand Up @@ -433,37 +433,37 @@
]
},
{
"name": "Minimax",
"name": "MiniMax",
"logo": "",
"tags": "LLM,TEXT EMBEDDING",
"status": "1",
"llm": [
{
"llm_name": "abab6.5",
"llm_name": "abab6.5-chat",
"tags": "LLM,CHAT,8k",
"max_tokens": 8192,
"model_type": "chat"
},
{
"llm_name": "abab6.5s",
"llm_name": "abab6.5s-chat",
"tags": "LLM,CHAT,245k",
"max_tokens": 245760,
"model_type": "chat"
},
{
"llm_name": "abab6.5t",
"llm_name": "abab6.5t-chat",
"tags": "LLM,CHAT,8k",
"max_tokens": 8192,
"model_type": "chat"
},
{
"llm_name": "abab6.5g",
"llm_name": "abab6.5g-chat",
"tags": "LLM,CHAT,8k",
"max_tokens": 8192,
"model_type": "chat"
},
{
"llm_name": "abab5.5s",
"llm_name": "abab5.5s-chat",
"tags": "LLM,CHAT,8k",
"max_tokens": 8192,
"model_type": "chat"
Expand Down
84 changes: 78 additions & 6 deletions rag/llm/chat_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
from rag.nlp import is_english
from rag.utils import num_tokens_from_string
from groq import Groq

import json
import requests

class Base(ABC):
def __init__(self, key, model_name, base_url):
Expand Down Expand Up @@ -475,11 +476,83 @@ def chat_streamly(self, system, history, gen_conf):


class MiniMaxChat(Base):
def __init__(self, key, model_name="abab6.5s-chat",
base_url="https://api.minimax.chat/v1/text/chatcompletion_v2"):
def __init__(
self,
key,
model_name,
base_url="https://api.minimax.chat/v1/text/chatcompletion_v2",
):
if not base_url:
base_url="https://api.minimax.chat/v1/text/chatcompletion_v2"
super().__init__(key, model_name, base_url)
base_url = "https://api.minimax.chat/v1/text/chatcompletion_v2"
self.base_url = base_url
self.model_name = model_name
self.api_key = key

def chat(self, system, history, gen_conf):
if system:
history.insert(0, {"role": "system", "content": system})
for k in list(gen_conf.keys()):
if k not in ["temperature", "top_p", "max_tokens"]:
del gen_conf[k]
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json",
}
payload = json.dumps(
{"model": self.model_name, "messages": history, **gen_conf}
)
try:
response = requests.request(
"POST", url=self.base_url, headers=headers, data=payload
)
print(response, flush=True)
response = response.json()
ans = response["choices"][0]["message"]["content"].strip()
if response["choices"][0]["finish_reason"] == "length":
ans += "...\nFor the content length reason, it stopped, continue?" if is_english(
[ans]) else "······\n由于长度的原因,回答被截断了,要继续吗?"
return ans, response["usage"]["total_tokens"]
except Exception as e:
return "**ERROR**: " + str(e), 0

def chat_streamly(self, system, history, gen_conf):
if system:
history.insert(0, {"role": "system", "content": system})
ans = ""
total_tokens = 0
try:
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json",
}
payload = json.dumps(
{
"model": self.model_name,
"messages": history,
"stream": True,
**gen_conf,
}
)
response = requests.request(
"POST",
url=self.base_url,
headers=headers,
data=payload,
)
for resp in response.text.split("\n\n")[:-1]:
resp = json.loads(resp[6:])
if "delta" in resp["choices"][0]:
text = resp["choices"][0]["delta"]["content"]
else:
continue
ans += text
total_tokens += num_tokens_from_string(text)
yield ans

except Exception as e:
yield ans + "\n**ERROR**: " + str(e)

yield total_tokens


class MistralChat(Base):
Expand Down Expand Up @@ -748,4 +821,3 @@ def __init__(self, key, model_name, base_url="https://openrouter.ai/api/v1"):
self.base_url = "https://openrouter.ai/api/v1"
self.client = OpenAI(base_url=self.base_url, api_key=key)
self.model_name = model_name

0 comments on commit 9ae57eb

Please sign in to comment.