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

Markdown JSON Mode #246

Merged
merged 13 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
63 changes: 63 additions & 0 deletions examples/vision/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import instructor
from openai import OpenAI
from typing import Iterable
from pydantic import BaseModel
import base64

client = instructor.patch(OpenAI(), mode=instructor.function_calls.Mode.MD_JSON)

class Circle(BaseModel):
x: int
y: int
color: str

def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')

def draw_circle(image_size, num_circles, path):

from PIL import Image, ImageDraw
import random

image = Image.new("RGB", image_size, "white")

draw = ImageDraw.Draw(image)
for _ in range(num_circles):
# Randomize the circle properties
radius = 100#random.randint(10, min(image_size)//5) # Radius between 10 and 1/5th of the smallest dimension
x = random.randint(radius, image_size[0] - radius)
y = random.randint(radius, image_size[1] - radius)
color = ['red', 'black', 'blue', 'green'][random.randint(0, 3)]

circle_position = (x - radius, y - radius, x + radius, y + radius)
print(f"Generating circle at {x, y} with color {color}")
draw.ellipse(circle_position, fill=color, outline="black")

image.save(path)

img_path = 'circle.jpg'
draw_circle((1024,1024), 1, img_path)
base64_image = encode_image(img_path)

response = client.chat.completions.create(
model="gpt-4-vision-preview",
max_tokens=1800,
response_model=Circle,
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": 'find the circle'},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
},
},
],
}
],
)

print(f"Found circle with center at x: {response.x}, y: {response.y} and color: {response.color}")
Comment on lines +43 to +63
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no error handling for the API call or file operations. Consider adding try-except blocks to handle potential exceptions and provide a graceful error message to the user.

10 changes: 10 additions & 0 deletions instructor/function_calls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import re
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The re module is imported but not used in the provided code. If it's not used elsewhere, consider removing the import to avoid unnecessary dependencies.

from docstring_parser import parse
from functools import wraps
from typing import Any, Callable
Expand All @@ -13,6 +14,7 @@ class Mode(enum.Enum):
FUNCTIONS: str = "function_call"
TOOLS: str = "tool_call"
JSON: str = "json_mode"
MD_JSON: str = "markdown_json_mode"


class openai_function:
Expand Down Expand Up @@ -237,6 +239,14 @@ def from_response(
context=validation_context,
strict=strict,
)
elif mode == Mode.MD_JSON:
pattern = r'```(?:json)?(.*?)```'
json_content = re.findall(pattern, message.content, re.DOTALL)[0].strip()
return cls.model_validate_json(
json_content.strip(),
Anmol6 marked this conversation as resolved.
Show resolved Hide resolved
context=validation_context,
strict=strict,
)
else:
raise ValueError(f"Invalid patch mode: {mode}")

Expand Down
22 changes: 15 additions & 7 deletions instructor/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,21 @@ def handle_response_model(
"type": "function",
"function": {"name": response_model.openai_schema["name"]},
}
elif mode == Mode.JSON:
new_kwargs["response_format"] = {"type": "json_object"}

# check that the first message is a system message
# if it is not, add a system message to the beginning
message = f"Make sure that your response to any message matches the json_schema below, do not deviate at all: \n{response_model.model_json_schema()['properties']}"

elif mode == Mode.JSON or mode == Mode.MD_JSON:
if mode == Mode.JSON:
new_kwargs["response_format"] = {"type": "json_object"}
# check that the first message is a system message
# if it is not, add a system message to the beginning
message = f"""Make sure that your response to any message matches the json_schema below,
do not deviate at all: \n{response_model.model_json_schema()['properties']}
"""
else:
message = f"""
As a genius expert, your task is to understand the content and provide
the parsed objects in json that match the following json_schema (do not deviate at all and its okay if you cant be exact). Use a
```json<JSON>``` block to provide your response:\n
{response_model.model_json_schema()['properties']}
Anmol6 marked this conversation as resolved.
Show resolved Hide resolved
"""
if new_kwargs["messages"][0]["role"] != "system":
new_kwargs["messages"].insert(
0,
Expand Down
Loading