Skip to content

OpenAI Plugin Now Supports Web Search Functionality #1569

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

Merged
merged 2 commits into from
Jul 4, 2025

Conversation

ksylvan
Copy link
Collaborator

@ksylvan ksylvan commented Jul 4, 2025

OpenAI Plugin Now Supports Web Search Functionality

Summary

This pull request adds web search functionality to the OpenAI plugin, expanding the existing search capability beyond Anthropic models to include OpenAI models. The implementation includes proper citation handling and formatting for search results.

Screenshots

These are both using gpt-4o as the default model

Before Change

image

After Change

image

Files Changed

cli/flags.go

  • Modified: Updated the description for the Search flag to reflect that it now supports both Anthropic and OpenAI models

plugins/ai/openai/openai.go

  • Modified: Added web search tool integration and citation extraction functionality
  • Added: Import for fmt package to support citation formatting
  • Enhanced: buildResponseParams method to include web search tool when enabled
  • Enhanced: extractText method to parse and format citations from search results

plugins/ai/openai/openai_test.go

  • Modified: Added comprehensive test coverage for the new web search functionality
  • Added: Imports for strings and responses packages
  • Added: Multiple test functions covering search scenarios and citation formatting

Code Changes

Web Search Tool Integration

The buildResponseParams method now includes web search tool configuration:

// Add web search tool if enabled
if opts.Search {
    webSearchTool := responses.ToolParamOfWebSearchPreview("web_search_preview")

    // Add user location if provided
    if opts.SearchLocation != "" {
        webSearchTool.OfWebSearchPreview.UserLocation = responses.WebSearchToolUserLocationParam{
            Type:     "approximate",
            Timezone: openai.String(opts.SearchLocation),
        }
    }

    ret.Tools = []responses.ToolUnionParam{webSearchTool}
}

Citation Extraction and Formatting

The extractText method has been significantly enhanced to handle citations:

func (o *Client) extractText(resp *responses.Response) (ret string) {
    var textParts []string
    var citations []string
    citationMap := make(map[string]bool) // To avoid duplicate citations

    // Extract text and citations from response
    // Format citations as markdown links under "Sources" section
}

Reason for Changes

This enhancement brings feature parity between the Anthropic and OpenAI plugins by adding web search capabilities to OpenAI models. The implementation allows users to leverage real-time web search results when using OpenAI models, with proper citation formatting for transparency and reference.

Impact of Changes

Positive Impacts

  • Feature Parity: OpenAI users now have access to web search functionality previously only available with Anthropic models
  • Enhanced User Experience: Search results include properly formatted citations for easy reference
  • Location Awareness: Users can specify search location for more relevant results
  • Duplicate Prevention: Citation deduplication ensures clean, readable output

Potential Risks

  • API Compatibility: The implementation relies on OpenAI's web search preview feature, which may change or be deprecated
  • Response Parsing: The citation extraction logic assumes specific response structure from OpenAI's API
  • Performance: Additional processing for citation extraction may slightly impact response time

Test Plan

The changes include comprehensive test coverage:

  1. Unit Tests: Test web search tool configuration with and without location parameters
  2. Citation Tests: Verify proper citation extraction, formatting, and deduplication
  3. Integration Tests: Ensure backward compatibility when search is disabled
  4. Edge Cases: Test handling of duplicate citations and empty citation lists

Run the test suite with:

go test ./plugins/ai/openai/...

Additional Notes

  • The web search feature uses OpenAI's preview API, which may require specific API access or model versions
  • Citation formatting follows markdown standards for consistency with existing output
  • The implementation maintains backward compatibility - existing functionality remains unchanged when search is disabled
  • Error handling for malformed citations or API responses should be monitored in production use

Breaking Changes: None - all changes are additive and backward compatible.

Dependencies: Requires OpenAI Go SDK with web search preview support.

…matting

## CHANGES

- Enable web search tool for OpenAI models
- Add location parameter support for search results
- Extract and format citations from search responses
- Implement citation deduplication to avoid duplicates
- Add comprehensive test coverage for search functionality
- Update CLI flag description to include OpenAI
- Format citations as markdown links with sources
@ksylvan ksylvan requested a review from Copilot July 4, 2025 07:11
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR extends the OpenAI plugin to support real-time web search results (with optional location) and formats citations in responses, while updating the CLI flag and adding corresponding tests.

  • Enhanced buildResponseParams to include a web search tool when enabled, supporting user-specified locations
  • Implemented extractText citation parsing and markdown formatting
  • Updated CLI flag description and added comprehensive unit tests for search integration and citation logic

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
cli/flags.go Updated Search flag description to indicate support for both Anthropic and OpenAI models
plugins/ai/openai/openai.go Integrated web search tool in buildResponseParams and added citation extraction/formatting in extractText
plugins/ai/openai/openai_test.go Added tests for buildResponseParams variations and for citation formatting/deduplication
Comments suppressed due to low confidence (3)

plugins/ai/openai/openai.go:187

  • Assigning ret.Tools directly will override any existing tools if more are added later; consider using append(ret.Tools, webSearchTool) instead of replacing the slice.
	if opts.Search {

plugins/ai/openai/openai_test.go:132

  • Add a test case for extractText when no citations are present to verify that the '## Sources' section is not appended.
func TestCitationFormatting(t *testing.T) {

plugins/ai/openai/openai.go:276

  • Breaking out after the first message item may skip additional 'message' entries; consider removing the break to ensure all output items are processed.
			break

@ksylvan ksylvan merged commit fc8c4ba into danielmiessler:main Jul 4, 2025
1 check passed
@ksylvan ksylvan deleted the 0703-openai-web-search branch July 4, 2025 07:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant