perf: 为图片base64函数添加lru

This commit is contained in:
RockChinQ 2024-05-16 20:52:17 +08:00
parent 37ef1c9fab
commit 91e23b8c11
4 changed files with 7 additions and 1 deletions

View File

@ -14,6 +14,7 @@ required_deps = {
"yaml": "pyyaml",
"aiohttp": "aiohttp",
"psutil": "psutil",
"async_lru": "async-lru",
}

View File

@ -24,6 +24,9 @@ class ToolCall(pydantic.BaseModel):
class ImageURLContentObject(pydantic.BaseModel):
url: str
def __str__(self):
return self.url[:128] + ('...' if len(self.url) > 128 else '')
class ContentElement(pydantic.BaseModel):

View File

@ -10,6 +10,7 @@ import openai
import openai.types.chat.chat_completion as chat_completion
import httpx
import aiohttp
import async_lru
from .. import api, entities, errors
from ....core import entities as core_entities, app
@ -46,7 +47,6 @@ class OpenAIChatCompletions(api.LLMAPIRequester):
self,
args: dict,
) -> chat_completion.ChatCompletion:
self.ap.logger.debug(f"req chat_completion with args {args}")
return await self.client.chat.completions.create(**args)
async def _make_msg(
@ -124,6 +124,7 @@ class OpenAIChatCompletions(api.LLMAPIRequester):
except openai.APIError as e:
raise errors.RequesterError(f'请求错误: {e.message}')
@async_lru.alru_cache(maxsize=128)
async def get_base64_str(
self,
original_url: str,

View File

@ -14,3 +14,4 @@ pydantic
websockets
urllib3
psutil
async-lru