leutu
2024-06-07 e72c7539be753d5d1692c450f931d90c01af4ade
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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<String, Object> 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));
    }
 
}