providers: implement native web search for OpenAI and Codex
This commit is contained in:
parent
3a230bdfce
commit
33f5a3b436
3 changed files with 40 additions and 2 deletions
|
|
@ -157,6 +157,10 @@ func (p *CodexProvider) GetDefaultModel() string {
|
||||||
return codexDefaultModel
|
return codexDefaultModel
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *CodexProvider) SupportsNativeSearch() bool {
|
||||||
|
return p.enableWebSearch
|
||||||
|
}
|
||||||
|
|
||||||
func resolveCodexModel(model string) (string, string) {
|
func resolveCodexModel(model string) (string, string) {
|
||||||
m := strings.ToLower(strings.TrimSpace(model))
|
m := strings.ToLower(strings.TrimSpace(model))
|
||||||
if m == "" {
|
if m == "" {
|
||||||
|
|
|
||||||
|
|
@ -55,3 +55,7 @@ func (p *HTTPProvider) Chat(
|
||||||
func (p *HTTPProvider) GetDefaultModel() string {
|
func (p *HTTPProvider) GetDefaultModel() string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *HTTPProvider) SupportsNativeSearch() bool {
|
||||||
|
return p.delegate.SupportsNativeSearch()
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -103,8 +103,11 @@ func (p *Provider) Chat(
|
||||||
"messages": common.SerializeMessages(messages),
|
"messages": common.SerializeMessages(messages),
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(tools) > 0 {
|
// When fallback uses a different provider (e.g. DeepSeek), that provider must not inject web_search_preview.
|
||||||
requestBody["tools"] = tools
|
nativeSearch, _ := options["native_search"].(bool)
|
||||||
|
nativeSearch = nativeSearch && isNativeSearchHost(p.apiBase)
|
||||||
|
if len(tools) > 0 || nativeSearch {
|
||||||
|
requestBody["tools"] = buildToolsList(tools, nativeSearch)
|
||||||
requestBody["tool_choice"] = "auto"
|
requestBody["tool_choice"] = "auto"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -195,6 +198,33 @@ func normalizeModel(model, apiBase string) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func buildToolsList(tools []ToolDefinition, nativeSearch bool) []any {
|
||||||
|
result := make([]any, 0, len(tools)+1)
|
||||||
|
for _, t := range tools {
|
||||||
|
if nativeSearch && strings.EqualFold(t.Function.Name, "web_search") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
result = append(result, t)
|
||||||
|
}
|
||||||
|
if nativeSearch {
|
||||||
|
result = append(result, map[string]any{"type": "web_search_preview"})
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) SupportsNativeSearch() bool {
|
||||||
|
return isNativeSearchHost(p.apiBase)
|
||||||
|
}
|
||||||
|
|
||||||
|
func isNativeSearchHost(apiBase string) bool {
|
||||||
|
u, err := url.Parse(apiBase)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
host := u.Hostname()
|
||||||
|
return host == "api.openai.com" || strings.HasSuffix(host, ".openai.azure.com")
|
||||||
|
}
|
||||||
|
|
||||||
// supportsPromptCacheKey reports whether the given API base is known to
|
// supportsPromptCacheKey reports whether the given API base is known to
|
||||||
// support the prompt_cache_key request field. Currently only OpenAI's own
|
// support the prompt_cache_key request field. Currently only OpenAI's own
|
||||||
// API and Azure OpenAI support this. All other OpenAI-compatible providers
|
// API and Azure OpenAI support this. All other OpenAI-compatible providers
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue