Skip to content

Commit

Permalink
ver0.4.6 0613接口更新、支持azure小变动
Browse files Browse the repository at this point in the history
  • Loading branch information
goldfishh committed Jul 4, 2023
1 parent 5433f3e commit bfd0e22
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 41 deletions.
49 changes: 33 additions & 16 deletions chatgpt_tool_hub/common/calculate_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,42 @@


# refer to https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb
def count_message_tokens(messages, model: str = "gpt-3.5-turbo"):
"""Returns the number of tokens used by a list of messages."""

def count_message_tokens(messages, model="gpt-3.5-turbo-0613"):
import tiktoken

if model.startswith("gpt-35"):
model = model.replace("gpt-35-", "gpt-3.5-")

"""Return the number of tokens used by a list of messages."""
try:
encoding = tiktoken.encoding_for_model(model)
except KeyError:
LOG.debug("Warning: model not found. Using cl100k_base encoding.")
encoding = tiktoken.get_encoding("cl100k_base")
if model != "gpt-3.5-turbo-0613" and model.startswith("gpt-3.5-turbo"):
return count_message_tokens(messages, model="gpt-3.5-turbo-0613")
elif model == "gpt-4":
return count_message_tokens(messages, model="gpt-4-0314")
elif model == "gpt-3.5-turbo-0613":
# 注意,0613 3.5版本特殊token计算数默认与0301一致,但未经测试
tokens_per_message = 4 # every message follows <|start|>{role/name}\n{content}<|end|>\n
tokens_per_name = -1 # if there's a name, the role is omitted
elif model == "gpt-4-0314":
if model in {
"gpt-3.5-turbo-0613",
"gpt-3.5-turbo-16k-0613",
"gpt-4-0314",
"gpt-4-32k-0314",
"gpt-4-0613",
"gpt-4-32k-0613",
}:
tokens_per_message = 3
tokens_per_name = 1
else:
LOG.warn(f"count_message_tokens() is not implemented for model {model}. "
f"Returning num tokens assuming gpt-3.5-turbo-0613.")
elif model == "gpt-3.5-turbo-0301":
tokens_per_message = 4 # every message follows <|start|>{role/name}\n{content}<|end|>\n
tokens_per_name = -1 # if there's a name, the role is omitted
elif "gpt-3.5-turbo" in model:
LOG.debug("Warning: gpt-3.5-turbo may update over time. Returning num tokens assuming gpt-3.5-turbo-0613.")
return count_message_tokens(messages, model="gpt-3.5-turbo-0613")
elif "gpt-4" in model:
LOG.debug("Warning: gpt-4 may update over time. Returning num tokens assuming gpt-4-0613.")
return count_message_tokens(messages, model="gpt-4-0613")
else:
raise NotImplementedError(
f"""num_tokens_from_messages() is not implemented for model {model}. See https://github.com/openai/openai-python/blob/main/chatml.md for information on how messages are converted to tokens."""
)
num_tokens = 0
for message in messages:
num_tokens += tokens_per_message
Expand All @@ -36,17 +49,21 @@ def count_message_tokens(messages, model: str = "gpt-3.5-turbo"):
return num_tokens


def count_string_tokens(string: str, model_name: str = "gpt-3.5-turbo") -> int:
def count_string_tokens(string: str, model_name: str = "gpt-35-turbo") -> int:
"""
Returns the number of tokens in a text string.
Args:
string (str): The text string.
model_name (str): The name of the encoding to use. (e.g., "gpt-3.5-turbo")
model_name (str): The name of the encoding to use. (e.g., "gpt-35-turbo")
Returns:
int: The number of tokens in the text string.
"""
import tiktoken

if model_name.startswith("gpt-35"):
model_name = model_name.replace("gpt-35-", "gpt-3.5-")

encoding = tiktoken.encoding_for_model(model_name)
return len(encoding.encode(string))
4 changes: 2 additions & 2 deletions chatgpt_tool_hub/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
verbose: bool = False
llm_cache: Optional[BaseCache] = None

# default for gpt-3.5
# default for gpt-35
ALL_MAX_TOKENS_NUM = 4000

# token manage strategy
Expand All @@ -26,7 +26,7 @@ def change_memory_max_tokens(memory_max_tokens_num: int):
def build_model_params(kwargs: dict) -> dict:
_api_key = get_from_dict_or_env(kwargs, "llm_api_key", "LLM_API_KEY")
_proxy = get_from_dict_or_env(kwargs, "proxy", "PROXY", "")
_model = get_from_dict_or_env(kwargs, "model_name", "MODEL_NAME", "gpt-3.5-turbo")
_model = get_from_dict_or_env(kwargs, "model_name", "MODEL_NAME", "gpt-35-turbo")
_timeout = get_from_dict_or_env(kwargs, "request_timeout", "REQUEST_TIMEOUT", 120)
_llm_api_base_url = get_from_dict_or_env(kwargs, "llm_api_base_url", "LLM_API_BASE_URL", openai_default_api_base)
_deployment_id = get_from_dict_or_env(kwargs, "deployment_id", "DEPLOYMENT_ID", "")
Expand Down
14 changes: 7 additions & 7 deletions chatgpt_tool_hub/models/chatgpt/chatgpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ class ChatOpenAI(BaseChatModel, BaseModel):
.. code-block:: python
from lib.chat_models import ChatOpenAI
openai = ChatOpenAI(model_name="gpt-3.5-turbo")
openai = ChatOpenAI(model_name="gpt-35-turbo")
"""

client: Any #: :meta private:
"""Model name to use."""
model_name: str = "gpt-3.5-turbo"
model_name: str = "gpt-35-turbo"
"""Holds any model parameters valid for `create` call not explicitly specified."""
model_kwargs: Dict[str, Any] = Field(default_factory=dict)
"""llm api base url"""
Expand Down Expand Up @@ -367,19 +367,19 @@ def get_num_tokens(self, text: str) -> int:
"This is needed in order to calculate get_num_tokens. "
"Please it install it with `pip install tiktoken`."
)
# create a GPT-3.5-Turbo encoder instance
# create a gpt-35-Turbo encoder instance
enc = tiktoken.encoding_for_model(self.model_name)

# encode the text using the GPT-3.5-Turbo encoder
# encode the text using the gpt-35-Turbo encoder
tokenized_text = enc.encode(text)

# calculate the number of tokens in the encoded text
return len(tokenized_text)

def get_num_tokens_from_messages(
self, messages: List[BaseMessage], model: str = "gpt-3.5-turbo"
self, messages: List[BaseMessage], model: str = "gpt-35-turbo"
) -> int:
"""Calculate num tokens for gpt-3.5-turbo with tiktoken package."""
"""Calculate num tokens for gpt-35-turbo with tiktoken package."""
try:
import tiktoken
except ImportError:
Expand All @@ -394,7 +394,7 @@ def get_num_tokens_from_messages(
encoding = tiktoken.encoding_for_model(model)
except KeyError:
encoding = tiktoken.get_encoding("cl100k_base")
if not model.startswith("gpt-3.5-turbo"):
if not model.startswith("gpt-35-turbo"):
raise NotImplementedError(
f"get_num_tokens_from_messages() is not presently implemented "
f"for model {model}."
Expand Down
2 changes: 1 addition & 1 deletion chatgpt_tool_hub/models/model_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def match_model(self, name: str) -> str:
return ""

def create_llm_model(self, **model_kwargs):
_model = get_from_dict_or_env(model_kwargs, "model_name", "MODEL_NAME", "gpt-3.5-turbo")
_model = get_from_dict_or_env(model_kwargs, "model_name", "MODEL_NAME", "gpt-35-turbo")
match_llm_model = self.match_model(_model)
if match_llm_model == "chatgpt":
return ChatOpenAI(**model_kwargs)
Expand Down
2 changes: 1 addition & 1 deletion chatgpt_tool_hub/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.4.5'
__version__ = '0.4.6'
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ beautifulsoup4~=4.12.0

# for openai
tenacity~=8.2.2
openai~=0.27.2
tiktoken~=0.3.2
openai~=0.27.4
tiktoken~=0.4.0

# all tool dependencies
arxiv
Expand Down
4 changes: 2 additions & 2 deletions requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ pyopenssl
# for models openai
tenacity~=8.2.2

openai~=0.27.2
openai~=0.27.4

SQLAlchemy~=2.0.7

tiktoken~=0.3.2
tiktoken~=0.4.0

arxiv

Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
'lxml',
'beautifulsoup4~=4.12.0',
"tenacity~=8.2.2",
"openai~=0.27.2",
"tiktoken~=0.3.2",
"openai~=0.27.4",
"tiktoken~=0.4.0",
"arxiv",
"wikipedia",
"wolframalpha",
Expand Down
16 changes: 8 additions & 8 deletions terminal_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def __init__(self, timeout: int):

self.app = self.create_app()

self.model = 'gpt-3.5-turbo'
self.model = 'gpt-35-turbo'
self.timeout = timeout

# todo 需要llm-os model支持
Expand Down Expand Up @@ -215,7 +215,7 @@ def set_model(self, new_model: str):
tokens_limit = 32768
elif self.model.startswith("gpt-4"):
tokens_limit = 8192
elif self.model.startswith("gpt-3.5-turbo"):
elif self.model.startswith("gpt-35-turbo"):
tokens_limit = 4096
else:
console.print(f"[red]没有该模型 {new_model} tokens信息,模型未变更: [bold cyan]{old_model}[/].")
Expand Down Expand Up @@ -246,11 +246,11 @@ class CustomCompleter(Completer):
]

available_models = [
"gpt-3.5-turbo",
"gpt-3.5-turbo-0301",
"gpt-3.5-turbo-0613",
"gpt-3.5-turbo-16k",
"gpt-3.5-turbo-16k-0613",
"gpt-35-turbo",
"gpt-35-turbo-0301",
"gpt-35-turbo-0613",
"gpt-35-turbo-16k",
"gpt-35-turbo-16k-0613",
"gpt-4",
"gpt-4-0314",
"gpt-4-32k",
Expand Down Expand Up @@ -478,7 +478,7 @@ def handle_command(command: str, llm_os: LLMOS):
/depth - 设置LLM-OS思考深度 (设置过大可能无法停止)
/reset - LLM-OS重置 (重新加载配置并重置聊天记录)
/timeout \[new_timeout] - 修改访问llm的请求超时时间
/model \[model_name] - 切换模型 (目前仅支持gpt-3.5)
/model \[model_name] - 切换模型 (目前仅支持gpt-35)
/last - 显示上一次LLM-OS的回复内容
/copy - 复制上一次LLM-OS的回复内容到粘贴板
/undo - 清除上一次与llm的对话记录 (包含问题和回复)
Expand Down

0 comments on commit bfd0e22

Please sign in to comment.