ফাংশন কলিং ব্যবহার করে স্ট্রাকচার্ড ডেটা বের করুন

গুগল এআই দেখুন 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 এর ফাংশন কলিং বৈশিষ্ট্য ব্যবহার করে স্কিমা সংজ্ঞায়িত করে এটিকে আরও কঠোর/শক্তিশালী করতে পারেন।

ফাংশন কলিং ব্যবহার করুন

আপনি যদি এখনও ফাংশন কলিং বেসিক টিউটোরিয়ালটি না দেখে থাকেন তবে নিশ্চিত করুন যে আপনি প্রথমে এটি করেছেন।

ফাংশন কল করার সাথে আপনার ফাংশন এবং এর পরামিতিগুলিকে API-এ একটি glm.FunctionDeclaration হিসাবে বর্ণনা করা হয়েছে। মৌলিক ক্ষেত্রে SDK ফাংশন এবং এর টীকা থেকে FunctionDeclaration তৈরি করতে পারে। SDK বর্তমানে নেস্টেড OBJECT ( dict ) প্যারামিটারের বিবরণ পরিচালনা করে না। তাই আপনাকে এখনকার জন্য তাদের স্পষ্টভাবে সংজ্ঞায়িত করতে হবে।

স্কিমা সংজ্ঞায়িত করুন

স্ট্রিং ফিল্ডের name , description , start_place_name , end_place_name সহ একটি বস্তু হিসাবে person সংজ্ঞায়িত করে শুরু করুন।

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']
)

তারপর person বস্তুর একটি ARRAY হিসাবে লোকেদের সংজ্ঞায়িত করুন:

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 কল করুন

যেমন আপনি ফাংশন কলিং বেসিকগুলিতে দেখেছেন এখন আপনি এই ফাংশন FunctionDeclaration genai.GenerativeModel কনস্ট্রাক্টরের tools আর্গুমেন্টে পাস করতে পারেন (নির্মাতা ফাংশন ঘোষণার সমতুল্য 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 বিশুদ্ধ পাঠ্য ইনপুট এবং পাঠ্য আউটপুট সহ কাঠামোগত ডেটা নিষ্কাশন সমস্যাগুলি পরিচালনা করতে পারে, ফাংশন কলিং ব্যবহার করা সম্ভবত আরও নির্ভরযোগ্য কারণ এটি আপনাকে একটি কঠোর স্কিমা সংজ্ঞায়িত করতে দেয় এবং একটি সম্ভাব্য ত্রুটি-প্রবণ পার্সিং পদক্ষেপকে সরিয়ে দেয়।