Skip to content

Commit

Permalink
Modify the log to not display the specific content of the request bod…
Browse files Browse the repository at this point in the history
…y by default.
  • Loading branch information
yym68686 committed Sep 18, 2024
1 parent 1440b13 commit 715dc63
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 22 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

setup(
name="modelmerge",
version="0.11.35",
version="0.11.36",
description="modelmerge is a multi-large language model API aggregator.",
long_description=Path.open(Path("README.md"), encoding="utf-8").read(),
long_description_content_type="text/markdown",
Expand Down
2 changes: 2 additions & 0 deletions src/ModelMerge/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def __init__(
reply_count: int = 1,
truncate_limit: int = None,
use_plugins: bool = True,
print_log: bool = False,
) -> None:
self.api_key: str = api_key
self.engine: str = engine
Expand Down Expand Up @@ -125,6 +126,7 @@ def __init__(
self.function_calls_counter = {}
self.function_call_max_loop = 10
self.use_plugins = use_plugins
self.print_log: bool = print_log

def add_to_conversation(
self,
Expand Down
13 changes: 8 additions & 5 deletions src/ModelMerge/models/chatgpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ def __init__(
reply_count: int = 1,
truncate_limit: int = None,
use_plugins: bool = True,
print_log: bool = False,
) -> None:
"""
Initialize Chatbot with API key (from https://platform.openai.com/account/api-keys)
"""
super().__init__(api_key, engine, api_url, system_prompt, proxy, timeout, max_tokens, temperature, top_p, presence_penalty, frequency_penalty, reply_count, truncate_limit, use_plugins=use_plugins)
super().__init__(api_key, engine, api_url, system_prompt, proxy, timeout, max_tokens, temperature, top_p, presence_penalty, frequency_penalty, reply_count, truncate_limit, use_plugins=use_plugins, print_log=print_log)
self.conversation: dict[str, list[dict]] = {
"default": [
{
Expand Down Expand Up @@ -399,8 +400,9 @@ def ask_stream(
# "content": mess["content"][0]["text"]
# }
for _ in range(3):
replaced_text = json.loads(re.sub(r'/9j/([A-Za-z0-9+/=]+)', '/9j/***', json.dumps(json_post)))
print(json.dumps(replaced_text, indent=4, ensure_ascii=False))
if self.print_log:
replaced_text = json.loads(re.sub(r'/9j/([A-Za-z0-9+/=]+)', '/9j/***', json.dumps(json_post)))
print(json.dumps(replaced_text, indent=4, ensure_ascii=False))
response = None
try:
response = self.session.post(
Expand Down Expand Up @@ -624,8 +626,9 @@ async def ask_stream_async(
total_tokens = 0

for _ in range(3):
replaced_text = json.loads(re.sub(r'/9j/([A-Za-z0-9+/=]+)', '/9j/***', json.dumps(json_post)))
print(json.dumps(replaced_text, indent=4, ensure_ascii=False))
if self.print_log:
replaced_text = json.loads(re.sub(r'/9j/([A-Za-z0-9+/=]+)', '/9j/***', json.dumps(json_post)))
print(json.dumps(replaced_text, indent=4, ensure_ascii=False))
try:
async with self.aclient.stream(
"post",
Expand Down
13 changes: 8 additions & 5 deletions src/ModelMerge/models/claude.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ def __init__(
top_p: float = 0.7,
timeout: float = 20,
use_plugins: bool = True,
print_log: bool = False,
):
super().__init__(api_key, engine, api_url, system_prompt, timeout=timeout, temperature=temperature, top_p=top_p, use_plugins=use_plugins)
super().__init__(api_key, engine, api_url, system_prompt, timeout=timeout, temperature=temperature, top_p=top_p, use_plugins=use_plugins, print_log=print_log)
# self.api_url = api_url
self.conversation = claudeConversation()

Expand Down Expand Up @@ -350,8 +351,9 @@ def ask_stream(
except:
pass

replaced_text = json.loads(re.sub(r'/9j/([A-Za-z0-9+/=]+)', '/9j/***', json.dumps(json_post)))
print(json.dumps(replaced_text, indent=4, ensure_ascii=False))
if self.print_log:
replaced_text = json.loads(re.sub(r'/9j/([A-Za-z0-9+/=]+)', '/9j/***', json.dumps(json_post)))
print(json.dumps(replaced_text, indent=4, ensure_ascii=False))

try:
response = self.session.post(
Expand Down Expand Up @@ -506,8 +508,9 @@ async def ask_stream_async(
except:
pass

replaced_text = json.loads(re.sub(r'/9j/([A-Za-z0-9+/=]+)', '/9j/***', json.dumps(json_post)))
print(json.dumps(replaced_text, indent=4, ensure_ascii=False))
if self.print_log:
replaced_text = json.loads(re.sub(r'/9j/([A-Za-z0-9+/=]+)', '/9j/***', json.dumps(json_post)))
print(json.dumps(replaced_text, indent=4, ensure_ascii=False))

try:
response = self.session.post(
Expand Down
14 changes: 8 additions & 6 deletions src/ModelMerge/models/gemini.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ def __init__(
top_p: float = 0.7,
timeout: float = 20,
use_plugins: bool = True,
print_log: bool = False,
):
url = api_url.format(model=engine, stream="streamGenerateContent", api_key=os.environ.get("GOOGLE_AI_API_KEY", api_key))
super().__init__(api_key, engine, url, system_prompt=system_prompt, timeout=timeout, temperature=temperature, top_p=top_p, use_plugins=use_plugins)
super().__init__(api_key, engine, url, system_prompt=system_prompt, timeout=timeout, temperature=temperature, top_p=top_p, use_plugins=use_plugins, print_log=print_log)
self.api_url: str = BaseAPI(url)

self.conversation: dict[str, list[dict]] = {
Expand Down Expand Up @@ -148,8 +149,9 @@ def ask_stream(
}
],
}
replaced_text = json.loads(re.sub(r'/9j/([A-Za-z0-9+/=]+)', '/9j/***', json.dumps(json_post)))
print(json.dumps(replaced_text, indent=4, ensure_ascii=False))
if self.print_log:
replaced_text = json.loads(re.sub(r'/9j/([A-Za-z0-9+/=]+)', '/9j/***', json.dumps(json_post)))
print(json.dumps(replaced_text, indent=4, ensure_ascii=False))

url = self.api_url.format(model=model or self.engine, stream="streamGenerateContent", api_key=self.api_key)

Expand Down Expand Up @@ -271,9 +273,9 @@ async def ask_stream_async(
url = self.api_url.source_api_url
print("url", url)

replaced_text = json.loads(re.sub(r'/9j/([A-Za-z0-9+/=]+)', '/9j/***', json.dumps(json_post)))
print(json.dumps(replaced_text, indent=4, ensure_ascii=False))

if self.print_log:
replaced_text = json.loads(re.sub(r'/9j/([A-Za-z0-9+/=]+)', '/9j/***', json.dumps(json_post)))
print(json.dumps(replaced_text, indent=4, ensure_ascii=False))

response_role: str = "model"
full_response: str = ""
Expand Down
13 changes: 8 additions & 5 deletions src/ModelMerge/models/vertex.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@ def __init__(
top_p: float = 0.7,
timeout: float = 20,
use_plugins: bool = True,
print_log: bool = False,
):
url = api_url.format(PROJECT_ID=os.environ.get("VERTEX_PROJECT_ID", project_id), MODEL_ID=engine, stream="streamGenerateContent")
super().__init__(api_key, engine, url, system_prompt=system_prompt, timeout=timeout, temperature=temperature, top_p=top_p, use_plugins=use_plugins)
super().__init__(api_key, engine, url, system_prompt=system_prompt, timeout=timeout, temperature=temperature, top_p=top_p, use_plugins=use_plugins, print_log=print_log)
self.conversation: dict[str, list[dict]] = {
"default": [],
}
Expand Down Expand Up @@ -205,8 +206,9 @@ def ask_stream(
}
],
}
replaced_text = json.loads(re.sub(r'/9j/([A-Za-z0-9+/=]+)', '/9j/***', json.dumps(json_post)))
print(json.dumps(replaced_text, indent=4, ensure_ascii=False))
if self.print_log:
replaced_text = json.loads(re.sub(r'/9j/([A-Za-z0-9+/=]+)', '/9j/***', json.dumps(json_post)))
print(json.dumps(replaced_text, indent=4, ensure_ascii=False))

url = self.api_url.format(model=model or self.engine, stream="streamGenerateContent", api_key=self.api_key)

Expand Down Expand Up @@ -333,8 +335,9 @@ async def ask_stream_async(
except:
pass

replaced_text = json.loads(re.sub(r'/9j/([A-Za-z0-9+/=]+)', '/9j/***', json.dumps(json_post)))
print(json.dumps(replaced_text, indent=4, ensure_ascii=False))
if self.print_log:
replaced_text = json.loads(re.sub(r'/9j/([A-Za-z0-9+/=]+)', '/9j/***', json.dumps(json_post)))
print(json.dumps(replaced_text, indent=4, ensure_ascii=False))

url = "https://us-central1-aiplatform.googleapis.com/v1/projects/{PROJECT_ID}/locations/us-central1/publishers/google/models/{MODEL_ID}:{stream}".format(PROJECT_ID=os.environ.get("VERTEX_PROJECT_ID"), MODEL_ID=model, stream="streamGenerateContent")
self.api_url = BaseAPI(url)
Expand Down

0 comments on commit 715dc63

Please sign in to comment.