程式環境都會用colab 來執行程式,如果要在其他環境執行,請自行修改哦
colab 事前準備:設定專案和 API 金鑰
載入gemini
#pip install -q -U google-generativeai
import google.generativeai as genai
API 金鑰
from google.colab import userdata
API_KEY=userdata.get('GOOGLE_API_KEY')
#genai.configure(api_key="YOUR_API_KEY")
# Configure the client library by providing your API key.
genai.configure(api_key=API_KEY)
在 Gemini API 中,平行函式呼叫指的是在一次請求中,同時呼叫多個函式。這對於需要同時執行多個任務、或者需要從多個資料來源獲取資訊的場景非常有用。
定義工具
def power_disco_ball(power: bool) -> bool:
"""供電旋轉的迪斯可球。"""
print(f"迪斯可球 是 {'旋轉!' if power else '停止.'}")
return True
def start_music(energetic: bool, loud: bool, bpm: int) -> str:
"""播放一些符合指定參數的音樂。
Args:
energetic:音樂是否充滿活力。
loud:音樂是否響亮。
bpm:音樂每分鐘的節拍數。
Returns: 正在播放的歌曲的名稱。
"""
print(f"Starting music! {energetic=} {loud=}, {bpm=}")
return "絕對不會放棄你。"
def dim_lights(brightness: float) -> bool:
"""調暗燈光.
Args:
brightness: 燈光的亮度,0.0為關閉,1.0為全亮。
"""
print(f"燈光現已設定為 {brightness:.0%}")
return True
可使用所有指定工具的指令來呼叫模型
# Set the model up with tools.
house_fns = [power_disco_ball, start_music, dim_lights]
model = genai.GenerativeModel(model_name="gemini-1.5-flash", tools=house_fns)
# Call the API.
chat = model.start_chat()
response = chat.send_message("把這個地方變成一個聚會!")
# Print out each of the function calls requested from this single call.
for part in response.parts:
if fn := part.function_call:
args = ", ".join(f"{key}={val}" for key, val in fn.args.items())
print(f"{fn.name}({args})")
每個顯示的結果都代表模型要求的單一函式呼叫。如要傳回結果,請按照要求的順序加入回應。
# Simulate the responses from the specified tools.
responses = {
"power_disco_ball": True,
"start_music": "Never gonna give you up.",
"dim_lights": True,
}
# Build the response parts.
response_parts = [
genai.protos.Part(function_response=genai.protos.FunctionResponse(name=fn, response={"result": val}))
for fn, val in responses.items()
]
response = chat.send_message(response_parts)
print(response.text)
回答
好的!我已經打開了迪斯可球、播放了活力十足的音樂,並將燈光調暗了一點。準備好派對吧!
def multiply(a:float, b:float):
"""returns a * b."""
return a*b
model = genai.GenerativeModel(model_name='gemini-1.5-flash',
tools=[multiply])
model._tools.to_proto()
回答
[function_declarations {
name: "multiply"
description: "returns a * b."
parameters {
type_: OBJECT
properties {
key: "a"
value {
type_: NUMBER
}
}
properties {
key: "b"
value {
type_: NUMBER
}
}
required: "a"
required: "b"
}
}]
calculator = genai.protos.Tool(
function_declarations=[
genai.protos.FunctionDeclaration(
name='multiply',
description="傳回兩個數字的乘積。",
parameters=genai.protos.Schema(
type=genai.protos.Type.OBJECT,
properties={
'a':genai.protos.Schema(type=genai.protos.Type.NUMBER),
'b':genai.protos.Schema(type=genai.protos.Type.NUMBER)
},
required=['a','b']
)
)
])
將此項目描述為與 JSON 相容的物件
calculator = {'function_declarations': [
{'name': 'multiply',
'description': '傳回兩個數字的乘積。',
'parameters': {'type_': 'OBJECT',
'properties': {
'a': {'type_': 'NUMBER'},
'b': {'type_': 'NUMBER'} },
'required': ['a', 'b']} }]}
將 genai.protos.Tool 的表示法或工具清單傳遞
model = genai.GenerativeModel('gemini-1.5-flash', tools=calculator)
chat = model.start_chat()
response = chat.send_message(
f"234551 X 325552 是多少?",
)
print(response)
回答
response:
GenerateContentResponse(
done=True,
iterator=None,
result=protos.GenerateContentResponse({
"candidates": [
{
"content": {
"parts": [
{
"function_call": {
"name": "multiply",
"args": {
"b": 325552.0,
"a": 234551.0
}
}
}
],
"role": "model"
},
"finish_reason": "STOP",
........
fc = response.candidates[0].content.parts[0].function_call
assert fc.name == 'multiply'
result = fc.args['a'] * fc.args['b']
result
回答
76358547152.0
將結果傳送至模型,即可繼續對話
response = chat.send_message(
genai.protos.Content(
parts=[genai.protos.Part(
function_response = genai.protos.FunctionResponse(
name='multiply',
response={'result': result}))]))
print(response.text)
回答
234551 X 325552 等於 76358547152。