feat(web-search): add ollama web search provider support #907
This commit is contained in:
@@ -47,6 +47,7 @@ export interface CustomAgentConfig {
|
||||
|
||||
// ===== 网络搜索设置 =====
|
||||
web_search_enabled?: boolean;
|
||||
web_search_provider_id?: string;
|
||||
web_search_max_results?: number;
|
||||
|
||||
// ===== 多轮对话设置 =====
|
||||
|
||||
@@ -5,7 +5,7 @@ export interface WebSearchProviderEntity {
|
||||
id?: string
|
||||
tenant_id?: number
|
||||
name: string
|
||||
provider: 'bing' | 'google' | 'duckduckgo' | 'tavily'
|
||||
provider: 'bing' | 'google' | 'duckduckgo' | 'tavily' | 'ollama'
|
||||
description?: string
|
||||
parameters: {
|
||||
api_key?: string
|
||||
|
||||
@@ -15,7 +15,7 @@ import AgentSelector from './AgentSelector.vue';
|
||||
import { getCaretCoordinates } from '@/utils/caret';
|
||||
import { listModels, type ModelConfig } from '@/api/model';
|
||||
import { listAgents, type CustomAgent, BUILTIN_QUICK_ANSWER_ID, BUILTIN_SMART_REASONING_ID } from '@/api/agent';
|
||||
import { getTenantWebSearchConfig } from '@/api/web-search';
|
||||
import { listWebSearchProviders, type WebSearchProviderEntity } from '@/api/web-search-provider';
|
||||
import { getConversationConfig, updateConversationConfig, type ConversationConfig } from '@/api/system';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
|
||||
@@ -192,6 +192,11 @@ const agentWebSearchEnabled = computed(() => {
|
||||
return currentAgentConfig.value?.web_search_enabled ?? true;
|
||||
});
|
||||
|
||||
const agentWebSearchProviderId = computed(() => {
|
||||
if (!hasAgentConfig.value) return '';
|
||||
return currentAgentConfig.value?.web_search_provider_id || '';
|
||||
});
|
||||
|
||||
// 网络搜索是否被智能体禁用(只读状态)- 只有明确设置为 false 时才禁用
|
||||
const isWebSearchDisabledByAgent = computed(() => {
|
||||
return hasAgentConfig.value && agentWebSearchEnabled.value === false;
|
||||
@@ -282,7 +287,6 @@ const isAgentEnabled = computed(() => settingsStore.isAgentEnabled);
|
||||
const isWebSearchEnabled = computed(() => settingsStore.isWebSearchEnabled);
|
||||
const selectedKbIds = computed(() => settingsStore.settings.selectedKnowledgeBases || []);
|
||||
const selectedFileIds = computed(() => settingsStore.settings.selectedFiles || []);
|
||||
const isWebSearchConfigured = ref(false);
|
||||
|
||||
// 获取已选择的知识库信息
|
||||
const knowledgeBases = ref<Array<{ id: string; name: string; type?: 'document' | 'faq'; knowledge_count?: number; chunk_count?: number }>>([]);
|
||||
@@ -521,19 +525,29 @@ watch(selectedFileIds, () => {
|
||||
loadFiles();
|
||||
}, { immediate: true });
|
||||
|
||||
const webSearchProviders = ref<WebSearchProviderEntity[]>([]);
|
||||
|
||||
const isWebSearchConfigured = computed(() => {
|
||||
const agentProviderId = agentWebSearchProviderId.value;
|
||||
if (agentProviderId) {
|
||||
return webSearchProviders.value.some(p => p.id === agentProviderId);
|
||||
}
|
||||
|
||||
return webSearchProviders.value.some(p => p.is_default);
|
||||
});
|
||||
|
||||
const loadWebSearchConfig = async () => {
|
||||
try {
|
||||
const response: any = await getTenantWebSearchConfig();
|
||||
const config = response?.data;
|
||||
const configured = !!(config && config.provider);
|
||||
isWebSearchConfigured.value = configured;
|
||||
const response = await listWebSearchProviders();
|
||||
const providers = (response as any)?.data;
|
||||
webSearchProviders.value = Array.isArray(providers) ? providers : [];
|
||||
|
||||
if (!configured && settingsStore.isWebSearchEnabled) {
|
||||
if (!isWebSearchConfigured.value && settingsStore.isWebSearchEnabled) {
|
||||
settingsStore.toggleWebSearch(false);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to load web search config:', error);
|
||||
isWebSearchConfigured.value = false;
|
||||
webSearchProviders.value = [];
|
||||
if (settingsStore.isWebSearchEnabled) {
|
||||
settingsStore.toggleWebSearch(false);
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@
|
||||
<t-option v-for="pt in providerTypes" :key="pt.id" :value="pt.id" :label="pt.name">
|
||||
<div class="provider-option">
|
||||
<span>{{ pt.name }}</span>
|
||||
<t-tag v-if="pt.free" theme="success" size="small" variant="light">{{ t('webSearchSettings.free') }}</t-tag>
|
||||
<t-tag v-if="isProviderFree(pt)" theme="success" size="small" variant="light">{{ t('webSearchSettings.free') }}</t-tag>
|
||||
</div>
|
||||
</t-option>
|
||||
</t-select>
|
||||
@@ -113,7 +113,7 @@
|
||||
<div class="dialog-footer">
|
||||
<div class="footer-left">
|
||||
<t-button
|
||||
v-if="selectedProviderType && !selectedProviderType.free"
|
||||
v-if="selectedProviderType && !isProviderFree(selectedProviderType)"
|
||||
theme="default"
|
||||
variant="outline"
|
||||
:loading="testing"
|
||||
@@ -179,6 +179,10 @@ const selectedProviderType = computed(() => {
|
||||
return providerTypes.value.find(pt => pt.id === providerForm.value.provider)
|
||||
})
|
||||
|
||||
const isProviderFree = (providerType: WebSearchProviderTypeInfo) => {
|
||||
return !providerType.requires_api_key && !providerType.requires_engine_id
|
||||
}
|
||||
|
||||
// ===== Methods =====
|
||||
const onProviderTypeChange = () => {
|
||||
providerForm.value.parameters = {}
|
||||
|
||||
@@ -76,7 +76,8 @@ func isValidProviderType(provider types.WebSearchProviderType) bool {
|
||||
case types.WebSearchProviderTypeBing,
|
||||
types.WebSearchProviderTypeGoogle,
|
||||
types.WebSearchProviderTypeDuckDuckGo,
|
||||
types.WebSearchProviderTypeTavily:
|
||||
types.WebSearchProviderTypeTavily,
|
||||
types.WebSearchProviderTypeOllama:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
@@ -101,6 +102,10 @@ func validateProviderParameters(provider types.WebSearchProviderType, params typ
|
||||
if params.APIKey == "" {
|
||||
return fmt.Errorf("API key is required for Tavily provider")
|
||||
}
|
||||
case types.WebSearchProviderTypeOllama:
|
||||
if params.APIKey == "" {
|
||||
return fmt.Errorf("API key is required for Ollama provider")
|
||||
}
|
||||
case types.WebSearchProviderTypeDuckDuckGo:
|
||||
// No API key required
|
||||
}
|
||||
|
||||
@@ -965,6 +965,7 @@ func registerWebSearchProviders(registry *infra_web_search.Registry) {
|
||||
registry.Register("google", infra_web_search.NewGoogleProvider)
|
||||
registry.Register("bing", infra_web_search.NewBingProvider)
|
||||
registry.Register("tavily", infra_web_search.NewTavilyProvider)
|
||||
registry.Register("ollama", infra_web_search.NewOllamaProvider)
|
||||
}
|
||||
|
||||
// registerIMAdapterFactories registers adapter factories for each IM platform
|
||||
|
||||
@@ -0,0 +1,155 @@
|
||||
package web_search
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/logger"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
)
|
||||
|
||||
const (
|
||||
// defaultOllamaWebSearchURL is the hardcoded Ollama web search API URL.
|
||||
// Not configurable by tenants — prevents SSRF.
|
||||
defaultOllamaWebSearchURL = "https://ollama.com/api/web_search"
|
||||
defaultOllamaTimeout = 10 * time.Second
|
||||
defaultOllamaResults = 5
|
||||
maxOllamaResults = 10 // Ollama限制最多10个结果
|
||||
)
|
||||
|
||||
// OllamaProvider implements web search using Ollama Cloud API
|
||||
type OllamaProvider struct {
|
||||
client *http.Client
|
||||
baseURL string
|
||||
apiKey string
|
||||
}
|
||||
|
||||
// NewOllamaProvider creates a new Ollama web search provider from parameters.
|
||||
func NewOllamaProvider(params types.WebSearchProviderParameters) (interfaces.WebSearchProvider, error) {
|
||||
if params.APIKey == "" {
|
||||
return nil, fmt.Errorf("API key is required for Ollama provider")
|
||||
}
|
||||
client := &http.Client{
|
||||
Timeout: defaultOllamaTimeout,
|
||||
}
|
||||
return &OllamaProvider{
|
||||
client: client,
|
||||
baseURL: defaultOllamaWebSearchURL, // Hardcoded — not tenant-configurable
|
||||
apiKey: params.APIKey,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Name returns the provider name
|
||||
func (p *OllamaProvider) Name() string {
|
||||
return "ollama"
|
||||
}
|
||||
|
||||
// Search performs a web search using Ollama Cloud API
|
||||
func (p *OllamaProvider) Search(
|
||||
ctx context.Context,
|
||||
query string,
|
||||
maxResults int,
|
||||
includeDate bool,
|
||||
) ([]*types.WebSearchResult, error) {
|
||||
if len(query) == 0 {
|
||||
return nil, fmt.Errorf("query is empty")
|
||||
}
|
||||
|
||||
if maxResults <= 0 {
|
||||
maxResults = defaultOllamaResults
|
||||
}
|
||||
|
||||
// Ollama限制最多10个结果
|
||||
if maxResults > maxOllamaResults {
|
||||
maxResults = maxOllamaResults
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "[WebSearch][Ollama] query=%q maxResults=%d url=%s", query, maxResults, p.baseURL)
|
||||
req, err := p.buildRequest(ctx, query, maxResults)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
results, err := p.doSearch(ctx, req)
|
||||
if err != nil {
|
||||
logger.Warnf(ctx, "[WebSearch][Ollama] failed: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
logger.Infof(ctx, "[WebSearch][Ollama] returned %d results", len(results))
|
||||
return results, nil
|
||||
}
|
||||
|
||||
func (p *OllamaProvider) buildRequest(ctx context.Context, query string, maxResults int) (*http.Request, error) {
|
||||
requestBody := map[string]interface{}{
|
||||
"query": query,
|
||||
"max_results": maxResults,
|
||||
}
|
||||
|
||||
bodyBytes, err := json.Marshal(requestBody)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to marshal request body: %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", p.baseURL, bytes.NewReader(bodyBytes))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Set("Authorization", "Bearer "+p.apiKey)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("User-Agent", defaultUserAgentHeader)
|
||||
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func (p *OllamaProvider) doSearch(ctx context.Context, req *http.Request) ([]*types.WebSearchResult, error) {
|
||||
resp, err := p.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
logger.Warnf(ctx, "[WebSearch][Ollama] API returned status %d: %s", resp.StatusCode, string(body))
|
||||
return nil, fmt.Errorf("ollama API returned status %d: %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
var respData ollamaSearchResponse
|
||||
if err := json.Unmarshal(body, &respData); err != nil {
|
||||
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
|
||||
}
|
||||
|
||||
results := make([]*types.WebSearchResult, 0, len(respData.Results))
|
||||
for _, item := range respData.Results {
|
||||
results = append(results, &types.WebSearchResult{
|
||||
Title: item.Title,
|
||||
URL: item.URL,
|
||||
Snippet: item.Snippet,
|
||||
Content: item.Content,
|
||||
Source: "ollama",
|
||||
})
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// ollamaSearchResponse defines the response structure for Ollama web search API.
|
||||
type ollamaSearchResponse struct {
|
||||
Results []ollamaSearchResult `json:"results"`
|
||||
}
|
||||
|
||||
type ollamaSearchResult struct {
|
||||
Title string `json:"title"`
|
||||
URL string `json:"url"`
|
||||
Content string `json:"content"`
|
||||
Snippet string `json:"snippet"`
|
||||
}
|
||||
@@ -18,6 +18,7 @@ const (
|
||||
WebSearchProviderTypeGoogle WebSearchProviderType = "google"
|
||||
WebSearchProviderTypeDuckDuckGo WebSearchProviderType = "duckduckgo"
|
||||
WebSearchProviderTypeTavily WebSearchProviderType = "tavily"
|
||||
WebSearchProviderTypeOllama WebSearchProviderType = "ollama"
|
||||
)
|
||||
|
||||
// WebSearchProviderEntity represents a configured web search provider instance for a tenant.
|
||||
@@ -153,5 +154,12 @@ func GetWebSearchProviderTypes() []WebSearchProviderTypeInfo {
|
||||
Description: "Tavily Search API (requires API key)",
|
||||
DocsURL: "https://tavily.com/",
|
||||
},
|
||||
{
|
||||
ID: "ollama",
|
||||
Name: "Ollama Web Search",
|
||||
RequiresAPIKey: true,
|
||||
Description: "Ollama Cloud web search (requires Ollama API key)",
|
||||
DocsURL: "https://docs.ollama.com/capabilities/web-search",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user