instructor
89 строк · 3.6 Кб
1from openai import OpenAI2from pydantic import BaseModel, Field3from rich.console import Console4
5import instructor6
7console = Console()8client = instructor.from_openai(9client=OpenAI(),10mode=instructor.Mode.TOOLS,11)
12
13
14class People(BaseModel):15id: str16name: str17role: str18reports: list[str] = Field(19default_factory=list, description="People who report to this person"20)21manages: list[str] = Field(22default_factory=list, description="People who this person manages"23)24
25
26class Organization(BaseModel):27people: list[People]28
29
30def extract(url: str):31return client.chat.completions.create_partial(32model="gpt-4-turbo",33max_tokens=4000,34response_model=Organization,35messages=[36{37"role": "user",38"content": [39{40"type": "image_url",41"image_url": {"url": url},42},43{44"type": "text",45"text": """46Analyze the organizational chart image and extract the relevant information to reconstruct the hierarchy.
47
48Create 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
55Ensure that the relationships between people are accurately captured in the reports and manages attributes.
56
57Return the list of People objects as the people attribute of an Organization object.
58""",59},60],61}62],63)64
65
66console.print(67extract(68"https://www.mindmanager.com/static/mm/images/features/org-chart/hierarchical-chart.png"69)70)
71"""
72Organization(
73people=[
74People(id='A1', name='Adele Morana', role='Founder, Chairman & CEO', reports=[], manages=['B1', 'C1', 'D1']),
75People(id='B1', name='Winston Cole', role='COO', reports=['A1'], manages=['E1']),
76People(id='C1', name='Marcus Kim', role='CFO', reports=['A1'], manages=['F1']),
77People(id='D1', name='Karin Ludovicicus', role='CPO', reports=['A1'], manages=['G1']),
78People(id='E1', name='Lea Erastos', role='Chief Business Officer', reports=['B1'], manages=['H1', 'I1']),
79People(id='F1', name='John McKinley', role='Chief Accounting Officer', reports=['C1'], manages=[]),
80People(id='G1', name='Ayda Williams', role='VP, Global Customer & Business Marketing', reports=['D1'], manages=['J1', 'K1']),
81People(id='H1', name='Zahida Mahtab', role='VP, Global Affairs & Communication', reports=['E1'], manages=[]),
82People(id='I1', name='Adelaide Zhu', role='VP, Central Services', reports=['E1'], manages=[]),
83People(id='J1', name='Gabriel Drummond', role='VP, Investor Relations', reports=['G1'], manages=[]),
84People(id='K1', name='Nicholas Brambilla', role='VP, Company Brand', reports=['G1'], manages=[]),
85People(id='L1', name='Felice Vasili', role='VP Finance', reports=['C1'], manages=[]),
86People(id='M1', name='Sandra Herminius', role='VP, Product Marketing', reports=['D1'], manages=[])
87]
88)
89"""
90