集成ollama-ai

引入依赖

        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-starter-model-ollama</artifactId>
            <version>1.0.0-M7</version>
        </dependency>

配置ollama


spring:
  ai:
    ollama:
      baseUrl: http://xxx:11434
      chat:
        model: xxx
        options:
          max_tokens: xxx
          top_p: 0.9
          top_k: 40
          temperature: 0.7
# 设置客户端超时时间
  @Bean
    RestClient.Builder restClientBuilder(RestClientBuilderConfigurer restClientBuilderConfigurer) {
        RestClient.Builder builder = RestClient.builder()
                .requestFactory(ClientHttpRequestFactories.get(new ClientHttpRequestFactorySettings(Duration.ofMillis(60),
                        Duration.ofSeconds(60), (SslBundle)null)));
        return restClientBuilderConfigurer.configure(builder);
    }

客户端使用

@Slf4j
@Service
@RequiredArgsConstructor
public class OllamaTest {


    private final OllamaChatModel ollamaChatModel;

    public String call(String prompt){

        log.info("call:提示词: {}",prompt);
        String call = ollamaChatModel.call(prompt);
        log.info("call:返回结果: {}",response);
        return response;
    }
}
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐