api

package
v0.5.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 13, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package api 提供常用 API 集成工具

本包实现多种常用 API 工具:

  • Weather: 天气查询
  • Stock: 股票数据
  • News: 新闻获取
  • Currency: 货币汇率
  • Translation: 文本翻译

设计借鉴:

  • LangChain: Tool 体系
  • Semantic Kernel: Plugin 系统

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterAPITools

func RegisterAPITools(registry interface {
	Register(tool.Tool) error
}, configs map[string]string) error

RegisterAPITools 注册所有 API 工具

Types

type CurrencyInput

type CurrencyInput struct {
	// From 源货币代码
	From string `json:"from" desc:"源货币代码,如 USD, CNY" required:"true"`

	// To 目标货币代码
	To string `json:"to" desc:"目标货币代码" required:"true"`

	// Amount 金额
	Amount float64 `json:"amount,omitempty" desc:"转换金额" default:"1"`
}

CurrencyInput 汇率查询输入

type CurrencyOutput

type CurrencyOutput struct {
	From      string    `json:"from"`
	To        string    `json:"to"`
	Rate      float64   `json:"rate"`
	Amount    float64   `json:"amount"`
	Converted float64   `json:"converted"`
	UpdatedAt time.Time `json:"updated_at"`
}

CurrencyOutput 汇率输出

type CurrencyTool

type CurrencyTool struct {
	APIKey     string
	HTTPClient *http.Client
}

CurrencyTool 货币汇率工具

func NewCurrencyTool

func NewCurrencyTool(apiKey string) *CurrencyTool

NewCurrencyTool 创建货币工具

func (*CurrencyTool) Description

func (t *CurrencyTool) Description() string

Description 工具描述

func (*CurrencyTool) Execute

func (t *CurrencyTool) Execute(ctx context.Context, args map[string]any) (tool.Result, error)

Execute 执行查询

func (*CurrencyTool) Name

func (t *CurrencyTool) Name() string

Name 工具名称

func (*CurrencyTool) Schema

func (t *CurrencyTool) Schema() *llm.Schema

Schema 输入 Schema

func (*CurrencyTool) Validate

func (t *CurrencyTool) Validate(args map[string]any) error

Validate 验证参数

type NewsArticle

type NewsArticle struct {
	Title       string    `json:"title"`
	Description string    `json:"description"`
	Content     string    `json:"content,omitempty"`
	URL         string    `json:"url"`
	ImageURL    string    `json:"image_url,omitempty"`
	Source      string    `json:"source"`
	PublishedAt time.Time `json:"published_at"`
}

NewsArticle 新闻文章

type NewsInput

type NewsInput struct {
	// Query 搜索关键词
	Query string `json:"query,omitempty" desc:"搜索关键词"`

	// Category 分类
	Category string `json:"category,omitempty" desc:"新闻分类" enum:"business,entertainment,general,health,science,sports,technology"`

	// Country 国家代码
	Country string `json:"country,omitempty" desc:"国家代码,如 cn, us" default:"cn"`

	// Language 语言
	Language string `json:"language,omitempty" desc:"语言代码" default:"zh"`

	// PageSize 返回数量
	PageSize int `json:"page_size,omitempty" desc:"返回新闻数量" default:"10"`
}

NewsInput 新闻查询输入

type NewsOutput

type NewsOutput struct {
	TotalResults int           `json:"total_results"`
	Articles     []NewsArticle `json:"articles"`
}

NewsOutput 新闻输出

type NewsProvider

type NewsProvider string

NewsProvider 新闻提供商

const (
	// NewsProviderNewsAPI NewsAPI.org
	NewsProviderNewsAPI NewsProvider = "newsapi"

	// NewsProviderGNews GNews
	NewsProviderGNews NewsProvider = "gnews"
)

type NewsTool

type NewsTool struct {
	APIKey     string
	Provider   NewsProvider
	HTTPClient *http.Client
}

NewsTool 新闻获取工具

func NewNewsTool

func NewNewsTool(apiKey string, provider NewsProvider) *NewsTool

NewNewsTool 创建新闻工具

func (*NewsTool) Description

func (t *NewsTool) Description() string

Description 工具描述

func (*NewsTool) Execute

func (t *NewsTool) Execute(ctx context.Context, args map[string]any) (tool.Result, error)

Execute 执行查询

func (*NewsTool) Name

func (t *NewsTool) Name() string

Name 工具名称

func (*NewsTool) Schema

func (t *NewsTool) Schema() *llm.Schema

Schema 输入 Schema

func (*NewsTool) Validate

func (t *NewsTool) Validate(args map[string]any) error

Validate 验证参数

type StockInput

type StockInput struct {
	// Symbol 股票代码
	Symbol string `json:"symbol" desc:"股票代码,如 AAPL, 600519.SH" required:"true"`

	// Type 查询类型:quote(实时报价), history(历史数据)
	Type string `json:"type,omitempty" desc:"查询类型" enum:"quote,history" default:"quote"`
}

StockInput 股票查询输入

type StockProvider

type StockProvider string

StockProvider 股票数据提供商

const (
	// StockProviderAlphaVantage Alpha Vantage
	StockProviderAlphaVantage StockProvider = "alphavantage"

	// StockProviderEastMoney 东方财富
	StockProviderEastMoney StockProvider = "eastmoney"
)

type StockQuote

type StockQuote struct {
	Symbol        string    `json:"symbol"`
	Name          string    `json:"name,omitempty"`
	Price         float64   `json:"price"`
	Change        float64   `json:"change"`
	ChangePercent float64   `json:"change_percent"`
	Open          float64   `json:"open"`
	High          float64   `json:"high"`
	Low           float64   `json:"low"`
	Volume        int64     `json:"volume"`
	MarketCap     float64   `json:"market_cap,omitempty"`
	UpdatedAt     time.Time `json:"updated_at"`
}

StockQuote 股票实时报价

type StockTool

type StockTool struct {
	APIKey     string
	Provider   StockProvider
	HTTPClient *http.Client
}

StockTool 股票数据工具

func NewStockTool

func NewStockTool(apiKey string, provider StockProvider) *StockTool

NewStockTool 创建股票工具

func (*StockTool) Description

func (t *StockTool) Description() string

Description 工具描述

func (*StockTool) Execute

func (t *StockTool) Execute(ctx context.Context, args map[string]any) (tool.Result, error)

Execute 执行查询

func (*StockTool) Name

func (t *StockTool) Name() string

Name 工具名称

func (*StockTool) Schema

func (t *StockTool) Schema() *llm.Schema

Schema 输入 Schema

func (*StockTool) Validate

func (t *StockTool) Validate(args map[string]any) error

Validate 验证参数

type TranslationInput

type TranslationInput struct {
	// Text 待翻译文本
	Text string `json:"text" desc:"待翻译的文本" required:"true"`

	// SourceLang 源语言(可选,自动检测)
	SourceLang string `json:"source_lang,omitempty" desc:"源语言代码,如 en, zh, ja"`

	// TargetLang 目标语言
	TargetLang string `json:"target_lang" desc:"目标语言代码" required:"true"`
}

TranslationInput 翻译输入

type TranslationOutput

type TranslationOutput struct {
	OriginalText   string  `json:"original_text"`
	TranslatedText string  `json:"translated_text"`
	SourceLang     string  `json:"source_lang"`
	TargetLang     string  `json:"target_lang"`
	Confidence     float64 `json:"confidence,omitempty"`
}

TranslationOutput 翻译输出

type TranslationProvider

type TranslationProvider string

TranslationProvider 翻译服务提供商

const (
	// TranslationProviderDeepL DeepL
	TranslationProviderDeepL TranslationProvider = "deepl"

	// TranslationProviderGoogle Google Translate
	TranslationProviderGoogle TranslationProvider = "google"

	// TranslationProviderBaidu 百度翻译
	TranslationProviderBaidu TranslationProvider = "baidu"
)

type TranslationTool

type TranslationTool struct {
	APIKey     string
	Provider   TranslationProvider
	HTTPClient *http.Client
}

TranslationTool 翻译工具

func NewTranslationTool

func NewTranslationTool(apiKey string, provider TranslationProvider) *TranslationTool

NewTranslationTool 创建翻译工具

func (*TranslationTool) Description

func (t *TranslationTool) Description() string

Description 工具描述

func (*TranslationTool) Execute

func (t *TranslationTool) Execute(ctx context.Context, args map[string]any) (tool.Result, error)

Execute 执行翻译

func (*TranslationTool) Name

func (t *TranslationTool) Name() string

Name 工具名称

func (*TranslationTool) Schema

func (t *TranslationTool) Schema() *llm.Schema

Schema 输入 Schema

func (*TranslationTool) Validate

func (t *TranslationTool) Validate(args map[string]any) error

Validate 验证参数

type WeatherInput

type WeatherInput struct {
	// Location 位置(城市名或坐标)
	Location string `json:"location" desc:"城市名称或经纬度坐标" required:"true"`

	// Units 温度单位:metric(摄氏度)或 imperial(华氏度)
	Units string `json:"units,omitempty" desc:"温度单位" enum:"metric,imperial" default:"metric"`

	// Lang 语言
	Lang string `json:"lang,omitempty" desc:"返回语言" default:"zh_cn"`
}

WeatherInput 天气查询输入

type WeatherOutput

type WeatherOutput struct {
	// Location 位置
	Location string `json:"location"`

	// Temperature 温度
	Temperature float64 `json:"temperature"`

	// FeelsLike 体感温度
	FeelsLike float64 `json:"feels_like"`

	// Humidity 湿度(百分比)
	Humidity int `json:"humidity"`

	// Description 天气描述
	Description string `json:"description"`

	// WindSpeed 风速
	WindSpeed float64 `json:"wind_speed"`

	// WindDirection 风向
	WindDirection string `json:"wind_direction,omitempty"`

	// Pressure 气压
	Pressure int `json:"pressure,omitempty"`

	// Visibility 能见度(米)
	Visibility int `json:"visibility,omitempty"`

	// UpdatedAt 更新时间
	UpdatedAt time.Time `json:"updated_at"`
}

WeatherOutput 天气查询输出

type WeatherProvider

type WeatherProvider string

WeatherProvider 天气服务提供商

const (
	// WeatherProviderOpenWeather OpenWeatherMap
	WeatherProviderOpenWeather WeatherProvider = "openweathermap"

	// WeatherProviderWeatherAPI WeatherAPI.com
	WeatherProviderWeatherAPI WeatherProvider = "weatherapi"

	// WeatherProviderQWeather 和风天气
	WeatherProviderQWeather WeatherProvider = "qweather"
)

type WeatherTool

type WeatherTool struct {
	// APIKey API 密钥
	APIKey string

	// Provider 天气服务提供商
	Provider WeatherProvider

	// HTTPClient HTTP 客户端
	HTTPClient *http.Client
}

WeatherTool 天气查询工具

func NewWeatherTool

func NewWeatherTool(apiKey string, provider WeatherProvider) *WeatherTool

NewWeatherTool 创建天气工具

func (*WeatherTool) Description

func (t *WeatherTool) Description() string

Description 工具描述

func (*WeatherTool) Execute

func (t *WeatherTool) Execute(ctx context.Context, args map[string]any) (tool.Result, error)

Execute 执行查询

func (*WeatherTool) Name

func (t *WeatherTool) Name() string

Name 工具名称

func (*WeatherTool) Schema

func (t *WeatherTool) Schema() *llm.Schema

Schema 输入 Schema

func (*WeatherTool) Validate

func (t *WeatherTool) Validate(args map[string]any) error

Validate 验证参数

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL