Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Proxied Model Support #379

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions camel/model_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ def run(self, *args, **kwargs):
"gpt-4-32k": 32768,
"gpt-4-turbo": 100000,
}
num_max_token = num_max_token_map[self.model_type.value]
num_max_token = num_max_token_map.get(self.model_type.value, os.environ['CHATDEV_NUM_MAX_TOKEN'])
num_max_completion_tokens = num_max_token - num_prompt_tokens
self.model_config_dict['max_tokens'] = num_max_completion_tokens

response = client.chat.completions.create(*args, **kwargs, model=self.model_type.value,
response = client.chat.completions.create(*args, **kwargs, model=os.environ.get('CHATDEV_CUSTOM_MODEL', self.model_type.value),
**self.model_config_dict)

cost = prompt_cost(
Expand Down
34 changes: 27 additions & 7 deletions chatdev/chat_env.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import re
import base64
import shutil
import signal
import subprocess
Expand All @@ -12,7 +13,10 @@
from chatdev.codes import Codes
from chatdev.documents import Documents
from chatdev.roster import Roster
from chatdev.utils import log_visualize
from chatdev.utils import (
log_visualize,
is_url
)
from ecl.memory import Memory

try:
Expand Down Expand Up @@ -214,12 +218,15 @@ def write_meta(self) -> None:

def generate_images_from_codes(self):
def download(img_url, file_name):
r = requests.get(img_url)
if is_url(img_url):
content = requests.get(img_url).content
else:
content = base64.b64decode(img_url)
filepath = os.path.join(self.env_dict['directory'], file_name)
if os.path.exists(filepath):
os.remove(filepath)
with open(filepath, "wb") as f:
f.write(r.content)
f.write(content)
print("{} Downloaded".format(filepath))

regex = r"(\w+.png)"
Expand All @@ -241,28 +248,39 @@ def download(img_url, file_name):
print("{}: {}".format(filename, desc))
if openai_new_api:
response = openai.images.generate(
model=os.environ.get('CHATDEV_CUSTOM_IMAGE_MODEL', None),
prompt=desc,
n=1,
size="256x256"
)
image_url = response.data[0].url
try:
image_url = response.data[0].url
except KeyError:
image_url = response.data['url']
else:
response = openai.Image.create(
model=os.environ.get('CHATDEV_CUSTOM_IMAGE_MODEL', None),
prompt=desc,
n=1,
size="256x256"
)
image_url = response['data'][0]['url']
try:
image_url = response['data'][0]['url']
except KeyError:
image_url = response['data']['url']
download(image_url, filename)

def get_proposed_images_from_message(self, messages):
def download(img_url, file_name):
r = requests.get(img_url)
if is_url(img_url):
content = requests.get(img_url).content
else:
content = base64.b64decode(img_url)
filepath = os.path.join(self.env_dict['directory'], file_name)
if os.path.exists(filepath):
os.remove(filepath)
with open(filepath, "wb") as f:
f.write(r.content)
f.write(content)
print("{} Downloaded".format(filepath))

regex = r"(\w+.png):(.*?)\n"
Expand Down Expand Up @@ -292,13 +310,15 @@ def download(img_url, file_name):

if openai_new_api:
response = openai.images.generate(
model=os.environ.get('CHATDEV_CUSTOM_IMAGE_MODEL', None),
prompt=desc,
n=1,
size="256x256"
)
image_url = response.data[0].url
else:
response = openai.Image.create(
model=os.environ.get('CHATDEV_CUSTOM_IMAGE_MODEL', None),
prompt=desc,
n=1,
size="256x256"
Expand Down
13 changes: 13 additions & 0 deletions chatdev/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import re
import time

from urllib.parse import urlparse

import markdown
import inspect

from camel.messages.system_messages import SystemMessage
from visualizer.app import send_msg

Expand Down Expand Up @@ -87,3 +90,13 @@ def escape_string(value):
value = re.sub(r'<[^>]*>', '', value)
value = value.replace("\n", " ")
return value

def is_url(url):
"""
Adapted from https://stackoverflow.com/questions/7160737/how-to-validate-a-url-in-python-malformed-or-not
"""
try:
result = urlparse(url)
return all([result.scheme, result.netloc])
except ValueError:
return False