Skip to content

Commit

Permalink
change to random choice
Browse files Browse the repository at this point in the history
  • Loading branch information
luyaxi committed Oct 22, 2024
1 parent ce31be7 commit 36e5331
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
8 changes: 5 additions & 3 deletions codelinker/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@


import os
import random
from typing import Literal
from pydantic import BaseModel
from copy import deepcopy
Expand Down Expand Up @@ -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
14 changes: 8 additions & 6 deletions codelinker/request/objGen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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.")
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
readme = "README.md"
Expand Down

0 comments on commit 36e5331

Please sign in to comment.