Skip to content

Commit

Permalink
Add search parameter (#124)
Browse files Browse the repository at this point in the history
- Add search parameter to be able to disable searching the web
  • Loading branch information
vsakkas authored Nov 26, 2023
1 parent 45b2d4e commit 721c4de
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 7 deletions.
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# <img src="https://raw.githubusercontent.com/vsakkas/sydney.py/master/images/logo.svg" width="28px" /> 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)

Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
license = "MIT"
Expand Down
8 changes: 8 additions & 0 deletions sydney/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 17 additions & 1 deletion sydney/sydney.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
DefaultComposeOptions,
DefaultOptions,
MessageType,
NoSearchOptions,
ResultValue,
)
from sydney.exceptions import (
Expand Down Expand Up @@ -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:
Expand All @@ -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"]
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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]:
"""
Expand All @@ -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.
Expand All @@ -490,6 +502,7 @@ async def ask(
context=context,
citations=citations,
suggestions=suggestions,
search=search,
raw=raw,
stream=False,
compose=False,
Expand Down Expand Up @@ -546,6 +559,7 @@ async def ask_stream(
context=context,
citations=citations,
suggestions=suggestions,
search=True,
raw=raw,
stream=True,
compose=False,
Expand Down Expand Up @@ -609,6 +623,7 @@ async def compose(
context=None,
citations=False,
suggestions=suggestions,
search=True,
raw=raw,
stream=False,
compose=True,
Expand Down Expand Up @@ -675,6 +690,7 @@ async def compose_stream(
context=None,
citations=False,
suggestions=suggestions,
search=True,
raw=raw,
stream=True,
compose=True,
Expand Down
5 changes: 3 additions & 2 deletions tests/test_ask.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 721c4de

Please sign in to comment.