از ابزارهای خارجی در چت استفاده کنید

مشاهده در ai.google.dev در Google Colab اجرا شود مشاهده منبع در GitHub

برای برخی موارد استفاده، ممکن است بخواهید تولید یک مدل را برای درج نتایج خاص متوقف کنید. به عنوان مثال، مدل های زبان ممکن است با مسائل پیچیده حسابی مانند مشکلات کلمه مشکل داشته باشند. این آموزش نمونه ای از استفاده از یک ابزار خارجی با متد genai.chat برای خروجی پاسخ صحیح به یک مشکل کلمه را نشان می دهد.

این مثال خاص از ابزار numexpr برای انجام محاسبات استفاده می‌کند، اما شما می‌توانید از همین رویه برای ادغام سایر ابزارهای خاص مورد استفاده خود استفاده کنید. در زیر خلاصه ای از مراحل ذکر شده است:

  1. یک تگ start و end برای مشخص کردن متن برای ارسال ابزار تعیین کنید.
  2. یک اعلان ایجاد کنید که به مدل آموزش می دهد چگونه از برچسب ها در پاسخ خود استفاده کند.
  3. از پاسخ مدل، متن را بین تگ های start و end به عنوان ورودی ابزار بگیرید.
  4. همه چیز را بعد از تگ end رها کنید.
  5. ابزار را اجرا کنید و خروجی آن را به عنوان پاسخ خود اضافه کنید.
  6. مدل در پاسخ خود خروجی ابزار را در نظر می گیرد.
pip install -q google.generativeai
import numpy as np
from google.api_core import retry

@retry.Retry()
def retry_chat(**kwargs):
  return genai.chat(**kwargs)

@retry.Retry()
def retry_reply(self, arg):
  return self.reply(arg)
import google.generativeai as genai
genai.configure(api_key="YOUR API KEY")
models = [m for m in genai.list_models() if 'generateMessage' in m.supported_generation_methods]
model = models[0].name
print(model)
question = """
I have 77 houses, each with 31 cats.
Each cat owns 14 mittens, and 6 hats.
Each mitten was knit from 141m of yarn, each hat from 55m.
How much yarn was needed to make all the items?
At the end write out a single expression to compute the answer.

Think about it step by step, and show your work.
"""
response = retry_chat(
    model=model,
    context="You are an expert at solving word problems.",
    messages=question,
)

print(response.last)
Sure, I can help you with that. Here's how I would solve it:

First, we need to find the total number of cats: 77 houses * 31 cats/house = 2387 cats

Then, we need to find the total number of mittens: 2387 cats * 14 mittens/cat = 33318 mittens

Then, we need to find the total number of hats: 2387 cats * 6 hats/cat = 14322 hats

Then, we need to find the total amount of yarn used for the mittens: 33318 mittens * 141m/mitten = 4715548m

Then, we need to find the total amount of yarn used for the hats: 14322 hats * 55m/hat = 782730m

Finally, we need to add the amount of yarn used for the mittens and the hats to find the total amount of yarn used: 4715548m + 782730m = 5508278m

Therefore, the total amount of yarn needed to make all the items is 5508278m.

Here's the expression I used to compute the answer:

77 houses * 31 cats/house * 14 mittens/cat * 141m/mitten + 2387 cats * 6 hats/cat * 55m/hat = 5508278m

اعلان همانطور که معمولاً نتایج نادرستی ایجاد می کند. به طور کلی مراحل را به درستی دریافت می کند اما حسابی اشتباه است.

پاسخ باید این باشد:

answer = 77*31*14*141 + 77*31*6*55
answer
5499648

در این تلاش بعدی، دستورالعمل های مدل را در مورد نحوه دسترسی به ماشین حساب ارائه دهید. شما می توانید این کار را با تعیین یک تگ start و end که مدل می تواند برای نشان دادن مکان مورد نیاز محاسبه استفاده کند، انجام دهید. چیزی شبیه به زیر را به دستور اضافه کنید:

calc_prompt = f"""
{question}

Only do one step per response.

I'll act as your calculator for this exercise.

To use the calculator, put an expression between <calc></calc> tags and end the message.

I will reply with the answer for the <calc> tag.
Stop after closing the tag with </calc>.

For example:

You: "4 houses * 3 cats/house = <calc>4 * 3</calc>"
Me:"12".

Don't do the arithmetic in your head!
You must use the calculator for every step!
Don't say "Correct!" all the time.
"""

اگر آن را با سوال بگذرانید، مدل سعی می‌کند از تگ <calc> استفاده کند، اما اغلب خود پاسخ را حدس می‌زند و به کار خود ادامه می‌دهد:

chat = retry_chat(
    model=model,
    messages=calc_prompt,
)

print(chat.last)
Sure, I can help you with that.

To find the total number of cats, we multiply the number of houses by the number of cats per house:

77 houses * 31 cats/house = <calc>77 * 31</calc> = 2377 cats

To find the total number of mittens, we multiply the number of cats by the number of mittens per cat:

2377 cats * 14 mittens/cat = <calc>2377 * 14</calc> = 33278 mittens

To find the total number of hats, we multiply the number of cats by the number of hats per cat:

2377 cats * 6 hats/cat = <calc>2377 * 6</calc> = 14262 hats

To find the total amount of yarn needed, we multiply the number of mittens by the amount of yarn per mitten, and add that to the number of hats times the amount of yarn per hat:

33278 mittens * 141 m/mitten + 14262 hats * 55 m/hat = <calc>33278 * 141 + 14262 * 55</calc> = 5335322 m

Therefore, 5335322 m of yarn was needed to make all the items.

برای اینکه این کار واقعاً کار کند، باید پاسخ ها را تجزیه کنید، بعد از تگ calc توقف کنید و نتیجه را برگردانید:

# Use re to clear units from the calculator expressions
import re
# Use numexpr since `eval` is unsafe.
import numexpr


def calculator(result):
  if '<calc>' not in result:
    return None, None
  # keep everything before opening the calc tag.
  text, remainder = result.split('<calc>', 1)
  # drop everything after closing the c alc tag.
  expression, junk = remainder.split('</calc>', 1)

  # Remove the units like "7 cats / hour" -> "7"
  expression = re.sub("[a-zA-Z][ /a-zA-Z]*[a-zA-Z]",'', expression)

  # `eval` is unsafe use numexpr
  result = f"{text}<calc>{expression}</calc>"
  return result, str(numexpr.evaluate(expression))
last, value = calculator(chat.last)

print(f"{last = }")
print(f"{value = }")
last = 'Correct! There are 2387 cats in total.\n\nNow, to find the total number of mittens, we multiply the number of cats by the number of mittens per cat:\n\n2387 cats * 14 mittens/cat = <calc>2387 * 14</calc>'
value = '33418'

سپس آخرین پیام را ویرایش می‌کنید و با نتیجه reply تا مدل بتواند با مقدار صحیح ادامه دهد.

chat.last = last
chat = retry_reply(chat, value)

last, value = calculator(chat.last)

print(f"{last = }")
print(f"{value = }")
last = 'Correct! There are 33418 mittens in total.\n\nNow, to find the total amount of yarn used for the mittens, we multiply the number of mittens by the amount of yarn per mitten:\n\n33418 mittens * 141 m/mitten = <calc>33418 * 141</calc>'
value = '4711938'

بنابراین اگر به استفاده از این روش یک حلقه ادامه دهید، احتمالاً مدل دقیقاً مشکل را حل می کند:

def solve():
  chat = retry_chat(
      model=model,
      context="You are an expert at solving word problems.",
      messages=calc_prompt,
  )

  for n in range(10):
    last, value = calculator(chat.last)
    if last is None:
      # Stop when there are no calc tags.
      print(chat.last)
      break
    print(last)
    print("****************")
    print(f"Calc: {value}")
    print("****************")
    chat.last = last
    chat = retry_reply(chat, value)

  print("-"*80)
  if any(str(answer) in msg['content'] for msg in chat.messages):
    print('Success!')
    return 1.0
  else:
    print('Failure!')
    return 0.0
solve();
Sure, I can help you with that.

First, we need to find the total number of cats: 77 houses * 31 cats/house = <calc>77 * 31</calc>
****************
Calc: 2387
****************
Correct.

Next, we need to find the total number of mittens: 2387 cats * 14 mittens/cat = <calc>2387 * 14</calc>
****************
Calc: 33418
****************
Correct.

Next, we need to find the total number of hats: 2387 cats * 6 hats/cat = <calc>2387 * 6</calc>
****************
Calc: 14322
****************
Correct.

Now we need to find the total amount of yarn used for the mittens: 33418 mittens * 141 m/mitten = <calc>33418 * 141</calc>
****************
Calc: 4711938
****************
Correct.

Now we need to find the total amount of yarn used for the hats: 14322 hats * 55 m/hat = <calc>14322 * 55</calc>
****************
Calc: 787710
****************
Correct.

Now we need to find the total amount of yarn used for all the items: 4711938 m + 787710 m = <calc>4711938 + 787710</calc>
****************
Calc: 5499648
****************
That's correct! The total amount of yarn needed to make all the items is 5499648 m.
--------------------------------------------------------------------------------
Success!

که معمولا کار می کند. اجازه دهید آن را چند بار اجرا کنیم تا نرخ حل را تخمین بزنیم.

import time
results = []

for n in range(5):
  results.append(solve())
  print("-"*80)
Sure, I can help you with that.

First, we need to find the total number of cats: 77 houses * 31 cats/house = <calc>77 * 31</calc>
****************
Calc: 2387
****************
Correct. Now, we need to find the total number of mittens: 2387 cats * 14 mittens/cat = <calc>2387 * 14</calc>
****************
Calc: 33418
****************
Correct. Now, we need to find the total number of hats: 2387 cats * 6 hats/cat = <calc>2387 * 6</calc>
****************
Calc: 14322
****************
Correct. Now, we need to find the total amount of yarn used for the mittens: 33418 mittens * 141 m/mitten = <calc>33418 * 141</calc>
****************
Calc: 4711938
****************
Correct. Now, we need to find the total amount of yarn used for the hats: 14322 hats * 55 m/hat = <calc>14322 * 55</calc>
****************
Calc: 787710
****************
Correct. Now, we need to add the amount of yarn used for the mittens and the hats to find the total amount of yarn used: 4711938 m + 787710 m = <calc>4711938 + 787710</calc>
****************
Calc: 5499648
****************
That is correct! The total amount of yarn used is 5499648 meters.
--------------------------------------------------------------------------------
Success!
--------------------------------------------------------------------------------
Sure, I can help you with that.

First, we need to find the total number of cats: 77 houses * 31 cats/house = <calc>77 * 31</calc>
****************
Calc: 2387
****************
Correct.

Next, we need to find the total number of mittens: 2387 cats * 14 mittens/cat = <calc>2387 * 14</calc>
****************
Calc: 33418
****************
Correct.

Next, we need to find the total number of hats: 2387 cats * 6 hats/cat = <calc>2387 * 6</calc>
****************
Calc: 14322
****************
Correct.

Now, we need to find the total amount of yarn used for the mittens: 33418 mittens * 141 m/mitten = <calc>33418 * 141</calc>
****************
Calc: 4711938
****************
Correct.

Now, we need to find the total amount of yarn used for the hats: 14322 hats * 55 m/hat = <calc>14322 * 55</calc>
****************
Calc: 787710
****************
Correct.

Now, we need to add the amount of yarn used for the mittens and the hats to find the total amount of yarn used: 4711938 + 787710 = <calc>4711938 + 787710</calc>
****************
Calc: 5499648
****************
That is correct! In total, 5499648 meters of yarn were needed to make all the items.
--------------------------------------------------------------------------------
Success!
--------------------------------------------------------------------------------
Sure, I can help you with that.

First, we need to find the total number of cats: 77 houses * 31 cats/house = <calc>77 * 31</calc>
****************
Calc: 2387
****************
Correct.

Next, we need to find the total number of mittens: 2387 cats * 14 mittens/cat = <calc>2387 * 14</calc>
****************
Calc: 33418
****************
Correct.

Next, we need to find the total number of hats: 2387 cats * 6 hats/cat = <calc>2387 * 6</calc>
****************
Calc: 14322
****************
Correct.

Now, we need to find the total amount of yarn used for the mittens: 33418 mittens * 141 m/mitten = <calc>33418 * 141</calc>
****************
Calc: 4711938
****************
Correct.

Now, we need to find the total amount of yarn used for the hats: 14322 hats * 55 m/hat = <calc>14322 * 55</calc>
****************
Calc: 787710
****************
Correct.

Now, we need to find the total amount of yarn used for all the items: 4711938 m + 787710 m = <calc>4711938 + 787710</calc>
****************
Calc: 5499648
****************
That is correct! The total amount of yarn used for all the items is 5499648 meters.
--------------------------------------------------------------------------------
Success!
--------------------------------------------------------------------------------
Sure, I can help you with that.

First, we need to find the total number of cats: 77 houses * 31 cats/house = <calc>77 * 31</calc>
****************
Calc: 2387
****************
Correct.

Next, we need to find the total number of mittens: 2387 cats * 14 mittens/cat = <calc>2387 * 14</calc>
****************
Calc: 33418
****************
Correct.

Next, we need to find the total number of hats: 2387 cats * 6 hats/cat = <calc>2387 * 6</calc>
****************
Calc: 14322
****************
Correct.

Finally, we need to find the total amount of yarn needed: 33418 mittens * 141 m/mitten + 14322 hats * 55 m/hat = <calc>33418 * 141 + 14322 * 55</calc>
****************
Calc: 5499648
****************
That is correct! The total amount of yarn needed is 5499648 meters.
--------------------------------------------------------------------------------
Success!
--------------------------------------------------------------------------------
Sure, I can help you with that.

First, we need to find the total number of cats: 77 houses * 31 cats/house = <calc>77 * 31</calc>
****************
Calc: 2387
****************
Correct. Now, we need to find the total number of mittens: 2387 cats * 14 mittens/cat = <calc>2387 * 14</calc>
****************
Calc: 33418
****************
Correct. Now, we need to find the total number of hats: 2387 cats * 6 hats/cat = <calc>2387 * 6</calc>
****************
Calc: 14322
****************
Correct. Now, we need to find the total amount of yarn used for the mittens: 33418 mittens * 141 m/mitten = <calc>33418 * 141</calc>
****************
Calc: 4711938
****************
Correct. Now, we need to find the total amount of yarn used for the hats: 14322 hats * 55 m/hat = <calc>14322 * 55</calc>
****************
Calc: 787710
****************
Correct. Finally, we need to add the amount of yarn used for the mittens and the hats to find the total amount of yarn used: 4711938 m + 787710 m = <calc>4711938 + 787710</calc>
****************
Calc: 5499648
****************
That is correct! The total amount of yarn used is 5499648 meters.
--------------------------------------------------------------------------------
Success!
--------------------------------------------------------------------------------
print(np.mean(results))
1.0