diff --git a/bot.py b/bot.py index db019c41..e7417cf2 100644 --- a/bot.py +++ b/bot.py @@ -589,6 +589,7 @@ async def post_init(application: Application) -> None: application.add_error_handler(error) if WEB_HOOK: + print("WEB_HOOK:", WEB_HOOK) application.run_webhook("127.0.0.1", PORT, webhook_url=WEB_HOOK) else: application.run_polling() \ No newline at end of file diff --git a/chatgpt2api/chatgpt2api.py b/chatgpt2api/chatgpt2api.py index ed3eef20..82726819 100644 --- a/chatgpt2api/chatgpt2api.py +++ b/chatgpt2api/chatgpt2api.py @@ -55,6 +55,20 @@ def get_filtered_keys_from_object(obj: object, *keys: str) -> Set[str]: "claude-2", ] +class openaiAPI: + def __init__( + self, + api_url: str = (os.environ.get("API_URL") or "https://api.openai.com/v1/chat/completions"), + ): + from urllib.parse import urlparse, urlunparse + self.source_api_url: str = api_url + parsed_url = urlparse(self.source_api_url) + self.base_url: str = urlunparse(parsed_url[:2] + ("",) * 4) + self.chat_url: str = urlunparse(parsed_url[:2] + ("/v1/chat/completions",) + ("",) * 3) + self.image_url: str = urlunparse(parsed_url[:2] + ("/v1/images/generations",) + ("",) * 3) + +bot_api_url = openaiAPI() + class Imagebot: def __init__( self, @@ -72,10 +86,7 @@ def dall_e_3( model: str = None, **kwargs, ): - if os.environ.get("API_URL") and "v1" in os.environ.get("API_URL"): - url = os.environ.get("API_URL").split("v1")[0] + "v1/images/generations" - else: - url = "https://api.openai.com/v1/images/generations" + url = bot_api_url.image_url headers = {"Authorization": f"Bearer {kwargs.get('api_key', self.api_key)}"} json_post = { @@ -280,21 +291,8 @@ def ask_stream( self.__truncate_conversation(convo_id=convo_id) # print(self.conversation[convo_id]) # Get response - if os.environ.get("API_URL") and os.environ.get("MODEL_NAME"): - # https://learn.microsoft.com/en-us/azure/cognitive-services/openai/chatgpt-quickstart?tabs=command-line&pivots=rest-api - url = ( - os.environ.get("API_URL") - + "openai/deployments/" - + os.environ.get("MODEL_NAME") - + "/chat/completions?api-version=2023-05-15" - ) - headers = {"Content-Type": "application/json", "api-key": self.api_key} - else: - url = ( - os.environ.get("API_URL") - or "https://api.openai.com/v1/chat/completions" - ) - headers = {"Authorization": f"Bearer {kwargs.get('api_key', self.api_key)}"} + url = bot_api_url.chat_url + headers = {"Authorization": f"Bearer {kwargs.get('api_key', self.api_key)}"} if self.engine == "gpt-4-1106-preview": model_max_tokens = kwargs.get("max_tokens", self.max_tokens) @@ -388,6 +386,7 @@ def ask_stream( yield from self.ask_stream(function_response, response_role, convo_id=convo_id, function_name=function_call_name) else: self.add_to_conversation(full_response, response_role, convo_id=convo_id) + print("total tokens:", self.get_token_count(convo_id)) async def ask_stream_async( self, @@ -413,7 +412,7 @@ async def ask_stream_async( # Get response async with self.aclient.stream( "post", - os.environ.get("API_URL") or "https://api.openai.com/v1/chat/completions", + bot_api_url.chat_url, headers={"Authorization": f"Bearer {kwargs.get('api_key', self.api_key)}"}, json={ "model": model or self.engine, @@ -472,6 +471,7 @@ async def ask_stream_async( full_response += content yield content self.add_to_conversation(full_response, response_role, convo_id=convo_id) + print("total tokens:", self.get_token_count(convo_id)) async def ask_async( self, @@ -662,6 +662,7 @@ def search_summary( chain_thread.start() full_response = yield from chainStreamHandler.generate_tokens() self.add_to_conversation(full_response, "assistant", convo_id=convo_id) + print("total tokens:", self.get_token_count(convo_id)) def rollback(self, n: int = 1, convo_id: str = "default") -> None: """ diff --git a/test/test.py b/test/test.py index 513d8b1d..db76fd09 100644 --- a/test/test.py +++ b/test/test.py @@ -25,10 +25,26 @@ # print(truncate_limit) import os -import sys -import json -sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +# import sys +# import json +# sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -from utils.function_call import function_call_list +# from utils.function_call import function_call_list -print(json.dumps(function_call_list["web_search"], indent=4)) +# print(json.dumps(function_call_list["web_search"], indent=4)) + +class openaiAPI: + def __init__( + self, + api_url: str = (os.environ.get("API_URL") or "https://api.openai.com/v1/chat/completions"), + ): + from urllib.parse import urlparse, urlunparse + self.source_api_url: str = api_url + parsed_url = urlparse(self.source_api_url) + self.base_url: str = urlunparse(parsed_url[:2] + ("",) * 4) + self.chat_url: str = urlunparse(parsed_url[:2] + ("/v1/chat/completions",) + ("",) * 3) + self.image_url: str = urlunparse(parsed_url[:2] + ("/v1/images/generations",) + ("",) * 3) + + +a = openaiAPI() +print(a.chat_url) \ No newline at end of file