Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Kwan的解忧杂货铺@新空间代码工作室
SpringBoot-kwan
提交
a9f2bf58
S
SpringBoot-kwan
项目概览
Kwan的解忧杂货铺@新空间代码工作室
/
SpringBoot-kwan
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
SpringBoot-kwan
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
a9f2bf58
编写于
9月 08, 2023
作者:
Kwan的解忧杂货铺@新空间代码工作室
🐭
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix:java调用open ai
上级
4d5ed5aa
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
254 addition
and
1 deletion
+254
-1
pom.xml
pom.xml
+11
-1
src/main/java/com/kwan/springbootkwan/entity/openai/AzureAIChatRequest.java
...kwan/springbootkwan/entity/openai/AzureAIChatRequest.java
+24
-0
src/main/java/com/kwan/springbootkwan/entity/openai/AzureAIChatResponse.java
...wan/springbootkwan/entity/openai/AzureAIChatResponse.java
+16
-0
src/main/java/com/kwan/springbootkwan/entity/openai/AzureAIChoice.java
.../com/kwan/springbootkwan/entity/openai/AzureAIChoice.java
+9
-0
src/main/java/com/kwan/springbootkwan/entity/openai/AzureAIClient.java
.../com/kwan/springbootkwan/entity/openai/AzureAIClient.java
+92
-0
src/main/java/com/kwan/springbootkwan/entity/openai/AzureAIMessage.java
...com/kwan/springbootkwan/entity/openai/AzureAIMessage.java
+9
-0
src/main/java/com/kwan/springbootkwan/entity/openai/AzureAIRole.java
...va/com/kwan/springbootkwan/entity/openai/AzureAIRole.java
+24
-0
src/main/java/com/kwan/springbootkwan/entity/openai/AzureAIUsage.java
...a/com/kwan/springbootkwan/entity/openai/AzureAIUsage.java
+16
-0
src/main/java/com/kwan/springbootkwan/entity/openai/Main.java
...main/java/com/kwan/springbootkwan/entity/openai/Main.java
+53
-0
未找到文件。
pom.xml
浏览文件 @
a9f2bf58
...
...
@@ -237,7 +237,17 @@
<dependency>
<groupId>
com.github.ben-manes.caffeine
</groupId>
<artifactId>
caffeine
</artifactId>
<!-- <version>3.1.2</version>-->
</dependency>
<dependency>
<groupId>
org.asynchttpclient
</groupId>
<artifactId>
async-http-client
</artifactId>
<version>
2.12.3
</version>
<type>
jar
</type>
</dependency>
<dependency>
<groupId>
com.google.code.gson
</groupId>
<artifactId>
gson
</artifactId>
<version>
2.10.1
</version>
</dependency>
</dependencies>
<build>
...
...
src/main/java/com/kwan/springbootkwan/entity/openai/AzureAIChatRequest.java
0 → 100644
浏览文件 @
a9f2bf58
package
com.kwan.springbootkwan.entity.openai
;
import
com.google.gson.annotations.SerializedName
;
import
lombok.Data
;
import
java.util.List
;
@Data
public
class
AzureAIChatRequest
{
private
List
<
AzureAIMessage
>
messages
;
private
Double
temperature
;
@SerializedName
(
"n"
)
private
Integer
choices
;
private
boolean
stream
;
private
String
stop
;
@SerializedName
(
"max_tokens"
)
private
Integer
maxTokens
;
@SerializedName
(
"presence_penalty"
)
private
Integer
presencePenalty
;
@SerializedName
(
"frequency_penalty"
)
private
Integer
frequencyPenalty
;
private
String
user
;
}
\ No newline at end of file
src/main/java/com/kwan/springbootkwan/entity/openai/AzureAIChatResponse.java
0 → 100644
浏览文件 @
a9f2bf58
package
com.kwan.springbootkwan.entity.openai
;
import
lombok.Data
;
import
java.util.List
;
@Data
public
class
AzureAIChatResponse
{
private
String
id
;
private
String
object
;
private
String
created
;
private
String
model
;
private
AzureAIUsage
usage
;
private
List
<
AzureAIChoice
>
choices
;
}
\ No newline at end of file
src/main/java/com/kwan/springbootkwan/entity/openai/AzureAIChoice.java
0 → 100644
浏览文件 @
a9f2bf58
package
com.kwan.springbootkwan.entity.openai
;
import
lombok.Data
;
@Data
public
class
AzureAIChoice
{
private
Object
message
;
}
src/main/java/com/kwan/springbootkwan/entity/openai/AzureAIClient.java
0 → 100644
浏览文件 @
a9f2bf58
package
com.kwan.springbootkwan.entity.openai
;
import
cn.hutool.core.date.BetweenFormatter
;
import
cn.hutool.core.date.DateUnit
;
import
cn.hutool.core.date.DateUtil
;
import
cn.hutool.json.JSONUtil
;
import
com.google.gson.Gson
;
import
lombok.extern.slf4j.Slf4j
;
import
org.asynchttpclient.AsyncHttpClient
;
import
org.asynchttpclient.DefaultAsyncHttpClient
;
import
org.asynchttpclient.Request
;
import
org.asynchttpclient.RequestBuilder
;
import
org.asynchttpclient.Response
;
import
java.io.Closeable
;
import
java.io.IOException
;
import
java.util.Date
;
import
java.util.concurrent.Future
;
@Slf4j
public
class
AzureAIClient
implements
Closeable
{
private
static
final
String
JSON
=
"application/json; charset=UTF-8"
;
private
final
boolean
closeClient
;
private
final
AsyncHttpClient
client
;
private
final
String
deploymentName
;
private
final
String
url
;
private
final
String
token
;
private
final
String
apiVersion
;
private
boolean
closed
=
false
;
Gson
gson
=
new
Gson
();
public
AzureAIClient
(
String
url
,
String
apiKey
,
String
deploymentName
,
String
apiVersion
)
throws
Exception
{
this
.
client
=
new
DefaultAsyncHttpClient
();
this
.
url
=
url
+
"/openai/deployments/"
+
deploymentName
+
"/"
;
this
.
token
=
apiKey
;
this
.
deploymentName
=
deploymentName
;
this
.
apiVersion
=
apiVersion
;
closeClient
=
true
;
}
public
boolean
isClosed
()
{
return
closed
||
client
.
isClosed
();
}
@Override
public
void
close
()
{
if
(
closeClient
&&
!
client
.
isClosed
())
{
try
{
client
.
close
();
}
catch
(
IOException
ex
)
{
}
}
closed
=
true
;
}
public
AzureAIChatResponse
sendChatRequest
(
AzureAIChatRequest
chatRequest
)
throws
Exception
{
Date
startDateOne
=
DateUtil
.
date
();
Future
<
Response
>
f
=
client
.
executeRequest
(
buildRequest
(
"POST"
,
"chat/completions?api-version="
+
apiVersion
,
gson
.
toJson
(
chatRequest
)));
Response
r
=
f
.
get
();
Date
endDateOne
=
DateUtil
.
date
();
// 获取开始时间和结束时间的时间差
long
betweenDateOne
=
DateUtil
.
between
(
startDateOne
,
endDateOne
,
DateUnit
.
MS
);
// 格式化时间
String
formatBetweenOne
=
DateUtil
.
formatBetween
(
betweenDateOne
,
BetweenFormatter
.
Level
.
MILLISECOND
);
log
.
info
(
String
.
format
(
"请求数据耗时(毫秒):%s"
,
formatBetweenOne
));
if
(
r
.
getStatusCode
()
!=
200
)
{
log
.
info
(
"Could not create chat request - server resposne was "
+
r
.
getStatusCode
()
+
" to url: "
+
url
+
"chat/completions?api-version=2023-03-15-preview"
);
return
null
;
}
else
{
Date
startDate
=
DateUtil
.
date
();
AzureAIChatResponse
azureAIChatResponse
=
JSONUtil
.
toBean
(
r
.
getResponseBody
(),
AzureAIChatResponse
.
class
);
Date
endDate
=
DateUtil
.
date
();
// 获取开始时间和结束时间的时间差
long
betweenDate
=
DateUtil
.
between
(
startDate
,
endDate
,
DateUnit
.
MS
);
// 格式化时间
String
formatBetween
=
DateUtil
.
formatBetween
(
betweenDate
,
BetweenFormatter
.
Level
.
MILLISECOND
);
log
.
info
(
String
.
format
(
"格式化数据耗时(毫秒):%s"
,
formatBetween
));
return
azureAIChatResponse
;
}
}
private
Request
buildRequest
(
String
type
,
String
subUrl
,
String
requestBody
)
{
RequestBuilder
builder
=
new
RequestBuilder
(
type
);
Request
request
=
builder
.
setUrl
(
this
.
url
+
subUrl
)
.
addHeader
(
"Accept"
,
JSON
)
.
addHeader
(
"Content-Type"
,
JSON
)
.
addHeader
(
"api-key"
,
this
.
token
)
.
setBody
(
requestBody
)
.
build
();
return
request
;
}
}
\ No newline at end of file
src/main/java/com/kwan/springbootkwan/entity/openai/AzureAIMessage.java
0 → 100644
浏览文件 @
a9f2bf58
package
com.kwan.springbootkwan.entity.openai
;
import
lombok.Data
;
@Data
public
class
AzureAIMessage
{
private
String
role
;
private
String
content
;
}
src/main/java/com/kwan/springbootkwan/entity/openai/AzureAIRole.java
0 → 100644
浏览文件 @
a9f2bf58
package
com.kwan.springbootkwan.entity.openai
;
import
com.google.gson.annotations.SerializedName
;
public
enum
AzureAIRole
{
@SerializedName
(
"assistant"
)
ASSISTANT
(
"assistant"
),
@SerializedName
(
"system"
)
SYSTEM
(
"system"
),
@SerializedName
(
"user"
)
USER
(
"user"
),
;
private
final
String
text
;
private
AzureAIRole
(
final
String
text
)
{
this
.
text
=
text
;
}
@Override
public
String
toString
()
{
return
text
;
}
}
\ No newline at end of file
src/main/java/com/kwan/springbootkwan/entity/openai/AzureAIUsage.java
0 → 100644
浏览文件 @
a9f2bf58
package
com.kwan.springbootkwan.entity.openai
;
import
com.google.gson.annotations.SerializedName
;
import
lombok.Data
;
@Data
public
class
AzureAIUsage
{
@SerializedName
(
"prompt_tokens"
)
private
int
promptTokens
;
@SerializedName
(
"completion_tokens"
)
private
int
completionTokens
;
@SerializedName
(
"total_tokens"
)
private
int
totalTokens
;
}
src/main/java/com/kwan/springbootkwan/entity/openai/Main.java
0 → 100644
浏览文件 @
a9f2bf58
package
com.kwan.springbootkwan.entity.openai
;
import
lombok.extern.slf4j.Slf4j
;
import
java.util.ArrayList
;
import
java.util.List
;
@Slf4j
public
class
Main
{
/**
* # 公司的key
* os.environ["OPENAI_API_KEY"] = ''
* os.environ["OPENAI_API_BASE"] = 'https://opencatgpt.openai.azure.com/'
* os.environ["OPENAI_API_TYPE"] = 'azure'
* os.environ["OPENAI_API_VERSION"] = '2023-05-15'
*/
public
static
void
main
(
String
[]
args
)
throws
Exception
{
// 装配请求集合
List
<
AzureAIMessage
>
azureAiMessageList
=
new
ArrayList
<>();
AzureAIChatRequest
azureAiChatRequest
=
new
AzureAIChatRequest
();
AzureAIMessage
azureAIMessage0
=
new
AzureAIMessage
();
azureAIMessage0
.
setRole
(
AzureAIRole
.
SYSTEM
.
toString
());
azureAIMessage0
.
setContent
(
"你是一个AI机器人,请根据提问进行回答"
);
azureAiMessageList
.
add
(
azureAIMessage0
);
execute
(
azureAiMessageList
,
azureAiChatRequest
,
"请解释一下java的多态"
);
}
private
static
void
execute
(
List
<
AzureAIMessage
>
azureAiMessageList
,
AzureAIChatRequest
azureAiChatRequest
,
String
question
)
throws
Exception
{
AzureAIMessage
azureAIMessage1
=
new
AzureAIMessage
();
azureAIMessage1
.
setRole
(
AzureAIRole
.
USER
.
toString
());
azureAIMessage1
.
setContent
(
question
);
azureAiMessageList
.
add
(
azureAIMessage1
);
azureAiChatRequest
.
setMessages
(
azureAiMessageList
);
azureAiChatRequest
.
setMaxTokens
(
1024
);
azureAiChatRequest
.
setTemperature
(
0.0
);
// 是否进行留式返回
azureAiChatRequest
.
setPresencePenalty
(
0
);
azureAiChatRequest
.
setFrequencyPenalty
(
0
);
azureAiChatRequest
.
setStop
(
null
);
AzureAIClient
azureAIClient
=
new
AzureAIClient
(
"https://opencatgpt.openai.azure.com/"
,
""
,
"gpt-35-turbo"
,
"2023-05-15"
);
AzureAIChatResponse
azureAIChatResponse
=
azureAIClient
.
sendChatRequest
(
azureAiChatRequest
);
System
.
out
.
println
(
azureAIChatResponse
.
getChoices
().
get
(
0
).
getMessage
());
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录