Documentation
¶
Overview ¶
Package api 提供常用 API 集成工具
本包实现多种常用 API 工具:
- Weather: 天气查询
- Stock: 股票数据
- News: 新闻获取
- Currency: 货币汇率
- Translation: 文本翻译
设计借鉴:
- LangChain: Tool 体系
- Semantic Kernel: Plugin 系统
Index ¶
- func RegisterAPITools(registry interface{ ... }, configs map[string]string) error
- type CurrencyInput
- type CurrencyOutput
- type CurrencyTool
- type NewsArticle
- type NewsInput
- type NewsOutput
- type NewsProvider
- type NewsTool
- type StockInput
- type StockProvider
- type StockQuote
- type StockTool
- type TranslationInput
- type TranslationOutput
- type TranslationProvider
- type TranslationTool
- type WeatherInput
- type WeatherOutput
- type WeatherProvider
- type WeatherTool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 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 创建新闻工具
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 创建股票工具
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 工具描述
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 创建天气工具
Click to show internal directories.
Click to hide internal directories.