-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathAiChatConfig.java
More file actions
155 lines (131 loc) · 5.01 KB
/
AiChatConfig.java
File metadata and controls
155 lines (131 loc) · 5.01 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
* Copyright (c) 2023 - present TinyEngine Authors.
* Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd.
*
* Use of this source code is governed by an MIT-style license.
*
* THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
* BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
* A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
*
*/
package com.tinyengine.it.config;
import com.tinyengine.it.common.enums.Enums;
import java.util.HashMap;
import java.util.Map;
/**
* The type Ai chat config.
*
* @since 2024-10-20
*/
public class AiChatConfig {
private static final String OPENAI_API_URL = "https://api.openai.com";
private static final String LOCAL_GPT_API_URL = "https://dashscope.aliyuncs.com/compatible-mode";
/**
* Gets AI chat config.
*
* @return the AI chat config
*/
public static Map<String, AiChatConfigData> getAiChatConfig(String model,String token) {
Map<String, AiChatConfigData> config = new HashMap<>();
Map<String, String> openaiHeaders = new HashMap<>();
// 根据model值判断添加对应的header
String openAiApiKey = Enums.FoundationModel.GPT_35_TURBO.getValue().equals(model) ? token : null;
openaiHeaders.put("Authorization", "Bearer " + openAiApiKey);
Map<String, String> localGptHeaders = new HashMap<>();
String localGptApiKey = Enums.FoundationModel.LOCAL_GPT.getValue().equals(model) ? token : null;
localGptHeaders.put("Authorization", "Bearer " + localGptApiKey);
Map<String, String> ernieBotHeaders = new HashMap<>();
config.put(Enums.FoundationModel.GPT_35_TURBO.getValue(), new AiChatConfigData(
OPENAI_API_URL + "/v1/chat/completions", createCommonRequestOption(), openaiHeaders, "openai"));
config.put(Enums.FoundationModel.LOCAL_GPT.getValue(), new AiChatConfigData(
LOCAL_GPT_API_URL + "/v1/chat/completions", createCommonRequestOption(), localGptHeaders, "!openai"));
String ernieBotAccessToken = Enums.FoundationModel.ERNIBOT_TURBO.getValue().equals(model) ? token : null;
config.put(Enums.FoundationModel.ERNIBOT_TURBO.getValue(),
new AiChatConfigData(
"https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro?access_token="
+ ernieBotAccessToken,
createCommonRequestOption(), ernieBotHeaders, "baidu"));
return config;
}
private static HttpRequestOption createCommonRequestOption() {
return new HttpRequestOption("POST", "json", "json", 10 * 60 * 1000);
// 10 minutes
}
/**
* The type Ai chat config data.
*/
public static class AiChatConfigData {
/**
* The Http request url.
*/
public final String httpRequestUrl;
/**
* The Http request option.
*/
public final HttpRequestOption httpRequestOption;
/**
* The Headers.
*/
public final Map<String, String> headers;
/**
* The Manufacturer.
*/
public final String manufacturer;
/**
* The Request body.
*/
public final Map<String, Object> requestBody;
/**
* Instantiates a new Ai chat config data.
*
* @param httpRequestUrl the http request url
* @param httpRequestOption the http request option
* @param headers the headers
* @param manufacturer the manufacturer
*/
public AiChatConfigData(String httpRequestUrl, HttpRequestOption httpRequestOption, Map<String, String> headers,
String manufacturer) {
this.httpRequestUrl = httpRequestUrl;
this.httpRequestOption = httpRequestOption;
this.headers = headers;
this.manufacturer = manufacturer;
this.requestBody = new HashMap<>();
}
}
/**
* The type Http request option.
*/
public static class HttpRequestOption {
/**
* The Method.
*/
public final String method;
/**
* The Data type.
*/
public final String dataType;
/**
* The Content type.
*/
public final String contentType;
/**
* The Timeout.
*/
public final int timeout;
/**
* Instantiates a new Http request option.
*
* @param method the method
* @param dataType the data type
* @param contentType the content type
* @param timeout the timeout
*/
public HttpRequestOption(String method, String dataType, String contentType, int timeout) {
this.method = method;
this.dataType = dataType;
this.contentType = contentType;
this.timeout = timeout;
}
}
}