Hermes 是一个流行的针对代理用例的微调模型系列,但与所有法学硕士一样,当被问及培训截止后的事件时,它会产生幻觉。通过 Scavio API 添加搜索基础,Hermes 可以在推理过程中访问实时网络数据。本教程将搜索工具集成到 Hermes Agent 的工具使用循环中,因此代理可以自主决定何时进行搜索并将结果合并到其响应中。每次搜索费用为 0.005 美元。
前置条件
- 已安装 Python 3.9+
- 请求已安装库
- 来自 scavio.dev 的 Scavio API 密钥
- Hermes 模型通过 Ollama、vLLM 或类似工具运行
操作指南
步骤 1: 构建搜索落地功能
创建一个搜索函数,返回适合包含在 Hermes 上下文窗口中的格式化结果。
import os, requests, json
SCAVIO_KEY = os.environ['SCAVIO_API_KEY']
H = {'x-api-key': SCAVIO_KEY, 'Content-Type': 'application/json'}
URL = 'https://api.scavio.dev/api/v1/search'
def grounded_search(query: str, num: int = 5) -> str:
"""Search the web and format results for LLM context."""
resp = requests.post(URL, headers=H,
json={'query': query, 'country_code': 'us', 'num_results': num})
resp.raise_for_status()
results = resp.json().get('organic_results', [])
if not results:
return 'No search results found.'
formatted = []
for i, r in enumerate(results, 1):
formatted.append(f'[{i}] {r["title"]}')
if r.get('snippet'):
formatted.append(f' {r["snippet"]}')
formatted.append(f' Source: {r["link"]}')
return '\n'.join(formatted)
results = grounded_search('Hermes 3 model capabilities 2026')
print(results[:400])步骤 2: 定义 Hermes 的工具架构
Hermes 使用 ChatML 工具使用格式。使用正确的架构定义搜索工具,以便 Hermes 知道何时以及如何调用它。
HERMES_TOOLS = [
{
'type': 'function',
'function': {
'name': 'web_search',
'description': 'Search the web for current information. Use this tool when you need up-to-date data, facts about recent events, or information beyond your training cutoff.',
'parameters': {
'type': 'object',
'properties': {
'query': {
'type': 'string',
'description': 'The search query. Be specific and include the year 2026 for recent information.'
}
},
'required': ['query']
}
}
}
]
def format_tools_for_hermes(tools: list) -> str:
"""Format tools for Hermes ChatML system prompt."""
tool_descriptions = []
for t in tools:
fn = t['function']
tool_descriptions.append(
f'Tool: {fn["name"]}\n'
f'Description: {fn["description"]}\n'
f'Parameters: {json.dumps(fn["parameters"], indent=2)}'
)
return '\n\n'.join(tool_descriptions)
print(format_tools_for_hermes(HERMES_TOOLS))步骤 3: 将搜索连入 Hermes 推理循环
将搜索工具集成到 Hermes 对话循环中。当Hermes输出工具调用时,执行搜索并反馈结果。
import ollama
def run_hermes_with_search(prompt: str, model: str = 'hermes3') -> str:
messages = [{'role': 'user', 'content': prompt}]
response = ollama.chat(model=model, messages=messages, tools=HERMES_TOOLS)
# Check if Hermes wants to use tools
if response.message.tool_calls:
for tc in response.message.tool_calls:
if tc.function.name == 'web_search':
query = tc.function.arguments.get('query', prompt)
search_results = grounded_search(query)
print(f'Hermes searched: "{query}"')
# Feed results back
messages.append(response.message)
messages.append({'role': 'tool', 'content': search_results})
final = ollama.chat(model=model, messages=messages)
return final.message.content
return response.message.content
result = run_hermes_with_search('What are the latest developments in AI regulation in 2026?')
print(f'\nHermes response:\n{result[:500]}')Python 示例
import os, requests
SCAVIO_KEY = os.environ['SCAVIO_API_KEY']
H = {'x-api-key': SCAVIO_KEY, 'Content-Type': 'application/json'}
def grounded_search(query, num=5):
resp = requests.post('https://api.scavio.dev/api/v1/search', headers=H,
json={'query': query, 'country_code': 'us', 'num_results': num})
results = resp.json().get('organic_results', [])
return '\n'.join(f'[{i+1}] {r["title"]}: {r.get("snippet", "")}' for i, r in enumerate(results))
def hermes_search(prompt):
# Simulate Hermes deciding to search
results = grounded_search(prompt)
print(f'Search grounding for Hermes:')
print(results[:400])
print(f'\nCost: $0.005')
hermes_search('AI regulation developments 2026')JavaScript 示例
const SCAVIO_KEY = process.env.SCAVIO_API_KEY;
async function groundedSearch(query) {
const resp = await fetch('https://api.scavio.dev/api/v1/search', {
method: 'POST',
headers: { 'x-api-key': SCAVIO_KEY, 'Content-Type': 'application/json' },
body: JSON.stringify({ query, country_code: 'us', num_results: 5 })
});
const data = await resp.json();
return (data.organic_results || []).map((r, i) => `[${i + 1}] ${r.title}: ${r.snippet || ''}`).join('\n');
}
async function hermesSearch(prompt) {
const results = await groundedSearch(prompt);
console.log('Search grounding for Hermes:');
console.log(results.slice(0, 400));
console.log('Cost: $0.005');
}
hermesSearch('AI regulation 2026');预期输出
Hermes searched: "AI regulation developments 2026"
[1] EU AI Act Enforcement Begins: What Companies Need to Know
The EU AI Act officially entered enforcement in March 2026...
Source: https://reuters.com/technology/eu-ai-act-enforcement
[2] US Congress Passes Bipartisan AI Safety Bill
The American AI Safety Act of 2026 introduces mandatory...
Source: https://nytimes.com/2026/04/us-ai-safety-bill
[3] China Updates AI Governance Framework
China's Cyberspace Administration released updated guidelines...
Source: https://scmp.com/tech/china-ai-governance
Cost: $0.005