DeepSeekRequest.java

DeepSeekRequest.java  请求访问本地DeepSeek

package ai;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;


/**
 * deekseek api test
 * 
 * 
参数                         类型           必填     说明
model        string  是        模型名称(deepseek-chat)
prompt       string  是        输入提示词
stream       bool    否        是否启用流式输出(默认false)
temperature  float   否        生成多样性(0.1-1.0)
 * 
 * 
 * @author ZengWenFeng
 * @date 2025.03.01
 * @mobile 13805029595
 * @email 117791303@qq.com
 */
public class DeepSeekRequest
{
	public static void main(String[] args)
	{
		try
		{
			// 目标 URL
			URL url = new URL("http://127.0.0.1:11434/api/generate");
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();

			// 设置请求方法为 POST
			connection.setRequestMethod("POST");
			connection.setRequestProperty("Content-Type", "application/json; utf-8");
			connection.setRequestProperty("Accept", "application/json");
			connection.setDoOutput(true);

			// 请求体(JSON 格式) zengwenfeng  2025.03.01 01:40
			String jsonInputString = "{"
					                     + "\"prompt\": \"mp4是什么意思?\","
					                     + "\"model\": \"deepseek-r1:1.5b\""
					                     + "}";

			// 发送请求
			try (OutputStream os = connection.getOutputStream())
			{
				byte[] input = jsonInputString.getBytes("utf-8");
				os.write(input, 0, input.length);
			}

			// 获取响应
			int responseCode = connection.getResponseCode();
			System.out.println("Response Code: " + responseCode);

			if (responseCode == HttpURLConnection.HTTP_OK)
			{
				try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8")))
				{
					StringBuilder response = new StringBuilder();
					String responseLine;
					
					while ((responseLine = br.readLine()) != null)
					{
						response.append(responseLine.trim());
					}
					
					System.out.println("Response: " + response.toString());
				}
			}
			else
			{
				System.out.println("Request failed with response code: " + responseCode);
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.util.UriComponentsBuilder; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; @Service public class DeepSeekService { @Autowired private RestTemplate restTemplate; @Autowired private DeepSeekConfig deepSeekConfig; private final ObjectMapper objectMapper = new ObjectMapper(); public String askDeepSeek(String question) throws JsonProcessingException { DeepSeekRequest request = new DeepSeekRequest(); request.setModel("deepseek-chat"); request.setStream(false); List<DeepSeekRequest.Message> messages = List.of( new DeepSeekRequest.Message("user", question) ); request.setMessages(messages); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); headers.setAuthorization("Bearer " + deepSeekConfig.getApiKey()); HttpEntity<String> entity = new HttpEntity<>(objectMapper.writeValueAsString(request), headers); UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(deepSeekConfig.getApiUrl()); ResponseEntity<String> response = restTemplate.postForEntity(builder.toUriString(), entity, String.class); if (response.getStatusCode().is2xxSuccessful()) { DeepSeekResponse deepSeekResponse = objectMapper.readValue(response.getBody(), DeepSeekResponse.class); if (deepSeekResponse != null && deepSeekResponse.getChoices() != null && !deepSeekResponse.getChoices().isEmpty()) { return deepSeekResponse.getChoices().get(0).getDelta().getContent(); } } return "No valid response from DeepSeek"; } } import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.http.ResponseEntity; @RestController public class DeepSeekController { @Autowired private DeepSeekService deepSeekService; @GetMapping("/ask") public ResponseEntity<String> askDeepSeek(@RequestParam String question) { try { String response = deepSeekService.askDeepSeek(question); return ResponseEntity.ok(response); } catch (Exception e) { return ResponseEntity.status(500).body("Error occurred while communicating with DeepSeek: " + e.getMessage()); } } } 根据以上代码帮我生成一个Vue2的deepseek对话页面代码
04-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值