Skip to content

Commit

Permalink
feat: ai支持获取地区天气
Browse files Browse the repository at this point in the history
ai模块使用function_call来增强ai,从而去参数化获取地域天气
  • Loading branch information
Clov614 committed Aug 15, 2024
1 parent 1c6e006 commit 6bf27ce
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module wechat-demo
go 1.21.0

require (
github.com/Clov614/go-ai-sdk v0.2.0
github.com/Clov614/go-ai-sdk v0.3.2
github.com/eatmoreapple/openwechat v1.4.7
github.com/gin-gonic/gin v1.10.0
github.com/google/uuid v1.6.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/Clov614/go-ai-sdk v0.2.0 h1:LnVAsrrwTsq6NaZe2wttkHe1y1Nfiumnrr1vKDzOBd4=
github.com/Clov614/go-ai-sdk v0.2.0/go.mod h1:S/GDuJSLlnNU47uEpY2FrN1GdP+A4mDXBQZQ2i6sCzk=
github.com/Clov614/go-ai-sdk v0.3.2 h1:CYn+HyB/HSisAgI58pFZn/wbleOK5ASSSJyhEZsdwic=
github.com/Clov614/go-ai-sdk v0.3.2/go.mod h1:S/GDuJSLlnNU47uEpY2FrN1GdP+A4mDXBQZQ2i6sCzk=
github.com/bytedance/sonic v1.11.9 h1:LFHENlIY/SLzDWverzdOvgMztTxcfcF+cqNsz9pK5zg=
github.com/bytedance/sonic v1.11.9/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
Expand Down
17 changes: 17 additions & 0 deletions rikkabot/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"os"
"sync"
"wechat-demo/rikkabot/logging"
"wechat-demo/rikkabot/utils/configutil"
)
Expand All @@ -33,6 +34,8 @@ type CommonConfig struct {
Interval int64 `comment:"The Heart Beat Interval" yaml:"heart_beat_interval"`
// todo 其他设置项
PluginConfig map[string]interface{} `comment:"插件的设置" yaml:"plugin_config"`

mu sync.RWMutex
}

// HttpServerConfig http 正向 HTTP API配置
Expand Down Expand Up @@ -126,6 +129,20 @@ func (c *CommonConfig) verifiability() {

}

// SetCustomPluginCfg 保存自定义插件设置
func (c *CommonConfig) SetCustomPluginCfg(pluginName string, pluginCfg interface{}) {
c.mu.Lock()
defer c.mu.Unlock()
c.PluginConfig[pluginName] = pluginCfg
}

func (c *CommonConfig) GetCustomPluginCfg(pluginName string) (interface{}, bool) {
c.mu.RLock()
defer c.mu.RUnlock()
plg, ok := c.PluginConfig[pluginName]
return plg, ok
}

var defaultPath = "./cfg/"

var defaultSaveFileName = "config.yaml"
Expand Down
57 changes: 57 additions & 0 deletions rikkabot/plugins/ai/weather_func2ai.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Package ai
// @Author Clover
// @Data 2024/8/15 下午10:38:00
// @Desc 天气模块
package ai

import (
"encoding/json"
ai_sdk "github.com/Clov614/go-ai-sdk"
"github.com/Clov614/go-ai-sdk/example_func_call/weather"
"github.com/Clov614/go-ai-sdk/global"
"wechat-demo/rikkabot/config"
"wechat-demo/rikkabot/logging"
)

type weatherCfg struct {
Key string `json:"key"`
}

func init() {
// 注册对话插件
cfg := config.GetConfig()
wCfgInterface, ok := cfg.GetCustomPluginCfg("weather_ai")
if !ok {
cfg.SetCustomPluginCfg("weather_ai", weatherCfg{Key: ""})
_ = cfg.Update() // 更新设置
logging.Fatal("weather_ai plugin config loaded empty please write the key about weather api in config.yaml", 12)
}
bytes, _ := json.Marshal(wCfgInterface)

Check failure on line 29 in rikkabot/plugins/ai/weather_func2ai.go

View workflow job for this annotation

GitHub Actions / build

Error return value of `encoding/json.Marshal` is not checked: unsafe type `interface{}` found (errchkjson)
var wcfg weatherCfg
json.Unmarshal(bytes, &wcfg)

Check failure on line 31 in rikkabot/plugins/ai/weather_func2ai.go

View workflow job for this annotation

GitHub Actions / build

Error return value of `json.Unmarshal` is not checked (errcheck)
w := weather.NewWeather(wcfg.Key)
funcCallInfo := ai_sdk.FuncCallInfo{
Function: ai_sdk.Function{
Name: "get_weather_by_city",
Description: "根据地址获取城市代码 cityAddress: 城市地址,如: 泉州市永春县 isMultiDay: 是否获取多日天气",
Parameters: ai_sdk.FunctionParameter{
Type: global.ObjType,
Properties: ai_sdk.Properties{
"city_addr": ai_sdk.Property{
Type: global.StringType,
Description: "地址,如:国家,城市,县、区地址",
},
"is_multi": ai_sdk.Property{
Type: global.BoolType,
Description: "是否获取多日天气",
},
},
Required: []string{"city_addr", "is_multi"},
},
Strict: false,
},
CallFunc: w,
//CustomTrigger: nil, // 暂时不测试
}
ai_sdk.FuncRegister.Register(&funcCallInfo, []string{"天气", "weather"})
}

0 comments on commit 6bf27ce

Please sign in to comment.