spring-ai icon indicating copy to clipboard operation
spring-ai copied to clipboard

feat: OpenAI Web Search Annotations

Open apappascs opened this issue 10 months ago • 1 comments

This PR adds support for retrieving web search annotations from the OpenAI API, as described in their web search documentation. This allows us to access citation URLs and their context within generated responses when using models like gpt-4o-search-preview.

Changes:

  • Added annotations (with Annotation and UrlCitation records) to ChatCompletionMessage in OpenAiApi.java.
  • Updated OpenAiChatModel to populate the annotations field (via metadata) for both regular and streaming responses.
  • Added integration tests (webSearchAnnotationsTest, streamWebSearchAnnotationsTest) to OpenAiChatModelIT.java.
  • Added GPT_4_O_SEARCH_PREVIEW and GPT_4_O_MINI_SEARCH_PREVIEW to OpenAiApi.ChatModel.
  • Added WebSearchOptions and related records to OpenAiApi.
  • Minor updates to ChatCompletionRequest and its Builder.

Resolves #2449

apappascs avatar Mar 13 '25 14:03 apappascs

As a temporary workaround, add this objectMapper to your main class

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        return mapper;
    }
}

zainlateef avatar Mar 16 '25 14:03 zainlateef

@apappascs Thanks very much for the PR! Rebased and merged as 6c9b4d6cd

ilayaperumalg avatar May 08 '25 18:05 ilayaperumalg