
實(shí)時(shí)航班追蹤背后的技術(shù):在線飛機(jī)追蹤器的工作原理
public abstract class AiModelStrategy {
/**
* SSE 流式對(duì)話
*/
public abstract void contextStreamChat(AiModeChatParams aiModeChatParams);
}
AiModeChatParams
的實(shí)現(xiàn)如下:
@Data
@NoArgsConstructor
public class AiModeChatParams {
@ApiModelProperty("用戶發(fā)送消息")
private String prompt;
@ApiModelProperty("會(huì)話 ID")
private String chatDialogueId;
@ApiModelProperty("模型選擇")
private String chatModel = ChatCompletion.Model.GPT_3_5_TURBO_16K.getName();
@ApiModelProperty(value = "對(duì)話機(jī)器人ID")
private String robotModelId;
@ApiModelProperty("最大 token 數(shù)量")
private int maxTokens = 2048;
@ApiModelProperty("密鑰集合")
private List<String> apiKeys = new ArrayList<>();
@ApiModelProperty("上下文消息")
private List<ChatMessage> chatMessageList;
@ApiModelProperty("請(qǐng)求路徑")
private String apiHost;
@ApiModelProperty("SSE")
private EventSourceListener eventSourceListener;
@ApiModelProperty("上傳圖片")
private List<String> imageList;
}
@Service
@Slf4j
public class AiModelFactory {
@Autowired
private Map<String, AiModelStrategy> map = new ConcurrentHashMap<>();
public AiModelStrategy getStrategy(String componentName) {
LogPrintUtils.info(log, "進(jìn)入外部產(chǎn)品工廠方法 : {}", componentName);
AiModelStrategy strategy = map.get(componentName);
if (strategy == null) {
strategy = map.get("stdNullStrategy");
}
LogPrintUtils.info(log, "帶走了:{}", strategy);
return strategy;
}
}
@Service
public class AmXfxhStrategy extends AiModelStrategy {
@Override
public void contextStreamChat(AiModeChatParams aiModeChatParams) {
// 構(gòu)建參數(shù)
// 發(fā)送請(qǐng)求
}
}
通過(guò)這種設(shè)計(jì),我們可以將各個(gè)大模型的請(qǐng)求構(gòu)建和發(fā)送邏輯放入策略工廠中,減少代碼重復(fù),提高系統(tǒng)的可擴(kuò)展性。
在對(duì)接大模型時(shí),接口文檔是非常重要的參考資料。它描述了軟件組件或系統(tǒng)中的接口,包括接口名稱(chēng)、方法、參數(shù)、返回值和異常等信息。通過(guò)接口文檔,我們可以了解如何調(diào)用大模型的 API。
以智譜 AI 的 GLM-4 模型為例,以下是通過(guò) HTTP 調(diào)用該模型的示例代碼:
private static final String API_URL = ""; // 替換為實(shí)際的 API URL
private static final String API_KEY = ""; // 填寫(xiě)您的 API Key
public static void main(String[] args) throws IOException {
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.build();
JSONObject requestBodyJson = new JSONObject();
requestBodyJson.put("model", "glm-4-plus");
JSONArray messages = new JSONArray();
messages.put(new JSONObject().put("role", "system")
.put("content", "你是一個(gè)樂(lè)于解答各種問(wèn)題的助手,你的任務(wù)是為用戶提供專(zhuān)業(yè)、準(zhǔn)確、有見(jiàn)地的建議。"));
messages.put(new JSONObject().put("role", "user")
.put("content", "如何實(shí)現(xiàn)重定向"));
requestBodyJson.put("messages", messages);
RequestBody body = RequestBody.create(
requestBodyJson.toString(),
MediaType.parse("application/json")
);
Request request = new Request.Builder()
.url(API_URL)
.addHeader("Authorization", "Bearer " + API_KEY)
.post(body)
.build();
try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response);
}
String responseBody = response.body().string();
JSONObject jsonResponse = new JSONObject(responseBody);
String assistantReply = jsonResponse.getJSONArray("choices")
.getJSONObject(0)
.getJSONObject("message")
.getString("content");
System.out.println(assistantReply);
}
}
通過(guò)以上代碼,我們可以看到如何創(chuàng)建 HTTP 請(qǐng)求,發(fā)送請(qǐng)求并解析響應(yīng)。
通過(guò)工廠策略模式,我們能夠高效地對(duì)接多家國(guó)內(nèi)已備案的大模型,減少代碼重復(fù),提升系統(tǒng)的可擴(kuò)展性。同時(shí),通過(guò)規(guī)范的接口文檔和 HTTP 調(diào)用,我們能夠更好地實(shí)現(xiàn)大模型的功能應(yīng)用。這種方法不僅適用于 AI大模型,也可以應(yīng)用于其他需要多策略、多接口對(duì)接的場(chǎng)景。
對(duì)比大模型API的內(nèi)容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉(zhuǎn)化潛力
一鍵對(duì)比試用API 限時(shí)免費(fèi)