استخراج داده های ساخت یافته با استفاده از فراخوانی تابع

مشاهده در گوگل AI در Google Colab اجرا شود مشاهده منبع در GitHub

در این آموزش شما از طریق یک مثال استخراج داده های ساختاریافته، با استفاده از Gemini API برای استخراج لیستی از شخصیت ها، روابط، چیزها و مکان ها از یک داستان کار خواهید کرد.

برپایی

pip install -U -q google-generativeai
import pathlib
import textwrap

import google.generativeai as genai
import google.ai.generativelanguage as glm


from IPython.display import display
from IPython.display import Markdown

from google.api_core import retry

def to_markdown(text):
  text = text.replace('•', '  *')
  return Markdown(textwrap.indent(text, '> ', predicate=lambda _: True))

هنگامی که کلید API را دارید، آن را به SDK منتقل کنید. شما می توانید این کار را به دو صورت انجام دهید:

  • کلید را در متغیر محیطی GOOGLE_API_KEY قرار دهید (SDK به طور خودکار آن را از آنجا دریافت می کند).
  • کلید را به genai.configure(api_key=...) بدهید
try:
    # Used to securely store your API key
    from google.colab import userdata

    # Or use `os.getenv('API_KEY')` to fetch an environment variable.
    GOOGLE_API_KEY=userdata.get('GOOGLE_API_KEY')
except ImportError:
    import os
    GOOGLE_API_KEY = os.environ['GOOGLE_API_KEY']

genai.configure(api_key=GOOGLE_API_KEY)

کار مثال

برای این آموزش شما موجودیت ها را از داستان های زبان طبیعی استخراج خواهید کرد. به عنوان نمونه، در زیر داستانی نوشته شده توسط جمینی آمده است.

new_story = False

if new_story:
  model = genai.GenerativeModel(model_name='models/gemini-1.5-pro-latest')

  response = model.generate_content("""
      Write a long story about a girl with magic backpack, her family, and at
      least one other charater. Make sure everyone has names. Don't forget to
      describe the contents of the backpack, and where everyone and everything
      starts and ends up.""", request_options={'retry': retry.Retry()})
  story = response.text
  print(response.candidates[0].citation_metadata)
else:
  story = """In the quaint town of Willow Creek, nestled amidst rolling hills and whispering willows, resided a young girl named Anya. As she stepped out of the creaky wooden door of her modest cottage, her heart skipped a beat with excitement and anticipation. Today was her first day of school, and she couldn't wait to show off her prized possession - a magical backpack.\n\nHanded down to her from her grandmother, the backpack was no ordinary satchel. Its soft, emerald-green fabric shimmered with an ethereal glow, and its leather straps held secrets that only Anya knew. Within its capacious interior lay an enchanted world, filled with wonders that would ignite her imagination and change her life forever.\n\nAnya's parents, kind-hearted Elise and wise-bearded Edward, bid her farewell with warm embraces. "Remember, my dear," whispered her mother, "use your magic wisely and for good." Her father added, "Always seek knowledge, and let the backpack be your trusted companion."\n\nWith a skip in her step, Anya set off towards the town's only schoolhouse. On her way, she passed her best friend, Samuel, a curious and adventurous boy with a mischievous grin. "Hey, Anya," he called out. "Can I see your backpack?"\n\nAnya hesitated for a moment before unzipping the flap and revealing its contents. Samuel's eyes widened in amazement as he peered inside. There, nestled amidst pencils and notebooks, were a shimmering sword, a book of ancient spells, a tiny compass that always pointed north, and a magical key that could open any lock.\n\nTogether, they marveled at the backpack's wonders, promising to keep its secrets safe. As they approached the schoolhouse, Anya noticed a group of older children huddled together, their faces etched with fear. Curiosity getting the better of her, she cautiously approached.\n\n"What's wrong?" she asked.\n\nA tall, lanky boy stepped forward. "There's a monster in the forest," he stammered. "It's been terrorizing the town, attacking animals and even people."\n\nAnya's heart sank. The town of Willow Creek was small and peaceful, and the thought of a monster brought a shiver down her spine. She knew she had to do something to protect her family and friends.\n\nWithout a moment's hesitation, Anya opened her backpack and retrieved the shimmering sword. With a determined gleam in her eye, she turned to her terrified peers. "Don't worry," she said, her voice steady. "I'll take care of it."\n\nWith Samuel close behind her, Anya ventured into the shadowy depths of the forest. The trees seemed to whisper secrets as she passed, and the undergrowth rustled with unseen creatures. As they walked deeper into the forest, the air grew heavy and the ground beneath their feet trembled.\n\nSuddenly, they came to a clearing, and there before their eyes was the monster - a massive beast with sharp teeth, glowing red eyes, and claws that could crush a human with ease. The creature roared, a thunderous sound that shook the forest to its core.\n\nFear surged through Anya, but she refused to let it consume her. She drew the sword from its sheath and charged towards the monster. The blade shimmered in the sunlight, and as it struck the beast's hide, a blinding light erupted, enveloping everything in its radiance.\n\nWhen the light faded, the monster was gone, and in its place was a pile of shattered crystals. Anya had defeated the creature with the magic of her backpack, proving that even the smallest of objects could hold the greatest of powers.\n\nAs she and Samuel returned to the town, they were greeted as heroes. The people of Willow Creek rejoiced, and the legend of Anya, the girl with the magic backpack, was passed down through generations. And so, Anya continued her adventures, using the backpack's wonders to make the world a better place, one magical step at a time."""
to_markdown(story)

دختر جوانی به نام آنیا در شهر عجیب ویلو کریک، در میان تپه‌ها و بیدهای زمزمه‌کننده، زندگی می‌کرد. هنگامی که از در چوبی ترش خورده کلبه‌ی ساده‌اش بیرون آمد، قلبش با هیجان و انتظار به تپش افتاد. امروز اولین روز مدرسه او بود، و او نمی توانست صبر کند تا دارایی ارزشمند خود - یک کوله پشتی جادویی را به نمایش بگذارد.

کوله پشتی که از مادربزرگش به او تحویل داده شده بود، یک کیف معمولی نبود. پارچه نرم و سبز زمردی آن با درخشش اثیری می درخشید و بند های چرمی آن رازهایی را در خود جای داده بود که فقط آنیا می دانست. در فضای داخلی بزرگ آن، دنیایی مسحور شده بود، پر از شگفتی هایی که تخیل او را شعله ور می کرد و زندگی او را برای همیشه تغییر می داد.

پدر و مادر آنیا، الیز مهربان و ادوارد ریش خردمند، با در آغوش گرفتن گرم با او خداحافظی کردند. مادرش زمزمه کرد: "یادت باشه عزیزم، از جادوی خود عاقلانه و برای خیر استفاده کن." پدرش افزود: همیشه به دنبال علم باشید و بگذارید کوله پشتی همراه مورد اعتماد شما باشد.

آنیا با یک پرش به سمت تنها مدرسه شهر حرکت کرد. در راه، او از کنار بهترین دوستش، ساموئل، پسری کنجکاو و ماجراجو با پوزخندی شیطنت آمیز گذشت. فریاد زد: هی، آنیا. "میتونم کوله پشتیتو ببینم؟"

آنیا قبل از اینکه زیپ فلپ را باز کند و محتویات آن را آشکار کند، لحظه ای تردید کرد. وقتی ساموئل به داخل نگاه کرد، چشمانش از تعجب گشاد شد. آنجا، در میان مدادها و دفترچه‌ها، شمشیری درخشان، کتابی از طلسم‌های باستانی، قطب‌نمای کوچکی که همیشه به سمت شمال بود، و کلیدی جادویی که می‌توانست هر قفلی را باز کند، قرار داشت.

آنها با هم از شگفتی های کوله پشتی شگفت زده شدند و قول دادند اسرار آن را ایمن نگه دارند. هنگامی که آنها به مدرسه نزدیک شدند، آنیا متوجه گروهی از بچه های بزرگتر شد که در کنار هم جمع شده بودند و چهره هایشان از ترس حک شده بود. کنجکاوی او را تحت تأثیر قرار داد، با احتیاط نزدیک شد.

"مشکل چیه؟" او پرسید.

پسری قد بلند و لاغر پا جلو رفت. او با لکنت گفت: "یه هیولا در جنگل وجود دارد." این شهر را به وحشت انداخته، به حیوانات و حتی مردم حمله کرده است.»

قلب آنیا فرو ریخت. شهر ویلو کریک کوچک و آرام بود، و فکر یک هیولا لرزه بر ستون فقرات او انداخت. او می دانست که باید برای محافظت از خانواده و دوستانش کاری انجام دهد.

بدون لحظه ای تردید، آنیا کوله پشتی خود را باز کرد و شمشیر درخشان را بیرون آورد. با درخششی مصمم در چشمانش، به طرف همسالان وحشت زده خود برگشت. با صدای ثابت گفت: نگران نباش. "من از آن مراقبت خواهم کرد."

در حالی که ساموئل در پشت سر او بود، آنیا به اعماق سایه جنگل رفت. به نظر می رسید که درختان با عبور او رازهایی را زمزمه می کردند و زیر درختان با موجودات نادیده خش خش می کردند. وقتی به عمق جنگل می رفتند، هوا سنگین می شد و زمین زیر پایشان می لرزید.

ناگهان، آنها به یک فضای خالی رسیدند، و آنجا در مقابل چشمان آنها هیولا بود - یک جانور عظیم با دندان های تیز، چشمان قرمز درخشان، و چنگال هایی که می توانست به راحتی یک انسان را خرد کند. این موجود غرش کرد، صدای رعد و برقی که جنگل را تا تهش تکان داد.

ترس در آنیا موج زد، اما او حاضر نشد او را درگیر کند. او شمشیر را از غلاف بیرون کشید و به سمت هیولا حرکت کرد. تیغه زیر نور خورشید می درخشید و وقتی به پوست جانور برخورد کرد، نوری کورکننده فوران کرد و همه چیز را در درخشش خود فرا گرفت.

وقتی نور محو شد، هیولا ناپدید شد و در جای خود انبوهی از کریستال های خرد شده بود. آنیا با جادوی کوله پشتی خود این موجود را شکست داده بود و ثابت می کرد که حتی کوچکترین اشیاء می توانند بزرگترین قدرت ها را در خود داشته باشند.

هنگامی که او و ساموئل به شهر بازگشتند، از آنها به عنوان قهرمان استقبال شد. مردم ویلو کریک شادی کردند و افسانه آنیا، دختری که کوله پشتی جادویی دارد، در نسل ها منتقل شد. و به این ترتیب، آنیا به ماجراجویی های خود ادامه داد و از شگفتی های کوله پشتی برای تبدیل جهان به مکانی بهتر، یک قدم جادویی استفاده کرد.

استفاده از زبان طبیعی

مدل های زبان بزرگ یک ابزار قدرتمند چند وظیفه ای هستند. غالباً می توانید آنچه را که می خواهید از جوزا بخواهید و آن را درست می کند.

هنگام تولید ساختارهای داده به این روش باید به چند نکته توجه کرد:

  • گاهی اوقات تجزیه با شکست مواجه می شود.
  • این طرح را نمی توان به شدت اجرا کرد.

در بخش بعدی این مشکلات را حل خواهید کرد. ابتدا یک دستور زبان طبیعی ساده را با طرحواره نوشته شده به صورت متن امتحان کنید. این بهینه سازی نشده است:

model = model = model = genai.GenerativeModel(
    model_name='models/gemini-1.5-pro-latest')

response = model.generate_content(
  textwrap.dedent("""\
    Please return JSON describing the the people, places, things and relationships from this story using the following schema:

    {"people": list[PERSON], "places":list[PLACE], "things":list[THING], "relationships": list[RELATIONSHIP]}

    PERSON = {"name": str, "description": str, "start_place_name": str, "end_place_name": str}
    PLACE = {"name": str, "description": str}
    THING = {"name": str, "description": str, "start_place_name": str, "end_place_name": str}
    RELATIONSHIP = {"person_1_name": str, "person_2_name": str, "relationship": str}

    All fields are required.

    Important: Only return a single piece of valid JSON text.

    Here is the story:

    """) + story,
  generation_config={'response_mime_type':'application/json'}
)
response.text
'{"people": [\n    {\n        "name": "Anya",\n        "description": "A young girl who lives in the town of Willow Creek with her parents, Elise and Edward. She possesses a magical backpack that was handed down to her from her grandmother.",\n        "start_place_name": "Willow Creek",\n        "end_place_name": "Willow Creek"\n    },\n    {\n        "name": "Elise",\n        "description": "Anya\'s kind-hearted mother",\n        "start_place_name": "Willow Creek",\n        "end_place_name": "Willow Creek"\n    },\n    {\n        "name": "Edward",\n        "description": "Anya\'s wise-bearded father",\n        "start_place_name": "Willow Creek",\n        "end_place_name": "Willow Creek"\n    },\n    {\n        "name": "Samuel",\n        "description": "Anya\'s best friend, a curious and adventurous boy with a mischievous grin.",\n        "start_place_name": "Willow Creek",\n        "end_place_name": "Willow Creek"\n    },\n    {\n        "name": "Monster",\n        "description": "A massive beast with sharp teeth, glowing red eyes, and claws that could crush a human with ease.",\n        "start_place_name": "Forest",\n        "end_place_name": "Forest"\n    }\n], "places": [\n    {\n        "name": "Willow Creek",\n        "description": "A quaint town nestled amidst rolling hills and whispering willows."\n    },\n    {\n        "name": "Forest",\n        "description": "A shadowy place with rustling undergrowth and whispering trees."\n    },\n    {\n        "name": "Schoolhouse",\n        "description": "The only school in the town of Willow Creek."\n    },\n    {\n        "name": "Anya\'s home",\n        "description": "A modest cottage with a creaky wooden door."\n    }\n], "things": [\n    {\n        "name": "Magic backpack",\n        "description": "A magical backpack that was handed down to Anya from her grandmother. Its soft, emerald-green fabric shimmered with an ethereal glow, and its leather straps held secrets that only Anya knew.",\n        "start_place_name": "Anya\'s home",\n        "end_place_name": "Forest"\n    },\n    {\n        "name": "Shimmering sword",\n        "description": "A sword that shimmered in the sunlight and could strike with blinding light.",\n        "start_place_name": "Magic backpack",\n        "end_place_name": "Forest"\n    },\n    {\n        "name": "Book of ancient spells",\n        "description": "A book that contained ancient spells.",\n        "start_place_name": "Magic backpack",\n        "end_place_name": "Forest"\n    },\n    {\n        "name": "Tiny compass",\n        "description": "A compass that always pointed north.",\n        "start_place_name": "Magic backpack",\n        "end_place_name": "Forest"\n    },\n    {\n        "name": "Magical key",\n        "description": "A key that could open any lock.",\n        "start_place_name": "Magic backpack",\n        "end_place_name": "Forest"\n    },\n    {\n        "name": "Shattered crystals",\n        "description": "The remains of the monster after it was defeated by Anya\'s magic backpack.",\n        "start_place_name": "Forest",\n        "end_place_name": "Forest"\n    }\n], "relationships": [\n    {\n        "person_1_name": "Anya",\n        "person_2_name": "Elise",\n        "relationship": "mother-daughter"\n    },\n    {\n        "person_1_name": "Anya",\n        "person_2_name": "Edward",\n        "relationship": "father-daughter"\n    },\n    {\n        "person_1_name": "Anya",\n        "person_2_name": "Samuel",\n        "relationship": "best friends"\n    }\n]}'

که یک رشته json را برگرداند. سعی کنید آن را تجزیه کنید:

import json

print(json.dumps(json.loads(response.text), indent=4))
{
    "people": [
        {
            "name": "Anya",
            "description": "A young girl who lives in the town of Willow Creek with her parents, Elise and Edward. She possesses a magical backpack that was handed down to her from her grandmother.",
            "start_place_name": "Willow Creek",
            "end_place_name": "Willow Creek"
        },
        {
            "name": "Elise",
            "description": "Anya's kind-hearted mother",
            "start_place_name": "Willow Creek",
            "end_place_name": "Willow Creek"
        },
        {
            "name": "Edward",
            "description": "Anya's wise-bearded father",
            "start_place_name": "Willow Creek",
            "end_place_name": "Willow Creek"
        },
        {
            "name": "Samuel",
            "description": "Anya's best friend, a curious and adventurous boy with a mischievous grin.",
            "start_place_name": "Willow Creek",
            "end_place_name": "Willow Creek"
        },
        {
            "name": "Monster",
            "description": "A massive beast with sharp teeth, glowing red eyes, and claws that could crush a human with ease.",
            "start_place_name": "Forest",
            "end_place_name": "Forest"
        }
    ],
    "places": [
        {
            "name": "Willow Creek",
            "description": "A quaint town nestled amidst rolling hills and whispering willows."
        },
        {
            "name": "Forest",
            "description": "A shadowy place with rustling undergrowth and whispering trees."
        },
        {
            "name": "Schoolhouse",
            "description": "The only school in the town of Willow Creek."
        },
        {
            "name": "Anya's home",
            "description": "A modest cottage with a creaky wooden door."
        }
    ],
    "things": [
        {
            "name": "Magic backpack",
            "description": "A magical backpack that was handed down to Anya from her grandmother. Its soft, emerald-green fabric shimmered with an ethereal glow, and its leather straps held secrets that only Anya knew.",
            "start_place_name": "Anya's home",
            "end_place_name": "Forest"
        },
        {
            "name": "Shimmering sword",
            "description": "A sword that shimmered in the sunlight and could strike with blinding light.",
            "start_place_name": "Magic backpack",
            "end_place_name": "Forest"
        },
        {
            "name": "Book of ancient spells",
            "description": "A book that contained ancient spells.",
            "start_place_name": "Magic backpack",
            "end_place_name": "Forest"
        },
        {
            "name": "Tiny compass",
            "description": "A compass that always pointed north.",
            "start_place_name": "Magic backpack",
            "end_place_name": "Forest"
        },
        {
            "name": "Magical key",
            "description": "A key that could open any lock.",
            "start_place_name": "Magic backpack",
            "end_place_name": "Forest"
        },
        {
            "name": "Shattered crystals",
            "description": "The remains of the monster after it was defeated by Anya's magic backpack.",
            "start_place_name": "Forest",
            "end_place_name": "Forest"
        }
    ],
    "relationships": [
        {
            "person_1_name": "Anya",
            "person_2_name": "Elise",
            "relationship": "mother-daughter"
        },
        {
            "person_1_name": "Anya",
            "person_2_name": "Edward",
            "relationship": "father-daughter"
        },
        {
            "person_1_name": "Anya",
            "person_2_name": "Samuel",
            "relationship": "best friends"
        }
    ]
}

این نسبتاً ساده است و اغلب کار می‌کند، اما به طور بالقوه می‌توانید با تعریف طرحواره با استفاده از ویژگی فراخوانی تابع API، آن را سخت‌تر/قوی‌تر کنید.

از فراخوانی تابع استفاده کنید

اگر هنوز آموزش اصول فراخوانی تابع را گذرانده اید، ابتدا مطمئن شوید که این کار را انجام داده اید.

با فراخوانی تابع، تابع شما و پارامترهای آن به عنوان glm.FunctionDeclaration در API توصیف می‌شوند. در موارد اساسی، SDK می تواند FunctionDeclaration از تابع و حاشیه نویسی های آن بسازد. SDK در حال حاضر شرح پارامترهای OBJECT تو در تو ( dict ) را مدیریت نمی کند. بنابراین در حال حاضر باید آنها را به صراحت تعریف کنید.

طرحواره را تعریف کنید

با تعریف person به عنوان یک شی با فیلدهای رشته ای name ، description ، start_place_name ، end_place_name شروع کنید.

person = glm.Schema(
    type = glm.Type.OBJECT,
    properties = {
        'name':  glm.Schema(type=glm.Type.STRING),
        'description':  glm.Schema(type=glm.Type.STRING),
        'start_place_name': glm.Schema(type=glm.Type.STRING),
        'end_place_name': glm.Schema(type=glm.Type.STRING)
    },
    required=['name', 'description', 'start_place_name', 'end_place_name']
)

سپس افراد را به عنوان ARRAY از اشیاء person تعریف کنید:

people = glm.Schema(
    type=glm.Type.ARRAY,
    items=person
)

سپس همین کار را برای هر یک از موجودیت هایی که می خواهید استخراج کنید انجام دهید:

place = glm.Schema(
    type = glm.Type.OBJECT,
    properties = {
        'name':  glm.Schema(type=glm.Type.STRING),
        'description':  glm.Schema(type=glm.Type.STRING),
    }
)

places = glm.Schema(
    type=glm.Type.ARRAY,
    items=place
)
thing = glm.Schema(
  type = glm.Type.OBJECT,
  properties = {
      'name':  glm.Schema(type=glm.Type.STRING),
      'description':  glm.Schema(type=glm.Type.STRING),
  }
)

things = glm.Schema(
    type=glm.Type.ARRAY,
    items=thing
)
relationship = glm.Schema(
    type = glm.Type.OBJECT,
    properties = {
        'person_1_name':  glm.Schema(type=glm.Type.STRING),
        'person_2_name':  glm.Schema(type=glm.Type.STRING),
        'relationship':  glm.Schema(type=glm.Type.STRING),
    }
)

relationships = glm.Schema(
    type=glm.Type.ARRAY,
    items=relationship
)

اکنون FunctionDeclaration بسازید:

add_to_database = glm.FunctionDeclaration(
    name="add_to_database",
    description=textwrap.dedent("""\
        Adds entities to the database.
        """),
    parameters=glm.Schema(
        type=glm.Type.OBJECT,
        properties = {
            'people': people,
            'places': places,
            'things': things,
            'relationships': relationships
        }
    )
)

با API تماس بگیرید

همانطور که در Function calling basics دیدید، اکنون می توانید این FunctionDeclaration را به آرگومان tools سازنده genai.GenerativeModel منتقل کنید (سازنده نیز یک نمایش JSON معادل از اعلان تابع را می پذیرد):

model = model = genai.GenerativeModel(
    model_name='models/gemini-1.5-pro-latest',
    tools = [add_to_database])

هر بار که API را فراخوانی می‌کنید، SDK ابزارها را به همراه دستور شما ارسال می‌کند و مدل باید تابعی را که تعریف کرده‌اید فراخوانی کند:

result = model.generate_content(f"""
Please add the people, places, things, and relationships from this story to the database:

{story}
""",
# Force a function call
tool_config={'function_calling_config':'ANY'})

اکنون متنی برای تجزیه وجود ندارد. نتیجه یک ساختار داده است .

'text' in result.candidates[0].content.parts[0]
False
'function_call' in result.candidates[0].content.parts[0]
True
fc = result.candidates[0].content.parts[0].function_call
print(type(fc))
<class 'google.ai.generativelanguage_v1beta.types.content.FunctionCall'>

کلاس glm.FunctionCall بر اساس بافرهای پروتکل Google است، آن را به یک شی سازگار با JSON آشناتر تبدیل کنید:

print(json.dumps(type(fc).to_dict(fc), indent=4))
{
    "name": "add_to_database",
    "args": {
        "things": [
            {
                "name": "Magical Backpack",
                "description": "Anya's prized possession, the Magical Backpack, is no ordinary satchel. Its soft, emerald-green fabric shimmers with an ethereal glow, and its leather straps have secrets that only Anya knows. Within its capacious interior lay an enchanted world, filled with wonders that would ignite her imagination and change her life forever."
            },
            {
                "name": "Shimmering Sword",
                "description": "Among the wonders in Anya's Magical Backpack, lies a shimmering sword. With a determined gleam in her eye, she retrieved the shimmering sword and charged towards the monster."
            },
            {
                "description": "Residing within the Magical Backpack, the Book of Ancient Spells holds secrets untold.",
                "name": "Book of Ancient Spells"
            },
            {
                "description": "Tucked away in the Magical Backpack is a tiny compass that always points north.",
                "name": "Tiny Compass that Always Points North"
            },
            {
                "description": "Hidden within the Magical Backpack is a magical key that can open any lock.",
                "name": "Magical Key that Can Open Any Lock"
            }
        ],
        "relationships": [
            {
                "relationship": "Mother-Daughter",
                "person_1_name": "Anya",
                "person_2_name": "Elise"
            },
            {
                "person_2_name": "Edward",
                "relationship": "Father-Daughter",
                "person_1_name": "Anya"
            },
            {
                "person_2_name": "Samuel",
                "person_1_name": "Anya",
                "relationship": "Best Friends"
            }
        ],
        "people": [
            {
                "name": "Anya",
                "description": "Anya, the main character of the story, is a young girl with a magical backpack.",
                "start_place_name": "Willow Creek",
                "end_place_name": "Unknown"
            },
            {
                "name": "Elise",
                "description": "Anya's mother, Elise is a kind-hearted woman.",
                "end_place_name": "Unknown",
                "start_place_name": "Willow Creek"
            },
            {
                "start_place_name": "Willow Creek",
                "end_place_name": "Unknown",
                "name": "Edward",
                "description": "Anya's father, Edward is a wise-bearded man."
            },
            {
                "end_place_name": "Unknown",
                "start_place_name": "Willow Creek",
                "description": "Anya's best friend, Samuel is a curious and adventurous boy with a mischievous grin.",
                "name": "Samuel"
            }
        ],
        "places": [
            {
                "description": "The quaint town of Willow Creek is nestled amidst rolling hills and whispering willows.",
                "name": "Willow Creek"
            },
            {
                "description": "The town's only schoolhouse.",
                "name": "Schoolhouse"
            },
            {
                "description": "A shadowy place filled with secrets and dangers, the Forest is home to a terrifying monster.",
                "name": "Forest"
            }
        ]
    }
}

نتیجه

در حالی که API می‌تواند مشکلات استخراج داده‌های ساخت‌یافته را با ورودی متن خالص و خروجی متن کنترل کند، استفاده از فراخوانی تابع احتمالاً قابل اعتمادتر است زیرا به شما امکان می‌دهد یک طرح دقیق تعریف کنید و یک مرحله تجزیه احتمالی مستعد خطا را حذف کنید.