config.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package websearch
  2. import (
  3. "encoding/json"
  4. "github.com/bytedance/sonic"
  5. )
  6. // Configuration structures
  7. type Config struct {
  8. Enable bool `json:"enable"`
  9. ForceSearch bool `json:"force_search"`
  10. MaxResults int `json:"max_results"`
  11. SearchRewrite SearchRewrite `json:"search_rewrite"`
  12. NeedReference bool `json:"need_reference"`
  13. ReferenceLocation string `json:"reference_location"` // content or other field
  14. ReferenceFormat string `json:"reference_format"`
  15. DefaultLanguage string `json:"default_language"`
  16. PromptTemplate string `json:"prompt_template"`
  17. SearchFrom []EngineConfig `json:"search_from"`
  18. }
  19. type SearchRewrite struct {
  20. Enable bool `json:"enable"`
  21. ModelName string `json:"model_name"`
  22. TimeoutMillisecond uint32 `json:"timeout_millisecond"`
  23. MaxCount int `json:"max_count"`
  24. AddRewriteUsage bool `json:"add_rewrite_usage"`
  25. RewriteUsageField string `json:"rewrite_usage_field"`
  26. }
  27. type EngineConfig struct {
  28. Type string `json:"type"` // bing, google, arxiv, searchxng
  29. MaxResults int `json:"max_results"`
  30. Spec json.RawMessage `json:"spec"`
  31. }
  32. func (e *EngineConfig) SpecExists() bool {
  33. return len(e.Spec) > 0
  34. }
  35. func (e *EngineConfig) LoadSpec(spec any) error {
  36. if !e.SpecExists() {
  37. return nil
  38. }
  39. return sonic.Unmarshal(e.Spec, spec)
  40. }
  41. // Engine-specific configuration structures
  42. type GoogleSpec struct {
  43. APIKey string `json:"api_key"`
  44. CX string `json:"cx"`
  45. }
  46. type BingSpec struct {
  47. APIKey string `json:"api_key"`
  48. }
  49. type ArxivSpec struct{}
  50. type SearchXNGSpec struct {
  51. BaseURL string `json:"base_url"`
  52. }