instructor

Форк
0
89 строк · 2.9 Кб
1
from openai import OpenAI
2
from pydantic import BaseModel, Field
3

4
client = OpenAI()
5

6

7
class SearchQuery(BaseModel):
8
    product_name: str
9
    query: str = Field(
10
        ...,
11
        description="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

15
class MultiSearchQuery(BaseModel):
16
    products: list[SearchQuery]
17

18

19
def extract_table(url: str):
20
    completion = client.chat.completions.create(
21
        model="gpt-4-vision-preview",
22
        max_tokens=1800,
23
        temperature=0,
24
        stop=["```"],
25
        messages=[
26
            {
27
                "role": "system",
28
                "content": f"""
29
                You are an expert system designed to extract products from images for a ecommerse application
30
                Please provide the product name and a descriptive query to search for the product.
31
                Accuratly identify every product in an image and provide a descriptive query to search for the product
32
                
33
                You just return a correctly formatted JSON object with the product name and query for each product in the image
34
                and 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
    )
58
    return MultiSearchQuery.model_validate_json(completion.choices[0].message.content)
59

60

61
if __name__ == "__main__":
62
    url = "https://mensfashionpostingcom.files.wordpress.com/2020/03/fbe79-img_5052.jpg?w=768"
63
    products = extract_table(url)
64
    print(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

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.