Java調(diào)用ChatGPT的實(shí)現(xiàn)代碼
Java調(diào)用ChatGPT的小插件
1. ChatGPT賬號(hào)準(zhǔn)備
很多博文有介紹怎么獲取賬號(hào),具體的可自行搜索。
準(zhǔn)備好ChatGPT帳號(hào)之后打開(kāi)openai的官網(wǎng)去創(chuàng)建API KEYS,鏈接:https://platform.openai.com/account/api-keys。注意:這里的API KEYS創(chuàng)建好以后一定要妥善保存,創(chuàng)建以后,第二次就無(wú)法再查看了,想要再看,只能刪除了API KEYS然后重新創(chuàng)建。

這里的API KEYS妥善保管后面會(huì)用到。
2. 配置階段
2.1 依賴引入
pom.xml中引入依賴
<dependency>
<groupId>io.github.asleepyfish</groupId>
<artifactId>chatgpt</artifactId>
<version>1.0.3</version>
</dependency>2.2 配置application.yml文件
在application.yml文件中配置chatgpt相關(guān)參數(shù)
chatgpt: model: text-davinci-003 token: sk-xxxxxxxxxxxxxxxxxxx retries: 10
這里的model是選擇chatgpt哪個(gè)模型,默認(rèn)填好的是最優(yōu)的模型了,token就是上面申請(qǐng)的API KEYS,retries指的是當(dāng)chatgpt第一次請(qǐng)求回答失敗時(shí),重新請(qǐng)求的次數(shù)(增加該參數(shù)的原因是因?yàn)榇罅吭L問(wèn)的原因,在某一個(gè)時(shí)刻,chatgpt服務(wù)將處于無(wú)法訪問(wèn)的情況)
2.3 @EnableChatGPT注解
啟動(dòng)類上加入@EnableChatGPT注解則將ChatGPT服務(wù)注入到Spring中。

3. 使用
提供了工具類OpenAiUtils,里面提供了相關(guān)方法進(jìn)行調(diào)用。
其中最簡(jiǎn)單的使用方法是:
OpenAiUtils.createCompletion(prompt);
入?yún)?code>prompt即輸入的問(wèn)題的字符串。
還提供一個(gè)通用的靜態(tài)方法是
public static List<String> createCompletion(CompletionRequest completionRequest) {...}入?yún)?code>CompletionRequest 里包含模型的一些可調(diào)參數(shù)。
OpenAiUtils類中還提供了多個(gè)可供選擇的靜態(tài)方法,可以自行查看。
上述方法的返回參數(shù)是一個(gè)list,是因?yàn)檎{(diào)整參數(shù)返回答案n可以一次性返回多條不同的解答(n為CompletionRequest類中一個(gè)參數(shù))。
4. 測(cè)試
demo的Git地址:
https://github.com/asleepyfish/chatgpt-demo
測(cè)試代碼:
@SpringBootTest
public class SpringTest {
@Test
public void chatGPTTest() {
OpenAiUtils.createCompletion("use c++ write QuickSort").forEach(System.out::println);
}
}ChatGPT輸出結(jié)果:
#include <iostream>?
using namespace std;?
// A utility function to swap two elements?
void swap(int* a, int* b)?
{?
?? ?int t = *a;?
?? ?*a = *b;?
?? ?*b = t;?
}?
/* This function takes last element as pivot, places?
the pivot element at its correct position in sorted?
array, and places all smaller (smaller than pivot)?
to left of pivot and all greater elements to right?
of pivot */
int partition (int arr[], int low, int high)?
{?
?? ?int pivot = arr[high]; // pivot?
?? ?int i = (low - 1); // Index of smaller element?
?? ?for (int j = low; j <= high - 1; j++)?
?? ?{?
?? ??? ?// If current element is smaller than the pivot?
?? ??? ?if (arr[j] < pivot)?
?? ??? ?{?
?? ??? ??? ?i++; // increment index of smaller element?
?? ??? ??? ?swap(&arr[i], &arr[j]);?
?? ??? ?}?
?? ?}?
?? ?swap(&arr[i + 1], &arr[high]);?
?? ?return (i + 1);?
}?
/* The main function that implements QuickSort?
arr[] --> Array to be sorted,?
low --> Starting index,?
high --> Ending index */
void quickSort(int arr[], int low, int high)?
{?
?? ?if (low < high)?
?? ?{?
?? ??? ?/* pi is partitioning index, arr[p] is now?
?? ??? ?at right place */
?? ??? ?int pi = partition(arr, low, high);?
?? ??? ?// Separately sort elements before?
?? ??? ?// partition and after partition?
?? ??? ?quickSort(arr, low, pi - 1);?
?? ??? ?quickSort(arr, pi + 1, high);?
?? ?}?
}?
/* Function to print an array */
void printArray(int arr[], int size)?
{?
?? ?int i;?
?? ?for (i = 0; i < size; i++)?
?? ??? ?cout << arr[i] << " ";?
?? ?cout << endl;?
}?
// Driver Code?
int main()?
{?
?? ?int arr[] = {10, 7, 8, 9, 1, 5};?
?? ?int n = sizeof(arr) / sizeof(arr[0]);?
?? ?quickSort(arr, 0, n - 1);?
?? ?cout << "Sorted array: " << endl;?
?? ?printArray(arr, n);?
?? ?return 0;?
}
到此這篇關(guān)于Java調(diào)用ChatGPT的實(shí)現(xiàn)代碼的文章就介紹到這了,更多相關(guān)Java調(diào)用ChatGPT內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解RocketMQ 消費(fèi)端如何監(jiān)聽(tīng)消息
這篇文章主要為大家介紹了RocketMQ 消費(fèi)端如何監(jiān)聽(tīng)消息示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
java實(shí)現(xiàn)商品信息管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)商品信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
SpringBoot詳細(xì)講解如何創(chuàng)建及刷新Spring容器bean
前面看spring源碼時(shí)可以發(fā)現(xiàn)refresh()方法十分重要。在這個(gè)方法中會(huì)加載beanDefinition,同時(shí)創(chuàng)建bean對(duì)象。那么在springboot中有沒(méi)有使用這個(gè)refresh()方法呢2022-06-06
支票金額大寫轉(zhuǎn)換示例(金額大寫轉(zhuǎn)換器)
這篇文章主要介紹了支票金額大寫轉(zhuǎn)換示例(金額大寫轉(zhuǎn)換器),需要的朋友可以參考下2014-02-02
java獲取IP歸屬地全網(wǎng)顯示開(kāi)源庫(kù)使用
這篇文章主要為大家介紹了java獲取IP歸屬地全網(wǎng)顯示的開(kāi)源庫(kù)使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
Spring?Boot?整合RocketMq實(shí)現(xiàn)消息過(guò)濾功能
這篇文章主要介紹了Spring?Boot?整合RocketMq實(shí)現(xiàn)消息過(guò)濾,本文講解了RocketMQ實(shí)現(xiàn)消息過(guò)濾,針對(duì)不同的業(yè)務(wù)場(chǎng)景選擇合適的方案即可,需要的朋友可以參考下2022-06-06
SpringBoot文件上傳功能的實(shí)現(xiàn)方法
這篇文章主要介紹了SpringBoot文件上傳功能的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
mybatis if傳入字符串?dāng)?shù)字踩坑記錄及解決
這篇文章主要介紹了mybatis if傳入字符串?dāng)?shù)字踩坑記錄及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02

