Java調(diào)用ChatGPT的實現(xiàn)代碼
Java調(diào)用ChatGPT的小插件
1. ChatGPT賬號準備
很多博文有介紹怎么獲取賬號,具體的可自行搜索。
準備好ChatGPT帳號之后打開openai的官網(wǎng)去創(chuàng)建API KEYS,鏈接:https://platform.openai.com/account/api-keys。注意:
這里的API KEYS創(chuàng)建好以后一定要妥善保存,創(chuàng)建以后,第二次就無法再查看了,想要再看,只能刪除了API KEYS然后重新創(chuàng)建。
這里的API KEYS妥善保管后面會用到。
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相關參數(shù)
chatgpt: model: text-davinci-003 token: sk-xxxxxxxxxxxxxxxxxxx retries: 10
這里的model是選擇chatgpt哪個模型,默認填好的是最優(yōu)的模型了,token就是上面申請的API KEYS,retries指的是當chatgpt第一次請求回答失敗時,重新請求的次數(shù)(增加該參數(shù)的原因是因為大量訪問的原因,在某一個時刻,chatgpt服務將處于無法訪問的情況)
2.3 @EnableChatGPT注解
啟動類上加入@EnableChatGPT
注解則將ChatGPT服務注入到Spring中。
3. 使用
提供了工具類OpenAiUtils
,里面提供了相關方法進行調(diào)用。
其中最簡單的使用方法是:
OpenAiUtils.createCompletion(prompt);
入?yún)?code>prompt即輸入的問題的字符串。
還提供一個通用的靜態(tài)方法是
public static List<String> createCompletion(CompletionRequest completionRequest) {...}
入?yún)?code>CompletionRequest 里包含模型的一些可調(diào)參數(shù)。
OpenAiUtils
類中還提供了多個可供選擇的靜態(tài)方法,可以自行查看。
上述方法的返回參數(shù)是一個list,是因為調(diào)整參數(shù)返回答案n
可以一次性返回多條不同的解答(n
為CompletionRequest
類中一個參數(shù))。
4. 測試
demo的Git地址:
https://github.com/asleepyfish/chatgpt-demo
測試代碼:
@SpringBootTest public class SpringTest { @Test public void chatGPTTest() { OpenAiUtils.createCompletion("use c++ write QuickSort").forEach(System.out::println); } }
ChatGPT輸出結果:
#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;? }
到此這篇關于Java調(diào)用ChatGPT的實現(xiàn)代碼的文章就介紹到這了,更多相關Java調(diào)用ChatGPT內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java中l(wèi)ist.contains()的用法及拓展
List集合相信大家在開發(fā)過程中幾乎都會用到,有時候難免會遇到集合里的數(shù)據(jù)是重復的,需要進行去除,下面這篇文章主要給大家介紹了關于Java中l(wèi)ist.contains()的用法及拓展的相關資料,需要的朋友可以參考下2023-03-03如何使用Jackson和JSON Pointer查詢解析任何JSON節(jié)點
本文介紹了JSON Pointer是字符串表達式,可以非常方便解析復雜JSON節(jié)點值,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09java中List對象列表實現(xiàn)去重或取出及排序的方法
這篇文章主要介紹了關于java中List對象列表實現(xiàn)去重或取出以及排序的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面跟著小編來一起學習學習吧。2017-08-08