From 36e5331ecea82238c0405e92bc4021b0161284c0 Mon Sep 17 00:00:00 2001 From: luyaxi Date: Tue, 22 Oct 2024 16:12:38 +0800 Subject: [PATCH] change to random choice --- codelinker/config.py | 8 +++++--- codelinker/request/objGen.py | 14 ++++++++------ pyproject.toml | 2 +- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/codelinker/config.py b/codelinker/config.py index 09bc96c..dd5feda 100644 --- a/codelinker/config.py +++ b/codelinker/config.py @@ -8,6 +8,7 @@ import os +import random from typing import Literal from pydantic import BaseModel from copy import deepcopy @@ -96,7 +97,8 @@ def get_apiconfig_by_model(self, model_name: str = None) -> "CodeLinkerConfig.AP dict: Dictionary containing the fetched API configuration. """ normalized_model_name = self.get_model_name(model_name) - apiconfig = deepcopy(self.api_keys[normalized_model_name][0]) - self.api_keys[normalized_model_name].append( - self.api_keys[normalized_model_name].pop(0)) + apiconfig = deepcopy(random.choice(self.api_keys[normalized_model_name])) + # apiconfig = deepcopy(self.api_keys[normalized_model_name][0]) + # self.api_keys[normalized_model_name].append( + # self.api_keys[normalized_model_name].pop(0)) return apiconfig diff --git a/codelinker/request/objGen.py b/codelinker/request/objGen.py index c324c4e..8effb72 100644 --- a/codelinker/request/objGen.py +++ b/codelinker/request/objGen.py @@ -12,6 +12,7 @@ from copy import deepcopy from tenacity import AsyncRetrying, RetryError, stop_after_attempt from logging import Logger +from concurrent.futures import ThreadPoolExecutor, as_completed from ..models import StructuredRet, StructureSchema from ..config import CodeLinkerConfig @@ -22,18 +23,17 @@ def __init__(self, config: CodeLinkerConfig, logger: Logger): self.config = config self.logger = logger self.chatcompletion_request_funcs = {} + self.hash2files = {} if self.config.request.use_cache: - loop = asyncio.get_event_loop() - loop.run_until_complete(self._load_cache_files()) + self._load_cache_files() if self.config.request.save_completions: os.makedirs(self.config.request.save_completions_path, exist_ok=True) - async def _load_cache_files(self): + def _load_cache_files(self): self.logger.warning( "use_cache is enabled, loading completions from cache...") - self.hash2files = {} files = glob.glob(os.path.join( self.config.request.save_completions_path, "*.json")) files.sort(key=os.path.getmtime) @@ -44,8 +44,10 @@ def load_file(file): self.hash2files[hash(json.dumps( data["request"], sort_keys=True))] = file - tasks = [asyncio.to_thread(load_file, file) for file in files] - await asyncio.gather(*tasks) + with ThreadPoolExecutor() as executor: + futures = [executor.submit(load_file, file) for file in files] + for f in as_completed(futures): + f.result() self.logger.warning( "Cache loaded and enabled, which may cause unexpected behavior.") diff --git a/pyproject.toml b/pyproject.toml index 6329eea..2d596b6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "codelinker" -version = "0.3.26" +version = "0.3.27" description = "A tool to link the code with large language models." authors = ["luyaxi "] readme = "README.md"