ollama请求文档

https://ollama.readthedocs.io/api/#_1

基础request、response类

request类

  /**
     * 模型名称
     */
    private String model;

    /**
     * 对话内容
     */
    private String prompt;

    /**
     * 返回格式
     */
    private String format;

    /**
     * 上下文
     */
    private List<Integer> context;

	//其余请求参数可自行根据文档添加

response类

 private String model;

    @JsonProperty("created_at")
    private String create_at;

    private String response;

    private boolean done;

    @JsonProperty("done_reason")
    private String done_reason;

    /**
     * 响应的对话编码,下一个请求发送保持对话记忆
     */
    private List<Long> context;

    /**
     * 响应花费时间  纳秒
     */
    @JsonProperty("eval_duration")
    private Long eval_duration;


    /**
     * 响应令牌数
     */
    @JsonProperty("eval_count")
    private Long eval_count;

    /**
     * 评估提示所花费的时间 纳秒
     */
    @JsonProperty("prompt_eval_duration")
    private Long prompt_eval_duration;

    /**
     * 提示中的令牌书
     */
    @JsonProperty("prompt_eval_count")
    private Long prompt_eval_count;

    /**
     * 加载模型所花费的时间
     */
    @JsonProperty("load_duration")
    private Long load_duration;

    /**
     * 生成响应所花费的时间
     */
    @JsonProperty("total_duration")
    private long total_duration;

    private String error;

流式请求方法

 public void getDeepSeekResponse(String sid, Session session, String message) {
        // 流处理
        HttpClient client = HttpClient.newBuilder()
                .connectTimeout(Duration.ofMinutes(10)) // 设置连接超时时间为10分钟
                .build();
        BaseOllamaRequestData bInstance = JSON.parseObject(message,BaseOllamaRequestData.class);
        log.info("请求AI参数:{}",JSON.toJSONString(bInstance));
               HttpRequest request = HttpRequest.newBuilder()
               //此处为ollama安装的地址
                .uri(URI.create("http://x.x.x.x:11434/api/generate"))
                .header("Content-Type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(JSON.toJSONString(bInstance)))
                .build();

        client.sendAsync(request, HttpResponse.BodyHandlers.ofInputStream())
                .thenApply(HttpResponse::body)
                .thenAccept(inputStream -> {
                    try (InputStream is = inputStream;
                         JsonParser jsonParser = jsonFactory.createParser(is)) {
                        while (jsonParser.nextToken() != null) {
                            if (jsonParser.currentToken() == JsonToken.START_OBJECT) {
                                BaseOllamaResponseData responseData = objectMapper.readValue(jsonParser, BaseOllamaResponseData.class);
                                log.info("读取到的数据--转换后:{},{}", sid, JSON.toJSONString(responseData));
                                //此处可对消息信息处理,我这边选择是通过websocket发送到前端
                                sendBackMessage(sid, session, JSON.toJSONString(responseData));
                            }
                        }
                    } catch (Exception e) {
                        log.error("请求AI错误:{}", e.getMessage());
                        sendBackMessage(sid, session, JSON.toJSONString(new BaseOllamaResponseData(true)));
                    }
                })
                .join();

    }

PS:此处仅展示基础方法,如需契合自身业务,需自行改动

END

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐