Java调用ChatGPT的实现代码
Java调用ChatGPT的小插件
1. ChatGPT账号准备
很多博文有介绍怎么获取账号,具体的可自行搜索。
准备好ChatGPT帐号之后打开openai的官网去创建API KEYS,链接:https://platform.openai.com/account/api-keys。注意:
这里的API KEYS创建好以后一定要妥善保存,创建以后,第二次就无法再查看了,想要再看,只能删除了API KEYS然后重新创建。
这里的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相关参数
chatgpt: model: text-davinci-003 token: sk-xxxxxxxxxxxxxxxxxxx retries: 10
这里的model是选择chatgpt哪个模型,默认填好的是最优的模型了,token就是上面申请的API KEYS,retries指的是当chatgpt第一次请求回答失败时,重新请求的次数(增加该参数的原因是因为大量访问的原因,在某一个时刻,chatgpt服务将处于无法访问的情况)
2.3 @EnableChatGPT注解
启动类上加入@EnableChatGPT
注解则将ChatGPT服务注入到Spring中。
3. 使用
提供了工具类OpenAiUtils
,里面提供了相关方法进行调用。
其中最简单的使用方法是:
OpenAiUtils.createCompletion(prompt);
入参prompt
即输入的问题的字符串。
还提供一个通用的静态方法是
public static List<String> createCompletion(CompletionRequest completionRequest) {...}
入参CompletionRequest
里包含模型的一些可调参数。
OpenAiUtils
类中还提供了多个可供选择的静态方法,可以自行查看。
上述方法的返回参数是一个list,是因为调整参数返回答案n
可以一次性返回多条不同的解答(n
为CompletionRequest
类中一个参数)。
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调用ChatGPT的实现代码的文章就介绍到这了,更多相关Java调用ChatGPT内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
详解Jenkins 实现Gitlab事件自动触发Jenkins构建及钉钉消息推送
这篇文章主要介绍了Jenkins 实现Gitlab事件自动触发Jenkins构建及钉钉消息推送,应该会对大家学习Jenkins有所启发2021-04-04详谈java中File类getPath()、getAbsolutePath()、getCanonical的区别
下面小编就为大家带来一篇详谈java中File类getPath()、getAbsolutePath()、getCanonical的区别。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-07-07SpringBoot+Mybatis plus+React实现条件选择切换搜索实践
本文主要介绍了SpringBoot+Mybatis plus+React实现条件选择切换搜索实践,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2021-09-09
最新评论