本教學課程說明如何反覆建立少量樣本,並從樣本儲存庫動態擷取這些樣本,藉此修正大型語言模型的行為。在本教學課程中,您會使用 gemini-2.0-flash
模型。
您將執行下列操作:
建立 Example Store 執行個體 (
ExampleStore
)。根據 Gemini 的回覆製作範例,並將這些範例上傳至範例商店執行個體。
從樣本儲存庫動態擷取範例,引導 LLM 做出預期行為。
清除所用資源。
事前準備
如要完成本教學課程中示範的步驟,請先設定專案和環境。
設定專案
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI API.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI API.
- 如果您選取了專案,請確認您具備該專案的 Vertex AI 使用者 (
roles/aiplatform.user
) IAM 角色。 執行下列指令,為 Example Store 安裝 Python 適用的 Vertex AI SDK。
pip install --upgrade google-cloud-aiplatform>=1.87.0
使用下列程式碼範例,匯入及初始化 Example Store 的 SDK。
import vertexai from vertexai.preview import example_stores vertexai.init( project="PROJECT_ID", location="LOCATION" )
更改下列內容:
PROJECT_ID:您的專案 ID。
LOCATION:您的區域。系統僅支援
us-central1
。
定義
get_current_weather
函式工具。您在後續步驟中建立的範例,會引導模型瞭解何時要叫用這個函式,以及要傳遞哪些引數。如要進一步瞭解範例如何提升函式呼叫成效和模型回應,請參閱「使用範例提升函式呼叫成效」。如要進一步瞭解如何建立函式呼叫應用程式,請參閱「函式呼叫簡介」。
from google.genai import types as genai_types get_current_weather_func = genai_types.FunctionDeclaration( name="get_current_weather", description="Get the current weather in a given location", parameters={ "type": "object", "properties": { "location": { "type": "string", "description": "The city name of the location for which to get the weather." } }, }, )
使用
get_current_weather
函式傳送要求給 Gemini,請 Gemini 生成內容。請參閱「建立 Gen AI SDK 的用戶端」。
from google import genai client = genai.Client( http_options=genai_types.HttpOptions(api_version="v1"), vertexai=True, project="PROJECT_ID",, location="LOCATION") user_content = genai_types.Content( role="user", parts=[Part(text="What is the weather like in Boston?")], ) response = client.models.generate_content( model="gemini-2.0-flash", user_content, config=genai_types.GenerateContentConfig( tools=[ genai_types.Tool(function_declarations=[get_current_weather_func])] ) )
請透過下列任一方式建立及上傳範例。
如果 LLM 的回覆顯示預期行為,請使用下列程式碼範例,根據回覆撰寫範例並上傳至範例商店。
function_response = genai_types.Content( parts=[ genai_types.Part( function_response={ "name": "get_current_weather", "response": { "location": "New York, NY", "temperature": 38, "description": "Partly Cloudy", "icon": "partly-cloudy", "humidity": 65, "wind": { "speed": 10, "direction": "NW" } } } ) ] ) final_model_response = genai_types.Content( role="model", parts=[genai_types.Part(text="The weather in NYC is 38 degrees and partly cloudy.")], ) example = { "contents_example": { "contents": [user_content.to_json_dict()], "expected_contents": [ {"content": response.candidates[0].content.to_json_dict()}, {"content": function_response.to_json_dict()}, {"content": final_model_response.to_json_dict()}, ], }, "search_key": user_content.parts[0].text, } example_store.upsert_examples(examples=[example])
或者,如果回覆未涵蓋您預期的所有函式或結果,或是模型難以進行推理,請使用下列程式碼範例撰寫回覆,修正模型行為。
expected_function_call = genai_types.Content( parts=[ genai_types.Part( function_call={ "name": "get_current_weather", "args": {"location": "New York, NY"} } ) ] ) function_response = genai_types.Content( parts=[ genai_types.Part( function_response={ "name": "get_current_weather", "response": { "location": "New York, NY", "temperature": 38, "description": "Partly Cloudy", "icon": "partly-cloudy", "humidity": 65, "wind": { "speed": 10, "direction": "NW" } } } ) ] ) final_model_response = genai_types.Content( role="model", parts=[genai_types.Part(text="The weather in NYC is 38 degrees and partly cloudy.")], ) example = { "contents_example": { "contents": [user_content.to_json_dict()], "expected_contents": [ {"content": expected_function_call.to_json_dict()}, {"content": function_response.to_json_dict()}, {"content": final_model_response.to_json_dict()}, ], }, "search_key": user_content.parts[0].text, } example_store.upsert_examples(examples=[example])
視需要重複步驟 2 和 3,製作並上傳多個範例。 如果模型出現非預期行為,或上傳的範例未涵蓋您預期的所有功能、結果或推理,您可以上傳其他範例。如要進一步瞭解何時需要上傳其他範例,請參閱「上傳範例」。
使用下列程式碼範例刪除 Example Store 執行個體。
example_store.delete()
刪除所有在本機建立的檔案。
驗證 Vertex AI
如要在本機開發環境中使用本頁的 Python 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
安裝 Google Cloud CLI。
如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI。
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
詳情請參閱 Google Cloud 驗證說明文件中的「 為本機開發環境設定 ADC」。
匯入程式庫
建立 Example Store 執行個體
使用下列程式碼範例,建立使用 text-embedding-005
嵌入模型的 Example Store 例項。
example_store = example_stores.ExampleStore.create(
example_store_config=example_stores.ExampleStoreConfig(
vertex_embedding_model="text-embedding-005"
)
)
請注意,建立範例商店需要幾分鐘。
如要進一步瞭解如何建立或重複使用範例商店執行個體,請參閱「建立範例商店執行個體」。
將範例上傳至 Example Store 執行個體
請按照下列步驟,在 Example Store 執行個體中撰寫並上傳範例。每個要求最多可上傳五個範例。
使用 Gemini 擷取及運用範例
根據與提示的相似度搜尋範例。接著即可將這些範例新增至提示,引導 LLM 做出預期行為。
定義輔助函式來設定範例格式
使用下列程式碼範例定義 ExampleStorePrompt
類別和輔助函式,以便搜尋及擷取範例。
import abc
import jinja2
import json
from google.protobuf import json_format
# --BOILERPLATE CODE FOR FORMATTING--
EXAMPLES_PREAMBLE = """<EXAMPLES>
The following are examples of user queries and model responses using the available python libraries.
Begin few-shot
"""
EXAMPLES_POSTAMBLE = """
End few-shot
Now, try to follow these examples and complete the following conversation:
</EXAMPLES>
"""
EXAMPLE_PREAMBLE = "EXAMPLE"
TEMPLATE = """
"""
class ExampleStorePrompt:
def __init__(
self, template = TEMPLATE, example_preamble = EXAMPLE_PREAMBLE,
examples_preamble = EXAMPLES_PREAMBLE,
examples_postamble = EXAMPLES_POSTAMBLE):
self.template = jinja2.Template(template)
self.example_preamble = example_preamble
self.examples_preamble = examples_preamble
self.examples_postamble = examples_postamble
@abc.abstractmethod
def process_function_response(self, function_response):
return json.dumps(function_response)
@abc.abstractmethod
def process_function_call(self, function_call):
args_list = []
for key, value in function_call.get("args", []).items():
if isinstance(value, str):
# Wrap strings in quotes.
value = f'"{value}"'
if isinstance(value, list):
value = ', '.join(
f'"{item}"' if isinstance(item, str)
else str(item) for item in value)
value = f"[{value}]"
if isinstance(value, dict):
value = json.dumps(value)
args_list.append(f'{key}={value}')
args = ", ".join(args_list)
return f"```\n{function_call.get('name')}({args})\n```"
@abc.abstractmethod
def process_part(self, part):
if "function_call" in part:
return self.process_function_call(part["function_call"])
if "text" in part:
return part.get("text")
if "function_response" in part:
return self.process_function_response(part["function_response"])
@abc.abstractmethod
def process_content(self, content):
response = []
for part in content.get("parts", []):
response.append(self.process_part(part))
return [content.get("role"), response]
@abc.abstractmethod
def example_formatter(self, example: dict):
response = []
for content in example.get("contents", []):
response.append(self.process_content(content))
for content in example.get("expected_contents", []):
content = content.get("content", {})
response.append(self.process_content(content))
return response
def get_prompt(self, examples: list):
if not examples:
return ""
contents_example = example.get("example", {}).get(
"stored_contents_example", {}).get("contents_example", {})
examples = [self.example_formatter(example) for example in examples]
return self.template.render(
examples=examples,
example_preamble=self.example_preamble,
examples_preamble=self.examples_preamble,
examples_postamble=self.examples_postamble
)
搜尋相關範例
使用下列程式碼範例,搜尋與 LLM 進行中對話相關的範例。接著,您可以使用輔助函式,在提示中加入這些範例。
query = "what's the fastest way to get to disney from lax"
# Search for relevant examples.
examples = example_store.search_examples(
{"stored_contents_example_key": query}, top_k=3)
prompt = ExampleStorePrompt().get_prompt(examples.get("results", []))
model_response = client.models.generate_content(
model="gemini-2.0-flash",
contents="How do I get to LAX?",
config=genai_types.GenerateContentConfig(
system_instruction=prompt,
tools=[
genai_types.Tool(function_declarations=[track_flight_status_function])]
)
)
反覆提升回覆品質
如要使用多樣本範例改善 Gemini 的回覆模式,請重複下列各節的步驟:
清除所用資源
如要清除此專案中使用的所有資源,您可以刪除用於本快速入門導覽課程的 Google Cloud 專案。
或者,您也可以按照下列步驟,刪除在本教學課程中建立的個別資源: