From 721c4dea2e9d77a8c0b150ff9160360015e36e71 Mon Sep 17 00:00:00 2001 From: vsakkas Date: Sun, 26 Nov 2023 22:26:46 +0200 Subject: [PATCH] Add search parameter (#124) - Add search parameter to be able to disable searching the web --- README.md | 19 ++++++++++++++++--- pyproject.toml | 2 +- sydney/enums.py | 8 ++++++++ sydney/sydney.py | 18 +++++++++++++++++- tests/test_ask.py | 5 +++-- 5 files changed, 45 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 82a95ae..d72ec7a 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Sydney.py -[![Latest Release](https://img.shields.io/github/v/release/vsakkas/sydney.py.svg)](https://github.com/vsakkas/sydney.py/releases/tag/v0.18.2) +[![Latest Release](https://img.shields.io/github/v/release/vsakkas/sydney.py.svg)](https://github.com/vsakkas/sydney.py/releases/tag/v0.19.0) [![Python](https://img.shields.io/badge/python-3.9+-blue.svg)](https://www.python.org/downloads/) [![MIT License](https://img.shields.io/badge/license-MIT-blue)](https://github.com/vsakkas/sydney.py/blob/master/LICENSE) @@ -165,7 +165,6 @@ async with SydneyClient() as sydney: Both versions of the `ask` method support the same parameters. - ### Attachment It is also possible to provide a URL to an image as an attachment, which will be used as input together with the prompt: @@ -176,7 +175,6 @@ async with SydneyClient() as sydney: print(response) ``` - ### Web Context You can also provide the contents of a web page as additional context to be used along with the prompt: @@ -187,6 +185,21 @@ async with SydneyClient() as sydney: print(response) ``` +### Web Search + +It is possible to determine if Copilot can search the web for information to use in the results: + +```python +async with SydneyClient() as sydney: + response = await sydney.ask("When was Bing Chat released?", search=False) + print(response) +``` + +Searching the web is enabled by default. + +> [!NOTE] +> Web search cannot be disabled when the response is streamed. + ### Compose You can ask Copilot to compose different types of content, such emails, articles, ideas and more: diff --git a/pyproject.toml b/pyproject.toml index b005f7a..479d068 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "sydney-py" -version = "0.18.2" +version = "0.19.0" description = "Python Client for Copilot (formerly named Bing Chat), also known as Sydney." authors = ["vsakkas "] license = "MIT" diff --git a/sydney/enums.py b/sydney/enums.py index eea50a2..2ab92db 100644 --- a/sydney/enums.py +++ b/sydney/enums.py @@ -51,6 +51,14 @@ class DefaultOptions(Enum): EREDIRECTURL = "eredirecturl" +class NoSearchOptions(Enum): + """ + Options that are used to disable search access. + """ + + NOSEARCHALL = "nosearchall" + + class DefaultComposeOptions(Enum): """ Options that are used in all compose API requests to Copilot. diff --git a/sydney/sydney.py b/sydney/sydney.py index b077ad0..5e38a6c 100644 --- a/sydney/sydney.py +++ b/sydney/sydney.py @@ -33,6 +33,7 @@ DefaultComposeOptions, DefaultOptions, MessageType, + NoSearchOptions, ResultValue, ) from sydney.exceptions import ( @@ -120,6 +121,7 @@ async def _get_session(self, force_close: bool = False) -> ClientSession: def _build_ask_arguments( self, prompt: str, + search: bool, attachment_info: dict | None = None, context: str | None = None, ) -> dict: @@ -135,6 +137,10 @@ def _build_ask_arguments( if self.bing_cookies: options_sets.extend(option.value for option in CookieOptions) + # Build option sets based on whether search is allowed or not. + if not search: + options_sets.extend(option.value for option in NoSearchOptions) + image_url, original_image_url = None, None if attachment_info: image_url = BING_BLOB_URL + attachment_info["blobId"] @@ -299,6 +305,7 @@ async def _ask( context: str | None = None, citations: bool = False, suggestions: bool = False, + search: bool = True, raw: bool = False, stream: bool = False, compose: bool = False, @@ -336,7 +343,9 @@ async def _ask( if compose: request = self._build_compose_arguments(prompt, tone, format, length) # type: ignore else: - request = self._build_ask_arguments(prompt, attachment_info, context) + request = self._build_ask_arguments( + prompt, search, attachment_info, context + ) self.invocation_id += 1 await self.wss_client.send(as_json(request)) @@ -457,6 +466,7 @@ async def ask( context: str | None = None, citations: bool = False, suggestions: bool = False, + search: bool = True, raw: bool = False, ) -> str | dict | tuple[str | dict, list | None]: """ @@ -474,6 +484,8 @@ async def ask( Whether to return any cited text. Default is False. suggestions : bool, optional Whether to return any suggested user responses. Default is False. + search: bool, optional + Whether to allow searching the web. Default is True. raw : bool, optional Whether to return the entire response object in raw JSON format. Default is False. @@ -490,6 +502,7 @@ async def ask( context=context, citations=citations, suggestions=suggestions, + search=search, raw=raw, stream=False, compose=False, @@ -546,6 +559,7 @@ async def ask_stream( context=context, citations=citations, suggestions=suggestions, + search=True, raw=raw, stream=True, compose=False, @@ -609,6 +623,7 @@ async def compose( context=None, citations=False, suggestions=suggestions, + search=True, raw=raw, stream=False, compose=True, @@ -675,6 +690,7 @@ async def compose_stream( context=None, citations=False, suggestions=suggestions, + search=True, raw=raw, stream=True, compose=True, diff --git a/tests/test_ask.py b/tests/test_ask.py index 4150e54..a741656 100644 --- a/tests/test_ask.py +++ b/tests/test_ask.py @@ -50,7 +50,7 @@ async def test_ask_creative() -> bool: "Hello, this is Bing. Nice to meet you! 😊", "Hi, this is Bing. I'm happy to chat with you. 😊 What would you like to talk about?", "Hi, this is Bing. I'm a chat mode of Microsoft Bing that can help you with various tasks and queries. I can also generate creative content such as poems, stories, code, essays, songs, celebrity parodies, and more. What would you like to talk about? 🤗", - "Hi, this is Bing. I'm glad you're here. 😊 I can help you with various tasks, such as searching the web, creating graphic art, generating creative content, and more. Just ask me anything and I'll do my best to assist you. 🙌 What would you like to do today,?" + "Hi, this is Bing. I'm glad you're here. 😊 I can help you with various tasks, such as searching the web, creating graphic art, generating creative content, and more. Just ask me anything and I'll do my best to assist you. 🙌 What would you like to do today,?", ] async with SydneyClient(style="creative") as sydney: @@ -226,7 +226,8 @@ async def test_ask_logic_precise() -> bool: async with SydneyClient(style="precise") as sydney: response = await sydney.ask( - "I have 4 apples today. I ate 3 apples yesterday. How many apples do I have today?" + "I have 4 apples today. I ate 3 apples yesterday. How many apples do I have today?", + search=False, ) score = 0