diff --git a/examples/open_source_examples/openrouter.py b/examples/open_source_examples/openrouter.py index 5589b6b..9f38dd2 100644 --- a/examples/open_source_examples/openrouter.py +++ b/examples/open_source_examples/openrouter.py @@ -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: diff --git a/examples/open_source_examples/perplexity.py b/examples/open_source_examples/perplexity.py index e28ada2..aecd9c8 100644 --- a/examples/open_source_examples/perplexity.py +++ b/examples/open_source_examples/perplexity.py @@ -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", diff --git a/examples/vision/run.py b/examples/vision/run.py index ce74985..49c76ec 100644 --- a/examples/vision/run.py +++ b/examples/vision/run.py @@ -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}") \ No newline at end of file +print( + f"Found circle with center at x: {response.x}, y: {response.y} and color: {response.color}" +) diff --git a/instructor/function_calls.py b/instructor/function_calls.py index a16e116..25324b5 100644 --- a/instructor/function_calls.py +++ b/instructor/function_calls.py @@ -1,5 +1,4 @@ import json -import re from docstring_parser import parse from functools import wraps from typing import Any, Callable diff --git a/tests/openai/test_modes.py b/tests/openai/test_modes.py index 92a1040..618762d 100644 --- a/tests/openai/test_modes.py +++ b/tests/openai/test_modes.py @@ -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", diff --git a/tests/openai/test_multitask.py b/tests/openai/test_multitask.py index ad42b85..86ff091 100644 --- a/tests/openai/test_multitask.py +++ b/tests/openai/test_multitask.py @@ -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):