package com.yb;/* *@title OllamaApiTest *@description *@author yb *@version 1.0 *@create 2024/5/28 21:12 */ import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.json.JSONObject; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.nio.charset.Charset; import java.util.HashMap; import java.util.Map; @SpringBootTest public class OllamaApiTest { @Test public void test() throws Exception{ CloseableHttpClient client = HttpClients.createDefault(); //ollama 默认端口是 11434 HttpPost post = new HttpPost("http://192.168.11.104:11434/api/generate"); post.addHeader("Content-type","application/json; charset=utf-8"); //参数 Map map = new HashMap<>(); map.put("model","llama3:8b"); map.put("prompt","你好"); //不以流式返回 map.put("stream",false); StringEntity stringEntity = new StringEntity(JSONObject.valueToString(map),Charset.forName("UTF-8")); post.setEntity(stringEntity); CloseableHttpResponse response = client.execute(post); HttpEntity entity = response.getEntity(); System.out.println(EntityUtils.toString(entity)); } }