import pathlib
from google.adk import Agent
from google.adk.skills import load_skill_from_dir
from google.adk.tools import skill_toolset
weather_skill = load_skill_from_dir(
pathlib.Path(__file__).parent /"skills"/"weather-skill")
defget_weather_tool(city: str) -> dict:
"""Retrieves the current weather report for a specified city."""return {
"status": "success",
"report": f"The weather in {city} is sunny with a temperature of 25°C.",
}
my_skill_toolset = skill_toolset.SkillToolset(
skills=[weather_skill],
additional_tools=[get_weather_tool],
)
root_agent = Agent(
model="gemini-flash-latest",
name="skill_user_agent",
description="An agent that can use specialized skills.",
instruction=(
"You are a helpful assistant that can leverage skills to perform tasks." ),
tools=[
my_skill_toolset,
],
)
from google.adk.skills import models
greeting_skill = models.Skill(
frontmatter=models.Frontmatter(
name="greeting-skill",
description=(
"A friendly greeting skill that can say hello to a specific person." ),
),
instructions=(
"Step 1: Read the 'references/hello_world.txt' file to understand how"" to greet the user. Step 2: Return a greeting based on the reference." ),
resources=models.Resources(
references={
"hello_world.txt": "Hello! So glad to have you here!",
"example.md": "This is an example reference.",
},
),
)
info
ADK Go 目前尚未提供用于内联技能的标准来源,
但未来可能会增加此功能。
要在代码中直接定义技能,您必须自己实现 skill.Source
接口,如下所示。
go55 行展开收起expand_more
import (
"context""io""slices""strings""google.golang.org/adk/v2/tool/skilltoolset/skill")
// 静态内存技能的示例实现。来源:typeStaticSourcestruct{}
func (s*StaticSource) ListFrontmatters(ctxcontext.Context) ([]*skill.Frontmatter, error) {
return []*skill.Frontmatter{
{Name: "greeting-skill", Description: "A friendly greeting skill that can say hello to a specific person."},
}, nil}
func (s*StaticSource) LoadFrontmatter(ctxcontext.Context, namestring) (*skill.Frontmatter, error) {
ifname!="greeting-skill" {
returnnil, skill.ErrSkillNotFound }
return&skill.Frontmatter{Name: "greeting-skill", Description: "A friendly greeting skill that can say hello to a specific person."}, nil}
func (s*StaticSource) LoadInstructions(ctxcontext.Context, namestring) (string, error) {
ifname!="greeting-skill" {
return"", skill.ErrSkillNotFound }
return"Step 1: Read the 'references/hello_world.txt' file to understand how to greet the user. Step 2: Return a greeting based on the reference.", nil}
func (s*StaticSource) ListResources(ctxcontext.Context, name, subpathstring) ([]string, error) {
ifname!="greeting-skill" {
returnnil, skill.ErrSkillNotFound }
if !slices.Contains([]string{"", ".", "references", "references/"}, subpath) {
returnnil, skill.ErrResourceNotFound }
return []string{"references/hello_world.txt", "references/example.md"}, nil}
func (s*StaticSource) LoadResource(ctxcontext.Context, name, resourcePathstring) (io.ReadCloser, error) {
ifname!="greeting-skill" {
returnnil, skill.ErrSkillNotFound }
switchresourcePath {
case"references/hello_world.txt":
returnio.NopCloser(strings.NewReader("Hello! So glad to have you here!")), nilcase"references/example.md":
returnio.NopCloser(strings.NewReader("This is an example reference.")), nildefault:
returnnil, skill.ErrResourceNotFound }
}