vectorvein 0.1.15__tar.gz → 0.1.16__tar.gz
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.
- {vectorvein-0.1.15 → vectorvein-0.1.16}/PKG-INFO +1 -1
- {vectorvein-0.1.15 → vectorvein-0.1.16}/pyproject.toml +1 -1
- {vectorvein-0.1.15 → vectorvein-0.1.16}/src/vectorvein/chat_clients/__init__.py +2 -1
- {vectorvein-0.1.15 → vectorvein-0.1.16}/src/vectorvein/chat_clients/anthropic_client.py +3 -5
- {vectorvein-0.1.15 → vectorvein-0.1.16}/src/vectorvein/chat_clients/openai_compatible_client.py +3 -5
- {vectorvein-0.1.15 → vectorvein-0.1.16}/src/vectorvein/chat_clients/utils.py +41 -1
- {vectorvein-0.1.15 → vectorvein-0.1.16}/README.md +0 -0
- {vectorvein-0.1.15 → vectorvein-0.1.16}/src/vectorvein/__init__.py +0 -0
- {vectorvein-0.1.15 → vectorvein-0.1.16}/src/vectorvein/chat_clients/baichuan_client.py +0 -0
- {vectorvein-0.1.15 → vectorvein-0.1.16}/src/vectorvein/chat_clients/base_client.py +0 -0
- {vectorvein-0.1.15 → vectorvein-0.1.16}/src/vectorvein/chat_clients/deepseek_client.py +0 -0
- {vectorvein-0.1.15 → vectorvein-0.1.16}/src/vectorvein/chat_clients/gemini_client.py +0 -0
- {vectorvein-0.1.15 → vectorvein-0.1.16}/src/vectorvein/chat_clients/groq_client.py +0 -0
- {vectorvein-0.1.15 → vectorvein-0.1.16}/src/vectorvein/chat_clients/local_client.py +0 -0
- {vectorvein-0.1.15 → vectorvein-0.1.16}/src/vectorvein/chat_clients/minimax_client.py +0 -0
- {vectorvein-0.1.15 → vectorvein-0.1.16}/src/vectorvein/chat_clients/mistral_client.py +0 -0
- {vectorvein-0.1.15 → vectorvein-0.1.16}/src/vectorvein/chat_clients/moonshot_client.py +0 -0
- {vectorvein-0.1.15 → vectorvein-0.1.16}/src/vectorvein/chat_clients/openai_client.py +0 -0
- {vectorvein-0.1.15 → vectorvein-0.1.16}/src/vectorvein/chat_clients/qwen_client.py +0 -0
- {vectorvein-0.1.15 → vectorvein-0.1.16}/src/vectorvein/chat_clients/yi_client.py +0 -0
- {vectorvein-0.1.15 → vectorvein-0.1.16}/src/vectorvein/chat_clients/zhipuai_client.py +0 -0
- {vectorvein-0.1.15 → vectorvein-0.1.16}/src/vectorvein/settings/__init__.py +0 -0
- {vectorvein-0.1.15 → vectorvein-0.1.16}/src/vectorvein/types/defaults.py +0 -0
- {vectorvein-0.1.15 → vectorvein-0.1.16}/src/vectorvein/types/enums.py +0 -0
- {vectorvein-0.1.15 → vectorvein-0.1.16}/src/vectorvein/types/llm_parameters.py +0 -0
- {vectorvein-0.1.15 → vectorvein-0.1.16}/src/vectorvein/utilities/media_processing.py +0 -0
- {vectorvein-0.1.15 → vectorvein-0.1.16}/src/vectorvein/utilities/retry.py +0 -0
@@ -20,7 +20,7 @@ from .deepseek_client import DeepSeekChatClient, AsyncDeepSeekChatClient
|
|
20
20
|
from ..types import defaults as defs
|
21
21
|
from ..types.enums import BackendType, ContextLengthControlType
|
22
22
|
from .anthropic_client import AnthropicChatClient, AsyncAnthropicChatClient
|
23
|
-
from .utils import format_messages, get_token_counts, ToolCallContentProcessor
|
23
|
+
from .utils import format_messages, get_token_counts, get_message_token_counts, ToolCallContentProcessor
|
24
24
|
|
25
25
|
|
26
26
|
BackendMap = {
|
@@ -125,5 +125,6 @@ __all__ = [
|
|
125
125
|
"get_token_counts",
|
126
126
|
"create_chat_client",
|
127
127
|
"create_async_chat_client",
|
128
|
+
"get_message_token_counts",
|
128
129
|
"ToolCallContentProcessor",
|
129
130
|
]
|
@@ -21,7 +21,7 @@ from google.auth import _helpers
|
|
21
21
|
|
22
22
|
from ..settings import settings
|
23
23
|
from ..types import defaults as defs
|
24
|
-
from .utils import cutoff_messages,
|
24
|
+
from .utils import cutoff_messages, get_message_token_counts
|
25
25
|
from .base_client import BaseChatClient, BaseAsyncChatClient
|
26
26
|
from ..types.enums import ContextLengthControlType, BackendType
|
27
27
|
from ..types.llm_parameters import ChatCompletionMessage, ChatCompletionDeltaMessage
|
@@ -199,12 +199,11 @@ class AnthropicChatClient(BaseChatClient):
|
|
199
199
|
|
200
200
|
if max_tokens is None:
|
201
201
|
max_output_tokens = self.model_setting.max_output_tokens
|
202
|
+
token_counts = get_message_token_counts(messages=messages, tools=tools_params, model=self.model_setting.id)
|
202
203
|
if max_output_tokens is not None:
|
203
|
-
token_counts = get_token_counts({"messages": messages, "tools_params": tools_params})
|
204
204
|
max_tokens = self.model_setting.context_length - token_counts
|
205
205
|
max_tokens = min(max(max_tokens, 1), max_output_tokens)
|
206
206
|
else:
|
207
|
-
token_counts = get_token_counts({"messages": messages, "tools_params": tools_params})
|
208
207
|
max_tokens = self.model_setting.context_length - token_counts
|
209
208
|
|
210
209
|
response = self._client.messages.create(
|
@@ -405,12 +404,11 @@ class AsyncAnthropicChatClient(BaseAsyncChatClient):
|
|
405
404
|
|
406
405
|
if max_tokens is None:
|
407
406
|
max_output_tokens = self.model_setting.max_output_tokens
|
407
|
+
token_counts = get_message_token_counts(messages=messages, tools=tools_params, model=self.model_setting.id)
|
408
408
|
if max_output_tokens is not None:
|
409
|
-
token_counts = get_token_counts({"messages": messages, "tools_params": tools_params})
|
410
409
|
max_tokens = self.model_setting.context_length - token_counts
|
411
410
|
max_tokens = min(max(max_tokens, 1), max_output_tokens)
|
412
411
|
else:
|
413
|
-
token_counts = get_token_counts({"messages": messages, "tools_params": tools_params})
|
414
412
|
max_tokens = self.model_setting.context_length - token_counts
|
415
413
|
|
416
414
|
response = await self._client.messages.create(
|
{vectorvein-0.1.15 → vectorvein-0.1.16}/src/vectorvein/chat_clients/openai_compatible_client.py
RENAMED
@@ -12,7 +12,7 @@ from openai import OpenAI, AsyncOpenAI, AzureOpenAI, AsyncAzureOpenAI
|
|
12
12
|
from .base_client import BaseChatClient, BaseAsyncChatClient
|
13
13
|
from .utils import (
|
14
14
|
cutoff_messages,
|
15
|
-
|
15
|
+
get_message_token_counts,
|
16
16
|
ToolCallContentProcessor,
|
17
17
|
generate_tool_use_system_prompt,
|
18
18
|
)
|
@@ -111,12 +111,11 @@ class OpenAICompatibleChatClient(BaseChatClient):
|
|
111
111
|
|
112
112
|
if max_tokens is None:
|
113
113
|
max_output_tokens = self.model_setting.max_output_tokens
|
114
|
+
token_counts = get_message_token_counts(messages=messages, tools=tools_params, model=self.model_setting.id)
|
114
115
|
if max_output_tokens is not None:
|
115
|
-
token_counts = get_token_counts({"messages": messages, "tools_params": tools_params})
|
116
116
|
max_tokens = self.model_setting.context_length - token_counts
|
117
117
|
max_tokens = min(max(max_tokens, 1), max_output_tokens)
|
118
118
|
else:
|
119
|
-
token_counts = get_token_counts({"messages": messages, "tools_params": tools_params})
|
120
119
|
max_tokens = self.model_setting.context_length - token_counts
|
121
120
|
|
122
121
|
response: ChatCompletion | Stream[ChatCompletionChunk] = self._client.chat.completions.create(
|
@@ -270,12 +269,11 @@ class AsyncOpenAICompatibleChatClient(BaseAsyncChatClient):
|
|
270
269
|
|
271
270
|
if max_tokens is None:
|
272
271
|
max_output_tokens = self.model_setting.max_output_tokens
|
272
|
+
token_counts = get_message_token_counts(messages=messages, tools=tools_params, model=self.model_setting.id)
|
273
273
|
if max_output_tokens is not None:
|
274
|
-
token_counts = get_token_counts({"messages": messages, "tools_params": tools_params})
|
275
274
|
max_tokens = self.model_setting.context_length - token_counts
|
276
275
|
max_tokens = min(max(max_tokens, 1), max_output_tokens)
|
277
276
|
else:
|
278
|
-
token_counts = get_token_counts({"messages": messages, "tools_params": tools_params})
|
279
277
|
max_tokens = self.model_setting.context_length - token_counts
|
280
278
|
|
281
279
|
response: ChatCompletion | AsyncStream[ChatCompletionChunk] = await self._client.chat.completions.create(
|
@@ -2,7 +2,7 @@
|
|
2
2
|
# @Date: 2024-07-26 14:48:55
|
3
3
|
import re
|
4
4
|
import json
|
5
|
-
|
5
|
+
from math import ceil
|
6
6
|
import httpx
|
7
7
|
import tiktoken
|
8
8
|
from anthropic import Anthropic
|
@@ -187,6 +187,46 @@ def get_token_counts(text: str | dict, model: str = "") -> int:
|
|
187
187
|
return len(chatgpt_encoding.encode(text))
|
188
188
|
|
189
189
|
|
190
|
+
def calculate_image_tokens(width: int, height: int, model: str = "gpt-4o"):
|
191
|
+
if width > 2048 or height > 2048:
|
192
|
+
aspect_ratio = width / height
|
193
|
+
if aspect_ratio > 1:
|
194
|
+
width, height = 2048, int(2048 / aspect_ratio)
|
195
|
+
else:
|
196
|
+
width, height = int(2048 * aspect_ratio), 2048
|
197
|
+
|
198
|
+
if width >= height and height > 768:
|
199
|
+
width, height = int((768 / height) * width), 768
|
200
|
+
elif height > width and width > 768:
|
201
|
+
width, height = 768, int((768 / width) * height)
|
202
|
+
|
203
|
+
tiles_width = ceil(width / 512)
|
204
|
+
tiles_height = ceil(height / 512)
|
205
|
+
total_tokens = 85 + 170 * (tiles_width * tiles_height)
|
206
|
+
|
207
|
+
return total_tokens
|
208
|
+
|
209
|
+
|
210
|
+
def get_message_token_counts(messages: list, tools: dict | None = None, model: str = "gpt-4o") -> int:
|
211
|
+
tokens = 0
|
212
|
+
formatted_messages = format_messages(messages, backend=BackendType.OpenAI, native_multimodal=True)
|
213
|
+
for message in formatted_messages:
|
214
|
+
content = message["content"]
|
215
|
+
if isinstance(content, str):
|
216
|
+
tokens += get_token_counts(content, model)
|
217
|
+
elif isinstance(content, list):
|
218
|
+
for item in content:
|
219
|
+
if isinstance(item, dict) and item["type"] == "text":
|
220
|
+
tokens += get_token_counts(item["text"], model)
|
221
|
+
elif isinstance(item, dict) and item["type"].startswith("image"):
|
222
|
+
# TODO: Get real image size
|
223
|
+
tokens += calculate_image_tokens(2048, 2048, model)
|
224
|
+
if tools is not None:
|
225
|
+
tokens += get_token_counts(json.dumps(tools, ensure_ascii=False), model)
|
226
|
+
|
227
|
+
return tokens
|
228
|
+
|
229
|
+
|
190
230
|
def cutoff_messages(
|
191
231
|
messages: list,
|
192
232
|
max_count: int = 16000,
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|