Skip to content

Commit

Permalink
check if bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
vinid committed Jul 7, 2024
1 parent f44350a commit 0d35013
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
6 changes: 3 additions & 3 deletions textgrad/engine/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ def __call__(self, prompt, **kwargs):

@retry(wait=wait_random_exponential(min=1, max=5), stop=stop_after_attempt(5))
def generate(self, content: Union[str, List[Union[str, bytes]]], system_prompt=None, **kwargs):
if isinstance(content, str):
if any(isinstance(item, bytes) for item in content):
return self._generate_text(content, system_prompt=system_prompt, **kwargs)

elif isinstance(content, list):
if (not self.is_multimodal):
if not self.is_multimodal:
raise NotImplementedError("Multimodal generation is only supported for Claude-3 and beyond.")

return self._generate_multimodal(content, system_prompt=system_prompt, **kwargs)
Expand Down Expand Up @@ -88,7 +88,7 @@ def _format_content(self, content: List[Union[str, bytes]]) -> List[dict]:

image_media_type = f"image/{image_type}"
base64_image = base64.b64encode(item).decode('utf-8')
formatted_content.append( {
formatted_content.append({
"type": "image",
"source": {
"type": "base64",
Expand Down
11 changes: 5 additions & 6 deletions textgrad/engine/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,14 @@ def __init__(

@retry(wait=wait_random_exponential(min=1, max=5), stop=stop_after_attempt(5))
def generate(self, content: Union[str, List[Union[str, bytes]]], system_prompt=None, **kwargs):
if isinstance(content, str):
return self._generate_text(content, system_prompt=system_prompt, **kwargs)

elif isinstance(content, list):
if (not self.is_multimodal):
if any(isinstance(item, bytes) for item in content):
if not self.is_multimodal:
raise NotImplementedError("Multimodal generation is only supported for GPT-4 models.")

return self._generate_multimodal(content, system_prompt=system_prompt, **kwargs)

return self._generate_text(content, system_prompt=system_prompt, **kwargs)

def _generate_text(
self, prompt, system_prompt=None, temperature=0, max_tokens=2000, top_p=0.99
):
Expand Down

0 comments on commit 0d35013

Please sign in to comment.