-
-
Notifications
You must be signed in to change notification settings - Fork 602
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
Markdown JSON Mode #246
Changes from 2 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a5d9e7d
first commit
Anmol6 abd6bae
s
Anmol6 f3fe7a7
Merge branch 'main' into add_vision_md_json
jxnl 20c80ec
Merge branch 'main' into add_vision_md_json
jxnl f9a930c
fix
Anmol6 9a261d8
add test
Anmol6 56ae384
s
Anmol6 de5dd17
fix retry
Anmol6 d35803a
Merge branch 'main' into add_vision_md_json
Anmol6 eeb16a4
update docs
Anmol6 336d3b3
format
Anmol6 f037b04
Merge branch 'main' into add_vision_md_json
Anmol6 b46b536
s
Anmol6 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import json | ||
import re | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
from docstring_parser import parse | ||
from functools import wraps | ||
from typing import Any, Callable | ||
|
@@ -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: | ||
|
@@ -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}") | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.