Skip to content

Commit

Permalink
✨ Feature: Support Gemini model to upload images.
Browse files Browse the repository at this point in the history
💻 Code: 1. Upgrade ModelMerge version to 0.8.0

2. Optimize the display of log images in base64 encoding, prevent the complete output of base64 encoding.
  • Loading branch information
yym68686 committed Jun 28, 2024
1 parent 2942310 commit d443d41
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 6 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.7.12",
version="0.8.0",
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
3 changes: 2 additions & 1 deletion src/ModelMerge/models/chatgpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,8 @@ def ask_stream(
json_post["max_tokens"] = model_max_tokens
print("api_url", self.api_url.chat_url)
for _ in range(2):
print(json.dumps(json_post, indent=4, ensure_ascii=False))
replaced_text = json.loads(re.sub(r'/9j/([A-Za-z0-9+/=]+)', '/9j/***', str(json_post)).replace("'", "\""))
print(json.dumps(replaced_text, indent=4, ensure_ascii=False))
try:
response = self.session.post(
self.api_url.chat_url,
Expand Down
4 changes: 3 additions & 1 deletion src/ModelMerge/models/claude.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import re
import json
import copy
import tiktoken
Expand Down Expand Up @@ -318,7 +319,8 @@ def ask_stream(
except:
pass

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

try:
response = self.session.post(
Expand Down
8 changes: 5 additions & 3 deletions src/ModelMerge/models/genimi.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import re
import json
import requests
import tiktoken
Expand Down Expand Up @@ -37,7 +38,7 @@ def add_to_conversation(
if convo_id not in self.conversation or pass_history == False:
self.reset(convo_id=convo_id)
# print("message", message)
self.conversation[convo_id].append({"role": role, "parts": [{"text": message}]})
self.conversation[convo_id].append({"role": role, "parts": message})
if total_tokens:
self.tokens_usage[convo_id] += total_tokens

Expand Down Expand Up @@ -124,7 +125,8 @@ def ask_stream(
}
],
}
print(json.dumps(json_post, indent=4, ensure_ascii=False))
replaced_text = json.loads(re.sub(r'/9j/([A-Za-z0-9+/=]+)', '/9j/***', str(json_post)).replace("'", "\""))
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 @@ -166,4 +168,4 @@ def ask_stream(
except Exception as e:
print("An error occurred:", e)

self.add_to_conversation(full_response, response_role, convo_id=convo_id)
self.add_to_conversation([{"text": full_response}], response_role, convo_id=convo_id)
9 changes: 9 additions & 0 deletions src/ModelMerge/utils/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ def get_image_message(image_url, message, engine = None):
}
}
)
if os.environ.get('GOOGLE_AI_API_KEY', None) and "gemini" in engine:
message.append(
{
"inlineData": {
"mimeType": "image/jpeg",
"data": base64_image.split(",")[1],
}
}
)
return message

def Document_extract(docurl, docpath=None, engine = None):
Expand Down

0 comments on commit d443d41

Please sign in to comment.