Skip to content

Commit

Permalink
added new models and GPT Vision with tools calling
Browse files Browse the repository at this point in the history
  • Loading branch information
Jayr Castro authored and Jayr Castro committed May 10, 2024
1 parent 9e0aa0b commit 5e413b2
Show file tree
Hide file tree
Showing 4 changed files with 307 additions and 15 deletions.
95 changes: 91 additions & 4 deletions serverless_openai/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Config:
def chat_completion(
self,
messages: Messages,
model: Union[TextCompletionModels, str] = TextCompletionModels.gpt4_0125,
model: Union[TextCompletionModels, str] = TextCompletionModels.gpt4_turbo,
tries: int = 5,
timeout: int = 500,
temperature: Optional[float] = 1,
Expand Down Expand Up @@ -187,7 +187,7 @@ def dall_e(
def vision(
self,
messages: VisionMessage,
model: Union[VisionModels, str] = VisionModels.gpt4_vision,
model: Union[VisionModels, str] = VisionModels.gpt4_turbo,
tries: int = 5,
timeout: int = 500,
temperature: Optional[float] = 1,
Expand Down Expand Up @@ -232,11 +232,11 @@ def vision(
def vision_longimage(
self,
messages: VisionMessage,
model: Union[VisionModels, str] = VisionModels.gpt4_vision,
model: Union[VisionModels, str] = VisionModels.gpt4_turbo,
tries: int = 5,
timeout: int = 500,
temperature: Optional[float] = 1,
max_tokens: Optional[int] = 1024,
max_tokens: Optional[int] = 1024
) -> OpenAIResults:

if isinstance(messages.image, list):
Expand Down Expand Up @@ -297,6 +297,93 @@ def vision_longimage(
except Exception as e:
print("ERROR:", e)
return OpenAIResults(result=False, result_json=res)

def vision_tools(
self,
messages: VisionMessage,
model: Union[VisionModels, str] = VisionModels.gpt4_turbo,
tries: int = 5,
timeout: int = 500,
temperature: Optional[float] = 1,
max_tokens: Optional[int] = 1024,
tools: Optional[List[dict]] = None,
tool_choice: Optional[str] = None,
response_format: Optional[dict] = {"type": "json_object"},
) -> OpenAIResults:

if isinstance(messages.image, list):
img_b64_list = []
for img in messages.image:
try:
TypeAdapter(HttpUrl).validate_python(img)
img_b64_list.append(img)
except:
if 'data:image/jpeg;base64' in img:
image_np = b64_to_np(img)
else:
image_np = urlimage_to_np(img)
img_b64_list.extend(crop_image(image_np))

elif isinstance(messages.image, str):
if 'data:image/jpeg;base64' in messages.image:
image_np = b64_to_np(messages.image)
else:
image_np = urlimage_to_np(messages.image)
img_b64_list = crop_image(image_np)

elif isinstance(messages.image, np.ndarray):
image_np = messages.image
img_b64_list = crop_image(image_np)

newm = [
{
"role": messages.role,
"content": [
{
"type": "text",
"text": messages.text
},
]
}
]
for b64 in img_b64_list:
newm[0]['content'].append(
{
"type": "image_url",
"image_url": {
"url": b64,
"detail": "high"
}
}
)

data = {
"model": model,
"messages": newm,
"temperature": temperature,
}

if max_tokens:
data['max_tokens'] = max_tokens

if tool_choice:
data["response_format"] = response_format
data["tools"] = tools
data["tool_choice"] = {"type": "function", "function": {"name": tool_choice}}

results = {}
for _ in range(tries):
try:
results = requests.post(self.completion_url, headers=self.headers, json=data, timeout=timeout).json()
if 'choices' in results:
res = results['choices'][0]['message']
res_json = json.loads(res['tool_calls'][0]['function']['arguments'], strict=False)
return OpenAIResults(result=res_json, result_json=results)
else:
print("RESULTS:", results)
except Exception as e:
print("ERROR:", e)
return OpenAIResults(result=False, result_json=results)

def embeddings(
self,
Expand Down
4 changes: 3 additions & 1 deletion serverless_openai/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class Roles(str, ExtendedEnum):
class TextCompletionModels(str, ExtendedEnum):
gpt4_1106 : str = "gpt-4-1106-preview"
gpt4 : str = "gpt-4"
gpt4_turbo : str = "gpt-4-turbo-preview"
gpt4_turbo : str = "gpt-4-turbo"
gpt4_turbo_prev : str = "gpt-4-turbo-preview"
gpt4_0125 : str = "gpt-4-0125-preview"
gpt35_turbo_0125: str = "gpt-3.5-turbo-0125"
gpt35_turbo_1106 : str = "gpt-3.5-turbo-1106"
Expand All @@ -45,6 +46,7 @@ class ImageCreationModels(str, ExtendedEnum):

class VisionModels(str, ExtendedEnum):
gpt4_vision : str = "gpt-4-vision-preview"
gpt4_turbo : str = "gpt-4-turbo"

class VisionMessage(BaseModel):
text: str
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup, find_packages

VERSION = '1.3.7'
VERSION = '1.3.8'
DESCRIPTION = "A package for using Openai in serverless environment"
LONG_DESCRIPTION = 'A package for using Openai with scraping and etc. in serverless application such as AWS Lambda and GCP Cloud Function'

Expand Down
Loading

0 comments on commit 5e413b2

Please sign in to comment.