|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const BASE_URL = 'https://open.bigmodel.cn/api/paas/v4'; |
|
const KEY = process.env.GLM_KEY; |
|
|
|
export interface IWebSearchResult { |
|
index: number; |
|
icon: string; |
|
title: string; |
|
link: string; |
|
content: string; |
|
media: string; |
|
refer: string; |
|
[x: string]: any; |
|
} |
|
|
|
export async function webSearch(query: string): Promise<IWebSearchResult[]> { |
|
const tools = [ |
|
{ |
|
type: 'web_search', |
|
web_search: { |
|
enable: true, |
|
search_result: true, |
|
search_query: query, |
|
}, |
|
}, |
|
]; |
|
const msg = [ |
|
{ |
|
role: 'user', |
|
content: 'hi', |
|
}, |
|
]; |
|
|
|
try { |
|
const baseUrl = `${BASE_URL}/chat/completions`; |
|
const res = await fetch(baseUrl, { |
|
method: 'POST', |
|
headers: { |
|
'Content-Type': 'application/json', |
|
Authorization: `Bearer ${KEY}`, |
|
}, |
|
body: JSON.stringify({ |
|
model: 'glm-4-flash', |
|
messages: msg, |
|
stream: false, |
|
tools, |
|
}), |
|
}); |
|
|
|
const data = await res.json(); |
|
return data?.web_search ?? []; |
|
} catch (error) { |
|
console.error(error); |
|
return []; |
|
} |
|
} |
|
|
|
export async function webSearchPro(query: string): Promise<IWebSearchResult[]> { |
|
const tool = 'web-search-pro'; |
|
const msg = [ |
|
{ |
|
role: 'user', |
|
content: query, |
|
}, |
|
]; |
|
|
|
try { |
|
const baseUrl = `${BASE_URL}/tools`; |
|
const res = await fetch(baseUrl, { |
|
method: 'POST', |
|
headers: { |
|
'Content-Type': 'application/json', |
|
Authorization: `Bearer ${KEY}`, |
|
}, |
|
body: JSON.stringify({ |
|
messages: msg, |
|
stream: false, |
|
tool, |
|
}), |
|
}); |
|
|
|
const data = await res.json(); |
|
const result = data.choices[0]?.message?.tool_calls; |
|
return result?.[0] ?? []; |
|
} catch (error) { |
|
console.error(error); |
|
return []; |
|
} |
|
} |
|
|