This commit is contained in:
Jason Liu
2023-12-01 20:35:34 -05:00
parent ae59ed434f
commit 7a9f57be7d
6 changed files with 29 additions and 26 deletions
+13 -10
View File
@@ -31,6 +31,7 @@ data = [
if __name__ == "__main__":
class UserDetail(BaseModel):
name: str = Field(description="Name extracted from the text")
age: int = Field(description="Age extracted from the text")
@@ -41,16 +42,18 @@ if __name__ == "__main__":
for content in data:
MaybeUser = Maybe(UserDetail)
user = client.chat.completions.create(
response_model=MaybeUser, model="teknium/openhermes-2.5-mistral-7b", messages=[
{
"role": "system",
"content": f"You are an expert at outputting json. You always output valid json based on this schema: {MaybeUser.model_json_schema()}",
},
{
"role": "user",
"content": f"Extract the user details from the following text: {content}. Match your response the correct schema",
},
]
response_model=MaybeUser,
model="teknium/openhermes-2.5-mistral-7b",
messages=[
{
"role": "system",
"content": f"You are an expert at outputting json. You always output valid json based on this schema: {MaybeUser.model_json_schema()}",
},
{
"role": "user",
"content": f"Extract the user details from the following text: {content}. Match your response the correct schema",
},
],
)
# Output the error or the result.
if user.error:
+1 -1
View File
@@ -59,7 +59,7 @@ if __name__ == "__main__":
messages=[
{
"role": "system",
"content": f"You are an expert at outputting json. You always output valid JSON based on the pydantic schema given to you.",
"content": "You are an expert at outputting json. You always output valid JSON based on the pydantic schema given to you.",
},
{
"role": "user",
+14 -12
View File
@@ -1,22 +1,23 @@
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')
return base64.b64encode(image_file.read()).decode("utf-8")
def draw_circle(image_size, num_circles, path):
from PIL import Image, ImageDraw
import random
@@ -25,10 +26,10 @@ def draw_circle(image_size, num_circles, path):
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
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)]
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}")
@@ -36,8 +37,9 @@ def draw_circle(image_size, num_circles, path):
image.save(path)
img_path = 'circle.jpg'
draw_circle((1024,1024), 1, img_path)
img_path = "circle.jpg"
draw_circle((1024, 1024), 1, img_path)
base64_image = encode_image(img_path)
response = client.chat.completions.create(
@@ -48,16 +50,16 @@ response = client.chat.completions.create(
{
"role": "user",
"content": [
{"type": "text", "text": 'find the circle'},
{"type": "text", "text": "find the circle"},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
},
"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}")
print(
f"Found circle with center at x: {response.x}, y: {response.y} and color: {response.color}"
)
-1
View File
@@ -1,5 +1,4 @@
import json
import re
from docstring_parser import parse
from functools import wraps
from typing import Any, Callable
-1
View File
@@ -62,7 +62,6 @@ def test_json_mode():
assert user.age == 25
def test_markdown_json_mode():
response = client.chat.completions.create(
model="gpt-3.5-turbo-1106",
+1 -1
View File
@@ -3,7 +3,7 @@ from openai import OpenAI
from pydantic import BaseModel
import instructor
from instructor.function_calls import OpenAISchema, Mode
from instructor.function_calls import Mode
class User(BaseModel):