Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
一缕荒烟
chatbot-api
提交
72ad219e
chatbot-api
项目概览
一缕荒烟
/
chatbot-api
与 Fork 源项目一致
Fork自
fuzhengwei / chatbot-api
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
chatbot-api
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
72ad219e
编写于
1月 19, 2023
作者:
小傅哥
⛹
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
小傅哥,feat:第4节:对接 ChatGPT API 接口
上级
c04c3d85
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
222 addition
and
2 deletion
+222
-2
chatbot-api-domain/src/main/java/cn/bugstack/chatbot/api/domain/ai/IOpenAI.java
.../main/java/cn/bugstack/chatbot/api/domain/ai/IOpenAI.java
+15
-0
chatbot-api-domain/src/main/java/cn/bugstack/chatbot/api/domain/ai/model/aggregates/AIAnswer.java
...tack/chatbot/api/domain/ai/model/aggregates/AIAnswer.java
+56
-0
chatbot-api-domain/src/main/java/cn/bugstack/chatbot/api/domain/ai/model/vo/Choices.java
...a/cn/bugstack/chatbot/api/domain/ai/model/vo/Choices.java
+50
-0
chatbot-api-domain/src/main/java/cn/bugstack/chatbot/api/domain/ai/service/OpenAI.java
...ava/cn/bugstack/chatbot/api/domain/ai/service/OpenAI.java
+66
-0
chatbot-api-interfaces/src/main/resources/application.yml
chatbot-api-interfaces/src/main/resources/application.yml
+2
-1
chatbot-api-interfaces/src/test/java/cn/bugstack/chatbot/api/test/ApiTest.java
...s/src/test/java/cn/bugstack/chatbot/api/test/ApiTest.java
+24
-1
chatbot-api-interfaces/src/test/java/cn/bugstack/chatbot/api/test/SpringBootRunTest.java
.../java/cn/bugstack/chatbot/api/test/SpringBootRunTest.java
+9
-0
未找到文件。
chatbot-api-domain/src/main/java/cn/bugstack/chatbot/api/domain/ai/IOpenAI.java
0 → 100644
浏览文件 @
72ad219e
package
cn.bugstack.chatbot.api.domain.ai
;
import
java.io.IOException
;
/**
* @author 小傅哥,微信:fustack
* @description ChatGPT open ai 接口:https://beta.openai.com/account/api-keys
* @github https://github.com/fuzhengwei
* @Copyright 公众号:bugstack虫洞栈 | 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获!
*/
public
interface
IOpenAI
{
String
doChatGPT
(
String
question
)
throws
IOException
;
}
chatbot-api-domain/src/main/java/cn/bugstack/chatbot/api/domain/ai/model/aggregates/AIAnswer.java
0 → 100644
浏览文件 @
72ad219e
package
cn.bugstack.chatbot.api.domain.ai.model.aggregates
;
import
cn.bugstack.chatbot.api.domain.ai.model.vo.Choices
;
import
java.util.List
;
/**
* @author 小傅哥,微信:fustack
* @description AI Answer
* @github https://github.com/fuzhengwei
* @copyright 公众号:bugstack虫洞栈 | 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获!
*/
public
class
AIAnswer
{
private
String
id
;
private
String
object
;
private
int
created
;
private
String
model
;
private
List
<
Choices
>
choices
;
public
void
setId
(
String
id
){
this
.
id
=
id
;
}
public
String
getId
(){
return
this
.
id
;
}
public
void
setObject
(
String
object
){
this
.
object
=
object
;
}
public
String
getObject
(){
return
this
.
object
;
}
public
void
setCreated
(
int
created
){
this
.
created
=
created
;
}
public
int
getCreated
(){
return
this
.
created
;
}
public
void
setModel
(
String
model
){
this
.
model
=
model
;
}
public
String
getModel
(){
return
this
.
model
;
}
public
void
setChoices
(
List
<
Choices
>
choices
){
this
.
choices
=
choices
;
}
public
List
<
Choices
>
getChoices
(){
return
this
.
choices
;
}
}
chatbot-api-domain/src/main/java/cn/bugstack/chatbot/api/domain/ai/model/vo/Choices.java
0 → 100644
浏览文件 @
72ad219e
package
cn.bugstack.chatbot.api.domain.ai.model.vo
;
/**
* @author 小傅哥,微信:fustack
* @description 选择
* @github https://github.com/fuzhengwei
* @Copyright 公众号:bugstack虫洞栈 | 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获!
*/
public
class
Choices
{
private
String
text
;
private
int
index
;
private
String
logprobs
;
private
String
finish_reason
;
public
String
getText
()
{
return
text
;
}
public
void
setText
(
String
text
)
{
this
.
text
=
text
;
}
public
int
getIndex
()
{
return
index
;
}
public
void
setIndex
(
int
index
)
{
this
.
index
=
index
;
}
public
String
getLogprobs
()
{
return
logprobs
;
}
public
void
setLogprobs
(
String
logprobs
)
{
this
.
logprobs
=
logprobs
;
}
public
String
getFinish_reason
()
{
return
finish_reason
;
}
public
void
setFinish_reason
(
String
finish_reason
)
{
this
.
finish_reason
=
finish_reason
;
}
}
chatbot-api-domain/src/main/java/cn/bugstack/chatbot/api/domain/ai/service/OpenAI.java
0 → 100644
浏览文件 @
72ad219e
package
cn.bugstack.chatbot.api.domain.ai.service
;
import
cn.bugstack.chatbot.api.domain.ai.IOpenAI
;
import
cn.bugstack.chatbot.api.domain.ai.model.aggregates.AIAnswer
;
import
cn.bugstack.chatbot.api.domain.ai.model.vo.Choices
;
import
com.alibaba.fastjson.JSON
;
import
org.apache.http.HttpStatus
;
import
org.apache.http.client.methods.CloseableHttpResponse
;
import
org.apache.http.client.methods.HttpPost
;
import
org.apache.http.entity.ContentType
;
import
org.apache.http.entity.StringEntity
;
import
org.apache.http.impl.client.CloseableHttpClient
;
import
org.apache.http.impl.client.HttpClientBuilder
;
import
org.apache.http.util.EntityUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Service
;
import
java.io.IOException
;
import
java.util.List
;
/**
* @author 小傅哥,微信:fustack
* @description
* @github https://github.com/fuzhengwei
* @Copyright 公众号:bugstack虫洞栈 | 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获!
*/
@Service
public
class
OpenAI
implements
IOpenAI
{
private
Logger
logger
=
LoggerFactory
.
getLogger
(
OpenAI
.
class
);
@Value
(
"${chatbot-api.openAiKey}"
)
private
String
openAiKey
;
@Override
public
String
doChatGPT
(
String
question
)
throws
IOException
{
CloseableHttpClient
httpClient
=
HttpClientBuilder
.
create
().
build
();
HttpPost
post
=
new
HttpPost
(
"https://api.openai.com/v1/completions"
);
post
.
addHeader
(
"Content-Type"
,
"application/json"
);
post
.
addHeader
(
"Authorization"
,
"Bearer "
+
openAiKey
);
String
paramJson
=
"{\"model\": \"text-davinci-003\", \"prompt\": \""
+
question
+
"\", \"temperature\": 0, \"max_tokens\": 1024}"
;
StringEntity
stringEntity
=
new
StringEntity
(
paramJson
,
ContentType
.
create
(
"text/json"
,
"UTF-8"
));
post
.
setEntity
(
stringEntity
);
CloseableHttpResponse
response
=
httpClient
.
execute
(
post
);
if
(
response
.
getStatusLine
().
getStatusCode
()
==
HttpStatus
.
SC_OK
)
{
String
jsonStr
=
EntityUtils
.
toString
(
response
.
getEntity
());
AIAnswer
aiAnswer
=
JSON
.
parseObject
(
jsonStr
,
AIAnswer
.
class
);
StringBuilder
answers
=
new
StringBuilder
();
List
<
Choices
>
choices
=
aiAnswer
.
getChoices
();
for
(
Choices
choice
:
choices
)
{
answers
.
append
(
choice
.
getText
());
}
return
answers
.
toString
();
}
else
{
throw
new
RuntimeException
(
"api.openai.com Err Code is "
+
response
.
getStatusLine
().
getStatusCode
());
}
}
}
chatbot-api-interfaces/src/main/resources/application.yml
浏览文件 @
72ad219e
...
...
@@ -3,4 +3,5 @@ server:
chatbot-api
:
groupId
:
28885518425541
cookie
:
__cuid=5330a556392a4c5b8084b4cbc165e0f3; amp_fef1e8=930aec23-e22e-4f11-8864-0389bd5095d1R...1g55hl79m.1g55hl79t.1.1.2; UM_distinctid=183e61195d535b-0bddac94679c75-19525635-1aeaa0-183e61195d7c52; sensorsdata2015jssdkcross={"distinct_id":"241858242255511","first_id":"17ebd0b4317ecb-0b27f672c2d3af-133a6253-1296000-17ebd0b4318ba7","props":{"$latest_traffic_source_type":"直接流量","$latest_search_keyword":"未取到值_直接打开","$latest_referrer":""},"$device_id":"17ebd0b4317ecb-0b27f672c2d3af-133a6253-1296000-17ebd0b4318ba7","identities":"eyIkaWRlbnRpdHlfY29va2llX2lkIjoiMTgwMmQ2YjZiOWIxZjMtMGQ4YzMzZjhmYTA3YmEtMzU3MzZhMDMtMTI5NjAwMC0xODAyZDZiNmI5YzEwODYiLCIkaWRlbnRpdHlfbG9naW5faWQiOiIyNDE4NTgyNDIyNTU1MTEifQ==","history_login_id":{"name":"$identity_login_id","value":"241858242255511"}}; abtest_env=product; zsxqsessionid=8fae9a083a4874ab833c2158a44deb82; zsxq_access_token=5D862869-1229-A9B6-1BC1-C662EC4B16DD_D625BA7FD9CBBDFA
\ No newline at end of file
cookie
:
__cuid=5330a556392a4c5b8084b4cbc165e0f3; amp_fef1e8=930aec23-e22e-4f11-8864-0389bd5095d1R...1g55hl79m.1g55hl79t.1.1.2; UM_distinctid=183e61195d535b-0bddac94679c75-19525635-1aeaa0-183e61195d7c52; sensorsdata2015jssdkcross={"distinct_id":"241858242255511","first_id":"17ebd0b4317ecb-0b27f672c2d3af-133a6253-1296000-17ebd0b4318ba7","props":{"$latest_traffic_source_type":"直接流量","$latest_search_keyword":"未取到值_直接打开","$latest_referrer":""},"$device_id":"17ebd0b4317ecb-0b27f672c2d3af-133a6253-1296000-17ebd0b4318ba7","identities":"eyIkaWRlbnRpdHlfY29va2llX2lkIjoiMTgwMmQ2YjZiOWIxZjMtMGQ4YzMzZjhmYTA3YmEtMzU3MzZhMDMtMTI5NjAwMC0xODAyZDZiNmI5YzEwODYiLCIkaWRlbnRpdHlfbG9naW5faWQiOiIyNDE4NTgyNDIyNTU1MTEifQ==","history_login_id":{"name":"$identity_login_id","value":"241858242255511"}}; abtest_env=product; zsxqsessionid=8fae9a083a4874ab833c2158a44deb82; zsxq_access_token=5D862869-1229-A9B6-1BC1-C662EC4B16DD_D625BA7FD9CBBDFA
openAiKey
:
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
\ No newline at end of file
chatbot-api-interfaces/src/test/java/cn/bugstack/chatbot/api/test/ApiTest.java
浏览文件 @
72ad219e
...
...
@@ -27,7 +27,7 @@ public class ApiTest {
HttpGet
get
=
new
HttpGet
(
"https://api.zsxq.com/v2/groups/28885518425541/topics?scope=unanswered_questions&count=20"
);
get
.
addHeader
(
"cookie"
,
"
__cuid=5330a556392a4c5b8084b4cbc165e0f3; amp_fef1e8=930aec23-e22e-4f11-8864-0389bd5095d1R...1g55hl79m.1g55hl79t.1.1.2; UM_distinctid=183e61195d535b-0bddac94679c75-19525635-1aeaa0-183e61195d7c52; sensorsdata2015jssdkcross={\"distinct_id\":\"241858242255511\",\"first_id\":\"17ebd0b4317ecb-0b27f672c2d3af-133a6253-1296000-17ebd0b4318ba7\",\"props\":{\"$latest_traffic_source_type\":\"直接流量\",\"$latest_search_keyword\":\"未取到值_直接打开\",\"$latest_referrer\":\"\"},\"$device_id\":\"17ebd0b4317ecb-0b27f672c2d3af-133a6253-1296000-17ebd0b4318ba7\",\"identities\":\"eyIkaWRlbnRpdHlfY29va2llX2lkIjoiMTgwMmQ2YjZiOWIxZjMtMGQ4YzMzZjhmYTA3YmEtMzU3MzZhMDMtMTI5NjAwMC0xODAyZDZiNmI5YzEwODYiLCIkaWRlbnRpdHlfbG9naW5faWQiOiIyNDE4NTgyNDIyNTU1MTEifQ==\",\"history_login_id\":{\"name\":\"$identity_login_id\",\"value\":\"241858242255511\"}}; abtest_env=product; zsxqsessionid=8fae9a083a4874ab833c2158a44deb82; zsxq_access_token=5D862869-1229-A9B6-1BC1-C662EC4B16DD_D625BA7FD9CBBDFA
"
);
get
.
addHeader
(
"cookie"
,
""
);
get
.
addHeader
(
"Content-Type"
,
"application/json;charset=utf8"
);
CloseableHttpResponse
response
=
httpClient
.
execute
(
get
);
...
...
@@ -67,4 +67,27 @@ public class ApiTest {
}
}
@Test
public
void
test_chatGPT
()
throws
IOException
{
CloseableHttpClient
httpClient
=
HttpClientBuilder
.
create
().
build
();
HttpPost
post
=
new
HttpPost
(
"https://api.openai.com/v1/completions"
);
post
.
addHeader
(
"Content-Type"
,
"application/json"
);
post
.
addHeader
(
"Authorization"
,
"Bearer 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"
);
String
paramJson
=
"{\"model\": \"text-davinci-003\", \"prompt\": \"帮我写一个java冒泡排序\", \"temperature\": 0, \"max_tokens\": 1024}"
;
StringEntity
stringEntity
=
new
StringEntity
(
paramJson
,
ContentType
.
create
(
"text/json"
,
"UTF-8"
));
post
.
setEntity
(
stringEntity
);
CloseableHttpResponse
response
=
httpClient
.
execute
(
post
);
if
(
response
.
getStatusLine
().
getStatusCode
()
==
HttpStatus
.
SC_OK
)
{
String
res
=
EntityUtils
.
toString
(
response
.
getEntity
());
System
.
out
.
println
(
res
);
}
else
{
System
.
out
.
println
(
response
.
getStatusLine
().
getStatusCode
());
}
}
}
chatbot-api-interfaces/src/test/java/cn/bugstack/chatbot/api/test/SpringBootRunTest.java
浏览文件 @
72ad219e
package
cn.bugstack.chatbot.api.test
;
import
cn.bugstack.chatbot.api.domain.ai.IOpenAI
;
import
cn.bugstack.chatbot.api.domain.zsxq.IZsxqApi
;
import
cn.bugstack.chatbot.api.domain.zsxq.model.aggregates.UnAnsweredQuestionsAggregates
;
import
cn.bugstack.chatbot.api.domain.zsxq.model.vo.Topics
;
...
...
@@ -35,6 +36,8 @@ public class SpringBootRunTest {
@Resource
private
IZsxqApi
zsxqApi
;
@Resource
private
IOpenAI
openAI
;
@Test
public
void
test_zsxqApi
()
throws
IOException
{
...
...
@@ -52,4 +55,10 @@ public class SpringBootRunTest {
}
}
@Test
public
void
test_openAi
()
throws
IOException
{
String
response
=
openAI
.
doChatGPT
(
"帮我写一个java冒泡排序"
);
logger
.
info
(
"测试结果:{}"
,
response
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录