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;
}

工廠實(shí)現(xiàn)

@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;
}
}

策略實(shí)現(xiàn)

@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ò)展性。

通過(guò) HTTP 封裝實(shí)現(xiàn)網(wǎng)絡(luò)模型 API

在對(duì)接大模型時(shí),接口文檔是非常重要的參考資料。它描述了軟件組件或系統(tǒng)中的接口,包括接口名稱(chēng)、方法、參數(shù)、返回值和異常等信息。通過(guò)接口文檔,我們可以了解如何調(diào)用大模型的 API。

規(guī)范實(shí)現(xiàn)接口文檔

以智譜 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)。

結(jié)論

通過(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)景。

FAQ

  1. 問(wèn):什么是工廠策略模式?
  1. 問(wèn):為什么需要對(duì)接多個(gè)大模型?
  1. 問(wèn):如何確保接口調(diào)用的安全性?
  1. 問(wèn):什么是 SSE 連接?
  1. 問(wèn):如何處理接口文檔中的敏感詞?

上一篇:

SQL AI自然語(yǔ)言處理的融合:技術(shù)深度解析與實(shí)操案例

下一篇:

小紅書(shū)API抓取與數(shù)據(jù)分析:TikTok難民的新機(jī)遇
#你可能也喜歡這些API文章!

我們有何不同?

API服務(wù)商零注冊(cè)

多API并行試用

數(shù)據(jù)驅(qū)動(dòng)選型,提升決策效率

查看全部API→
??

熱門(mén)場(chǎng)景實(shí)測(cè),選對(duì)API

#AI文本生成大模型API

對(duì)比大模型API的內(nèi)容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉(zhuǎn)化潛力

25個(gè)渠道
一鍵對(duì)比試用API 限時(shí)免費(fèi)

#AI深度推理大模型API

對(duì)比大模型API的邏輯推理準(zhǔn)確性、分析深度、可視化建議合理性

10個(gè)渠道
一鍵對(duì)比試用API 限時(shí)免費(fèi)