From ad642a83a619663c0a1f69ba2879883241a66851 Mon Sep 17 00:00:00 2001 From: rex <1073853456@qq.com> Date: Thu, 4 Apr 2024 21:11:16 +0800 Subject: [PATCH 1/2] remove costs --- chattool/asynctool.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/chattool/asynctool.py b/chattool/asynctool.py index c1daece..088d3ab 100644 --- a/chattool/asynctool.py +++ b/chattool/asynctool.py @@ -54,6 +54,7 @@ async def async_process_msgs( chatlogs:List[List[Dict]] , nproc:int=1 , timeout:int=0 , timeinterval:int=0 + , showcost:bool=False , **options )->List[bool]: """Process messages asynchronously @@ -100,7 +101,7 @@ async def chat_complete(ind, locker, chat_log, chkpoint, **options): chat = Chat(chat_log) async with locker: # locker | not necessary for normal IO chat.save(chkpoint, index=ind) - return ind, resp.cost() + return ind, resp.cost() if showcost else 0 async with sem, aiohttp.ClientSession() as session: tasks = [] @@ -124,7 +125,6 @@ async def chat_complete(ind, locker, chat_log, chkpoint, **options): def async_chat_completion( msgs:Union[List[List[Dict]], str] , chkpoint:str - , model:str='gpt-3.5-turbo' , api_key:Union[str, None]=None , chat_url:Union[str, None]=None , max_tries:int=1 @@ -132,11 +132,13 @@ def async_chat_completion( msgs:Union[List[List[Dict]], str] , timeout:int=0 , timeinterval:int=0 , clearfile:bool=False - , notrun:bool=False - , msg2log:Union[Callable, None]=None + , wait:bool=False + , showcost:bool=False , data2chat:Union[Callable, None]=None + , msg2log:Union[Callable, None]=None , max_requests:int=-1 , ncoroutines:int=1 + , notrun:bool=False , **options ): """Asynchronous chat completion @@ -151,24 +153,24 @@ def async_chat_completion( msgs:Union[List[List[Dict]], str] timeout (int, optional): timeout for the API call. Defaults to 0(no timeout). timeinterval (int, optional): time interval between two API calls. Defaults to 0. clearfile (bool, optional): whether to clear the checkpoint file. Defaults to False. - notrun (bool, optional): whether to run the async process. It should be True - when use in Jupyter Notebook. Defaults to False. + wait (bool, optional): wait for the `await` command. Defaults to False. msg2log (Union[Callable, None], optional): function to convert message to chat log. Defaults to None. data2chat (Union[Callable, None], optional): function to convert data to Chat object. Defaults to None. + notrun (bool, optional): (Deprecated) wait for the `await` command. Defaults to False. max_requests (int, optional): (Deprecated)maximum number of requests to make. Defaults to -1. ncoroutines (int, optional): (Deprecated)number of coroutines. Defaults to 1. Returns: List[Dict]: list of responses """ - # convert chatlogs + # convert msg to chatlogs if data2chat is not None: msg2log = lambda data: data2chat(data).chat_log elif msg2log is None: # By default, use method from the Chat object msg2log = lambda data: Chat(data).chat_log - # use nproc instead of ncoroutines + # number of coroutines nproc = max(nproc, ncoroutines) chatlogs = [msg2log(log) for log in msgs] if clearfile and os.path.exists(chkpoint): @@ -184,6 +186,8 @@ def async_chat_completion( msgs:Union[List[List[Dict]], str] else: raise Exception("chat_url is not provided!") chat_url = chattool.request.normalize_url(chat_url) + if 'model' not in options: + options['model'] = chattool.model if chattool.model else "gpt-3.5-turbo" # run async process assert nproc > 0, "nproc must be greater than 0!" max_tries = max(max_tries, max_requests) @@ -193,13 +197,13 @@ def async_chat_completion( msgs:Union[List[List[Dict]], str] "api_key": api_key, "chat_url": chat_url, "max_tries": max_tries, + "showcost": showcost, "nproc": nproc, "timeout": timeout, "timeinterval": timeinterval, - "model": model, **options } - if notrun: # when use in Jupyter Notebook + if notrun or wait: # when use in Jupyter Notebook return async_process_msgs(**args) # return the async object else: return asyncio.run(async_process_msgs(**args)) \ No newline at end of file From a0fbc6b2e59bcf6ef0afdbed87094cb69fcd6382 Mon Sep 17 00:00:00 2001 From: rex <1073853456@qq.com> Date: Thu, 4 Apr 2024 21:14:26 +0800 Subject: [PATCH 2/2] update api of valid models --- chattool/__init__.py | 2 +- chattool/request.py | 2 -- setup.py | 2 +- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/chattool/__init__.py b/chattool/__init__.py index b2dd0e6..0828118 100644 --- a/chattool/__init__.py +++ b/chattool/__init__.py @@ -2,7 +2,7 @@ __author__ = """Rex Wang""" __email__ = '1073853456@qq.com' -__version__ = '3.1.3' +__version__ = '3.1.4' import os, sys, requests from .chattype import Chat, Resp diff --git a/chattool/request.py b/chattool/request.py index a080f7b..1a23e23 100644 --- a/chattool/request.py +++ b/chattool/request.py @@ -95,7 +95,6 @@ def valid_models(api_key:str, model_url:str, gpt_only:bool=True): """ headers = { "Authorization": "Bearer " + api_key, - "Content-Type": "application/json" } model_response = requests.get(normalize_url(model_url), headers=headers) if model_response.status_code == 200: @@ -132,7 +131,6 @@ def filecontent(api_key:str, base_url:str, fileid:str): """Returns the contents of the specified file""" headers = { "Authorization": "Bearer " + api_key, - "Content-Type": "application/json" } fileurl = normalize_url(os.path.join(base_url, "v1/files", fileid, "content")) resp = requests.get(fileurl, headers=headers) diff --git a/setup.py b/setup.py index f448ede..85e2f07 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ with open('README.md') as readme_file: readme = readme_file.read() -VERSION = '3.1.3' +VERSION = '3.1.4' requirements = [ 'Click>=7.0', 'requests>=2.20', "responses>=0.23", 'aiohttp>=3.8',