網(wǎng)站 微信認(rèn)證百度搜索引擎營銷如何實現(xiàn)
在當(dāng)今科技飛速發(fā)展的時代,人工智能領(lǐng)域不斷涌現(xiàn)出令人矚目的創(chuàng)新成果,其中DeepSeek模型無疑成為了眾多關(guān)注焦點。它憑借著先進(jìn)的技術(shù)和卓越的性能,在行業(yè)內(nèi)掀起了一股熱潮,吸引了無數(shù)目光。然而,如同許多前沿技術(shù)在發(fā)展初期所面臨的挑戰(zhàn)一樣,DeepSeek模型在實際應(yīng)用中也暴露出了一系列亟待解決的問題,這些問題猶如一道道屏障,限制了其更廣泛的應(yīng)用和深入的發(fā)展。而訊飛開放平臺,憑借其強(qiáng)大的實力和豐富的經(jīng)驗,正致力于為解決這些痛點提供全面而有效的解決方案。
首先,服務(wù)不穩(wěn)定是DeepSeek模型當(dāng)前面臨的一個突出問題。在實際應(yīng)用中,用戶對于服務(wù)的穩(wěn)定性有著極高的要求。無論是企業(yè)的生產(chǎn)運(yùn)營,還是個人的日常使用,都希望能夠獲得持續(xù)、穩(wěn)定的服務(wù)支持。然而,DeepSeek模型由于各種因素的影響,時常出現(xiàn)服務(wù)波動的情況,這給用戶帶來了極大的困擾。比如,在進(jìn)行重要任務(wù)處理時,突然的服務(wù)中斷可能導(dǎo)致數(shù)據(jù)丟失、工作延誤等一系列嚴(yán)重后果。訊飛開放平臺深知服務(wù)穩(wěn)定性的重要性,通過優(yōu)化自身的技術(shù)架構(gòu)和服務(wù)體系,采用先進(jìn)的云計算技術(shù)和分布式存儲方案,確保平臺的高可用性和可靠性。同時,訊飛還建立了完善的監(jiān)控和預(yù)警機(jī)制,能夠?qū)崟r監(jiān)測服務(wù)的運(yùn)行狀態(tài),及時發(fā)現(xiàn)并解決潛在的問題,為用戶的使用提供了堅實的保障。
HTTP調(diào)用源代碼:
?
package day;import com.google.gson.Gson;
import org.json.JSONArray;
import org.json.JSONObject;import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.List;public class MaasApiClient {public static Gson gson = new Gson();public static void main(String[] args) {try {URL url = new URL("http://maas-api.cn-huabei-1.xf-yun.com/v1/chat/completions");HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 設(shè)置請求頭connection.setRequestMethod("POST");connection.setRequestProperty("Content-Type", "application/json");connection.setRequestProperty("Authorization", "Bearer ");connection.setDoOutput(true);connection.setChunkedStreamingMode(0); // 啓用分塊傳輸// 構(gòu)建JSON請求體JSONObject requestBody = new JSONObject();requestBody.put("model", "xdeepseekr1");JSONArray messages = new JSONArray();JSONObject message = new JSONObject();message.put("role", "user");message.put("content", "你是誰?");messages.put(message);requestBody.put("messages", messages);requestBody.put("temperature", 0.01);requestBody.put("stream", true);requestBody.put("max_tokens", 4096);// 發(fā)送請求try (OutputStream os = connection.getOutputStream()) {byte[] input = requestBody.toString().getBytes(StandardCharsets.UTF_8);os.write(input, 0, input.length);}// 處理流式返回try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {String line;while ((line = reader.readLine()) != null) {// System.out.println(line);if (!line.contains("[DONE]")) {line = line.replace("data: ", "");// System.out.println(line);JsonParse jsonParse = gson.fromJson(line, JsonParse.class);if (!line.isEmpty()) {List<Choices> choicesList = jsonParse.choices;for (Choices temp : choicesList) {System.out.print(temp.delta.reasoning_content);}}}}}connection.disconnect();} catch (Exception e) {e.printStackTrace();}}
}class JsonParse {List<Choices> choices;
}class Choices {Delta delta;
}class Delta {String reasoning_content;
}
Webscoket調(diào)用源代碼:
package org.example;import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import okhttp3.*;import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;public class BigModelNew extends WebSocketListener {// 以下信息從服務(wù)管控頁面獲取:https://training.xfyun.cn/modelServicepublic static final String hostUrl = "wss://maas-api.cn-huabei-1.xf-yun.com/v1.1/chat"; // 服務(wù)地址public static final String appid = "";public static final String apiSecret = "";public static final String apiKey = "";public static final String patch_id = "0"; //調(diào)用微調(diào)大模型時必傳,否則不傳。對應(yīng)為模型服務(wù)卡片上的resourceIdpublic static final String domain = "xdeepseekr1"; //從服務(wù)管控獲取要訪問服務(wù)的serviceID:https://training.xfyun.cn/modelServicepublic static List<RoleContent> historyList = new ArrayList<>(); // 對話歷史存儲集合public static String totalAnswer = ""; // 大模型的答案匯總 你是滿血671b版本嗎// 環(huán)境治理的重要性 環(huán)保 人口老齡化 我愛我的祖國 你是誰 明天合肥的天氣 我的名字叫大王 我叫什么名字// 點評一下安徽的發(fā)展和經(jīng)濟(jì),以及工資和發(fā)展的差距,說的要直接,可以public static String NewQuestion = "";public static final Gson gson = new Gson();// 個性化參數(shù)private String userId;private Boolean wsCloseFlag;private static Boolean totalFlag = true; // 控制提示用戶是否輸入// 構(gòu)造函數(shù)public BigModelNew(String userId, Boolean wsCloseFlag) {this.userId = userId;this.wsCloseFlag = wsCloseFlag;}// 主函數(shù)public static void main(String[] args) throws Exception {// 個性化參數(shù)入口,如果是并發(fā)使用,可以在這里模擬while (true) {if (totalFlag) {Scanner scanner = new Scanner(System.in);System.out.print("我:");totalFlag = false;NewQuestion = scanner.nextLine();// 構(gòu)建鑒權(quán)urlString authUrl = getAuthUrl(hostUrl, apiKey, apiSecret);OkHttpClient client = new OkHttpClient.Builder().build();String url = authUrl.toString().replace("http://", "ws://").replace("https://", "wss://");Request request = new Request.Builder().url(url).build();for (int i = 0; i < 1; i++) {totalAnswer = "";WebSocket webSocket = client.newWebSocket(request, new BigModelNew(i + "", false));}} else {Thread.sleep(200);}}}public static boolean canAddHistory() { // 由于歷史記錄最大上線1.2W左右,需要判斷是能能加入歷史int history_length = 0;for (RoleContent temp : historyList) {history_length = history_length + temp.content.length();}if (history_length > 12000) {historyList.remove(0);historyList.remove(1);historyList.remove(2);historyList.remove(3);historyList.remove(4);return false;} else {return true;}}// 線程來發(fā)送音頻與參數(shù)class MyThread extends Thread {private WebSocket webSocket;public MyThread(WebSocket webSocket) {this.webSocket = webSocket;}public void run() {try {JSONObject requestJson = new JSONObject();String[] arr = new String[1];arr[0] = patch_id;JSONObject header = new JSONObject(); // header參數(shù)header.put("app_id", appid);header.put("uid", UUID.randomUUID().toString().substring(0, 10));header.put("patch_id", arr);JSONObject parameter = new JSONObject(); // parameter參數(shù)JSONObject chat = new JSONObject();chat.put("domain", domain); //調(diào)用微調(diào)大模型時,對應(yīng)為模型服務(wù)卡片上的serviceidchat.put("temperature", 0.5);chat.put("max_tokens", 4096); //請根據(jù)不同模型支持范圍,適當(dāng)調(diào)整該值的大小parameter.put("chat", chat);JSONObject payload = new JSONObject(); // payload參數(shù)JSONObject message = new JSONObject();JSONArray text = new JSONArray();// 歷史問題獲取if (historyList.size() > 0) {for (RoleContent tempRoleContent : historyList) {text.add(JSON.toJSON(tempRoleContent));}}// 最新問題RoleContent roleContent = new RoleContent();roleContent.role = "user";roleContent.content = NewQuestion;text.add(JSON.toJSON(roleContent));historyList.add(roleContent);message.put("text", text);payload.put("message", message);requestJson.put("header", header);requestJson.put("parameter", parameter);requestJson.put("payload", payload);System.err.println(requestJson); // 可以打印看每次的傳參明細(xì)webSocket.send(requestJson.toString());// 等待服務(wù)端返回完畢后關(guān)閉while (true) {// System.err.println(wsCloseFlag + "---");Thread.sleep(200);if (wsCloseFlag) {break;}}webSocket.close(1000, "");} catch (Exception e) {e.printStackTrace();}}}@Overridepublic void onOpen(WebSocket webSocket, Response response) {super.onOpen(webSocket, response);System.out.print("大模型:");MyThread myThread = new MyThread(webSocket);myThread.start();}@Overridepublic void onMessage(WebSocket webSocket, String text) {// System.out.println(userId + "用來區(qū)分那個用戶的結(jié)果" + text);JsonParse myJsonParse = gson.fromJson(text, JsonParse.class);if (myJsonParse.header.code != 0) {System.out.println("發(fā)生錯誤,錯誤碼為:" + myJsonParse.header.code);System.out.println("本次請求的sid為:" + myJsonParse.header.sid);webSocket.close(1000, "");}List<Text> textList = myJsonParse.payload.choices.text;for (Text temp : textList) {System.out.print(temp.content);totalAnswer = totalAnswer + temp.content;}if (myJsonParse.header.status == 2) {// 可以關(guān)閉連接,釋放資源System.out.println();System.out.println("*************************************************************************************");if (canAddHistory()) {RoleContent roleContent = new RoleContent();roleContent.setRole("assistant");roleContent.setContent(totalAnswer);historyList.add(roleContent);} else {historyList.remove(0);RoleContent roleContent = new RoleContent();roleContent.setRole("assistant");roleContent.setContent(totalAnswer);historyList.add(roleContent);}wsCloseFlag = true;totalFlag = true;}}@Overridepublic void onFailure(WebSocket webSocket, Throwable t, Response response) {super.onFailure(webSocket, t, response);try {if (null != response) {int code = response.code();System.out.println("onFailure code:" + code);System.out.println("onFailure body:" + response.body().string());if (101 != code) {System.out.println("connection failed");System.exit(0);}}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}// 鑒權(quán)方法public static String getAuthUrl(String hostUrl, String apiKey, String apiSecret) throws Exception {String newUrl = hostUrl.toString().replace("ws://", "http://").replace("wss://", "https://");URL url = new URL(newUrl);// 時間SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);format.setTimeZone(TimeZone.getTimeZone("GMT"));String date = format.format(new Date());// 拼接String preStr = "host: " + url.getHost() + "\n" + "date: " + date + "\n" + "GET " + url.getPath() + " HTTP/1.1";// System.err.println(preStr);// SHA256加密Mac mac = Mac.getInstance("hmacsha256");SecretKeySpec spec = new SecretKeySpec(apiSecret.getBytes(StandardCharsets.UTF_8), "hmacsha256");mac.init(spec);byte[] hexDigits = mac.doFinal(preStr.getBytes(StandardCharsets.UTF_8));// Base64加密String sha = Base64.getEncoder().encodeToString(hexDigits);// System.err.println(sha);// 拼接String authorization = String.format("api_key=\"%s\", algorithm=\"%s\", headers=\"%s\", signature=\"%s\"", apiKey, "hmac-sha256", "host date request-line", sha);// 拼接地址HttpUrl httpUrl = Objects.requireNonNull(HttpUrl.parse("https://" + url.getHost() + url.getPath())).newBuilder().//addQueryParameter("authorization", Base64.getEncoder().encodeToString(authorization.getBytes(StandardCharsets.UTF_8))).//addQueryParameter("date", date).//addQueryParameter("host", url.getHost()).//build();//System.err.println(httpUrl.toString());return httpUrl.toString();}//返回的json結(jié)果拆解class JsonParse {Header header;Payload payload;}class Header {int code;int status;String sid;}class Payload {Choices choices;}class Choices {List<Text> text;}class Text {String role;String content;}class RoleContent {String role;String content;public String getRole() {return role;}public void setRole(String role) {this.role = role;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}}
}