-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🔍 Added Bing Search Plugin #2298
Draft
itzraiss
wants to merge
1
commit into
danny-avila:main
Choose a base branch
from
itzraiss:Bing_Search_Plugin
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+201
−1
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
const { z } = require('zod'); | ||
const { Tool } = require('@langchain/core/tools'); | ||
const { getEnvironmentVariable } = require('@langchain/core/utils/env'); | ||
|
||
class BingSearch extends Tool { | ||
static lc_name() { | ||
return 'BingSearch'; | ||
} | ||
|
||
constructor(fields = {}) { | ||
super(fields); | ||
this.envVarApiKey = 'BING_SEARCH_API_KEY'; | ||
this.apiKey = fields.apiKey ?? getEnvironmentVariable(this.envVarApiKey); | ||
|
||
this.kwargs = fields?.kwargs ?? {}; | ||
this.name = 'bing'; | ||
this.description = | ||
'A search engine optimized for comprehensive, accurate, and trusted results. Useful for when you need to answer questions about current events.'; | ||
|
||
this.schema = z.object({ | ||
query: z.string().min(1).describe('The search query string.'), | ||
max_results: z | ||
.number() | ||
.min(1) | ||
.max(50) | ||
.optional() | ||
.describe('The maximum number of search results to return. Defaults to 10.'), | ||
market: z | ||
.string() | ||
.optional() | ||
.describe('The market where the results come from. Defaults to "en-US".'), | ||
}); | ||
} | ||
|
||
async _call(input) { | ||
const validationResult = this.schema.safeParse(input); | ||
if (!validationResult.success) { | ||
throw new Error(`Validation failed: ${JSON.stringify(validationResult.error.issues)}`); | ||
} | ||
|
||
const { query, max_results = 15 } = validationResult.data; | ||
|
||
try { | ||
const response = await fetch( | ||
`https://api.bing.microsoft.com/v7.0/search?q=${encodeURIComponent( | ||
query, | ||
)}&count=${max_results}`, | ||
{ | ||
method: 'GET', | ||
headers: { | ||
'Ocp-Apim-Subscription-Key': this.apiKey, | ||
'Content-Type': 'application/json', | ||
}, | ||
}, | ||
); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Request failed with status ${response.status}: ${response.statusText}`); | ||
} | ||
|
||
const json = await response.json(); | ||
|
||
if (json.error) { | ||
throw new Error(`Request failed: ${json.error.message}`); | ||
} | ||
|
||
const webPages = json.webPages?.value || []; | ||
const rankingResponse = json.rankingResponse?.value || []; | ||
const entities = json.entities?.value || []; | ||
const news = json.news?.value || []; | ||
const computation = json.computation?.value || []; | ||
const timeZone = json.timeZone?.value || []; | ||
const spellSuggestion = json.spellSuggestion?.value || ''; | ||
const translations = json.translations?.value || []; | ||
|
||
const results = { | ||
webPages, | ||
entities, | ||
news, | ||
computation, | ||
timeZone, | ||
translations, | ||
spellSuggestion, | ||
rankingResponse, | ||
}; | ||
|
||
return JSON.stringify(results); | ||
} catch (error) { | ||
throw new Error(`Request failed: ${error.message}`); | ||
} | ||
} | ||
} | ||
|
||
module.exports = BingSearch; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
--- | ||
title: 🔎 Bing Search | ||
description: How to set up and use the Bing Search Plugin, which allows you to query Bing with GPT's help. | ||
weight: -7 | ||
--- | ||
|
||
# Bing Search Plugin | ||
Through the plugins endpoint, you can use Bing Search for answers to your questions with assistance from GPT! To get started, you need to get a Bing Search API key. You can then define these as follows in your `.env` file: | ||
```env | ||
BING_SEARCH_API_KEY="...." | ||
``` | ||
|
||
## Create or login to your account on Azure Portal | ||
|
||
**1.** Visit [Azure Portal](https://azure.microsoft.com/en-us/) and click on `Get started` or `Try Azure for Free` to create an account and sign in. | ||
|
||
**2.** Choose pay per use or Azure Free with $200. | ||
|
||
![image](https://raw.githubusercontent.com/itzraiss/images/main/Captura%20de%20tela%202023-11-26%20151647.png?token=GHSAT0AAAAAACJ4TKEINPEOAV3LEPNPBDNCZLEKLAQ) | ||
|
||
## 1\. Access your control panel & Click on `Create a Resource`.. | ||
|
||
![image](https://raw.githubusercontent.com/itzraiss/images/main/Captura%20de%20tela%202023-11-26%20151706.png?token=GHSAT0AAAAAACJ4TKEJDXD7E76YLZEV52Z4ZLEKLCQ) | ||
|
||
## 2\. Search for `Bing Search V7` in the bar and press enter. | ||
|
||
![image](https://raw.githubusercontent.com/itzraiss/images/main/Captura%20de%20tela%202024-04-03%20110147.png) | ||
|
||
## 3\. Now, click on `Create` e select `Bing Search V7`. | ||
|
||
## 4\. Configure the basics settings and click on 'Review + create'.. | ||
|
||
**Pricing Tier (F1 is FREE)' Check out the prices of the service [here](https://www.microsoft.com/en-us/bing/apis/pricing).** | ||
|
||
![image](https://raw.githubusercontent.com/itzraiss/images/main/Captura%20de%20tela%202024-04-03%20110620.png) | ||
|
||
## 5\. Now, click on `Create`. | ||
|
||
![image](https://raw.githubusercontent.com/itzraiss/images/main/Captura%20de%20tela%202024-04-03%20111024.png) | ||
|
||
## 6\. You will be redirected to the resource dashboard. Click on `Keys & Endpoint` on the **left-hand** panel. | ||
|
||
![image](https://raw.githubusercontent.com/itzraiss/images/main/image.png) | ||
|
||
## 7\. Now, copy your key to your .env file | ||
|
||
![image](https://raw.githubusercontent.com/itzraiss/images/main/Captura%20de%20tela%202024-04-03%20111714.png) | ||
|
||
# Conclusion | ||
|
||
### Now, you will be able to conduct searches using Bing Search V7 in endpoint `Plugins & Assistants `. **Congratulations!** 🎉🎉 | ||
|
||
**Plugins:** | ||
|
||
![image](https://raw.githubusercontent.com/itzraiss/images/main/Captura%20de%20tela%202024-04-03%20113022.png) | ||
|
||
**Assistants:** | ||
|
||
![image](https://raw.githubusercontent.com/itzraiss/images/main/Captura%20de%20tela%202024-04-03%20113029.png) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The webPages object is a list of search hits with "snippets", really short extracts of the found pages [1].
It looks to me like this code will only pass back these snippets to the OpenAI Assistants API, and not the full page contents, or did I misunderstand?
Usually with the previous API, you would have to scrape each of the pages and extract important contents with readability.js before passing that back as the response.
[1] https://learn.microsoft.com/en-us/bing/search-apis/bing-web-search/search-responses#webpages-answer