instructor
89 строк · 2.9 Кб
1from openai import OpenAI
2from pydantic import BaseModel, Field
3
4client = OpenAI()
5
6
7class SearchQuery(BaseModel):
8product_name: str
9query: str = Field(
10...,
11description="A descriptive query to search for the product, include adjectives, and the product type. will be used to serve relevant products to the user.",
12)
13
14
15class MultiSearchQuery(BaseModel):
16products: list[SearchQuery]
17
18
19def extract_table(url: str):
20completion = client.chat.completions.create(
21model="gpt-4-vision-preview",
22max_tokens=1800,
23temperature=0,
24stop=["```"],
25messages=[
26{
27"role": "system",
28"content": f"""
29You are an expert system designed to extract products from images for a ecommerse application
30Please provide the product name and a descriptive query to search for the product.
31Accuratly identify every product in an image and provide a descriptive query to search for the product
32
33You just return a correctly formatted JSON object with the product name and query for each product in the image
34and follows the schema below:
35
36{MultiSearchQuery.model_json_schema()}
37""",
38},
39{
40"role": "user",
41"content": [
42{
43"type": "text",
44"text": "Extract the products from the image, and describe them in a query in JSON format",
45},
46{
47"type": "image_url",
48"image_url": {"url": url},
49},
50],
51},
52{
53"role": "assistant",
54"content": "Here is the following search queries for the products in the image\n ```json",
55},
56],
57)
58return MultiSearchQuery.model_validate_json(completion.choices[0].message.content)
59
60
61if __name__ == "__main__":
62url = "https://mensfashionpostingcom.files.wordpress.com/2020/03/fbe79-img_5052.jpg?w=768"
63products = extract_table(url)
64print(products.model_dump_json(indent=2))
65"""
66{
67"products": [
68{
69"product_name": "Olive Green Shirt",
70"query": "Olive green casual long sleeve button-down shirt"
71},
72{
73"product_name": "Black Jeans",
74"query": "Slim fit black jeans for men"
75},
76{
77"product_name": "Sunglasses",
78"query": "Classic brown aviator sunglasses"
79},
80{
81"product_name": "Leather Strap Watch",
82"query": "Minimalist men's watch with black leather strap"
83},
84{
85"product_name": "Beige Sneakers",
86"query": "Men's beige lace-up fashion sneakers with white soles"
87}
88]}
89"""
90