通过结构化 SERP API(而不是原始 Web 获取)路由查询,通过 Web 搜索奠定本地 LLM(Ollama、LM Studio、vLLM)的基础。结构化 JSON 使用 600-800 个标记,而原始 HTML 使用 4,000-8,000 个标记,更适合有限的本地模型上下文窗口。
前置条件
- Ollama 或 LM Studio 本地运行
- Scavio API 密钥
- Python 3.8+
- 支持函数调用的模型(例如 llama3、mistral)
操作指南
步骤 1: 创建搜索功能
构建一个返回结构化结果的搜索工具。
Python
import requests, os
def web_search(query, platform='google'):
resp = requests.post('https://api.scavio.dev/api/v1/search',
headers={'x-api-key': os.environ['SCAVIO_API_KEY'],
'Content-Type': 'application/json'},
json={'query': query, 'country_code': 'us', 'platform': platform})
data = resp.json()
return [{'title': r.get('title', ''), 'snippet': r.get('snippet', ''),
'url': r.get('link', '')}
for r in data.get('organic_results', [])[:5]]步骤 2: 与奥拉玛整合
使用搜索功能作为 Ollama 对话中的工具。
Python
import ollama
def grounded_query(question):
search_results = web_search(question)
context = '\n'.join([f"- {r['title']}: {r['snippet']}" for r in search_results])
response = ollama.chat(model='llama3', messages=[{
'role': 'user',
'content': f'Based on these search results:\n{context}\n\nAnswer: {question}'
}])
return response['message']['content']
answer = grounded_query('What is the latest Next.js version?')
print(answer)Python 示例
Python
import requests, os, ollama
def web_search(query):
r = requests.post('https://api.scavio.dev/api/v1/search',
headers={'x-api-key': os.environ['SCAVIO_API_KEY'],
'Content-Type': 'application/json'},
json={'query': query, 'country_code': 'us'}).json()
return [{'title': r.get('title', ''), 'snippet': r.get('snippet', '')}
for r in r.get('organic_results', [])[:5]]
def grounded_chat(question, model='llama3'):
results = web_search(question)
context = '\n'.join([f'- {r["title"]}: {r["snippet"]}' for r in results])
response = ollama.chat(model=model, messages=[{
'role': 'system',
'content': 'Answer based on the provided search results. Cite sources.'
}, {
'role': 'user',
'content': f'Search results:\n{context}\n\nQuestion: {question}'
}])
return response['message']['content']
print(grounded_chat('What is the current Stripe API version?'))JavaScript 示例
JavaScript
const H = {'x-api-key': process.env.SCAVIO_API_KEY, 'Content-Type': 'application/json'};
async function webSearch(query) {
const r = await fetch('https://api.scavio.dev/api/v1/search', {
method: 'POST', headers: H,
body: JSON.stringify({query, country_code: 'us'})
}).then(r => r.json());
return (r.organic_results || []).slice(0, 5).map(r => ({
title: r.title, snippet: r.snippet || ''
}));
}
async function groundedChat(question) {
const results = await webSearch(question);
const context = results.map(r => `- ${r.title}: ${r.snippet}`).join('\n');
// Pass context + question to your local LLM via its API
console.log(`Grounding context (${context.length} chars) for: ${question}`);
return context;
}
groundedChat('What is the current Next.js version?');预期输出
JSON
Local LLM responses grounded in current web data. The model cites search results instead of relying on potentially outdated training data.