You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在LangChain4J-Spring中实现gpt-4o-search-preview模型的技术咨询

LangChain4J整合OpenAI gpt-4o-search-preview及设置web_search_options

1. 基础配置:指定目标模型

首先在SpringBoot配置文件(如application.properties)中设置模型名称:

langchain4j.open-ai.chat-model.model-name=gpt-4o-search-preview
langchain4j.open-ai.api-key=你的OpenAI API密钥

2. 传递web_search_options参数

LangChain4J通过extraOptions参数传递OpenAI API的自定义配置,你可以根据场景选择以下两种方式:

方式一:全局固定配置

创建自定义OpenAiChatModel Bean,全局设置web_search_options参数:

import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.openai.OpenAiChatModelBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Map;

@Configuration
public class OpenAiConfig {

    @Bean
    public OpenAiChatModel openAiChatModel() {
        return new OpenAiChatModelBuilder()
                .apiKey("你的OpenAI API密钥")
                .modelName("gpt-4o-search-preview")
                .extraOptions(Map.of(
                        "web_search_options", Map.of(
                                "user_location", Map.of(
                                        "type", "approximate",
                                        "approximate", Map.of(
                                                "country", "GB",
                                                "city", "London",
                                                "region", "London"
                                        )
                                )
                        )
                ))
                .build();
    }
}

然后在Assistant接口中指定使用该模型:

@AiService(model = "openAiChatModel")
public interface Assistant {
    @SystemMessage("{{systemMessage}}")
    Result<String> chat(@MemoryId long memoryId, @UserMessage String userMessage, @V("systemMessage") String systemMessage);
}

方式二:动态参数配置

如果需要根据每个请求动态调整web_search_options(比如根据用户位置信息),直接在业务代码中调用OpenAiChatModel的generate方法:

@Service
public class ChatAiService {

    @Autowired
    private OpenAiModerationModel moderationModel;

    @Autowired
    private OpenAiChatModel openAiChatModel;

    public String sendRequest(ChatAiDTO chatAiDTO) {
        String userInput = chatAiDTO.getInput();
        UserDTO loggedUser = LoginContext.getLoggedUser();
        String systemMessagePrompt = "You are an educated assistant. Answer me in HTML language.";
        String response = "";

        try {
            // 内容审核校验
            Response<Moderation> moderate = moderationModel.moderate(userInput);
            if (moderate.content().flagged()) {
                throw new ModerationException("Blocked by moderation!");
            }

            // 动态构建web_search_options(可根据用户数据灵活调整)
            Map<String, Object> webSearchOptions = Map.of(
                    "user_location", Map.of(
                            "type", "approximate",
                            "approximate", Map.of(
                                    "country", "GB",
                                    "city", "London",
                                    "region", "London"
                            )
                    )
            );

            // 组装对话消息
            List<ChatMessage> messages = List.of(
                    SystemMessage.from(systemMessagePrompt),
                    UserMessage.from(userInput)
            );

            // 调用模型并传递自定义参数
            AiMessage aiMessage = openAiChatModel.generate(
                    messages,
                    OpenAiChatModelOptions.builder()
                            .extraOptions(Map.of("web_search_options", webSearchOptions))
                            .build()
            ).content();

            response = aiMessage.text();

        } catch (ModerationException e) {
            response = "Blocked by moderation!";
        }
        return response;
    }
}

注意事项

  • 需使用LangChain4J 0.35.0及以上版本,以支持gpt-4o-search-preview模型和最新的OpenAI API参数。
  • 全局配置适合参数固定的场景,动态配置更适配个性化请求需求。

内容的提问来源于stack exchange,提问作者KenobiBastila

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 22:33:13