已完成功能
效果图如下:
后续功能
- 查询余额,避免过度消耗;
- 限制聊天次数和长度,避免被刷;
- 记录聊天历史,有迹可循;
- 接入TTS,完成文字转语音;
- 接入MidJourney;
- 接入D-ID,实现文字生成视频;
- 接入OpenAI的其他接口,完成一些demo;
- 接入微信小程序;
前端代码简单示例: javascript示例
// message为需要发送的消息
const eventSource = new EventSource(`127.0.0.1:8080/openai/chat/stream?prompt=${message}&user=${user}`);
// 收到消息处理
eventSource.onmessage = function(event) {
// 略...
}
vue示例
const eventSource = new EventSource(`127.0.0.1:8080/openai/chat/stream?prompt=${message}&user=${user}`);
sse.addEventListener('open', (function () {
console.log('open')
}))
sse.addEventListener('message', function (res) {
console.log(res.data)
}))
sse.addEventListener('error', function () {
console.log('error')
}))
后端代码简单示例(注意produces = MediaType.TEXT_EVENT_STREAM_VALUE
不能少):
@RestController
@RequestMapping("/openai")
@RequiredArgsConstructor
public class ChatController {
private final UserChatService userChatService;
@GetMapping(value = "/chat/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<String> streamCompletions(String prompt, String user) {
Assert.hasLength(user, "user不能为空");
Assert.hasLength(prompt, "prompt不能为空");
return userChatService.send(MessageType.TEXT, prompt, user);
}
}
postman示例图
openai:
urls:
moderations: "https://api.openai.com/v1/moderations"
completions: "https://api.openai.com/v1/completions"
edits: "https://api.openai.com/v1/edits"
chat-completions: "https://api.openai.com/v1/chat/completions"
list-models: "https://api.openai.com/v1/models"
retrieve-model: "https://api.openai.com/v1/models/{model}"
embeddings: "https://api.openai.com/v1/embeddings"
list-files: "https://api.openai.com/v1/files"
upload-file: "https://api.openai.com/v1/files"
delete-file: "https://api.openai.com/v1/files/{file_id}"
retrieve-file: "https://api.openai.com/v1/files/{file_id}"
retrieve-file-content: "https://api.openai.com/v1/files/{file_id}/content"
create_fine_tune: "https://api.openai.com/v1/fine-tunes"
list_fine_tune: "https://api.openai.com/v1/fine-tunes"
retrieve_fine_tune: "https://api.openai.com/v1/fine-tunes/{fine_tune_id}"
cancel_fine_tune: "https://api.openai.com/v1/fine-tunes/{fine_tune_id}/cancel"
list_fine_tune_events: "https://api.openai.com/v1/fine-tunes/{fine_tune_id}/events"
delete_fine_tune_events: "https://api.openai.com/v1/models/{model}"
create-transcription: "https://api.openai.com/v1/audio/transcriptions"
create-translation: "https://api.openai.com/v1/audio/translations"
create_image: "https://api.openai.com/v1/images/generations"
create_image_edit: "https://api.openai.com/v1/images/edits"
create_image_variation: "https://api.openai.com/v1/images/variations"
billing-credit-grants: "https://api.openai.com/dashboard/billing/credit_grants"
users: "https://api.openai.com/v1/organizations/{organizationId}/users"
billing-subscription: "https://api.openai.com/v1/dashboard/billing/subscription"
billing-usage: "https://api.openai.com/v1/dashboard/billing/usage?start_date={start_date}&end_date={end_date}"