Browse Source

Refa: remove max tokens since no one needs it. (#5690)

### What problem does this PR solve?

#5646 #5640

### Type of change

- [x] Refactoring
tags/v0.17.1
Kevin Hu 8 months ago
parent
commit
251ba7f058
No account linked to committer's email address
1 changed files with 50 additions and 36 deletions
  1. 50
    36
      rag/llm/chat_model.py

+ 50
- 36
rag/llm/chat_model.py View File

def chat(self, system, history, gen_conf): def chat(self, system, history, gen_conf):
if system: if system:
history.insert(0, {"role": "system", "content": system}) history.insert(0, {"role": "system", "content": system})
if "max_tokens" in gen_conf:
del gen_conf["max_tokens"]
try: try:
response = self.client.chat.completions.create( response = self.client.chat.completions.create(
model=self.model_name, model=self.model_name,
def chat_streamly(self, system, history, gen_conf): def chat_streamly(self, system, history, gen_conf):
if system: if system:
history.insert(0, {"role": "system", "content": system}) history.insert(0, {"role": "system", "content": system})
if "max_tokens" in gen_conf:
del gen_conf["max_tokens"]
ans = "" ans = ""
total_tokens = 0 total_tokens = 0
try: try:
def chat(self, system, history, gen_conf): def chat(self, system, history, gen_conf):
if system: if system:
history.insert(0, {"role": "system", "content": system}) history.insert(0, {"role": "system", "content": system})
if "max_tokens" in gen_conf:
del gen_conf["max_tokens"]
try: try:
response = self.client.chat.completions.create( response = self.client.chat.completions.create(
model=self.model_name, model=self.model_name,
def chat_streamly(self, system, history, gen_conf): def chat_streamly(self, system, history, gen_conf):
if system: if system:
history.insert(0, {"role": "system", "content": system}) history.insert(0, {"role": "system", "content": system})
if "max_tokens" in gen_conf:
del gen_conf["max_tokens"]
ans = "" ans = ""
total_tokens = 0 total_tokens = 0
try: try:
super().__init__(key, model_name, "https://dashscope.aliyuncs.com/compatible-mode/v1") super().__init__(key, model_name, "https://dashscope.aliyuncs.com/compatible-mode/v1")


def chat(self, system, history, gen_conf): def chat(self, system, history, gen_conf):
if "max_tokens" in gen_conf:
del gen_conf["max_tokens"]
if self.model_name.lower().find("deepseek") >= 0: if self.model_name.lower().find("deepseek") >= 0:
return super().chat(system, history, gen_conf) return super().chat(system, history, gen_conf)


from http import HTTPStatus from http import HTTPStatus
if system: if system:
history.insert(0, {"role": "system", "content": system}) history.insert(0, {"role": "system", "content": system})
if "max_tokens" in gen_conf:
del gen_conf["max_tokens"]
ans = "" ans = ""
tk_count = 0 tk_count = 0
try: try:
yield tk_count yield tk_count


def chat_streamly(self, system, history, gen_conf): def chat_streamly(self, system, history, gen_conf):
if "max_tokens" in gen_conf:
del gen_conf["max_tokens"]
if self.model_name.lower().find("deepseek") >= 0: if self.model_name.lower().find("deepseek") >= 0:
return super().chat_streamly(system, history, gen_conf) return super().chat_streamly(system, history, gen_conf)


def chat(self, system, history, gen_conf): def chat(self, system, history, gen_conf):
if system: if system:
history.insert(0, {"role": "system", "content": system}) history.insert(0, {"role": "system", "content": system})
if "max_tokens" in gen_conf:
del gen_conf["max_tokens"]
try: try:
if "presence_penalty" in gen_conf: if "presence_penalty" in gen_conf:
del gen_conf["presence_penalty"] del gen_conf["presence_penalty"]
def chat_streamly(self, system, history, gen_conf): def chat_streamly(self, system, history, gen_conf):
if system: if system:
history.insert(0, {"role": "system", "content": system}) history.insert(0, {"role": "system", "content": system})
if "max_tokens" in gen_conf:
del gen_conf["max_tokens"]
if "presence_penalty" in gen_conf: if "presence_penalty" in gen_conf:
del gen_conf["presence_penalty"] del gen_conf["presence_penalty"]
if "frequency_penalty" in gen_conf: if "frequency_penalty" in gen_conf:
def chat(self, system, history, gen_conf): def chat(self, system, history, gen_conf):
if system: if system:
history.insert(0, {"role": "system", "content": system}) history.insert(0, {"role": "system", "content": system})
if "max_tokens" in gen_conf:
del gen_conf["max_tokens"]
try: try:
options = {} options = {}
if "temperature" in gen_conf: if "temperature" in gen_conf:
def chat_streamly(self, system, history, gen_conf): def chat_streamly(self, system, history, gen_conf):
if system: if system:
history.insert(0, {"role": "system", "content": system}) history.insert(0, {"role": "system", "content": system})
if "max_tokens" in gen_conf:
del gen_conf["max_tokens"]
options = {} options = {}
if "temperature" in gen_conf: if "temperature" in gen_conf:
options["temperature"] = gen_conf["temperature"] options["temperature"] = gen_conf["temperature"]
from rag.svr.jina_server import Prompt from rag.svr.jina_server import Prompt
if system: if system:
history.insert(0, {"role": "system", "content": system}) history.insert(0, {"role": "system", "content": system})
if "max_tokens" in gen_conf:
gen_conf["max_new_tokens"] = gen_conf.pop("max_tokens")
return Prompt(message=history, gen_conf=gen_conf) return Prompt(message=history, gen_conf=gen_conf)


def _stream_response(self, endpoint, prompt): def _stream_response(self, endpoint, prompt):
yield num_tokens_from_string(answer) yield num_tokens_from_string(answer)


def chat(self, system, history, gen_conf): def chat(self, system, history, gen_conf):
if "max_tokens" in gen_conf:
del gen_conf["max_tokens"]
prompt = self._prepare_prompt(system, history, gen_conf) prompt = self._prepare_prompt(system, history, gen_conf)
chat_gen = self._stream_response("/chat", prompt) chat_gen = self._stream_response("/chat", prompt)
ans = next(chat_gen) ans = next(chat_gen)
return ans, total_tokens return ans, total_tokens


def chat_streamly(self, system, history, gen_conf): def chat_streamly(self, system, history, gen_conf):
if "max_tokens" in gen_conf:
del gen_conf["max_tokens"]
prompt = self._prepare_prompt(system, history, gen_conf) prompt = self._prepare_prompt(system, history, gen_conf)
return self._stream_response("/stream", prompt) return self._stream_response("/stream", prompt)


def chat_streamly(self, system, history, gen_conf): def chat_streamly(self, system, history, gen_conf):
if system: if system:
history.insert(0, {"role": "system", "content": 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]
ans = "" ans = ""
total_tokens = 0 total_tokens = 0
try: try:
def chat(self, system, history, gen_conf): def chat(self, system, history, gen_conf):
from botocore.exceptions import ClientError from botocore.exceptions import ClientError
for k in list(gen_conf.keys()): for k in list(gen_conf.keys()):
if k not in ["temperature", "top_p", "max_tokens"]:
if k not in ["top_p", "max_tokens"]:
del gen_conf[k] del gen_conf[k]
if "max_tokens" in gen_conf:
gen_conf["maxTokens"] = gen_conf["max_tokens"]
_ = gen_conf.pop("max_tokens")
if "top_p" in gen_conf:
gen_conf["topP"] = gen_conf["top_p"]
_ = gen_conf.pop("top_p")
for item in history: for item in history:
if not isinstance(item["content"], list) and not isinstance(item["content"], tuple): if not isinstance(item["content"], list) and not isinstance(item["content"], tuple):
item["content"] = [{"text": item["content"]}] item["content"] = [{"text": item["content"]}]
def chat_streamly(self, system, history, gen_conf): def chat_streamly(self, system, history, gen_conf):
from botocore.exceptions import ClientError from botocore.exceptions import ClientError
for k in list(gen_conf.keys()): for k in list(gen_conf.keys()):
if k not in ["temperature", "top_p", "max_tokens"]:
if k not in ["top_p", "max_tokens"]:
del gen_conf[k] del gen_conf[k]
if "max_tokens" in gen_conf:
gen_conf["maxTokens"] = gen_conf["max_tokens"]
_ = gen_conf.pop("max_tokens")
if "top_p" in gen_conf:
gen_conf["topP"] = gen_conf["top_p"]
_ = gen_conf.pop("top_p")
for item in history: for item in history:
if not isinstance(item["content"], list) and not isinstance(item["content"], tuple): if not isinstance(item["content"], list) and not isinstance(item["content"], tuple):
item["content"] = [{"text": item["content"]}] item["content"] = [{"text": item["content"]}]


if system: if system:
self.model._system_instruction = content_types.to_content(system) self.model._system_instruction = content_types.to_content(system)

if 'max_tokens' in gen_conf:
gen_conf['max_output_tokens'] = gen_conf['max_tokens']
for k in list(gen_conf.keys()): for k in list(gen_conf.keys()):
if k not in ["temperature", "top_p", "max_output_tokens"]:
if k not in ["temperature", "top_p", "max_tokens"]:
del gen_conf[k] del gen_conf[k]
for item in history: for item in history:
if 'role' in item and item['role'] == 'assistant': if 'role' in item and item['role'] == 'assistant':


if system: if system:
self.model._system_instruction = content_types.to_content(system) self.model._system_instruction = content_types.to_content(system)
if 'max_tokens' in gen_conf:
gen_conf['max_output_tokens'] = gen_conf['max_tokens']
for k in list(gen_conf.keys()): for k in list(gen_conf.keys()):
if k not in ["temperature", "top_p", "max_output_tokens"]:
if k not in ["temperature", "top_p", "max_tokens"]:
del gen_conf[k] del gen_conf[k]
for item in history: for item in history:
if 'role' in item and item['role'] == 'assistant': if 'role' in item and item['role'] == 'assistant':
def chat(self, system, history, gen_conf): def chat(self, system, history, gen_conf):
if system: if system:
history.insert(0, {"role": "system", "content": system}) history.insert(0, {"role": "system", "content": system})
if "max_tokens" in gen_conf:
del gen_conf["max_tokens"]
if "top_p" in gen_conf: if "top_p" in gen_conf:
gen_conf["p"] = gen_conf.pop("top_p") gen_conf["p"] = gen_conf.pop("top_p")
if "frequency_penalty" in gen_conf and "presence_penalty" in gen_conf: if "frequency_penalty" in gen_conf and "presence_penalty" in gen_conf:
def chat_streamly(self, system, history, gen_conf): def chat_streamly(self, system, history, gen_conf):
if system: if system:
history.insert(0, {"role": "system", "content": system}) history.insert(0, {"role": "system", "content": system})
if "max_tokens" in gen_conf:
del gen_conf["max_tokens"]
if "top_p" in gen_conf: if "top_p" in gen_conf:
gen_conf["p"] = gen_conf.pop("top_p") gen_conf["p"] = gen_conf.pop("top_p")
if "frequency_penalty" in gen_conf and "presence_penalty" in gen_conf: if "frequency_penalty" in gen_conf and "presence_penalty" in gen_conf:


def chat(self, system, history, gen_conf): def chat(self, system, history, gen_conf):
if "max_tokens" in gen_conf: if "max_tokens" in gen_conf:
gen_conf["max_new_tokens"] = gen_conf.pop("max_tokens")
del gen_conf["max_tokens"]
if system: if system:
self.system = system self.system = system
prompt = "\n".join( prompt = "\n".join(


def chat_streamly(self, system, history, gen_conf): def chat_streamly(self, system, history, gen_conf):
if "max_tokens" in gen_conf: if "max_tokens" in gen_conf:
gen_conf["max_new_tokens"] = gen_conf.pop("max_tokens")
del gen_conf["max_tokens"]
if system: if system:
self.system = system self.system = system
prompt = "\n".join( prompt = "\n".join(
_history = [{k.capitalize(): v for k, v in item.items()} for item in history] _history = [{k.capitalize(): v for k, v in item.items()} for item in history]
if system: if system:
_history.insert(0, {"Role": "system", "Content": system}) _history.insert(0, {"Role": "system", "Content": system})
if "max_tokens" in gen_conf:
del gen_conf["max_tokens"]
if "temperature" in gen_conf: if "temperature" in gen_conf:
_gen_conf["Temperature"] = gen_conf["temperature"] _gen_conf["Temperature"] = gen_conf["temperature"]
if "top_p" in gen_conf: if "top_p" in gen_conf:
_history = [{k.capitalize(): v for k, v in item.items()} for item in history] _history = [{k.capitalize(): v for k, v in item.items()} for item in history]
if system: if system:
_history.insert(0, {"Role": "system", "Content": system}) _history.insert(0, {"Role": "system", "Content": system})

if "max_tokens" in gen_conf:
del gen_conf["max_tokens"]
if "temperature" in gen_conf: if "temperature" in gen_conf:
_gen_conf["Temperature"] = gen_conf["temperature"] _gen_conf["Temperature"] = gen_conf["temperature"]
if "top_p" in gen_conf: if "top_p" in gen_conf:
0)) / 2 0)) / 2
) + 1 ) + 1
if "max_tokens" in gen_conf: if "max_tokens" in gen_conf:
gen_conf["max_output_tokens"] = gen_conf["max_tokens"]
del gen_conf["max_tokens"]
ans = "" ans = ""


try: try:
0)) / 2 0)) / 2
) + 1 ) + 1
if "max_tokens" in gen_conf: if "max_tokens" in gen_conf:
gen_conf["max_output_tokens"] = gen_conf["max_tokens"]
del gen_conf["max_tokens"]
ans = "" ans = ""
total_tokens = 0 total_tokens = 0


def chat(self, system, history, gen_conf): def chat(self, system, history, gen_conf):
if system: if system:
self.system = system self.system = system
if "max_tokens" not in gen_conf:
gen_conf["max_tokens"] = 4096
if "presence_penalty" in gen_conf: if "presence_penalty" in gen_conf:
del gen_conf["presence_penalty"] del gen_conf["presence_penalty"]
if "frequency_penalty" in gen_conf: if "frequency_penalty" in gen_conf:
def chat_streamly(self, system, history, gen_conf): def chat_streamly(self, system, history, gen_conf):
if system: if system:
self.system = system self.system = system
if "max_tokens" not in gen_conf:
gen_conf["max_tokens"] = 4096
if "presence_penalty" in gen_conf: if "presence_penalty" in gen_conf:
del gen_conf["presence_penalty"] del gen_conf["presence_penalty"]
if "frequency_penalty" in gen_conf: if "frequency_penalty" in gen_conf:
self.system = system self.system = system


if "claude" in self.model_name: if "claude" in self.model_name:
if "max_tokens" not in gen_conf:
gen_conf["max_tokens"] = 4096
if "max_tokens" in gen_conf:
del gen_conf["max_tokens"]
try: try:
response = self.client.messages.create( response = self.client.messages.create(
model=self.model_name, model=self.model_name,
self.system = system self.system = system


if "claude" in self.model_name: if "claude" in self.model_name:
if "max_tokens" not in gen_conf:
gen_conf["max_tokens"] = 4096
if "max_tokens" in gen_conf:
del gen_conf["max_tokens"]
ans = "" ans = ""
total_tokens = 0 total_tokens = 0
try: try:


yield response._chunks[-1].usage_metadata.total_token_count yield response._chunks[-1].usage_metadata.total_token_count



class GPUStackChat(Base): class GPUStackChat(Base):
def __init__(self, key=None, model_name="", base_url=""): def __init__(self, key=None, model_name="", base_url=""):
if not base_url: if not base_url:

Loading…
Cancel
Save