Skip to content

Commit

Permalink
开源搜索接口部分代码
Browse files Browse the repository at this point in the history
  • Loading branch information
fatwang2 committed Apr 24, 2024
1 parent 6cc9197 commit f9175ea
Show file tree
Hide file tree
Showing 7 changed files with 757 additions and 253 deletions.
13 changes: 2 additions & 11 deletions README-EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,12 @@
<a href="https://www.buymeacoffee.com/fatwang2" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" style="height: 60px !important;width: 217px !important;" ></a>

# Version Updates
- V0.2.4, 20240424,support for Groq in Cloudflare Worker
- V0.2.5, 20240425, open source the code for the search api
- V0.2.4, 20240424, support for Groq in Cloudflare Worker
- V0.2.3, 20240423, support for Azure OpenAI in Cloudflare Worker. It also introduces the ability to use an authorization code and customize the user's request key.
- V0.2.2, 20240420, support Moonshot API
- V0.2.1, 20240310, supports Google, Bing, Duckduckgo, Search1API for news-type searches; supports adjusting the number of search results via the MAX_RESULTS environment variable; supports adjusting the number of in-depth searches desired via the CRAWL_RESULTS environment variable.
- V0.2.0,20240310,Optimized openai.js, cloudflare worker version, really faster this time!
- V0.1.9, 20240318, optimized the handling of streams in openai.js for faster speed, recommend updating; fixed the audio issue in the server deployment version; added a sponsored button on Github.
- V0.1.8, 20240305, support search1api search service, update Gemini version search variable configuration, open news search capability, add risk statement
- V0.1.7, 20240224, Gemini version supports streaming output and is compatible with vision model
- V0.1.6, 20240221, Supports Gemini model, can be temporarily configured through Cloudflare worker method
- V0.1.5, 20240205, supports news search, making it more convenient to quickly browse news
- V0.1.4, 20240120, Supports one-click deployment with Zeabur, very convenient, highly recommended!
- V0.1.3, 20240120, Supports local deployment, can be deployed on your own server

For more historical updates, please see [Version History](https://github.com/fatwang2/search2ai/releases)

Expand Down Expand Up @@ -124,9 +118,6 @@ This project provides some additional configuration options, which can be set th
| `AUTH_KEYS` | No | If you want users to define a separate authorization code as a key when making requests, you need to fill this in. Required if azure is selected | `000,1111,2222` |
| `OPENAI_API_KEY` | No | If you want users to define a separate authorization code as a key when requesting openai, you need to fill this in | `sk-xxx` |

# Risk statement
To ensure the persistence of this project, certain interface requests will be forwarded via [search1api](https://search.search2ai.one). Please be assured that this forwarding service does not save any private data.

# Future Iterations
- Fix streaming output issues in Vercel project
- Improve the speed of streaming output
Expand Down
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<a href="https://www.buymeacoffee.com/fatwang2" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" style="height: 60px !important;width: 217px !important;" ></a>

# 版本更新
- V0.2.5,20240425,为了解决隐私担忧,开源搜索接口部分的代码
- V0.2.4,20240424,支持 Groq 的llama-3、mistral等模型,速度起飞
- V0.2.3,20240423,Cloudflare Worker版本支持Azure OpenAI;支持授权码,可自定义用户的请求key
- V0.2.2,20240420,支持Moonshot的非流式模式
Expand Down Expand Up @@ -118,11 +119,6 @@ http://localhost:3014/v1/chat/completions
| `AUTH_KEYS` | No | 如果希望用户请求的时候单独定义授权码作为key,则需要填写,如选azure则必填| `000,1111,2222`|
| `OPENAI_API_KEY` | No | 如果希望用户请求openai的时候也单独定义授权码作为key,则需要填写| `sk-xxx`|



# 风险声明
为了确保本项目的持久运行,某些接口请求将通过[search1api](https://search.search2ai.one)进行转发。请放心,该转发服务不会保存任何隐私数据。

# 后续迭代
- 修复Vercel项目流式输出问题
- 提升流式输出的速度
Expand Down
146 changes: 111 additions & 35 deletions search2gemini.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,119 @@ addEventListener('fetch', event => {
'Access-Control-Max-Age': '86400', // 预检请求结果的缓存时间
};
async function search(query) {
console.log('search function started with query:', query);

try {
const url = "https://search.search2ai.one";
const headers= {
"Content-Type": "application/json",
"Authorization": typeof SEARCH1API_KEY !== 'undefined' ? `Bearer ${SEARCH1API_KEY}` : '',
"google_cx": typeof GOOGLE_CX !== 'undefined' ? GOOGLE_CX : '',
"google_key": typeof GOOGLE_KEY !== 'undefined' ? GOOGLE_KEY : '',
"serpapi_key": typeof SERPAPI_KEY !== 'undefined' ? SERPAPI_KEY : '',
"serper_key": typeof SERPER_KEY !== 'undefined' ? SERPER_KEY : '',
"bing_key": typeof BING_KEY !== 'undefined' ? BING_KEY : '',
"apibase": typeof APIBASE !== 'undefined' ? APIBASE : 'https://api.openai.com'
};
const body = {
"query": query,
search_service: SEARCH_SERVICE,
max_results: typeof MAX_RESULTS !== "undefined" ? MAX_RESULTS : "5",
crawl_results:typeof CRAWL_RESULTS !== "undefined" ? MAX_RESULTS : "0",
};

const response = await fetch(url, {
method: 'POST',
headers: headers,
body: JSON.stringify(body)
});

if (response.ok) {
console.log('search function successfully completed');
return response.json();
} else {
throw new Error("Unable to make request");
}
} catch (error) {
return {"status": "ERROR: " + error.message};
console.log(`正在使用 ${SEARCH_SERVICE} 进行自定义搜索: ${JSON.stringify(query)}`);
try {
let response;

switch (SEARCH_SERVICE) {
case "search1api":
response = await fetch("https://search.search2ai.one", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: typeof SEARCH1API_KEY !== "undefined" ? `Bearer ${SEARCH1API_KEY}` : "",
},
body: JSON.stringify({
query,
max_results: typeof MAX_RESULTS !== "undefined" ? MAX_RESULTS : "5",
crawl_results: typeof CRAWL_RESULTS !== "undefined" ? MAX_RESULTS : "0",
}),
});
break;

case "google":
const googleApiUrl = `https://www.googleapis.com/customsearch/v1?cx=${GOOGLE_CX}&key=${GOOGLE_KEY}&q=${encodeURIComponent(query)}`;
const googleResponse = await fetch(googleApiUrl);
const googleData = await googleResponse.json();
results = googleData.items.slice(0, MAX_RESULTS).map((item) => ({
title: item.title,
link: item.link,
snippet: item.snippet
}));
break;

case "bing":
const bingApiUrl = `https://api.bing.microsoft.com/v7.0/search?q=${encodeURIComponent(query)}`;
const bingResponse = await fetch(bingApiUrl, {
headers: { "Ocp-Apim-Subscription-Key": BING_KEY }
});
const bingData = await bingResponse.json();
results = bingData.webPages.value.slice(0, MAX_RESULTS).map((item) => ({
title: item.name,
link: item.url,
snippet: item.snippet
}));
break;

case "serpapi":
const serpApiUrl = `https://serpapi.com/search?api_key=${SERPAPI_KEY}&engine=google&q=${encodeURIComponent(query)}&google_domain=google.com`;
const serpApiResponse = await fetch(serpApiUrl);
const serpApiData = await serpApiResponse.json();
results = serpApiData.organic_results.slice(0, MAX_RESULTS).map((item) => ({
title: item.title,
link: item.link,
snippet: item.snippet
}));
break;

case "serper":
const gl = typeof GL !== "undefined" ? GL : "us";
const hl = typeof HL !== "undefined" ? HL : "en";
const serperApiUrl = "https://google.serper.dev/search";
const serperResponse = await fetch(serperApiUrl, {
method: "POST",
headers: {
"X-API-KEY": SERPER_KEY,
"Content-Type": "application/json"
},
body: JSON.stringify({ q: query, gl: gl, hl: hl })
});
const serperData = await serperResponse.json();
results = serperData.organic.slice(0, MAX_RESULTS).map((item) => ({
title: item.title,
link: item.link,
snippet: item.snippet
}));
break;

case "duckduckgo":
const duckDuckGoApiUrl = "https://ddg.search2ai.online/search";
const body = {
q: query,
max_results: typeof MAX_RESULTS !== "undefined" ? MAX_RESULTS : "5"
};
const duckDuckGoResponse = await fetch(duckDuckGoApiUrl, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(body)
});
const duckDuckGoData = await duckDuckGoResponse.json();
results = duckDuckGoData.results.map((item) => ({
title: item.title,
link: item.href,
snippet: item.body
}));
break;

default:
console.error(`不支持的搜索服务: ${SEARCH_SERVICE}`);
return `不支持的搜索服务: ${SEARCH_SERVICE}`;
}

const data = {
results: results
};

console.log('自定义搜索服务调用完成');
return JSON.stringify(data);

} catch (error) {
console.error(`在 search 函数中捕获到错误: ${error}`);
return `在 search 函数中捕获到错误: ${error}`;
}
}

async function parse_function_response(message) {
const function_call = message[0]["functionCall"];
Expand Down

0 comments on commit f9175ea

Please sign in to comment.