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相關(guān)參數(shù)
chatgpt: model: text-davinci-003 token: sk-xxxxxxxxxxxxxxxxxxx retries: 10
這里的model是選擇chatgpt哪個模型,默認填好的是最優(yōu)的模型了,token就是上面申請的API KEYS,retries指的是當chatgpt第一次請求回答失敗時,重新請求的次數(shù)(增加該參數(shù)的原因是因為大量訪問的原因,在某一個時刻,chatgpt服務(wù)將處于無法訪問的情況)
2.3 @EnableChatGPT注解
啟動類上加入@EnableChatGPT注解則將ChatGPT服務(wù)注入到Spring中。

3. 使用
提供了工具類OpenAiUtils,里面提供了相關(guān)方法進行調(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輸出結(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的實現(xiàn)代碼的文章就介紹到這了,更多相關(guān)Java調(diào)用ChatGPT內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot詳細講解如何創(chuàng)建及刷新Spring容器bean
前面看spring源碼時可以發(fā)現(xiàn)refresh()方法十分重要。在這個方法中會加載beanDefinition,同時創(chuàng)建bean對象。那么在springboot中有沒有使用這個refresh()方法呢2022-06-06
支票金額大寫轉(zhuǎn)換示例(金額大寫轉(zhuǎn)換器)
這篇文章主要介紹了支票金額大寫轉(zhuǎn)換示例(金額大寫轉(zhuǎn)換器),需要的朋友可以參考下2014-02-02
Spring?Boot?整合RocketMq實現(xiàn)消息過濾功能
這篇文章主要介紹了Spring?Boot?整合RocketMq實現(xiàn)消息過濾,本文講解了RocketMQ實現(xiàn)消息過濾,針對不同的業(yè)務(wù)場景選擇合適的方案即可,需要的朋友可以參考下2022-06-06

