instructor

Форк
0
/
run_vision_org.py 
89 строк · 3.6 Кб
1
from openai import OpenAI
2
from pydantic import BaseModel, Field
3
from rich.console import Console
4

5
import instructor
6

7
console = Console()
8
client = instructor.from_openai(
9
    client=OpenAI(),
10
    mode=instructor.Mode.TOOLS,
11
)
12

13

14
class People(BaseModel):
15
    id: str
16
    name: str
17
    role: str
18
    reports: list[str] = Field(
19
        default_factory=list, description="People who report to this person"
20
    )
21
    manages: list[str] = Field(
22
        default_factory=list, description="People who this person manages"
23
    )
24

25

26
class Organization(BaseModel):
27
    people: list[People]
28

29

30
def extract(url: str):
31
    return client.chat.completions.create_partial(
32
        model="gpt-4-turbo",
33
        max_tokens=4000,
34
        response_model=Organization,
35
        messages=[
36
            {
37
                "role": "user",
38
                "content": [
39
                    {
40
                        "type": "image_url",
41
                        "image_url": {"url": url},
42
                    },
43
                    {
44
                        "type": "text",
45
                        "text": """
46
                            Analyze the organizational chart image and extract the relevant information to reconstruct the hierarchy.
47
                            
48
                            Create a list of People objects, where each person has the following attributes:
49
                            - id: A unique identifier for the person
50
                            - name: The person's name
51
                            - role: The person's role or position in the organization
52
                            - reports: A list of IDs of people who report directly to this person
53
                            - manages: A list of IDs of people who this person manages
54
                            
55
                            Ensure that the relationships between people are accurately captured in the reports and manages attributes.
56
                            
57
                            Return the list of People objects as the people attribute of an Organization object.
58
                        """,
59
                    },
60
                ],
61
            }
62
        ],
63
    )
64

65

66
console.print(
67
    extract(
68
        "https://www.mindmanager.com/static/mm/images/features/org-chart/hierarchical-chart.png"
69
    )
70
)
71
"""
72
Organization(
73
    people=[
74
        People(id='A1', name='Adele Morana', role='Founder, Chairman & CEO', reports=[], manages=['B1', 'C1', 'D1']),
75
        People(id='B1', name='Winston Cole', role='COO', reports=['A1'], manages=['E1']),
76
        People(id='C1', name='Marcus Kim', role='CFO', reports=['A1'], manages=['F1']),
77
        People(id='D1', name='Karin Ludovicicus', role='CPO', reports=['A1'], manages=['G1']),
78
        People(id='E1', name='Lea Erastos', role='Chief Business Officer', reports=['B1'], manages=['H1', 'I1']),
79
        People(id='F1', name='John McKinley', role='Chief Accounting Officer', reports=['C1'], manages=[]),
80
        People(id='G1', name='Ayda Williams', role='VP, Global Customer & Business Marketing', reports=['D1'], manages=['J1', 'K1']),
81
        People(id='H1', name='Zahida Mahtab', role='VP, Global Affairs & Communication', reports=['E1'], manages=[]),
82
        People(id='I1', name='Adelaide Zhu', role='VP, Central Services', reports=['E1'], manages=[]),
83
        People(id='J1', name='Gabriel Drummond', role='VP, Investor Relations', reports=['G1'], manages=[]),
84
        People(id='K1', name='Nicholas Brambilla', role='VP, Company Brand', reports=['G1'], manages=[]),
85
        People(id='L1', name='Felice Vasili', role='VP Finance', reports=['C1'], manages=[]),
86
        People(id='M1', name='Sandra Herminius', role='VP, Product Marketing', reports=['D1'], manages=[])
87
    ]
88
)
89
"""
90

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

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

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

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