-
Notifications
You must be signed in to change notification settings - Fork 4.3k
feat: Add domain filtering options to web_search tool #2578
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
Open
MengAiDev
wants to merge
1
commit into
openai:main
from
MengAiDev:feature/web-search-domain-filtering
+174
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,25 @@ | ||
from openai import OpenAI | ||
|
||
client = OpenAI() | ||
|
||
# Example with domain filtering | ||
response = client.responses.create( | ||
model="gpt-4o", | ||
tools=[ | ||
{ | ||
"type": "web_search_preview", | ||
"user_location": { | ||
"type": "approximate", | ||
"country": "US", | ||
"city": "San Francisco", | ||
}, | ||
# Include only academic and official sources | ||
"include_domains": ["arxiv.org", "openai.com", "nature.com", "*.edu", "*.gov"], | ||
# Exclude social media and forums | ||
"exclude_domains": ["medium.com", "reddit.com", "quora.com"] | ||
} | ||
], | ||
input="Latest AI research papers", | ||
) | ||
|
||
print(response.output_text) |
This file contains hidden or 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 hidden or 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,61 @@ | ||
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. | ||
|
||
from __future__ import annotations | ||
|
||
import re | ||
from typing import List, Optional | ||
from typing_extensions import Literal | ||
|
||
__all__ = ["DomainValidator"] | ||
|
||
|
||
class DomainValidator: | ||
"""Utility class for validating domain formats.""" | ||
|
||
DOMAIN_PATTERN = re.compile( | ||
r'^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$' | ||
) | ||
|
||
WILDCARD_DOMAIN_PATTERN = re.compile( | ||
r'^\*(?:\.(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,})?$' | ||
) | ||
|
||
@classmethod | ||
def validate_domain(cls, domain: str) -> bool: | ||
"""Validate a single domain format. | ||
|
||
Args: | ||
domain: The domain to validate (e.g., "example.com" or "*.example.com") | ||
|
||
Returns: | ||
True if the domain format is valid, False otherwise | ||
""" | ||
if not domain or not isinstance(domain, str): | ||
return False | ||
|
||
# Check for wildcard domains | ||
if domain.startswith('*.'): | ||
return bool(cls.WILDCARD_DOMAIN_PATTERN.match(domain)) | ||
|
||
# Check for regular domains | ||
return bool(cls.DOMAIN_PATTERN.match(domain)) | ||
|
||
@classmethod | ||
def validate_domains(cls, domains: List[str]) -> List[str]: | ||
"""Validate a list of domains and return only valid ones. | ||
|
||
Args: | ||
domains: List of domains to validate | ||
|
||
Returns: | ||
List of valid domains | ||
""" | ||
if not domains: | ||
return [] | ||
|
||
valid_domains = [] | ||
for domain in domains: | ||
if cls.validate_domain(domain): | ||
valid_domains.append(domain) | ||
|
||
return valid_domains |
This file contains hidden or 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 hidden or 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 hidden or 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,62 @@ | ||
import pytest | ||
from openai._utils import DomainValidator | ||
|
||
|
||
class TestDomainValidator: | ||
def test_validate_valid_domains(self): | ||
"""Test validation of valid domain formats.""" | ||
valid_domains = [ | ||
"example.com", | ||
"sub.example.com", | ||
"openai.com", | ||
"arxiv.org", | ||
"nature.com", | ||
"*.example.com", | ||
"*.edu", | ||
"*.gov" | ||
] | ||
|
||
for domain in valid_domains: | ||
assert DomainValidator.validate_domain(domain), f"Domain {domain} should be valid" | ||
|
||
def test_validate_invalid_domains(self): | ||
"""Test validation of invalid domain formats.""" | ||
invalid_domains = [ | ||
"", | ||
"invalid", | ||
"example..com", | ||
".example.com", | ||
"example.", | ||
"example.com.", | ||
"https://example.com", | ||
"http://example.com", | ||
"example.com/path", | ||
"example.com?query=param", | ||
"*.invalid*", | ||
"*.", | ||
"*" | ||
] | ||
|
||
for domain in invalid_domains: | ||
assert not DomainValidator.validate_domain(domain), f"Domain {domain} should be invalid" | ||
|
||
def test_validate_domains_list(self): | ||
"""Test validation of a list of domains.""" | ||
mixed_domains = [ | ||
"example.com", # valid | ||
"invalid", # invalid | ||
"openai.com", # valid | ||
"https://bad.com", # invalid | ||
"*.edu", # valid | ||
"" # invalid | ||
] | ||
|
||
expected_valid = ["example.com", "openai.com", "*.edu"] | ||
actual_valid = DomainValidator.validate_domains(mixed_domains) | ||
|
||
assert actual_valid == expected_valid | ||
|
||
def test_validate_empty_domains_list(self): | ||
"""Test validation of an empty domains list.""" | ||
assert DomainValidator.validate_domains([]) == [] | ||
assert DomainValidator.validate_domains(None) == [] |
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.