Skip to content

Commit

Permalink
1. Add webhook URL display
Browse files Browse the repository at this point in the history
2. Compatible with different forms of API URLs

3. Add total tokens display
  • Loading branch information
yym68686 committed Nov 26, 2023
1 parent 858fa60 commit 38f4a3e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 25 deletions.
1 change: 1 addition & 0 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
41 changes: 21 additions & 20 deletions chatgpt2api/chatgpt2api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 = {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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:
"""
Expand Down
26 changes: 21 additions & 5 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 38f4a3e

Please sign in to comment.